isl_hash.h File Reference

#include <isl_stdint.h>

Go to the source code of this file.

Data Structures

struct  isl_hash_table_entry
struct  isl_hash_table

Defines

#define isl_hash_init()   (2166136261u)
#define isl_hash_byte(h, b)
#define isl_hash_hash(h, h2)
#define isl_hash_bits(h, bits)

Functions

uint32_t isl_hash_string (uint32_t hash, const char *s)
struct isl_hash_tableisl_hash_table_alloc (struct isl_ctx *ctx, int min_size)
void isl_hash_table_free (struct isl_ctx *ctx, struct isl_hash_table *table)
int isl_hash_table_init (struct isl_ctx *ctx, struct isl_hash_table *table, int min_size)
void isl_hash_table_clear (struct isl_hash_table *table)
struct isl_hash_table_entryisl_hash_table_find (struct isl_ctx *ctx, struct isl_hash_table *table, uint32_t key_hash, int(*eq)(const void *entry, const void *val), const void *val, int reserve)
void isl_hash_table_remove (struct isl_ctx *ctx, struct isl_hash_table *table, struct isl_hash_table_entry *entry)


Define Documentation

 
#define isl_hash_init (  )     (2166136261u)

Definition at line 10 of file isl_hash.h.

#define isl_hash_byte ( h,
 ) 

Value:

do {                                    \
                                        h *= 16777619;                  \
                                        h ^= b;                         \
                                } while(0)

Definition at line 11 of file isl_hash.h.

#define isl_hash_hash ( h,
h2   ) 

Value:

do {                                                            \
                isl_hash_byte(h, (h2) & 0xFF);                          \
                isl_hash_byte(h, ((h2) >> 8) & 0xFF);                   \
                isl_hash_byte(h, ((h2) >> 16) & 0xFF);                  \
                isl_hash_byte(h, ((h2) >> 24) & 0xFF);                  \
        } while(0)

Definition at line 15 of file isl_hash.h.

#define isl_hash_bits ( h,
bits   ) 

Value:

((bits) == 32) ? (h) :                                          \
        ((bits) >= 16) ?                                                \
              ((h) >> (bits)) ^ ((h) & (((uint32_t)1 << (bits)) - 1)) : \
              (((h) >> (bits)) ^ (h)) & (((uint32_t)1 << (bits)) - 1)

Definition at line 22 of file isl_hash.h.


Function Documentation

uint32_t isl_hash_string ( uint32_t  hash,
const char *  s 
)

Definition at line 5 of file isl_hash.c.

00006 {
00007         for (; *s; s++)
00008                 isl_hash_byte(hash, *s);
00009         return hash;
00010 }

struct isl_hash_table* isl_hash_table_alloc ( struct isl_ctx *  ctx,
int  min_size 
) [read]

Definition at line 45 of file isl_hash.c.

00046 {
00047         struct isl_hash_table *table = NULL;
00048 
00049         table = isl_alloc_type(ctx, struct isl_hash_table);
00050         if (isl_hash_table_init(ctx, table, min_size))
00051                 goto error;
00052         return table;
00053 error:
00054         isl_hash_table_free(ctx, table);
00055         return NULL;
00056 }

void isl_hash_table_free ( struct isl_ctx *  ctx,
struct isl_hash_table table 
)

Definition at line 65 of file isl_hash.c.

00066 {
00067         if (!table)
00068                 return;
00069         isl_hash_table_clear(table);
00070         free(table);
00071 }

int isl_hash_table_init ( struct isl_ctx *  ctx,
struct isl_hash_table table,
int  min_size 
)

Definition at line 23 of file isl_hash.c.

00025 {
00026         size_t size;
00027 
00028         if (!table)
00029                 return -1;
00030 
00031         if (min_size < 2)
00032                 min_size = 2;
00033         table->bits = ffs(round_up(4 * (min_size + 1) / 3 - 1)) - 1;
00034         table->n = 0;
00035 
00036         size = 2 << table->bits;
00037         table->entries = isl_calloc_array(ctx, struct isl_hash_table_entry,
00038                                           size);
00039         if (!table->entries)
00040                 return -1;
00041 
00042         return 0;
00043 }

void isl_hash_table_clear ( struct isl_hash_table table  ) 

Definition at line 58 of file isl_hash.c.

00059 {
00060         if (!table)
00061                 return;
00062         free(table->entries);
00063 }

struct isl_hash_table_entry* isl_hash_table_find ( struct isl_ctx *  ctx,
struct isl_hash_table table,
uint32_t  key_hash,
int(*)(const void *entry, const void *val)  eq,
const void *  val,
int  reserve 
) [read]

Definition at line 73 of file isl_hash.c.

00078 {
00079         size_t size;
00080         uint32_t h, key_bits;
00081 
00082         key_bits = isl_hash_bits(key_hash, table->bits);
00083         size = 2 << table->bits;
00084         for (h = key_bits; table->entries[h].data; h = (h+1) % size)
00085                 if (table->entries[h].hash == key_hash &&
00086                     eq(table->entries[h].data, val))
00087                         return &table->entries[h];
00088 
00089         if (!reserve)
00090                 return NULL;
00091 
00092         assert(4 * table->n < 3 * size); /* XXX */
00093         table->n++;
00094         table->entries[h].hash = key_hash;
00095 
00096         return &table->entries[h];
00097 }

void isl_hash_table_remove ( struct isl_ctx *  ctx,
struct isl_hash_table table,
struct isl_hash_table_entry entry 
)

Definition at line 99 of file isl_hash.c.

00102 {
00103         int h, h2, last_h;
00104         size_t size;
00105 
00106         if (!table || !entry)
00107                 return;
00108 
00109         size = 2 << table->bits;
00110         h = entry - table->entries;
00111         isl_assert(ctx, h >= 0 && h < size, return);
00112 
00113         for (h2 = h+1; table->entries[h2 % size].data; h2++) {
00114                 uint32_t bits = isl_hash_bits(table->entries[h2 % size].hash,
00115                                                 table->bits);
00116                 uint32_t offset = (size + bits - (h+1)) % size;
00117                 if (offset <= h2 - (h+1))
00118                         continue;
00119                 *entry = table->entries[h2 % size];
00120                 h = h2;
00121                 entry = &table->entries[h % size];
00122         }
00123 
00124         entry->hash = 0;
00125         entry->data = NULL;
00126         table->n--;
00127 }


Generated on Fri Jul 17 16:32:57 2009 for CLooG / ISL by  doxygen 1.5.9