2
#include "fch_buckets.h"
9
typedef struct __fch_bucket_entry_t
15
typedef struct __fch_bucket_t
17
fch_bucket_entry_t * entries;
18
cmph_uint32 capacity, size;
23
static void fch_bucket_new(fch_bucket_t *bucket)
27
bucket->entries = NULL;
31
static void fch_bucket_destroy(fch_bucket_t *bucket)
35
for (i = 0; i < bucket->size; i++)
37
free((bucket->entries + i)->value);
39
free(bucket->entries);
43
static void fch_bucket_reserve(fch_bucket_t *bucket, cmph_uint32 size)
46
if (bucket->capacity < size)
48
cmph_uint32 new_capacity = bucket->capacity + 1;
49
DEBUGP("Increasing current capacity %u to %u\n", bucket->capacity, size);
50
while (new_capacity < size)
54
bucket->entries = (fch_bucket_entry_t *)realloc(bucket->entries, sizeof(fch_bucket_entry_t)*new_capacity);
55
assert(bucket->entries);
56
bucket->capacity = new_capacity;
57
DEBUGP("Increased\n");
61
static void fch_bucket_insert(fch_bucket_t *bucket, char *val, cmph_uint32 val_length)
64
fch_bucket_reserve(bucket, bucket->size + 1);
65
(bucket->entries + bucket->size)->value = val;
66
(bucket->entries + bucket->size)->length = val_length;
71
static cmph_uint8 fch_bucket_is_empty(fch_bucket_t *bucket)
74
return (cmph_uint8)(bucket->size == 0);
77
static cmph_uint32 fch_bucket_size(fch_bucket_t *bucket)
83
static char * fch_bucket_get_key(fch_bucket_t *bucket, cmph_uint32 index_key)
85
assert(bucket); assert(index_key < bucket->size);
86
return (bucket->entries + index_key)->value;
89
static cmph_uint32 fch_bucket_get_length(fch_bucket_t *bucket, cmph_uint32 index_key)
91
assert(bucket); assert(index_key < bucket->size);
92
return (bucket->entries + index_key)->length;
95
static void fch_bucket_print(fch_bucket_t * bucket, cmph_uint32 index)
99
fprintf(stderr, "Printing bucket %u ...\n", index);
100
for (i = 0; i < bucket->size; i++)
102
fprintf(stderr, " key: %s\n", (bucket->entries + i)->value);
106
//////////////////////////////////////////////////////////////////////////////////////
108
struct __fch_buckets_t
110
fch_bucket_t * values;
111
cmph_uint32 nbuckets, max_size;
115
fch_buckets_t * fch_buckets_new(cmph_uint32 nbuckets)
118
fch_buckets_t *buckets = (fch_buckets_t *)malloc(sizeof(fch_buckets_t));
120
buckets->values = (fch_bucket_t *)calloc((size_t)nbuckets, sizeof(fch_bucket_t));
121
for (i = 0; i < nbuckets; i++) fch_bucket_new(buckets->values + i);
122
assert(buckets->values);
123
buckets->nbuckets = nbuckets;
124
buckets->max_size = 0;
128
cmph_uint8 fch_buckets_is_empty(fch_buckets_t * buckets, cmph_uint32 index)
130
assert(index < buckets->nbuckets);
131
return fch_bucket_is_empty(buckets->values + index);
134
void fch_buckets_insert(fch_buckets_t * buckets, cmph_uint32 index, char * key, cmph_uint32 length)
136
assert(index < buckets->nbuckets);
137
fch_bucket_insert(buckets->values + index, key, length);
138
if (fch_bucket_size(buckets->values + index) > buckets->max_size)
140
buckets->max_size = fch_bucket_size(buckets->values + index);
144
cmph_uint32 fch_buckets_get_size(fch_buckets_t * buckets, cmph_uint32 index)
146
assert(index < buckets->nbuckets);
147
return fch_bucket_size(buckets->values + index);
151
char * fch_buckets_get_key(fch_buckets_t * buckets, cmph_uint32 index, cmph_uint32 index_key)
153
assert(index < buckets->nbuckets);
154
return fch_bucket_get_key(buckets->values + index, index_key);
157
cmph_uint32 fch_buckets_get_keylength(fch_buckets_t * buckets, cmph_uint32 index, cmph_uint32 index_key)
159
assert(index < buckets->nbuckets);
160
return fch_bucket_get_length(buckets->values + index, index_key);
163
cmph_uint32 fch_buckets_get_max_size(fch_buckets_t * buckets)
165
return buckets->max_size;
168
cmph_uint32 fch_buckets_get_nbuckets(fch_buckets_t * buckets)
170
return buckets->nbuckets;
173
cmph_uint32 * fch_buckets_get_indexes_sorted_by_size(fch_buckets_t * buckets)
176
cmph_uint32 sum = 0, value;
177
cmph_uint32 *nbuckets_size = (cmph_uint32 *) calloc((size_t)buckets->max_size + 1, sizeof(cmph_uint32));
178
cmph_uint32 * sorted_indexes = (cmph_uint32 *) calloc((size_t)buckets->nbuckets, sizeof(cmph_uint32));
180
// collect how many buckets for each size.
181
for(i = 0; i < buckets->nbuckets; i++) nbuckets_size[fch_bucket_size(buckets->values + i)] ++;
183
// calculating offset considering a decreasing order of buckets size.
184
value = nbuckets_size[buckets->max_size];
185
nbuckets_size[buckets->max_size] = sum;
186
for(i = (int)buckets->max_size - 1; i >= 0; i--)
189
value = nbuckets_size[i];
190
nbuckets_size[i] = sum;
193
for(i = 0; i < buckets->nbuckets; i++)
195
sorted_indexes[nbuckets_size[fch_bucket_size(buckets->values + i)]] = (cmph_uint32)i;
196
nbuckets_size[fch_bucket_size(buckets->values + i)] ++;
199
return sorted_indexes;
202
void fch_buckets_print(fch_buckets_t * buckets)
205
for (i = 0; i < buckets->nbuckets; i++) fch_bucket_print(buckets->values + i, i);
208
void fch_buckets_destroy(fch_buckets_t * buckets)
211
for (i = 0; i < buckets->nbuckets; i++) fch_bucket_destroy(buckets->values + i);
212
free(buckets->values);