dict.c 9.6 KB

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