This commit is contained in:
blueloveTH 2023-10-13 11:01:48 +08:00
parent 163a66fb86
commit 535c1d39e0

View File

@ -13,20 +13,18 @@ constexpr T default_invalid_value(){
else return Discarded(); else return Discarded();
} }
// #define PK_LOOP_K(B) for(int i=0; i<kCapacity; i++) { B(i) } #define PK_SMALL_NAME_DICT_CAPACITY 12
#define PK_LOOP_K(B) B(0) B(1) B(2) B(3) B(4) B(5) B(6) B(7) B(8) B(9) B(10) B(11) #define PK_LOOP_K(B) for(int i=0; i<PK_SMALL_NAME_DICT_CAPACITY; i++) { B(i) }
template<typename V> template<typename V>
struct SmallNameDict{ struct SmallNameDict{
using K = StrName; using K = StrName;
static_assert(std::is_pod_v<V>); static_assert(std::is_pod_v<V>);
static const int kCapacity = 12;
bool _is_small; bool _is_small;
uint16_t _size; uint16_t _size;
K _keys[kCapacity]; K _keys[PK_SMALL_NAME_DICT_CAPACITY];
V _values[kCapacity]; V _values[PK_SMALL_NAME_DICT_CAPACITY];
SmallNameDict(): _is_small(true), _size(0) {} SmallNameDict(): _is_small(true), _size(0) {}
@ -37,6 +35,7 @@ struct SmallNameDict{
if(_keys[i] == key){ _values[i] = val; return true; } \ if(_keys[i] == key){ _values[i] = val; return true; } \
if(_keys[i].empty()) slot = i; \ if(_keys[i].empty()) slot = i; \
#pragma unroll (PK_SMALL_NAME_DICT_CAPACITY)
PK_LOOP_K(BLOCK) PK_LOOP_K(BLOCK)
#undef BLOCK #undef BLOCK
@ -49,6 +48,7 @@ struct SmallNameDict{
V try_get(K key) const { V try_get(K key) const {
#define BLOCK(i) if(_keys[i] == key) return _values[i]; #define BLOCK(i) if(_keys[i] == key) return _values[i];
#pragma unroll (PK_SMALL_NAME_DICT_CAPACITY)
PK_LOOP_K(BLOCK) PK_LOOP_K(BLOCK)
#undef BLOCK #undef BLOCK
return default_invalid_value<V>(); return default_invalid_value<V>();
@ -56,6 +56,7 @@ struct SmallNameDict{
V* try_get_2(K key) { V* try_get_2(K key) {
#define BLOCK(i) if(_keys[i] == key) return &_values[i]; #define BLOCK(i) if(_keys[i] == key) return &_values[i];
#pragma unroll (PK_SMALL_NAME_DICT_CAPACITY)
PK_LOOP_K(BLOCK) PK_LOOP_K(BLOCK)
#undef BLOCK #undef BLOCK
return nullptr; return nullptr;
@ -63,6 +64,7 @@ struct SmallNameDict{
bool contains(K key) const { bool contains(K key) const {
#define BLOCK(i) if(_keys[i] == key) return true; #define BLOCK(i) if(_keys[i] == key) return true;
#pragma unroll (PK_SMALL_NAME_DICT_CAPACITY)
PK_LOOP_K(BLOCK) PK_LOOP_K(BLOCK)
#undef BLOCK #undef BLOCK
return false; return false;
@ -70,6 +72,7 @@ struct SmallNameDict{
bool del(K key){ bool del(K key){
#define BLOCK(i) if(_keys[i] == key){ _keys[i] = StrName(); _size--; return true; } #define BLOCK(i) if(_keys[i] == key){ _keys[i] = StrName(); _size--; return true; }
#pragma unroll (PK_SMALL_NAME_DICT_CAPACITY)
PK_LOOP_K(BLOCK) PK_LOOP_K(BLOCK)
#undef BLOCK #undef BLOCK
return false; return false;
@ -78,19 +81,21 @@ struct SmallNameDict{
template<typename Func> template<typename Func>
void apply(Func func) const { void apply(Func func) const {
#define BLOCK(i) if(!_keys[i].empty()) func(_keys[i], _values[i]); #define BLOCK(i) if(!_keys[i].empty()) func(_keys[i], _values[i]);
#pragma unroll (PK_SMALL_NAME_DICT_CAPACITY)
PK_LOOP_K(BLOCK) PK_LOOP_K(BLOCK)
#undef BLOCK #undef BLOCK
} }
void clear(){ void clear(){
#define BLOCK(i) _keys[i] = StrName(); #define BLOCK(i) _keys[i] = StrName();
#pragma unroll (PK_SMALL_NAME_DICT_CAPACITY)
PK_LOOP_K(BLOCK) PK_LOOP_K(BLOCK)
#undef BLOCK #undef BLOCK
_size = 0; _size = 0;
} }
uint16_t size() const { return _size; } uint16_t size() const { return _size; }
uint16_t capacity() const { return kCapacity; } uint16_t capacity() const { return PK_SMALL_NAME_DICT_CAPACITY; }
}; };
inline const uint16_t kHashSeeds[] = {9629, 43049, 13267, 59509, 39251, 1249, 27689, 9719, 19913}; inline const uint16_t kHashSeeds[] = {9629, 43049, 13267, 59509, 39251, 1249, 27689, 9719, 19913};