dict.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. #define DICT_MAX_LOAD 0.75
  7. #define DICT_HASH_NEXT(h) ((h) * 5 + 1)
  8. #define DICT_HASH_TRANS(h) ((int)((h) & 0xffffffff)) // used for tansform value from __hash__
  9. #define PK_DICT_COMPACT_MODE 1
  10. struct pkpy_DictEntry {
  11. pkpy_Var key;
  12. pkpy_Var val;
  13. };
  14. inline extern int pkpy_Dict__idx_size(const pkpy_Dict* self) {
  15. #if PK_DICT_COMPACT_MODE
  16. if(self->_htcap < 255) return 1;
  17. if(self->_htcap < 65535) return 2;
  18. #endif
  19. return 4;
  20. }
  21. inline extern int pkpy_Dict__idx_null(const pkpy_Dict* self) {
  22. #if PK_DICT_COMPACT_MODE
  23. if(self->_htcap < 255) return 255;
  24. if(self->_htcap < 65535) return 65535;
  25. #endif
  26. return -1;
  27. }
  28. inline extern int pkpy_Dict__ht_byte_size(const pkpy_Dict* self) { return self->_htcap * pkpy_Dict__idx_size(self); }
  29. void pkpy_Dict__ctor(pkpy_Dict* self) {
  30. self->count = 0;
  31. c11_vector__ctor(&self->_entries, sizeof(struct pkpy_DictEntry));
  32. self->_htcap = 16;
  33. self->_hashtable = malloc(pkpy_Dict__ht_byte_size(self));
  34. memset(self->_hashtable, 0xff, pkpy_Dict__ht_byte_size(self));
  35. }
  36. void pkpy_Dict__dtor(pkpy_Dict* self) {
  37. c11_vector__dtor(&self->_entries);
  38. free(self->_hashtable);
  39. }
  40. pkpy_Dict pkpy_Dict__copy(const pkpy_Dict* self) {
  41. int ht_size = pkpy_Dict__ht_byte_size(self);
  42. void* ht_clone = malloc(ht_size);
  43. memcpy(ht_clone, self->_hashtable, ht_size);
  44. return (pkpy_Dict){.count = self->count,
  45. ._entries = c11_vector__copy(&self->_entries),
  46. ._htcap = self->_htcap,
  47. ._hashtable = ht_clone};
  48. }
  49. static int pkpy_Dict__htget(const pkpy_Dict* self, int h) {
  50. #if PK_DICT_COMPACT_MODE
  51. const int *p = (const int*)(((const char*)self->_hashtable) + h * pkpy_Dict__idx_size(self));
  52. return (*p) & pkpy_Dict__idx_null(self);
  53. #else
  54. return ((const int*)self->_hashtable)[h];
  55. #endif
  56. }
  57. static void pkpy_Dict__htset(pkpy_Dict* self, int h, int v) {
  58. #if PK_DICT_COMPACT_MODE
  59. int *p = (int*)(((char*)self->_hashtable) + h * pkpy_Dict__idx_size(self));
  60. *p = v | (*p & ~pkpy_Dict__idx_null(self));
  61. #else
  62. ((int*)self->_hashtable)[h] = v;
  63. #endif
  64. }
  65. static int pkpy_Dict__probe0(const pkpy_Dict* self, void* vm, pkpy_Var key, int hash) {
  66. const int null = pkpy_Dict__idx_null(self);
  67. const int mask = self->_htcap - 1;
  68. for(int h = hash & mask;; h = DICT_HASH_NEXT(h) & mask) {
  69. int idx = pkpy_Dict__htget(self, h);
  70. if(idx == null) return h;
  71. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
  72. if(pkpy_Var__is_null(&entry->key)) return h;
  73. }
  74. PK_UNREACHABLE();
  75. }
  76. static int pkpy_Dict__probe1(const pkpy_Dict* self, void* vm, pkpy_Var key, int hash) {
  77. const int null = pkpy_Dict__idx_null(self);
  78. const int mask = self->_htcap - 1;
  79. for(int h = hash & mask;; h = DICT_HASH_NEXT(h) & mask) {
  80. int idx = pkpy_Dict__htget(self, h);
  81. if(idx == null) return h;
  82. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
  83. if(pkpy_Var__is_null(&entry->key)) continue;
  84. if(pkpy_Var__eq__(vm, entry->key, key)) return h;
  85. }
  86. PK_UNREACHABLE();
  87. }
  88. static void pkpy_Dict__extendht(pkpy_Dict* self, void* vm) {
  89. free(self->_hashtable);
  90. self->_htcap *= 2;
  91. self->_hashtable = malloc(pkpy_Dict__ht_byte_size(self));
  92. memset(self->_hashtable, 0xff, pkpy_Dict__ht_byte_size(self));
  93. for(int i = 0; i < self->_entries.count; i++) {
  94. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, i);
  95. if(pkpy_Var__is_null(&entry->key)) continue;
  96. int rhash = DICT_HASH_TRANS(pkpy_Var__hash__(vm, entry->key));
  97. int h = pkpy_Dict__probe0(self, vm, entry->key, rhash);
  98. pkpy_Dict__htset(self, h, i);
  99. }
  100. }
  101. bool pkpy_Dict__set(pkpy_Dict* self, void* vm, pkpy_Var key, pkpy_Var val) {
  102. int hash = DICT_HASH_TRANS(pkpy_Var__hash__(vm, key));
  103. int h = pkpy_Dict__probe1(self, vm, key, hash);
  104. int idx = pkpy_Dict__htget(self, h);
  105. if(idx == pkpy_Dict__idx_null(self)) {
  106. idx = self->_entries.count;
  107. c11_vector__push(struct pkpy_DictEntry,
  108. &self->_entries,
  109. ((struct pkpy_DictEntry){
  110. .key = key,
  111. .val = val,
  112. }));
  113. h = pkpy_Dict__probe0(self, vm, key, hash);
  114. pkpy_Dict__htset(self, h, idx);
  115. self->count += 1;
  116. if(self->count >= self->_htcap * DICT_MAX_LOAD) pkpy_Dict__extendht(self, vm);
  117. return true;
  118. }
  119. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
  120. if(pkpy_Var__eq__(vm, entry->key, key)) {
  121. entry->val = val;
  122. } else {
  123. self->count += 1;
  124. h = pkpy_Dict__probe0(self, vm, key, hash);
  125. idx = pkpy_Dict__htget(self, h);
  126. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
  127. entry->key = key;
  128. entry->val = val;
  129. }
  130. return false;
  131. }
  132. bool pkpy_Dict__contains(const pkpy_Dict* self, void* vm, pkpy_Var key) {
  133. int hash = DICT_HASH_TRANS(pkpy_Var__hash__(vm, key));
  134. int h = pkpy_Dict__probe1(self, vm, key, hash);
  135. int idx = pkpy_Dict__htget(self, h);
  136. if(idx == pkpy_Dict__idx_null(self)) return false;
  137. return true;
  138. }
  139. static bool pkpy_Dict__refactor(pkpy_Dict* self, void* vm) {
  140. int deleted_slots = self->_entries.count - self->count;
  141. if(deleted_slots <= 8 || deleted_slots < self->_entries.count * (1 - DICT_MAX_LOAD)) return false;
  142. // shrink
  143. // free(self->_hashtable);
  144. // while(self->_htcap * DICT_MAX_LOAD / 2 > self->count && self->_htcap >= 32)
  145. // self->_htcap /= 2;
  146. // self->_hashtable = malloc(pkpy_Dict__ht_byte_size(self));
  147. memset(self->_hashtable, 0xff, pkpy_Dict__ht_byte_size(self));
  148. int new_cnt = 0;
  149. for (int i = 0; i < self->_entries.count; ++i) {
  150. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, i);
  151. if(pkpy_Var__is_null(&entry->key)) continue;
  152. if (i > new_cnt) c11__setitem(struct pkpy_DictEntry, &self->_entries, new_cnt, *entry);
  153. new_cnt += 1;
  154. }
  155. self->_entries.count = new_cnt;
  156. for(int i = 0; i < self->_entries.count; i++) {
  157. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, i);
  158. if(pkpy_Var__is_null(&entry->key)) continue;
  159. int rhash = DICT_HASH_TRANS(pkpy_Var__hash__(vm, entry->key));
  160. int h = pkpy_Dict__probe0(self, vm, entry->key, rhash);
  161. pkpy_Dict__htset(self, h, i);
  162. }
  163. return true;
  164. }
  165. bool pkpy_Dict__del(pkpy_Dict* self, void* vm, pkpy_Var key) {
  166. int hash = DICT_HASH_TRANS(pkpy_Var__hash__(vm, key));
  167. int h = pkpy_Dict__probe1(self, vm, key, hash);
  168. int idx = pkpy_Dict__htget(self, h), null = pkpy_Dict__idx_null(self);
  169. if(idx == null) return false;
  170. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
  171. pkpy_Var__set_null(&entry->key);
  172. self->count -= 1;
  173. pkpy_Dict__refactor(self, vm);
  174. return true;
  175. }
  176. const pkpy_Var *pkpy_Dict__try_get(const pkpy_Dict* self, void* vm, pkpy_Var key) {
  177. int hash = DICT_HASH_TRANS(pkpy_Var__hash__(vm, key));
  178. int h = pkpy_Dict__probe1(self, vm, key, hash);
  179. int idx = pkpy_Dict__htget(self, h);
  180. if(idx == pkpy_Dict__idx_null(self)) return NULL;
  181. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
  182. return &entry->val;
  183. }
  184. void pkpy_Dict__update(pkpy_Dict *self, void *vm, const pkpy_Dict *other) {
  185. for(int i = 0; i < other->_entries.count; i++) {
  186. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &other->_entries, i);
  187. if(pkpy_Var__is_null(&entry->key)) continue;
  188. pkpy_Dict__set(self, vm, entry->key, entry->val);
  189. }
  190. }
  191. void pkpy_Dict__clear(pkpy_Dict *self) {
  192. self->count = 0;
  193. self->_entries.count = 0;
  194. memset(self->_hashtable, 0xff, pkpy_Dict__ht_byte_size(self));
  195. }
  196. static int pkpy_Dict__next_entry_idx(const pkpy_Dict* self, int idx) {
  197. while (idx < self->_entries.count) {
  198. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
  199. if(!pkpy_Var__is_null(&entry->key)) break;
  200. idx++;
  201. }
  202. return idx;
  203. }
  204. pkpy_DictIter pkpy_Dict__iter(const pkpy_Dict *self) {
  205. return (pkpy_DictIter){
  206. ._dict = self,
  207. ._index = pkpy_Dict__next_entry_idx(self, 0),
  208. };
  209. }
  210. bool pkpy_DictIter__next(pkpy_DictIter *self, pkpy_Var *key, pkpy_Var *val) {
  211. if(self->_index >= self->_dict->_entries.count) return false;
  212. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_dict->_entries, self->_index);
  213. if(pkpy_Var__is_null(&entry->key)) return false;
  214. if (key) *key = entry->key;
  215. if (val) *val = entry->val;
  216. self->_index = pkpy_Dict__next_entry_idx(self->_dict, self->_index + 1);
  217. return true;
  218. }