From f766be85aaa5a4cc031cab4b05c277c4e8e2383f Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Fri, 13 Oct 2023 11:07:43 +0800 Subject: [PATCH] ... --- include/pocketpy/namedict.h | 48 ++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/include/pocketpy/namedict.h b/include/pocketpy/namedict.h index 0b5f3846..22f49827 100644 --- a/include/pocketpy/namedict.h +++ b/include/pocketpy/namedict.h @@ -14,7 +14,7 @@ constexpr T default_invalid_value(){ } #define PK_SMALL_NAME_DICT_CAPACITY 12 -#define PK_LOOP_K(B) for(int i=0; i struct SmallNameDict{ @@ -31,13 +31,11 @@ struct SmallNameDict{ bool try_set(K key, V val){ int slot = -1; -#define BLOCK(i) \ - if(_keys[i] == key){ _values[i] = val; return true; } \ - if(_keys[i].empty()) slot = i; \ - #pragma unroll (PK_SMALL_NAME_DICT_CAPACITY) - PK_LOOP_K(BLOCK) -#undef BLOCK + PK_SMALL_NAME_DICT_LOOP( + if(_keys[i] == key){ _values[i] = val; return true; } + if(_keys[i].empty()) slot = i; + ) if(slot == -1) return false; _keys[slot] = key; @@ -47,50 +45,50 @@ struct SmallNameDict{ } V try_get(K key) const { -#define BLOCK(i) if(_keys[i] == key) return _values[i]; #pragma unroll (PK_SMALL_NAME_DICT_CAPACITY) - PK_LOOP_K(BLOCK) -#undef BLOCK + PK_SMALL_NAME_DICT_LOOP( + if(_keys[i] == key) return _values[i]; + ) return default_invalid_value(); } V* try_get_2(K key) { -#define BLOCK(i) if(_keys[i] == key) return &_values[i]; #pragma unroll (PK_SMALL_NAME_DICT_CAPACITY) - PK_LOOP_K(BLOCK) -#undef BLOCK + PK_SMALL_NAME_DICT_LOOP( + if(_keys[i] == key) return &_values[i]; + ) return nullptr; } bool contains(K key) const { -#define BLOCK(i) if(_keys[i] == key) return true; #pragma unroll (PK_SMALL_NAME_DICT_CAPACITY) - PK_LOOP_K(BLOCK) -#undef BLOCK + PK_SMALL_NAME_DICT_LOOP( + if(_keys[i] == key) return true; + ) return false; } bool del(K key){ -#define BLOCK(i) if(_keys[i] == key){ _keys[i] = StrName(); _size--; return true; } #pragma unroll (PK_SMALL_NAME_DICT_CAPACITY) - PK_LOOP_K(BLOCK) -#undef BLOCK + PK_SMALL_NAME_DICT_LOOP( + if(_keys[i] == key){ _keys[i] = StrName(); _size--; return true; } + ) return false; } template void apply(Func func) const { -#define BLOCK(i) if(!_keys[i].empty()) func(_keys[i], _values[i]); #pragma unroll (PK_SMALL_NAME_DICT_CAPACITY) - PK_LOOP_K(BLOCK) -#undef BLOCK + PK_SMALL_NAME_DICT_LOOP( + if(!_keys[i].empty()) func(_keys[i], _values[i]); + ) } void clear(){ -#define BLOCK(i) _keys[i] = StrName(); #pragma unroll (PK_SMALL_NAME_DICT_CAPACITY) - PK_LOOP_K(BLOCK) -#undef BLOCK + PK_SMALL_NAME_DICT_LOOP( + _keys[i] = StrName(); + ) _size = 0; }