dict.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 PK_DICT_COMPACT_MODE 1
  8. struct pkpy_DictEntry {
  9. pkpy_Var key;
  10. pkpy_Var val;
  11. };
  12. inline extern int pkpy_Dict__idx_size(const pkpy_Dict* self) {
  13. #if PK_DICT_COMPACT_MODE
  14. if(self->_htcap < 255) return 1;
  15. if(self->_htcap < 65535) return 2;
  16. #endif
  17. return 4;
  18. }
  19. inline extern unsigned int pkpy_Dict__idx_null(const pkpy_Dict* self) {
  20. #if PK_DICT_COMPACT_MODE
  21. // if(self->_htcap < 255) return 255;
  22. // if(self->_htcap < 65535) return 65535;
  23. // return 4294967295u; // 2^32 - 1
  24. return (1u << ((pkpy_Dict__idx_size(self) * 8) & 31)) - 1u;
  25. #endif
  26. return 4294967295u;
  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 unsigned int pkpy_Dict__htget(const pkpy_Dict* self, int h) {
  50. #if PK_DICT_COMPACT_MODE
  51. const unsigned int *p = (const unsigned int*)(((const char*)self->_hashtable) + h * pkpy_Dict__idx_size(self));
  52. return (*p) & pkpy_Dict__idx_null(self);
  53. #else
  54. return ((const unsigned int*)self->_hashtable)[h];
  55. #endif
  56. }
  57. static void pkpy_Dict__htset(pkpy_Dict* self, int h, unsigned int v) {
  58. #if PK_DICT_COMPACT_MODE
  59. unsigned int *p = (unsigned int*)(((char*)self->_hashtable) + h * pkpy_Dict__idx_size(self));
  60. *p = v | (*p & ~pkpy_Dict__idx_null(self));
  61. #else
  62. ((unsigned int*)self->_hashtable)[h] = v;
  63. #endif
  64. }
  65. static int pkpy_Dict__probe0(const pkpy_Dict* self, void* vm, pkpy_Var key, int64_t hash) {
  66. const int null = pkpy_Dict__idx_null(self);
  67. const int mask = self->_htcap - 1;
  68. for(int h = hash & mask;; h = (h * 5 + 1) & mask) {
  69. unsigned 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, int64_t hash) {
  77. const int null = pkpy_Dict__idx_null(self);
  78. const int mask = self->_htcap - 1;
  79. for(int h = hash & mask;; h = (h * 5 + 1) & mask) {
  80. unsigned 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 h = pkpy_Dict__probe0(self, vm, entry->key, pkpy_Var__hash__(vm, entry->key));
  97. pkpy_Dict__htset(self, h, i);
  98. }
  99. }
  100. bool pkpy_Dict__set(pkpy_Dict* self, void* vm, pkpy_Var key, pkpy_Var val) {
  101. int64_t hash = pkpy_Var__hash__(vm, key);
  102. int h = pkpy_Dict__probe1(self, vm, key, hash);
  103. unsigned int idx = pkpy_Dict__htget(self, h);
  104. if(idx == pkpy_Dict__idx_null(self)) {
  105. idx = self->_entries.count;
  106. c11_vector__push(struct pkpy_DictEntry,
  107. &self->_entries,
  108. ((struct pkpy_DictEntry){
  109. .key = key,
  110. .val = val,
  111. }));
  112. h = pkpy_Dict__probe0(self, vm, key, hash);
  113. pkpy_Dict__htset(self, h, idx);
  114. self->count += 1;
  115. if(self->count >= self->_htcap * DICT_MAX_LOAD) pkpy_Dict__extendht(self, vm);
  116. return true;
  117. }
  118. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
  119. if(pkpy_Var__eq__(vm, entry->key, key)) {
  120. entry->val = val;
  121. } else {
  122. self->count += 1;
  123. h = pkpy_Dict__probe0(self, vm, key, hash);
  124. unsigned idx = pkpy_Dict__htget(self, h);
  125. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
  126. entry->key = key;
  127. entry->val = val;
  128. }
  129. return false;
  130. }
  131. bool pkpy_Dict__contains(const pkpy_Dict* self, void* vm, pkpy_Var key) {
  132. int64_t hash = pkpy_Var__hash__(vm, key);
  133. int h = pkpy_Dict__probe1(self, vm, key, hash);
  134. unsigned int idx = pkpy_Dict__htget(self, h);
  135. if(idx == pkpy_Dict__idx_null(self)) return false;
  136. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
  137. assert(pkpy_Var__eq__(vm, entry->key, key));
  138. return true;
  139. }
  140. static bool pkpy_Dict__refactor(pkpy_Dict* self, void* vm) {
  141. int deleted_slots = self->_entries.count - self->count;
  142. if(deleted_slots <= 8 || deleted_slots < self->_entries.count * (1 - DICT_MAX_LOAD)) return false;
  143. // shrink
  144. free(self->_hashtable);
  145. while(self->_htcap * DICT_MAX_LOAD / 2 > self->count && self->_htcap >= 32)
  146. self->_htcap /= 2;
  147. self->_hashtable = malloc(pkpy_Dict__ht_byte_size(self));
  148. memset(self->_hashtable, 0xff, pkpy_Dict__ht_byte_size(self));
  149. c11_vector old_entries = self->_entries;
  150. c11_vector__ctor(&self->_entries, sizeof(struct pkpy_DictEntry));
  151. for(int i = 0; i < old_entries.count; i++) {
  152. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &old_entries, i);
  153. if(pkpy_Var__is_null(&entry->key)) continue;
  154. int j = self->_entries.count;
  155. c11_vector__push(struct pkpy_DictEntry, &self->_entries, *entry);
  156. int h = pkpy_Dict__probe0(self, vm, entry->key, pkpy_Var__hash__(vm, entry->key));
  157. pkpy_Dict__htset(self, h, j);
  158. }
  159. c11_vector__dtor(&old_entries);
  160. return true;
  161. }
  162. bool pkpy_Dict__del(pkpy_Dict* self, void* vm, pkpy_Var key) {
  163. int64_t hash = pkpy_Var__hash__(vm, key);
  164. int h = pkpy_Dict__probe1(self, vm, key, hash);
  165. unsigned int idx = pkpy_Dict__htget(self, h), null = pkpy_Dict__idx_null(self);
  166. if(idx == null) return false;
  167. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
  168. assert(pkpy_Var__eq__(vm, entry->key, key));
  169. pkpy_Var__set_null(&entry->key);
  170. self->count -= 1;
  171. pkpy_Dict__refactor(self, vm);
  172. return true;
  173. }
  174. const pkpy_Var *pkpy_Dict__try_get(const pkpy_Dict* self, void* vm, pkpy_Var key) {
  175. int64_t hash = pkpy_Var__hash__(vm, key);
  176. int h = pkpy_Dict__probe1(self, vm, key, hash);
  177. unsigned int idx = pkpy_Dict__htget(self, h);
  178. if(idx == pkpy_Dict__idx_null(self)) return NULL;
  179. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
  180. assert(pkpy_Var__eq__(vm, entry->key, key));
  181. return &entry->val;
  182. }
  183. void pkpy_Dict__update(pkpy_Dict *self, void *vm, const pkpy_Dict *other) {
  184. for(int i = 0; i < other->_entries.count; i++) {
  185. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &other->_entries, i);
  186. if(pkpy_Var__is_null(&entry->key)) continue;
  187. pkpy_Dict__set(self, vm, entry->key, entry->val);
  188. }
  189. }
  190. void pkpy_Dict__clear(pkpy_Dict *self) {
  191. pkpy_Dict__dtor(self);
  192. pkpy_Dict__ctor(self);
  193. }
  194. static int pkpy_Dict__next_entry_idx(const pkpy_Dict* self, int idx) {
  195. while (idx < self->_entries.count) {
  196. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
  197. if(!pkpy_Var__is_null(&entry->key)) break;
  198. idx++;
  199. }
  200. return idx;
  201. }
  202. pkpy_DictIter pkpy_Dict__iter(const pkpy_Dict *self) {
  203. return (pkpy_DictIter){
  204. ._dict = self,
  205. ._index = pkpy_Dict__next_entry_idx(self, 0),
  206. };
  207. }
  208. bool pkpy_DictIter__next(pkpy_DictIter *self, pkpy_Var *key, pkpy_Var *val) {
  209. if(self->_index >= self->_dict->_entries.count) return false;
  210. struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_dict->_entries, self->_index);
  211. if(pkpy_Var__is_null(&entry->key)) return false;
  212. if (key) *key = entry->key;
  213. if (val) *val = entry->val;
  214. self->_index = pkpy_Dict__next_entry_idx(self->_dict, self->_index + 1);
  215. return true;
  216. }