def tpd_dict_free(TP, tpd_dict *self): tp_free(tp, self->items) tp_free(tp, self) /* void tpd_dict_reset(tpd_dict *self) { memset(self->items,0,self->alloc*sizeof(tpd_item)); self->len = 0; self->used = 0; self->cur = 0; }*/ def tpd_dict_hashset(TP, tpd_dict *self, int hash, tp_obj k, tp_obj v): tpd_item item ##int i,idx = hash & self->mask ##for (i=idx; ialloc; i++): ## int n = i & self->mask for (int i=0; i < self->alloc; i++): int n = i if (self->items[n].used > 0) { continue; } if (self->items[n].used == 0) { self->used += 1; } item.used = 1 item.hash = hash item.key = k item.val = v self->items[n] = item self->len += 1 return def tpd_dict_realloc(TP, tpd_dict *self, int len): tpd_item *items = self->items int i,alloc = self->alloc len = _tp_max(8,len) self->items = (tpd_item*)tp_malloc(tp, len*sizeof(tpd_item)) self->alloc = len ##self->mask = len-1 self->len = 0; self->used = 0 for (i=0; i int: int i,idx = hash & self->mask for (i=idx; ialloc; i++): int n = i & self->mask if (self->items[n].used == 0) { break; } if (self->items[n].used < 0) { continue; } if (self->items[n].hash != hash) { continue; } if (tp_cmp(tp, self->items[n].key, k) != 0) { continue; } return n return -1 */ def tpd_dict_hashfind(TP, tpd_dict * self, int hash, tp_obj k) -> int: for (int i=0; ialloc; i++): if self->items[i].hash == hash: return i return -1 def tpd_dict_new(TP) -> tpd_dict* : tpd_dict *self = (tpd_dict*) tp_malloc(tp, sizeof(tpd_dict)) return self def tpd_dict_new() -> tpd_dict* : tpd_dict *self = (tpd_dict*) tp_malloc(NULL, sizeof(tpd_dict)) return self def tpd_dict_hashsetx(TP, tpd_dict * self, int hash, tp_obj k, tp_obj v): if defined(DEBUG): print("tpd_dict_hashsetx") print("hash=", hash) std::cout << "key=" << k << std::endl; int n = tpd_dict_hashfind(tp, self, hash, k) if n == -1: if self->len >= (self->alloc/2): tpd_dict_realloc(tp, self, self->alloc*2) elif self->used >= (self->alloc /4 * 3): /* compact the dict */ tpd_dict_realloc(tp, self, self->alloc) tpd_dict_hashset(tp, self, hash, k, v) else: self->items[n].val = v /* broken by removing mask - DEPRECATED def tpd_dict_next(TP, tpd_dict *self) -> int: if not self->len: return -1 while 1: ##self->cur = ((self->cur + 1) & self->mask) self->cur ++ if self->items[self->cur].used > 0: return self->cur */ def tpd_dict_get(TP, tpd_dict * self, int n) ->tp_obj: return self->items[n].val def tpd_dict_del(TP, tpd_dict * self, int n): self->items[n].used = -1 self->len -= 1 def tpd_dict_get_by_stdstring(TP, tpd_dict* self, std::string key) ->tp_obj: for i in range(self->alloc): if self->items[i].used != 1: continue elif std::string(self->items[i].key) == key: return self->items[i].val throw "tpd_dict_get_by_stdstring failed"