2 #include <HashVector.h>
4 // Find the slot for the keyed element, and return pointer to it, or
5 // to the first of holes encountered while considering collisions.
6 // Returns a pointer to the place for the item, or 0 in case of OOM or
7 // overfull HashVector (i.e. 0 shouldn't happen).
8 // If itemkey is set, then the itemkey callback function is used for
9 // obtaining a temporary key from the item.
10 static void **HashVector_find_slot(
11 HashVector *hv, void *key, unsigned long *i, int itemkey )
14 // Get actual key from keying item
15 key = ItemKeyFun_itemkey( hv->type, key );
17 unsigned long index = ItemKeyFun_hashcode( hv->type, key ) % hv->table.size;
22 p = Vector_entry( &hv->table, (*i) );
25 ItemKeyFun_releasekey( hv->type, key );
27 return 0; // This basically means OOM, and is a failure condition.
31 ItemKeyFun_releasekey( hv->type, key );
33 return ( hole )? hole : p; // Not found; it's place is here.
35 if ( (*p) == HV_HOLE ) {
37 hole = p; // Remember the first hole
39 } else if ( ItemKeyFun_haskey( hv->type, (*p), key ) ) {
41 ItemKeyFun_releasekey( hv->type, key );
45 if ( ++(*i) == hv->table.size ) {
46 (*i) = 0; // Roll-around the table
48 if ( (*i) == index ) {
50 ItemKeyFun_releasekey( hv->type, key );
52 return 0; // Overfull HashVector!
57 // Find the keyed item
58 void *HashVector_find(HashVector *hv,void *key) {
59 VectorIndex index = 0;
60 void **slot = HashVector_find_slot( hv, key, &index, 0 );
61 return ( slot && *slot && *slot != HV_HOLE )? *slot : 0;
64 // Find any element at or after the index that admits to the key.
65 // Update index and return item.
66 void *HashVector_next(HashVector *hv,VectorIndex *index) {
67 for ( ; (*index) < hv->table.size; (*index)++ ) {
68 void **p = Vector_next_used( &hv->table, index );
72 if ( *p && *p != HV_HOLE ) {
76 (*index) = hv->table.size;
80 static int capture_item(Vector *pv,unsigned long ix,void *item,void *data) {
82 if ( item && item != HV_HOLE ) {
83 HashVector_add( (HashVector *) data, item );
88 static int iter_item(unsigned long ix,void *item,void *data) {
90 if ( item && item != HV_HOLE ) {
91 HashVector_add( (HashVector *) data, item );
96 static void HashVector_resize(HashVector *hv,unsigned long new_size) {
98 hv->table.size = new_size;
99 hv->table.entries = 0;
102 if ( new_size < hv->table.size ) {
103 Vector_resize( &tmp.table, 0, capture_item, hv );
105 Vector_iterate( &tmp.table, 0, iter_item, hv );
109 // Add the given element.
110 int HashVector_add(HashVector *hv,void *item) {
112 void **p = HashVector_find_slot( hv, item, &i, 1 );
114 return -1; // OOM or overfull HashVector
117 if ( *p != HV_HOLE ) {
118 return 0; // Already added.
120 hv->holes--; // about to reuse a hole
124 if ( hv->fill + hv->holes > hv->table.size / 2 ) {
125 HashVector_resize( hv, hv->table.size * 2 );
130 // Delete the given item
131 int HashVector_delete(HashVector *hv,void *item) {
133 void **p = HashVector_find_slot( hv, item, &i, 1 );
140 // Check if subsequent slot is occupied
141 if ( ++i >= hv->table.size ) {
144 void **q = Vector_access( &hv->table, i, 0, 0 );
152 if ( hv->table.size > VECTOR_SLOTS( &hv->table ) &&
153 hv->fill < hv->table.size / 4 ) {
154 HashVector_resize( hv, hv->table.size / 2 );
159 Vector *HashVector_contents(
160 HashVector *hv,enum VectorVariant variant,Vector *v)
163 if ( hv->fill == 0 ) {
166 v = (Vector*) malloc( sizeof( Vector ) );
168 Vector_resize( v, 0, Vector_clear_any, 0 );
170 (*v) = (Vector) { .variant = variant, 0, 0 };
171 if ( hv->fill == 0 ) {
174 Vector_resize( v, hv->fill, 0, 0 );
177 for ( i = 0; i < v->size; i++, j++ ) {
178 Vector_set( v, i, HashVector_next( hv, &j ) );
183 // A simple binary hashcode, (before modulo table size)
184 unsigned long HashVector_hashcode(unsigned char *key,unsigned long n) {
185 unsigned long value = 5381;
187 value += ( value << 5 ) + *(key++);
193 HashVector *HashVector_create(enum VectorVariant variant,ItemKeyFun *type) {
194 HashVector *hv = (HashVector*) malloc( sizeof( HashVector ) );
195 (*hv) = (HashVector) {