PIPS
hashpointer.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include "linear_assert.h"
#include "boolean.h"
+ Include dependency graph for hashpointer.c:

Go to the source code of this file.

Data Structures

struct  paire
 
struct  linear_hashtable_st
 hidden structure to store the hashtable. More...
 

Macros

#define debug_assert(a)
 expected headers: the internal structure does not need to be available! struct linear_hashtable_st; typedef struct linear_hashtable_st * linear_hashtable_pt; More...
 
#define debug_assert_coherent(h)   debug_assert(linear_hashtable_coherent_p(h))
 
#define FREE_CHUNK   ((void *) 0)
 
#define EMPTIED_CHUNK   ((void *) -1)
 
#define HASHTABLE_INITIAL_SIZE   (17)
 size of internal table. More...
 

Typedefs

typedef struct linear_hashtable_stlinear_hashtable_pt
 hidden structure to store the hashtable. More...
 

Functions

static uintptr_t key_location (linear_hashtable_pt h, void *k, bool toget)
 returns the location to put or get k in h. More...
 
static void linear_hashtable_print (FILE *file, linear_hashtable_pt h)
 
void linear_hashtable_dump (linear_hashtable_pt h)
 convenient function to be called from gdb. More...
 
bool linear_hashtable_coherent_p (linear_hashtable_pt h)
 check hashtable coherency More...
 
linear_hashtable_pt linear_hashtable_make (void)
 constructor. More...
 
void linear_hashtable_free (linear_hashtable_pt h)
 destructor More...
 
static void linear_hashtable_extend (linear_hashtable_pt h)
 
static void linear_hashtable_internal_put (linear_hashtable_pt h, void *k, void *v, bool once)
 
void linear_hashtable_put (linear_hashtable_pt h, void *k, void *v)
 
void linear_hashtable_put_once (linear_hashtable_pt h, void *k, void *v)
 
bool linear_hashtable_isin (linear_hashtable_pt h, void *k)
 
bool linear_hashtable_remove (linear_hashtable_pt h, void *k)
 
void * linear_hashtable_get (linear_hashtable_pt h, void *k)
 
int linear_hashtable_nitems (linear_hashtable_pt h)
 

Macro Definition Documentation

◆ debug_assert

#define debug_assert (   a)

expected headers: the internal structure does not need to be available! struct linear_hashtable_st; typedef struct linear_hashtable_st * linear_hashtable_pt;

#define LINEAR_HASHTABLE_DEBUG 1

Definition at line 50 of file hashpointer.c.

◆ debug_assert_coherent

#define debug_assert_coherent (   h)    debug_assert(linear_hashtable_coherent_p(h))

Definition at line 53 of file hashpointer.c.

◆ EMPTIED_CHUNK

#define EMPTIED_CHUNK   ((void *) -1)

Definition at line 56 of file hashpointer.c.

◆ FREE_CHUNK

#define FREE_CHUNK   ((void *) 0)

Definition at line 55 of file hashpointer.c.

◆ HASHTABLE_INITIAL_SIZE

#define HASHTABLE_INITIAL_SIZE   (17)

size of internal table.

should be a not too big odd number.

Definition at line 160 of file hashpointer.c.

Typedef Documentation

◆ linear_hashtable_pt

hidden structure to store the hashtable.

Function Documentation

◆ key_location()

static uintptr_t key_location ( linear_hashtable_pt  h,
void *  k,
bool  toget 
)
static

returns the location to put or get k in h.

should not loop to put!

if !loop and toget, the initial index is returned. it is checked against the expected key before returning the value.

Definition at line 75 of file hashpointer.c.

