PIPS
newgen_set.h
Go to the documentation of this file.
1 /*
2 
3  $Id: newgen_set.h 1357 2016-03-02 08:18:50Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of NewGen.
8 
9  NewGen is free software: you can redistribute it and/or modify it under the
10  terms of the GNU General Public License as published by the Free Software
11  Foundation, either version 3 of the License, or any later version.
12 
13  NewGen is distributed in the hope that it will be useful, but WITHOUT ANY
14  WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16  License for more details.
17 
18  You should have received a copy of the GNU General Public License along with
19  NewGen. If not, see <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifndef SET_INCLUDED
24 #define SET_INCLUDED
25 /*
26 
27  Pierre Jouvelot (3 Avril 1989)
28 
29  Set (of CHAR *) package interface.
30 
31  WARNING: You aren't allowed to use = or == between sets. Always use
32  SET_ASSIGN and SET_EQUAL.
33 */
34 
35 #include "newgen_types.h"
36 #include "newgen_hash.h"
37 
38 typedef struct _set_chunk * set;
39 
40 /* Note: hash_chunk is not included in set_type */
41 typedef enum {
47 
48 #define set_undefined ((set)(-16))
49 #define set_undefined_p(s) ((s)==set_undefined)
50 
51 // old compatibility, do not use
52 #define set_equal(s1,s2) set_equal_p(s1,s2)
53 
54 #define SET_MAP(element,code,the_set) \
55  { \
56  HASH_MAP(_set_map_key, element, code, \
57  set_private_get_hash_table(the_set)); \
58  }
59 
60 /**
61  * enumerate set elements in their internal order.
62  * caution, this enumeration is not deterministic!
63  *
64  * @param var_type is the plain type name (*not* capitalized).
65  * @param var variable name, unique in scope
66  * @param the_set expression that lead to a set, for instance a variable
67  *
68  * SET_FOREACH(var_type, var, the_set) {
69  * instructions;
70  * }
71  *
72  * note that due to variables which are declared in the current scope:
73  * - the "var" name must be unique in the scope, and is used as a
74  * suffix for declaring temporaries.
75  * - put braces around the macro when using it as a loop body or
76  * condition case.
77  */
78 #define SET_FOREACH(type_name, the_item, the_set) \
79  hash_table NGMID(h) = set_private_get_hash_table(the_set); \
80  void * NGMID(i) = NULL; \
81  for (type_name the_item; \
82  (NGMID(i) = \
83  hash_table_scan(NGMID(h), NGMID(i), (void **) &the_item, NULL));)
84 
85 /* what about this replacement?
86 #define SET_MAP(the_item, the_code, the_set) \
87  { SET_FOREACH(void *, the_item, the_set) the_code; }
88 */
89 
90 /* functions implemented in set.c */
91 // CONSTRUCTORS
93 extern set set_make(set_type);
94 extern set set_singleton(set_type, const void *);
95 extern set set_dup(const set);
96 // DESTRUCTORA
97 extern void set_free(set);
98 extern void sets_free(set,...);
99 // OBSERVERS
100 extern int set_size(const set);
101 extern int set_own_allocated_memory(const set);
102 extern set_type set_get_type(const set);
103 // do not call this one, please...
105 // TESTS
106 extern bool set_belong_p(const set, const void *);
107 extern bool list_in_set_p(const list, const set);
108 extern bool set_equal_p(const set, const set);
109 extern bool set_empty_p(const set);
110 extern bool set_inclusion_p(const set, const set);
111 extern bool set_intersection_p(const set, const set);
112 // OPERATIONS
113 extern set set_clear(set);
114 extern set set_assign(set, const set);
115 extern set set_append_list(set, const list);
116 extern set set_assign_list(set, const list);
117 extern set set_add_element(set, const set, const void *);
118 extern set set_add_elements(set, const set, const void * e, ...);
119 extern set set_union(set, const set, const set);
120 extern set set_intersection(set, const set, const set);
121 extern set set_difference(set, const set, const set);
122 extern set set_del_element(set, const set, const void *);
123 extern set set_delfree_element(set, const set, const void *);
124 extern void gen_set_closure_iterate(void (*)(void *, set), set, bool);
125 extern void gen_set_closure(void (*)(void *, set), set);
126 // CONVERSIONS
127 extern string set_to_string(string, const set, gen_string_func_t);
128 extern void set_fprint(FILE *, string, const set, gen_string_func_t);
130 // no not use set_to_list, the output is not deterministic
131 extern list set_to_list(const set);
132 // See set_append_list for the conversion from list to set
133 
134 #endif // SET_INCLUDED
@ hash_int
Definition: newgen_hash.h:32
@ hash_string
Definition: newgen_hash.h:32
@ hash_private
Definition: newgen_hash.h:32
@ hash_pointer
Definition: newgen_hash.h:32
_uint(* hash_rank_t)(const void *, size_t)
Definition: newgen_hash.h:44
int(* hash_equals_t)(const void *, const void *)
Definition: newgen_hash.h:45
set set_generic_make(set_type, hash_equals_t, hash_rank_t)
what about this replacement? #define SET_MAP(the_item, the_code, the_set) \ { SET_FOREACH(void *,...
Definition: set.c:83
set set_delfree_element(set, const set, const void *)
May be useful for string sets ...
Definition: set.c:276
bool set_empty_p(const set)
tell whether set s is empty.
Definition: set.c:367
set set_assign_list(set, const list)
assigns a list contents to a set all duplicated elements are lost
Definition: set.c:474
bool set_intersection_p(const set, const set)
returns whether s1 n s2 <> 0 complexity of the intersection
Definition: set.c:288
set set_singleton(set_type, const void *)
create a singleton set of any type but hash_private
Definition: set.c:113
set set_del_element(set, const set, const void *)
Definition: set.c:265
set set_intersection(set, const set, const set)
Definition: set.c:229
struct _set_chunk * set
Definition: newgen_set.h:38
list set_to_list(const set)
create a list from a set the set is not freed
Definition: set.c:436
bool list_in_set_p(const list, const set)
Definition: set.c:201
list set_to_sorted_list(const set, gen_cmp_func_t)
Definition: set.c:447
set set_assign(set, const set)
Assign a set with the content of another set.
Definition: set.c:129
void sets_free(set,...)
Free several sets in one call.
Definition: set.c:340
bool set_equal_p(const set, const set)
returns whether s1 == s2
Definition: set.c:316
int set_size(const set)
returns the number of items in s.
Definition: set.c:359
set set_difference(set, const set, const set)
Definition: set.c:256
int set_own_allocated_memory(const set)
Definition: set.c:423
hash_table set_private_get_hash_table(const set)
return the internal hash table of set s
Definition: set.c:68
void set_free(set)
Definition: set.c:332
bool set_inclusion_p(const set, const set)
return whether s1 \included s2
Definition: set.c:305
set set_clear(set)
Assign the empty set to s s := {}.
Definition: set.c:326
string set_to_string(string, const set, gen_string_func_t)
return allocated string for set s
Definition: set.c:513
void gen_set_closure(void(*)(void *, set), set)
a set-based implementation of gen_closure that does not go twice in the same object.
Definition: set.c:418
bool set_belong_p(const set, const void *)
Definition: set.c:194
set set_union(set, const set, const set)
Definition: set.c:211
void set_fprint(FILE *, string, const set, gen_string_func_t)
print set s to file stream out.
Definition: set.c:524
set_type
Note: hash_chunk is not included in set_type.
Definition: newgen_set.h:41
@ set_pointer
Definition: newgen_set.h:44
@ set_private
Definition: newgen_set.h:45
@ set_int
Definition: newgen_set.h:43
@ set_string
Definition: newgen_set.h:42
set set_append_list(set, const list)
add list l items to set s, which is returned.
Definition: set.c:460
void gen_set_closure_iterate(void(*)(void *, set), set, bool)
Definition: set.c:372
set_type set_get_type(const set)
return the type of set s
Definition: set.c:75
set set_add_elements(set, const set, const void *e,...)
Definition: set.c:171
set set_dup(const set)
Definition: set.c:143
set set_make(set_type)
Create an empty set of any type but hash_private.
Definition: set.c:102
set set_add_element(set, const set, const void *)
Definition: set.c:152
string(* gen_string_func_t)(const void *)
Definition: newgen_types.h:111
int(* gen_cmp_func_t)(const void *, const void *)
Definition: newgen_types.h:114
FI: I do not understand why the type is duplicated at the set level.
Definition: set.c:59
The structure used to build lists in NewGen.
Definition: newgen_list.h:41