|
| 1 | +#ifndef DICTIONARY_H_ |
| 2 | +#define DICTIONARY_H_ |
| 3 | + |
| 4 | +#include <stdlib.h> |
| 5 | + |
| 6 | +#define DEFINE_DICTIONARY(Name, _KeyType, _ValueType) \ |
| 7 | + typedef _KeyType Name##KeyType; \ |
| 8 | + typedef _ValueType Name##ValueType; \ |
| 9 | + \ |
| 10 | + struct Name##Entry { \ |
| 11 | + Name##KeyType key; \ |
| 12 | + Name##ValueType value; \ |
| 13 | + }; \ |
| 14 | + \ |
| 15 | + struct Name { \ |
| 16 | + struct Name##Entry** _buckets; \ |
| 17 | + int _capacity; \ |
| 18 | + int _size; \ |
| 19 | + }; \ |
| 20 | + \ |
| 21 | + int Name##_cmp(const Name##KeyType* a, const Name##KeyType* b); \ |
| 22 | + int Name##_hash(const Name##KeyType* key); \ |
| 23 | + void Name##_key_copy(Name##KeyType* dest, const Name##KeyType* src); \ |
| 24 | + void Name##_value_copy(Name##ValueType* dest, const Name##ValueType* src); \ |
| 25 | + \ |
| 26 | + struct Name* Name##_new(const int capacity); \ |
| 27 | + void Name##_delete(struct Name* self); \ |
| 28 | + int Name##_capacity(struct Name* self); \ |
| 29 | + Name##ValueType* Name##_get(struct Name* self, const Name##KeyType key); \ |
| 30 | + int Name##_hash(const Name##KeyType* key); \ |
| 31 | + void Name##_put(struct Name* self, const Name##KeyType key, \ |
| 32 | + const Name##ValueType value); \ |
| 33 | + int Name##_size(struct Name* self); \ |
| 34 | + \ |
| 35 | + int _Name##_probe_for_free(struct Name* self, const Name##KeyType* key); \ |
| 36 | + int _Name##_probe_for_hit(struct Name* self, const Name##KeyType* key); \ |
| 37 | + \ |
| 38 | + struct Name* Name##_new(const int capacity) { \ |
| 39 | + struct Name* dict = (struct Name*)calloc(1, sizeof(struct Name)); \ |
| 40 | + dict->_buckets = \ |
| 41 | + (struct Name##Entry**)calloc(capacity, sizeof(struct Name##Entry*)); \ |
| 42 | + dict->_capacity = capacity; \ |
| 43 | + dict->_size = 0; \ |
| 44 | + return dict; \ |
| 45 | + } \ |
| 46 | + \ |
| 47 | + void Name##_delete(struct Name* self) { \ |
| 48 | + for (int i = 0; i < self->_capacity; ++i) { \ |
| 49 | + free(self->_buckets[i]); \ |
| 50 | + } \ |
| 51 | + free(self->_buckets); \ |
| 52 | + free(self); \ |
| 53 | + } \ |
| 54 | + \ |
| 55 | + int Name##_capacity(struct Name* self) { return self->_capacity; } \ |
| 56 | + \ |
| 57 | + Name##ValueType* Name##_get(struct Name* self, const Name##KeyType key) { \ |
| 58 | + int i = _Name##_probe_for_hit(self, &key); \ |
| 59 | + if (self->_buckets[i]) { \ |
| 60 | + return &(self->_buckets[i]->value); \ |
| 61 | + } else { \ |
| 62 | + return NULL; \ |
| 63 | + } \ |
| 64 | + } \ |
| 65 | + \ |
| 66 | + void Name##_put(struct Name* self, const Name##KeyType key, \ |
| 67 | + const Name##ValueType value) { \ |
| 68 | + int i = _Name##_probe_for_hit(self, &key); \ |
| 69 | + if (self->_buckets[i]) { \ |
| 70 | + Name##_value_copy(&(self->_buckets[i]->value), &value); \ |
| 71 | + } else { \ |
| 72 | + i = _Name##_probe_for_free(self, &key); \ |
| 73 | + self->_buckets[i] = \ |
| 74 | + (struct Name##Entry*)calloc(1, sizeof(struct Name##Entry)); \ |
| 75 | + Name##_key_copy(&(self->_buckets[i]->key), &key); \ |
| 76 | + Name##_value_copy(&(self->_buckets[i]->value), &value); \ |
| 77 | + ++(self->_size); \ |
| 78 | + } \ |
| 79 | + } \ |
| 80 | + \ |
| 81 | + int Name##_size(struct Name* self) { return self->_size; } \ |
| 82 | + \ |
| 83 | + int _Name##_probe_for_free(struct Name* self, const Name##KeyType* key) { \ |
| 84 | + int hash = Name##_hash(key); \ |
| 85 | + int i = hash % (self->_capacity); \ |
| 86 | + while (self->_buckets[i]) { \ |
| 87 | + i = (i + 1) % (self->_capacity); \ |
| 88 | + } \ |
| 89 | + return i; \ |
| 90 | + } \ |
| 91 | + \ |
| 92 | + int _Name##_probe_for_hit(struct Name* self, const Name##KeyType* key) { \ |
| 93 | + int hash = Name##_hash(key); \ |
| 94 | + int i = hash % (self->_capacity); \ |
| 95 | + while (self->_buckets[i] && Name##_cmp(key, &(self->_buckets[i]->key))) { \ |
| 96 | + i = (i + 1) % (self->_capacity); \ |
| 97 | + } \ |
| 98 | + return i; \ |
| 99 | + } |
| 100 | + |
| 101 | +#endif // DICTIONARY_H_ |
0 commit comments