PIPS
newgen_hash.h
Go to the documentation of this file.
1 /*
2 
3  $Id: newgen_hash.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 newgen_hash_included
24 #define newgen_hash_included
25 
26 #define HASH_DEFAULT_SIZE 7
27 
28 /* Equality and rank functions are provided for strings, integers,
29  pointers and Newgen chunks. The user can provide his/her own
30  functions by using hash_private. */
31 typedef enum hash_key_type {
34 
35 /* Define hash_table structure which is hidden.
36  * The only thing we know about it is that the entries are in an array
37  * pointed to by hash_table_array(htp), it contains hash_table_size(htp)
38  * elements. These elements can be read with hash_entry_val(...) or
39  * hash_entry_key(...). If the key is HASH_ENTRY_FREE or
40  * HASH_ENTRY_FREE_FOR_PUT then the slot is empty.
41  */
42 
43 typedef struct __hash_table *hash_table;
44 typedef _uint (* hash_rank_t)(const void *, size_t);
45 typedef int (* hash_equals_t)(const void *, const void *);
46 
47 /* Value of an undefined hash_table
48  */
49 #define hash_table_undefined ((hash_table)gen_chunk_undefined)
50 #define hash_table_undefined_p(h) ((h)==hash_table_undefined)
51 
52 /* value returned by hash_get() when the key is not found; could also be
53  called HASH_KEY_NOT_FOUND, but it's semantically a value; this bottom
54  value will be user-definable in a future release of NewGen */
55 
56 #define HASH_UNDEFINED_VALUE ((void *) gen_chunk_undefined)
57 
58 #define hash_table_empty_p(htp) (hash_table_entry_count(htp) == 0)
59 
60 #define HASH_MAP(k, v, code, ht) \
61  { \
62  hash_table NGMID(h) = (ht) ; \
63  register void * NGMID(i) = NULL; \
64  void *k, *v; \
65  while ((NGMID(i) = \
66  hash_table_scan(NGMID(h), NGMID(i), &k, &v))) { \
67  code; \
68  } \
69  }
70 
71 #define HASH_FOREACH(key_type, k, value_type, v, ht) \
72  hash_table NGMID(h) = (ht); \
73  register void * NGMID(i) = NULL; \
74  key_type k; \
75  value_type v; \
76  while((NGMID(i) = \
77  hash_table_scan(NGMID(h), NGMID(i), (void**) &k, (void**) &v)))
78 
79 // hash_put_or_update() uses the warn_on_redefinition
80 #define hash_put_or_update(h, k, v) hash_overwrite(h, k, v)
81 
82 // functions implemented in hash.c
83 
84 // MISC
85 extern void hash_warn_on_redefinition(void);
86 extern void hash_dont_warn_on_redefinition(void);
87 extern bool hash_warn_on_redefinition_p(void);
88 
89 // CONSTRUCTORS
91  size_t size,
92  hash_equals_t equals_p,
94 extern hash_table hash_table_make(hash_key_type key_type, size_t size);
95 
96 // DESTRUCTOR
97 extern void hash_table_free(hash_table);
98 
99 // OPERATIONS
100 extern void hash_table_clear(hash_table);
101 extern void * hash_delget(hash_table, const void *, void **);
102 extern void * hash_del(hash_table, const void *);
103 extern void * hash_get(const hash_table, const void *);
104 extern list hash_get_default_empty_list(const hash_table, const void *);
105 
106 extern void hash_put(hash_table, const void *, const void *);
107 extern void hash_update(hash_table, const void *, const void *);
108 extern bool hash_defined_p(const hash_table, const void *);
109 extern void hash_overwrite(hash_table, const void *, const void *);
110 
111 // DUMP
112 extern void hash_table_print_header(const hash_table, FILE *);
113 extern void hash_table_print(const hash_table);
115  const hash_table);
116 
117 // OBSERVERS
118 extern int hash_table_entry_count(const hash_table);
119 extern int hash_table_size(const hash_table);
124 extern void * hash_table_scan(const hash_table, void *, void **, void **);
125 
126 // MAP STUFF (for newgen generated code based on hash tables)
127 extern void * hash_map_get(const hash_table, const void *);
128 extern void hash_map_put(hash_table, const void *, const void *);
129 extern void hash_map_update(hash_table, const void *, const void *);
130 extern void * hash_map_del(const hash_table, const void *);
131 extern bool hash_map_defined_p(const hash_table, const void *);
132 
133 // UTILS
134 extern _uint hash_string_rank(const void *, size_t);
135 
136 #endif // newgen_hash_included
137 
138 /* that is all
139  */
void const char const char const int
static entity rank
void hash_table_print_header(const hash_table, FILE *)
this function prints the header of the hash_table pointed to by htp on the opened stream fout.
Definition: hash.c:510
void hash_update(hash_table, const void *, const void *)
update key->val in htp, that MUST be pre-existent.
Definition: hash.c:491
void hash_map_put(hash_table, const void *, const void *)
Definition: hash.c:895
int hash_table_entry_count(const hash_table)
now we define observers in order to hide the hash_table type...
Definition: hash.c:818
list hash_get_default_empty_list(const hash_table, const void *)
Like hash_get() but returns an empty list instead of HASH_UNDEFINED_VALUE when a key is not found.
Definition: hash.c:475
hash_key_type
Equality and rank functions are provided for strings, integers, pointers and Newgen chunks.
Definition: newgen_hash.h:31
@ hash_int
Definition: newgen_hash.h:32
@ hash_chunk
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
int hash_table_size(const hash_table)
returns the size of the internal array.
Definition: hash.c:825
bool hash_map_defined_p(const hash_table, const void *)
Definition: hash.c:888
_uint(* hash_rank_t)(const void *, size_t)
Definition: newgen_hash.h:44
void hash_dont_warn_on_redefinition(void)
Definition: hash.c:188
hash_table hash_table_make(hash_key_type key_type, size_t size)
Definition: hash.c:294
void hash_overwrite(hash_table, const void *, const void *)
hash_put which allows silent overwrite...
Definition: hash.c:344
bool hash_warn_on_redefinition_p(void)
Definition: hash.c:193
void * hash_map_get(const hash_table, const void *)
newgen mapping to newgen hash...
Definition: hash.c:878
hash_key_type hash_table_type(const hash_table)
returns the type of the hash_table.
Definition: hash.c:832
void hash_table_fprintf(FILE *, gen_string_func_t, gen_string_func_t, const hash_table)
This function prints the content of the hash_table pointed to by htp on file descriptor f,...
Definition: hash.c:548
void hash_table_print(const hash_table)
this function prints the content of the hash_table pointed to by htp on stderr.
Definition: hash.c:525
void * hash_table_scan(const hash_table, void *, void **, void **)
Definition: hash.c:844
void * hash_get(const hash_table, const void *)
this function retrieves in the hash table pointed to by htp the couple whose key is equal to key.
Definition: hash.c:449
void hash_table_clear(hash_table)
Clears all entries of a hash table HTP.
Definition: hash.c:305
_uint hash_string_rank(const void *, size_t)
en s'inspirant vaguement de Fast Hashing of Variable-Length Text Strings Peter K.
Definition: hash.c:642
void hash_warn_on_redefinition(void)
these function set the variable should_i_warn_on_redefinition to the value true or false
Definition: hash.c:183
void * hash_del(hash_table, const void *)
this function removes from the hash table pointed to by htp the couple whose key is equal to key.
Definition: hash.c:439
void hash_put(hash_table, const void *, const void *)
This functions stores a couple (key,val) in the hash table pointed to by htp.
Definition: hash.c:364
int hash_table_own_allocated_memory(const hash_table)
Definition: hash.c:869
hash_table hash_table_generic_make(hash_key_type key_type, size_t size, hash_equals_t equals_p, hash_rank_t rank)
this function makes a hash table of size size.
Definition: hash.c:223
void hash_map_update(hash_table, const void *, const void *)
Definition: hash.c:925
void * hash_map_del(const hash_table, const void *)
Definition: hash.c:905
hash_rank_t hash_table_rank_function(const hash_table)
Definition: hash.c:939
int(* hash_equals_t)(const void *, const void *)
Definition: newgen_hash.h:45
hash_equals_t hash_table_equals_function(const hash_table)
Because the hash table data structure is hidden in this source file hash.c and not exported via the n...
Definition: hash.c:934
struct __hash_table * hash_table
Define hash_table structure which is hidden.
Definition: newgen_hash.h:43
bool hash_defined_p(const hash_table, const void *)
true if key has e value in htp.
Definition: hash.c:484
void * hash_delget(hash_table, const void *, void **)
deletes key from the hash table.
Definition: hash.c:398
void hash_table_free(hash_table)
this function deletes a hash table that is no longer useful.
Definition: hash.c:327
string(* gen_string_func_t)(const void *)
Definition: newgen_types.h:111
uintptr_t _uint
Definition: newgen_types.h:54
size_t size_t
Definition: properties.c:413
size_t size
Definition: hash.c:66
The structure used to build lists in NewGen.
Definition: newgen_list.h:41