From 17fd51b967caedabe9cf2feaebc40b6c5e8121d0 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Thu, 12 Oct 2023 21:50:41 +0800 Subject: [PATCH] ... --- include/pocketpy/namedict.h | 54 +++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/include/pocketpy/namedict.h b/include/pocketpy/namedict.h index c58240c3..50fca846 100644 --- a/include/pocketpy/namedict.h +++ b/include/pocketpy/namedict.h @@ -28,15 +28,17 @@ struct SmallNameDict{ SmallNameDict(): _is_small(true), _size(0) {} bool try_set(K key, V val){ + int slot = -1; for(int i=0; i& template struct LargeNameDict { + PK_ALWAYS_PASS_BY_POINTER(LargeNameDict) + using Item = std::pair; - static constexpr uint16_t __Capacity = 32; + static constexpr uint16_t kInitialCapacity = 32; static_assert(is_pod::value); bool _is_small; float _load_factor; - uint16_t _capacity; uint16_t _size; uint16_t _hash_seed; + + uint16_t _capacity; + uint16_t _critical_size; uint16_t _mask; + Item* _items; #define HASH_PROBE_0(key, ok, i) \ @@ -144,38 +151,26 @@ while(!_items[i].first.empty()) { \ _items = (Item*)malloc(_capacity * sizeof(Item)); \ memset(_items, 0, _capacity * sizeof(Item)); \ - LargeNameDict(float load_factor=kInstAttrLoadFactor): - _is_small(false), - _load_factor(load_factor), _capacity(__Capacity), _size(0), - _hash_seed(kHashSeeds[0]), _mask(__Capacity-1) { + LargeNameDict(float load_factor=kInstAttrLoadFactor): _is_small(false), _load_factor(load_factor), _size(0), _hash_seed(kHashSeeds[0]) { + _set_capacity(kInitialCapacity); NAMEDICT_ALLOC() } - LargeNameDict(const LargeNameDict& other) { - memcpy(this, &other, sizeof(LargeNameDict)); - NAMEDICT_ALLOC() - for(int i=0; i<_capacity; i++) _items[i] = other._items[i]; - } - - LargeNameDict& operator=(const LargeNameDict& other) { - free(_items); - memcpy(this, &other, sizeof(LargeNameDict)); - NAMEDICT_ALLOC() - for(int i=0; i<_capacity; i++) _items[i] = other._items[i]; - return *this; - } - ~LargeNameDict(){ free(_items); } - LargeNameDict(LargeNameDict&&) = delete; - LargeNameDict& operator=(LargeNameDict&&) = delete; uint16_t size() const { return _size; } uint16_t capacity() const { return _capacity; } + void _set_capacity(uint16_t val){ + _capacity = val; + _critical_size = val * _load_factor; + _mask = val - 1; + } + T operator[](StrName key) const { bool ok; uint16_t i; HASH_PROBE_0(key, ok, i); - if(!ok) throw std::out_of_range(fmt("NameDict key not found: ", key)); + if(!ok) throw std::out_of_range(fmt("LargeNameDict key not found: ", key)); return _items[i].second; } @@ -184,7 +179,7 @@ while(!_items[i].first.empty()) { \ HASH_PROBE_1(key, ok, i); if(!ok) { _size++; - if(_size > _capacity*_load_factor){ + if(_size > _critical_size){ _rehash(true); HASH_PROBE_1(key, ok, i); } @@ -197,8 +192,7 @@ while(!_items[i].first.empty()) { \ Item* old_items = _items; uint16_t old_capacity = _capacity; if(resize){ - _capacity *= 2; - _mask = _capacity - 1; + _set_capacity(_capacity * 2); } NAMEDICT_ALLOC() for(uint16_t i=0; i