76 {
77  uintptr_t index = ((((uintptr_t) k)
78  & ~(((uintptr_t)1) << ((sizeof(uintptr_t)*CHAR_BIT) - 1)))
79  % (h->size));
80  uintptr_t loop = h->size;
81 
82  while (loop-- && !(h->contents[index].key==FREE_CHUNK ||
83  h->contents[index].key==k ||
84  (toget && h->contents[index].key==EMPTIED_CHUNK)))
85  index = (index+1) % h->size;
86 
87  assert(!(!loop && !toget)); /* should not loop to put! */
88 
89  /* if !loop and toget, the initial index is returned.
90  * it is checked against the expected key before returning the value.
91  */
92  debug_assert(index>=0 && index<h->size &&
93  (h->contents[index].key==FREE_CHUNK ||
94  h->contents[index].key==EMPTIED_CHUNK ||
95  (h->contents[index].key==k || (!loop && toget))));
96 
97  return index;
98 }
#define FREE_CHUNK
Definition: hashpointer.c:55
#define debug_assert(a)
expected headers: the internal structure does not need to be available! struct linear_hashtable_st; t...
Definition: hashpointer.c:50
#define EMPTIED_CHUNK
Definition: hashpointer.c:56
#define assert(ex)
Definition: newgen_assert.h:41
#define uintptr_t
Definition: stdint.in.h:295
size_t size
number of association stored
Definition: hashpointer.c:68
paire * contents
size of internal array
Definition: hashpointer.c:69
void * key
Definition: hashpointer.c:59

References assert, linear_hashtable_st::contents, debug_assert, EMPTIED_CHUNK, FREE_CHUNK, paire::key, linear_hashtable_st::size, and uintptr_t.

Referenced by linear_hashtable_coherent_p(), linear_hashtable_extend(), linear_hashtable_get(), linear_hashtable_internal_put(), linear_hashtable_isin(), linear_hashtable_print(), and linear_hashtable_remove().

+ Here is the caller graph for this function:

◆ linear_hashtable_coherent_p()

bool linear_hashtable_coherent_p ( linear_hashtable_pt  h)

check hashtable coherency

coherent size/nitems.

check number of item stored.

check key index

Definition at line 128 of file hashpointer.c.

129 {
130  uintptr_t i, n;
131 
132  /* coherent size/nitems. */
133  if (h->nitems >= h->size)
134  return false;
135 
136  /* check number of item stored. */
137  for(i=0, n=0; i<h->size; i++)
138  {
139  void * k = h->contents[i].key;
140  if (k!=FREE_CHUNK && k!=EMPTIED_CHUNK)
141  {
142  /* check key index */
143  uintptr_t index = key_location(h, k, true);
144  if (index!=i) return false;
145  n++;
146  }
147  }
148 
149  if (n!=h->nitems)
150  return false;
151 
152  return true;
153 }
static uintptr_t key_location(linear_hashtable_pt h, void *k, bool toget)
returns the location to put or get k in h.
Definition: hashpointer.c:75

References linear_hashtable_st::contents, EMPTIED_CHUNK, FREE_CHUNK, paire::key, key_location(), linear_hashtable_st::nitems, linear_hashtable_st::size, and uintptr_t.

+ Here is the call graph for this function:

◆ linear_hashtable_dump()

void linear_hashtable_dump ( linear_hashtable_pt  h)

convenient function to be called from gdb.

hashpointer.c

Definition at line 122 of file hashpointer.c.

123 {
124  linear_hashtable_print(stderr, h);
125 }
static void linear_hashtable_print(FILE *file, linear_hashtable_pt h)
Definition: hashpointer.c:102

References linear_hashtable_print().

+ Here is the call graph for this function:

◆ linear_hashtable_extend()

static void linear_hashtable_extend ( linear_hashtable_pt  h)
static

check malloc

the expected number of items was moved.

Definition at line 197 of file hashpointer.c.

198 {
199  paire * oldcontents;
200  uintptr_t i, oldsize, moved_nitems;
201 
203 
204  moved_nitems = h->nitems;
205  oldcontents = h->contents;
206  oldsize = h->size;
207 
208  h->size = 2*oldsize + 1;
209  h->contents = (paire*) malloc(sizeof(paire)*h->size);
210  assert(h->contents); /* check malloc */
211 
212  for (i=0; i<h->size; i++)
213  h->contents[i].key = FREE_CHUNK,
214  h->contents[i].val = FREE_CHUNK;
215 
216  for (i=0; i<oldsize; i++)
217  {
218  register void * k = oldcontents[i].key;
219  if (k!=FREE_CHUNK && k!=EMPTIED_CHUNK)
220  {
221  register int index = key_location(h, k, false);
222  h->contents[index].key = k;
223  h->contents[index].val = oldcontents[i].val;
224  moved_nitems--;
225  }
226  }
227 
228  assert(moved_nitems==0); /* the expected number of items was moved. */
229  free(oldcontents);
230 
232 }
void * malloc(YYSIZE_T)
void free(void *)
#define debug_assert_coherent(h)
Definition: hashpointer.c:53
void * val
Definition: hashpointer.c:60

