remove _version

This commit is contained in:
方而静 2024-06-14 09:32:58 +08:00
parent 3d90bd0392
commit 6e780173f9
2 changed files with 4 additions and 17 deletions

View File

@ -9,7 +9,6 @@ extern "C" {
#include "pocketpy/common/vector.h" #include "pocketpy/common/vector.h"
typedef struct { typedef struct {
unsigned int _version; /** used internelly to detect iterator invalidation */
int count; /** number of elements in the dictionary */ int count; /** number of elements in the dictionary */
c11_vector _entries; /** contains `pkpy_DictEntry` (hidden type) */ c11_vector _entries; /** contains `pkpy_DictEntry` (hidden type) */
int _htcap; /** capacity of the hashtable, always a power of 2 */ int _htcap; /** capacity of the hashtable, always a power of 2 */

View File

@ -24,7 +24,6 @@ inline static int pkpy_Dict__idx_null(const pkpy_Dict* self) {
inline static int pkpy_Dict__ht_byte_size(const pkpy_Dict* self) { return self->_htcap * pkpy_Dict__idx_size(self); } inline static int pkpy_Dict__ht_byte_size(const pkpy_Dict* self) { return self->_htcap * pkpy_Dict__idx_size(self); }
void pkpy_Dict__ctor(pkpy_Dict* self) { void pkpy_Dict__ctor(pkpy_Dict* self) {
self->_version = 0;
self->count = 0; self->count = 0;
c11_vector__ctor(&self->_entries, sizeof(struct pkpy_DictEntry)); c11_vector__ctor(&self->_entries, sizeof(struct pkpy_DictEntry));
self->_htcap = 16; self->_htcap = 16;
@ -41,8 +40,7 @@ pkpy_Dict pkpy_Dict__copy(const pkpy_Dict* self) {
int ht_size = pkpy_Dict__ht_byte_size(self); int ht_size = pkpy_Dict__ht_byte_size(self);
void* ht_clone = malloc(ht_size); void* ht_clone = malloc(ht_size);
memcpy(ht_clone, self->_hashtable, ht_size); memcpy(ht_clone, self->_hashtable, ht_size);
return (pkpy_Dict){._version = 0, return (pkpy_Dict){.count = self->count,
.count = self->count,
._entries = c11_vector__copy(&self->_entries), ._entries = c11_vector__copy(&self->_entries),
._htcap = self->_htcap, ._htcap = self->_htcap,
._hashtable = ht_clone}; ._hashtable = ht_clone};
@ -96,7 +94,6 @@ static int pkpy_Dict__probe1(const pkpy_Dict* self, void* vm, pkpy_Var key, int6
} }
static void pkpy_Dict__extendht(pkpy_Dict* self, void* vm) { static void pkpy_Dict__extendht(pkpy_Dict* self, void* vm) {
self->_version += 1;
free(self->_hashtable); free(self->_hashtable);
self->_htcap *= 2; self->_htcap *= 2;
self->_hashtable = malloc(pkpy_Dict__ht_byte_size(self)); self->_hashtable = malloc(pkpy_Dict__ht_byte_size(self));
@ -117,7 +114,6 @@ bool pkpy_Dict__set(pkpy_Dict* self, void* vm, pkpy_Var key, pkpy_Var val) {
int idx = pkpy_Dict__htget(self, h); int idx = pkpy_Dict__htget(self, h);
if(idx == pkpy_Dict__idx_null(self)) { if(idx == pkpy_Dict__idx_null(self)) {
self->_version += 1;
idx = self->_entries.count; idx = self->_entries.count;
c11_vector__push(struct pkpy_DictEntry, c11_vector__push(struct pkpy_DictEntry,
&self->_entries, &self->_entries,
@ -137,7 +133,6 @@ bool pkpy_Dict__set(pkpy_Dict* self, void* vm, pkpy_Var key, pkpy_Var val) {
if(pkpy_Var__eq__(vm, entry->key, key)) { if(pkpy_Var__eq__(vm, entry->key, key)) {
entry->val = val; entry->val = val;
} else { } else {
self->_version += 1;
self->count += 1; self->count += 1;
h = pkpy_Dict__probe0(self, vm, key, hash); h = pkpy_Dict__probe0(self, vm, key, hash);
idx = pkpy_Dict__htget(self, h); idx = pkpy_Dict__htget(self, h);
@ -165,7 +160,6 @@ static bool pkpy_Dict__refactor(pkpy_Dict* self, void* vm) {
if(deleted_slots <= 8 || deleted_slots < self->_entries.count * 0.25) return false; if(deleted_slots <= 8 || deleted_slots < self->_entries.count * 0.25) return false;
// shrink // shrink
self->_version += 1;
free(self->_hashtable); free(self->_hashtable);
while(self->_htcap * 0.375 > self->count && self->_htcap >= 32) while(self->_htcap * 0.375 > self->count && self->_htcap >= 32)
self->_htcap /= 2; self->_htcap /= 2;
@ -195,7 +189,6 @@ bool pkpy_Dict__del(pkpy_Dict* self, void* vm, pkpy_Var key) {
struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx); struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
assert(pkpy_Var__eq__(vm, entry->key, key)); assert(pkpy_Var__eq__(vm, entry->key, key));
self->_version += 1;
pkpy_Var__set_null(&entry->key); pkpy_Var__set_null(&entry->key);
self->count -= 1; self->count -= 1;
pkpy_Dict__refactor(self, vm); pkpy_Dict__refactor(self, vm);
@ -223,19 +216,16 @@ void pkpy_Dict__update(pkpy_Dict *self, void *vm, const pkpy_Dict *other) {
} }
void pkpy_Dict__clear(pkpy_Dict *self) { void pkpy_Dict__clear(pkpy_Dict *self) {
int v = self->_version;
pkpy_Dict__dtor(self); pkpy_Dict__dtor(self);
pkpy_Dict__ctor(self); pkpy_Dict__ctor(self);
self->_version = v + 1;
} }
static int pkpy_Dict__next_entry_idx(const pkpy_Dict* self, int idx) { static int pkpy_Dict__next_entry_idx(const pkpy_Dict* self, int idx) {
if (idx >= self->_entries.count) return idx; while (idx < self->_entries.count) {
do {
struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx); struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, idx);
if(!pkpy_Var__is_null(&entry->key)) break; if(!pkpy_Var__is_null(&entry->key)) break;
idx++; idx++;
} while (idx < self->_entries.count); }
return idx; return idx;
} }
@ -243,16 +233,14 @@ pkpy_DictIter pkpy_Dict__iter(const pkpy_Dict *self) {
return (pkpy_DictIter){ return (pkpy_DictIter){
._dict = self, ._dict = self,
._index = pkpy_Dict__next_entry_idx(self, 0), ._index = pkpy_Dict__next_entry_idx(self, 0),
._version = self->_version,
}; };
} }
bool pkpy_DictIter__next(pkpy_DictIter *self, pkpy_Var *key, pkpy_Var *val) { bool pkpy_DictIter__next(pkpy_DictIter *self, pkpy_Var *key, pkpy_Var *val) {
if(self->_version != self->_dict->_version) return false;
if(self->_index >= self->_dict->_entries.count) return false; if(self->_index >= self->_dict->_entries.count) return false;
struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_dict->_entries, self->_index); struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_dict->_entries, self->_index);
assert(!pkpy_Var__is_null(&entry->key)); if(pkpy_Var__is_null(&entry->key)) return false;
if (key) *key = entry->key; if (key) *key = entry->key;
if (val) *val = entry->val; if (val) *val = entry->val;