Repository navigation
Expand file tree
/
Copy pathunsigned_vec_hash_set.h
More file actions
169 lines (126 loc) · 3.77 KB
/
Copy pathunsigned_vec_hash_set.h
File metadata and controls
169 lines (126 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
Miguel Ramirez, Nir Lipovetzky, Hector Geffner
C^3: A planner for the sequential, satisficing track of the IPC-6
Copyright (C) 2008
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __UNSIGNED_VEC_HASH_SET__
#define __UNSIGNED_VEC_HASH_SET__
#include <stdlib.h>
#include "jenkins_c_version.h"
#include "ff.h"
typedef struct Item {
unsigned len;
unsigned *elems;
Bool pos, neg, use;
/* below field only specified if using as hashtable rather than
set */
int value;
} Item;
void print_item(Item *a) {
unsigned i;
printf("Item length %d, entries ", a->len);
for ( i = 0; i < a->len; i++ ) {
printf("%d, ", a->elems[i]);
}
}
Bool items_equal(Item *a, Item *b) {
unsigned i;
if(a->len != b->len)
return FALSE;
for(i = 0; i < a->len; i++) {
if(a->elems[i] != b->elems[i])
return FALSE;
}
return TRUE;
}
typedef struct Bucket {
unsigned size;
unsigned capacity;
Item **items;
} Bucket;
typedef struct Table {
unsigned size;
Bucket *buckets;
} Table;
#define INITIAL_CAPACITY 16
Table* new_table ( unsigned hash_sz ) {
unsigned i;
Table *t = malloc(sizeof(Table));
t->size = hash_sz;
t->buckets = calloc(hash_sz, sizeof(Bucket));
for(i = 0; i < hash_sz; i++) {
t->buckets[i].size = 0;
t->buckets[i].capacity = INITIAL_CAPACITY;
t->buckets[i].items = malloc(t->buckets[i].capacity * sizeof(Item*));
}
return t;
}
void clear( Table* t ) {
unsigned i, j;
for( i = 0; i < t->size; i++ ) {
t->buckets[i].size = 0;
}
}
void free_table ( Table *t ) {
unsigned i, j;
for( i = 0; i < t->size; i++ ) {
free(t->buckets[i].items);
}
free(t->buckets);
free(t);
}
unsigned compute_hash( Item *item ) {
unsigned i;
unsigned h = 0;
for ( i = 0; i < item->len; i++ ) {
h = jenkins_hash( (ub1*)(&(item->elems[i])), sizeof(unsigned), h );
}
return h;
}
Item* contains_item_w_hash( Table *t, Item *i, unsigned h ) {
unsigned j;
unsigned address = (t->size - 1) & h;
for(j = 0; j < t->buckets[address].size; j++) {
if (items_equal(i, t->buckets[address].items[j])) {
return t->buckets[address].items[j];
}
}
return NULL;
}
Item* contains_item( Table *t, Item *i) {
return contains_item_w_hash( t, i, compute_hash(i) );
}
void add_item_w_hash( Table *t, Item *i, unsigned h ) {
unsigned j;
unsigned address = (t->size - 1) & h;
for(j = 0; j < t->buckets[address].size; j++) {
if (items_equal(i, t->buckets[address].items[j])) {
return;
}
}
if(t->buckets[address].capacity == t->buckets[address].size) {
t->buckets[address].capacity *= 2;
t->buckets[address].items =
realloc(t->buckets[address].items, (t->buckets[address].capacity * sizeof(Item *)) );
}
t->buckets[address].items[t->buckets[address].size++] = i;
}
void add_item( Table *t, Item *i ) {
add_item_w_hash(t, i, compute_hash(i));
}
void remove_item( unsigned h, Item *i ) {
printf(" REMOVE ITEM IN HASH TABLE NOT IMPLEMENTED!!!! " );
exit(0);
return;
}
#endif // unsigned_vec_hash_set.h