References assert, linear_hashtable_st::contents, debug_assert_coherent, EMPTIED_CHUNK, free(), FREE_CHUNK, paire::key, key_location(), malloc(), linear_hashtable_st::nitems, linear_hashtable_st::size, uintptr_t, and paire::val.

Referenced by linear_hashtable_internal_put().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ linear_hashtable_free()

void linear_hashtable_free ( linear_hashtable_pt  h)

destructor

Definition at line 189 of file hashpointer.c.

190 {
192 
193  free(h->contents);
194  free(h);
195 }

References linear_hashtable_st::contents, debug_assert_coherent, and free().

Referenced by base_append(), base_included_p(), base_union(), fortran_data_to_prec_for_variables(), sc_to_minimal_basis(), transitive_closure_from_two_bases(), and vect_check().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ linear_hashtable_get()

void* linear_hashtable_get ( linear_hashtable_pt  h,
void *  k 
)

Definition at line 293 of file hashpointer.c.

294 {
295  register int index = key_location(h, k, true);
296  return h->contents[index].key==k ? h->contents[index].val: FREE_CHUNK;
297 }

References linear_hashtable_st::contents, FREE_CHUNK, paire::key, key_location(), and paire::val.

+ Here is the call graph for this function:

◆ linear_hashtable_internal_put()

static void linear_hashtable_internal_put ( linear_hashtable_pt  h,
void *  k,
void *  v,
bool  once 
)
static

no special values!

50% limit to extend

is it already in?

no

where should it be put?

if it is in, the once option must not be set

update number of stored items

Definition at line 236 of file hashpointer.c.

238 {
239  register int index;
240 
241  assert(k!=FREE_CHUNK && k!=EMPTIED_CHUNK); /* no special values! */
243 
244  if ((h->nitems*2) > h->size) { /* 50% limit to extend */
246  }
247 
248  index = key_location(h, k, true); /* is it already in? */
249  if (h->contents[index].key!=k) /* no */
250  index = key_location(h, k, false); /* where should it be put? */
251  else
252  assert(!once); /* if it is in, the once option must not be set */
253 
254  if (h->contents[index].key!=k) /* update number of stored items */
255  h->nitems++;
256 
257  h->contents[index].key = k;
258  h->contents[index].val = v;
259 
261 }
static void linear_hashtable_extend(linear_hashtable_pt h)
Definition: hashpointer.c:197

References assert, linear_hashtable_st::contents, debug_assert_coherent, EMPTIED_CHUNK, FREE_CHUNK, paire::key, key_location(), linear_hashtable_extend(), linear_hashtable_st::nitems, linear_hashtable_st::size, and paire::val.

Referenced by linear_hashtable_put(), and linear_hashtable_put_once().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ linear_hashtable_isin()

bool linear_hashtable_isin ( linear_hashtable_pt  h,
void *  k 
)

Definition at line 273 of file hashpointer.c.

274 {
275  return h->contents[key_location(h, k, true)].key==k;
276 }

References linear_hashtable_st::contents, paire::key, and key_location().

Referenced by base_append(), base_included_p(), base_to_set(), base_union(), contains_variables(), fortran_data_to_prec_for_variables(), sc_to_minimal_basis(), and vect_check().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ linear_hashtable_make()

linear_hashtable_pt linear_hashtable_make ( void  )

constructor.

returns a newly allocated hashtable.

check malloc

check malloc

Definition at line 165 of file hashpointer.c.

