dict.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "pocketpy/objects/dict.h"
  2. #include "pocketpy/common/utils.h"
  3. #include <stdlib.h>
  4. #include <assert.h>
  5. #include <string.h>
  6. struct pkpy_DictEntry {
  7. int64_t hash;
  8. pkpy_Var key;
  9. pkpy_Var val;
  10. };
  11. inline static int pkpy_Dict__idx_size(const pkpy_Dict* self) {
  12. if(self->count < 255) return 1;
  13. if(self->count < 65535) return 2;
  14. return 4;
  15. }
  16. inline static int pkpy_Dict__idx_null(const pkpy_Dict* self) {
  17. if(self->count < 255) return 255;
  18. if(self->count < 65535) return 65535;
  19. return 4294967295;
  20. }
  21. inline static int pkpy_Dict__ht_byte_size(const pkpy_Dict* self) { return self->_htcap * pkpy_Dict__idx_size(self); }
  22. void pkpy_Dict__ctor(pkpy_Dict* self) {
  23. self->_version = 0;
  24. self->count = 0;
  25. c11_vector__ctor(&self->_entries, sizeof(struct pkpy_DictEntry));
  26. self->_htcap = 16;
  27. self->_hashtable = malloc(pkpy_Dict__ht_byte_size(self));
  28. memset(self->_hashtable, 0xff, pkpy_Dict__ht_byte_size(self));
  29. }
  30. void pkpy_Dict__dtor(pkpy_Dict* self) {
  31. c11_vector__dtor(&self->_entries);
  32. free(self->_hashtable);
  33. }
  34. pkpy_Dict pkpy_Dict__copy(const pkpy_Dict* self) {
  35. int ht_size = pkpy_Dict__ht_byte_size(self);
  36. void* ht_clone = malloc(ht_size);
  37. memcpy(ht_clone, self->_hashtable, ht_size);
  38. return (pkpy_Dict){._version = 0,
  39. .count = self->count,
  40. ._entries = c11_vector__copy(&self->_entries),
  41. ._htcap = self->_htcap,
  42. ._hashtable = ht_clone};
  43. }
  44. static int pkpy_Dict__htget(const pkpy_Dict* self, int h) {
  45. int sz = pkpy_Dict__idx_size(self);
  46. switch(sz) {
  47. case 1: return ((uint8_t*)self->_hashtable)[h];
  48. case 2: return ((uint16_t*)self->_hashtable)[h];
  49. case 4: return ((uint32_t*)self->_hashtable)[h];
  50. default: PK_UNREACHABLE();
  51. }
  52. }
  53. static void pkpy_Dict__htset(pkpy_Dict* self, int h, int v) {
  54. int sz = pkpy_Dict__idx_size(self);
  55. switch(sz) {
  56. case 1: ((uint8_t*)self->_hashtable)[h] = v; break;
  57. case 2: ((uint16_t*)self->_hashtable)[h] = v; break;
  58. case 4: ((uint32_t*)self->_hashtable)[h] = v; break;
  59. default: PK_UNREACHABLE();
  60. }
  61. }
  62. static int pkpy_Dict__probe(const pkpy_Dict* self, void* vm, pkpy_Var key, int64_t hash) {
  63. const int null = pkpy_Dict__idx_null(self);
  64. const int mask = self->_htcap - 1;
  65. for(int h = hash & mask;; h = (h + 1) & mask) {
  66. int idx = pkpy_Dict__htget(self, h);
  67. if(idx == null) return h;
  68. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
  69. if(pkpy_Var__is_null(&entry->key)) return h;
  70. if(entry->hash == hash && pkpy_Var__eq__(vm, entry->key, key)) return h;
  71. }
  72. PK_UNREACHABLE();
  73. }
  74. static void pkpy_Dict__extendht(pkpy_Dict* self, void* vm) {
  75. self->_version += 1;
  76. free(self->_hashtable);
  77. self->_htcap *= 2;
  78. void* new_ht = malloc(pkpy_Dict__ht_byte_size(self));
  79. memset(new_ht, 0xff, pkpy_Dict__ht_byte_size(self));
  80. for(int i = 0; i < self->_entries.count; i++) {
  81. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, i);
  82. if(pkpy_Var__is_null(&entry->key)) continue;
  83. int h = pkpy_Dict__probe(self, vm, entry->key, entry->hash);
  84. pkpy_Dict__htset(self, h, i);
  85. }
  86. }
  87. static bool pkpy_Dict__refactor(pkpy_Dict* self, void* vm) {
  88. int deleted_slots = self->_entries.count - self->count;
  89. if(deleted_slots < self->_entries.count * 0.25) return false;
  90. // shrink
  91. self->_version += 1;
  92. free(self->_hashtable);
  93. while(self->_htcap * 0.375 > self->count && self->_htcap >= 32)
  94. self->_htcap /= 2;
  95. self->_hashtable = malloc(pkpy_Dict__ht_byte_size(self));
  96. memset(self->_hashtable, 0xff, pkpy_Dict__ht_byte_size(self));
  97. c11_vector new_entries;
  98. c11_vector__ctor(&new_entries, sizeof(struct pkpy_DictEntry));
  99. for(int i = 0; i < self->_entries.count; i++) {
  100. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, i);
  101. if(pkpy_Var__is_null(&entry->key)) continue;
  102. int j = new_entries.count;
  103. c11_vector__push(struct pkpy_DictEntry, &new_entries, *entry);
  104. pkpy_Dict__htset(self, pkpy_Dict__probe(self, vm, entry->key, entry->hash), j);
  105. }
  106. c11_vector__dtor(&self->_entries);
  107. self->_entries = new_entries;
  108. return true;
  109. }
  110. bool pkpy_Dict__set(pkpy_Dict* self, void* vm, pkpy_Var key, pkpy_Var val) {
  111. int hash = pkpy_Var__hash__(vm, key);
  112. int h = pkpy_Dict__probe(self, vm, key, hash);
  113. int idx = pkpy_Dict__htget(self, h);
  114. if(idx == pkpy_Dict__idx_null(self)) {
  115. self->_version += 1;
  116. idx = self->_entries.count;
  117. c11_vector__push(struct pkpy_DictEntry,
  118. &self->_entries,
  119. ((struct pkpy_DictEntry){
  120. .hash = hash,
  121. .key = key,
  122. .val = val,
  123. }));
  124. pkpy_Dict__htset(self, h, idx);
  125. self->count += 1;
  126. if(self->count >= self->_htcap * 0.75) pkpy_Dict__extendht(self, vm);
  127. return true;
  128. }
  129. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
  130. assert(entry->hash == hash && pkpy_Var__eq__(vm, entry->key, key));
  131. entry->val = val;
  132. return false;
  133. }
  134. bool pkpy_Dict__contains(const pkpy_Dict* self, void* vm, pkpy_Var key) {
  135. int hash = pkpy_Var__hash__(vm, key);
  136. int h = pkpy_Dict__probe(self, vm, key, hash);
  137. int idx = pkpy_Dict__htget(self, h);
  138. if(idx == pkpy_Dict__idx_null(self)) return false;
  139. return true;
  140. }
  141. bool pkpy_Dict__del(pkpy_Dict* self, void* vm, pkpy_Var key) {
  142. int hash = pkpy_Var__hash__(vm, key);
  143. int h = pkpy_Dict__probe(self, vm, key, hash);
  144. int idx = pkpy_Dict__htget(self, h), null = pkpy_Dict__idx_null(self);
  145. if(idx == null) return false;
  146. self->_version += 1;
  147. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
  148. assert(entry->hash == hash && pkpy_Var__eq__(vm, entry->key, key));
  149. pkpy_Var__set_null(&entry->key);
  150. pkpy_Dict__htset(self, h, null);
  151. pkpy_Dict__refactor(self, vm);
  152. self->count -= 1;
  153. return true;
  154. }
  155. const pkpy_Var *pkpy_Dict__try_get(const pkpy_Dict* self, void* vm, pkpy_Var key) {
  156. int hash = pkpy_Var__hash__(vm, key);
  157. int h = pkpy_Dict__probe(self, vm, key, hash);
  158. int idx = pkpy_Dict__htget(self, h);
  159. if(idx == pkpy_Dict__idx_null(self)) return NULL;
  160. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
  161. assert(entry->hash == hash && pkpy_Var__eq__(vm, entry->key, key));
  162. return &entry->val;
  163. }
  164. void pkpy_Dict__update(pkpy_Dict *self, void *vm, const pkpy_Dict *other) {
  165. for(int i = 0; i < other->_entries.count; i++) {
  166. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &other->_entries, i);
  167. if(pkpy_Var__is_null(&entry->key)) continue;
  168. pkpy_Dict__set(self, vm, entry->key, entry->val);
  169. }
  170. }
  171. void pkpy_Dict__clear(pkpy_Dict *self) {
  172. int v = self->_version;
  173. pkpy_Dict__dtor(self);
  174. pkpy_Dict__ctor(self);
  175. self->_version = v + 1;
  176. }
  177. static int pkpy_Dict__next_entry_idx(const pkpy_Dict* self, int idx) {
  178. do {
  179. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
  180. if(!pkpy_Var__is_null(&entry->key)) break;
  181. idx++;
  182. } while (idx < self->_entries.count);
  183. return idx;
  184. }
  185. pkpy_DictIter pkpy_Dict__iter(const pkpy_Dict *self) {
  186. return (pkpy_DictIter){
  187. ._dict = self,
  188. ._index = pkpy_Dict__next_entry_idx(self, 0),
  189. ._version = self->_version,
  190. };
  191. }
  192. bool pkpy_DictIter__next(pkpy_DictIter *self, pkpy_Var *key, pkpy_Var *val) {
  193. if(self->_version != self->_dict->_version) return false;
  194. if(self->_index >= self->_dict->_entries.count) return false;
  195. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_dict->_entries, self->_index);
  196. assert(!pkpy_Var__is_null(&entry->key));
  197. if (key) *key = entry->key;
  198. if (val) *val = entry->val;
  199. self->_index = pkpy_Dict__next_entry_idx(self->_dict, self->_index + 1);
  200. return true;
  201. }