better dict clear

This commit is contained in:
方而静 2024-06-14 12:28:55 +08:00
parent a8ca70ca74
commit 7549f1b95a
2 changed files with 11 additions and 3 deletions

View File

@ -8,6 +8,7 @@ extern "C" {
#include "pocketpy/objects/pyvar.h" #include "pocketpy/objects/pyvar.h"
#include "pocketpy/common/vector.h" #include "pocketpy/common/vector.h"
/** @brief `pkpy_Dict` is the Dict type in Python */
typedef struct { typedef struct {
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) */
@ -15,9 +16,9 @@ typedef struct {
void* _hashtable; /** contains indecies, can be `u8`, `u16` or `u32` according to size*/ void* _hashtable; /** contains indecies, can be `u8`, `u16` or `u32` according to size*/
} pkpy_Dict; } pkpy_Dict;
/** @brief `pkpy_DictIter` is used to iterate over a `pkpy_Dict` */
typedef struct { typedef struct {
const pkpy_Dict* _dict; const pkpy_Dict* _dict;
unsigned int _version;
int _index; int _index;
} pkpy_DictIter; } pkpy_DictIter;

View File

@ -227,8 +227,15 @@ 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) {
pkpy_Dict__dtor(self); self->count = 0;
pkpy_Dict__ctor(self); c11_vector__dtor(&self->_entries);
c11_vector__ctor(&self->_entries, sizeof(struct pkpy_DictEntry));
if (self->_hashtable > 16) {
free(self->_hashtable);
self->_htcap = 16;
self->_hashtable = malloc(pkpy_Dict__ht_byte_size(self));
}
memset(self->_hashtable, 0xff, pkpy_Dict__ht_byte_size(self));
} }
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) {