166 {
168  register int i, size = HASHTABLE_INITIAL_SIZE;
169 
170  h = (linear_hashtable_pt) malloc(sizeof(struct linear_hashtable_st));
171  assert(h); /* check malloc */
172 
173  h->size = size;
174  h->nitems = 0;
175  h->contents = (paire*) malloc(sizeof(paire)*size);
176 
177  assert(h->contents); /* check malloc */
178 
179  for (i=0; i<size; i++)
180  h->contents[i].key = FREE_CHUNK,
181  h->contents[i].val = FREE_CHUNK;
182 
184 
185  return h;
186 }
struct linear_hashtable_st * linear_hashtable_pt
hidden structure to store the hashtable.
#define HASHTABLE_INITIAL_SIZE
size of internal table.
Definition: hashpointer.c:160
hidden structure to store the hashtable.
Definition: hashpointer.c:66

References assert, linear_hashtable_st::contents, debug_assert_coherent, FREE_CHUNK, HASHTABLE_INITIAL_SIZE, paire::key, malloc(), linear_hashtable_st::nitems, linear_hashtable_st::size, and paire::val.

Referenced by base_append(), base_included_p(), base_union(), fortran_data_to_prec_for_variables(), sc_to_minimal_basis(), transitive_closure_from_two_bases(), and vect_check().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ linear_hashtable_nitems()

int linear_hashtable_nitems ( linear_hashtable_pt  h)

Definition at line 299 of file hashpointer.c.

300 {
301  return h->nitems;
302 }

References linear_hashtable_st::nitems.

◆ linear_hashtable_print()

static void linear_hashtable_print ( FILE *  file,
linear_hashtable_pt  h 
)
static

Definition at line 102 of file hashpointer.c.

103 {
104  uintptr_t i;
105 
106  fprintf(file, "[linear_hashtable_dump] hash=%p size=%zd nitems=%zd\n",
107  h, h->size, h->nitems);
108 
109  for (i=0; i<h->size; i++)
110  {
111  void * k = h->contents[i].key;
112  fprintf(file, "%zd (%td): 0x%p -> 0x%p\n",
113  i,
114  (k!=FREE_CHUNK && k!=EMPTIED_CHUNK)? key_location(h, k, true): ~(uintptr_t)0,
115  k, h->contents[i].val);
116  }
117 
118  fprintf(file, "[linear_hashtable_dump] done.\n");
119 }
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...

References linear_hashtable_st::contents, EMPTIED_CHUNK, fprintf(), FREE_CHUNK, paire::key, key_location(), linear_hashtable_st::nitems, linear_hashtable_st::size, uintptr_t, and paire::val.

Referenced by linear_hashtable_dump().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ linear_hashtable_put()

void linear_hashtable_put ( linear_hashtable_pt  h,
void *  k,
void *  v 
)

Definition at line 263 of file hashpointer.c.

264 {
265  linear_hashtable_internal_put(h, k, v, false);
266 }
static void linear_hashtable_internal_put(linear_hashtable_pt h, void *k, void *v, bool once)
Definition: hashpointer.c:237

References linear_hashtable_internal_put().

Referenced by base_to_set(), and vect_check().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ linear_hashtable_put_once()

void linear_hashtable_put_once ( linear_hashtable_pt  h,
void *  k,
void *  v 
)

Definition at line 268 of file hashpointer.c.

269 {
270  linear_hashtable_internal_put(h, k, v, true);
271 }

References linear_hashtable_internal_put().

Referenced by base_append(), base_included_p(), base_union(), fortran_data_to_prec_for_variables(), and sc_to_minimal_basis().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ linear_hashtable_remove()

bool linear_hashtable_remove ( linear_hashtable_pt  h,
void *  k 
)

Definition at line 278 of file hashpointer.c.

279 {
280  register int index = key_location(h, k, true);
281 
282  if (h->contents[index].key==k)
283  {
284  h->contents[index].key = EMPTIED_CHUNK;
285  h->contents[index].val = FREE_CHUNK;
286  h->nitems--;
287  return true;
288  }
289 
290  return false;
291 }

References linear_hashtable_st::contents, EMPTIED_CHUNK, FREE_CHUNK, paire::key, key_location(), linear_hashtable_st::nitems, and paire::val.

+ Here is the call graph for this function: