mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 19:40:18 +00:00
...
This commit is contained in:
parent
535c1d39e0
commit
f766be85aa
@ -14,7 +14,7 @@ constexpr T default_invalid_value(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
#define PK_SMALL_NAME_DICT_CAPACITY 12
|
#define PK_SMALL_NAME_DICT_CAPACITY 12
|
||||||
#define PK_LOOP_K(B) for(int i=0; i<PK_SMALL_NAME_DICT_CAPACITY; i++) { B(i) }
|
#define PK_SMALL_NAME_DICT_LOOP(B) for(int i=0; i<PK_SMALL_NAME_DICT_CAPACITY; i++) { B }
|
||||||
|
|
||||||
template<typename V>
|
template<typename V>
|
||||||
struct SmallNameDict{
|
struct SmallNameDict{
|
||||||
@ -31,13 +31,11 @@ struct SmallNameDict{
|
|||||||
bool try_set(K key, V val){
|
bool try_set(K key, V val){
|
||||||
int slot = -1;
|
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)
|
#pragma unroll (PK_SMALL_NAME_DICT_CAPACITY)
|
||||||
PK_LOOP_K(BLOCK)
|
PK_SMALL_NAME_DICT_LOOP(
|
||||||
#undef BLOCK
|
if(_keys[i] == key){ _values[i] = val; return true; }
|
||||||
|
if(_keys[i].empty()) slot = i;
|
||||||
|
)
|
||||||
|
|
||||||
if(slot == -1) return false;
|
if(slot == -1) return false;
|
||||||
_keys[slot] = key;
|
_keys[slot] = key;
|
||||||
@ -47,50 +45,50 @@ struct SmallNameDict{
|
|||||||
}
|
}
|
||||||
|
|
||||||
V try_get(K key) const {
|
V try_get(K key) const {
|
||||||
#define BLOCK(i) if(_keys[i] == key) return _values[i];
|
|
||||||
#pragma unroll (PK_SMALL_NAME_DICT_CAPACITY)
|
#pragma unroll (PK_SMALL_NAME_DICT_CAPACITY)
|
||||||
PK_LOOP_K(BLOCK)
|
PK_SMALL_NAME_DICT_LOOP(
|
||||||
#undef BLOCK
|
if(_keys[i] == key) return _values[i];
|
||||||
|
)
|
||||||
return default_invalid_value<V>();
|
return default_invalid_value<V>();
|
||||||
}
|
}
|
||||||
|
|
||||||
V* try_get_2(K key) {
|
V* try_get_2(K key) {
|
||||||
#define BLOCK(i) if(_keys[i] == key) return &_values[i];
|
|
||||||
#pragma unroll (PK_SMALL_NAME_DICT_CAPACITY)
|
#pragma unroll (PK_SMALL_NAME_DICT_CAPACITY)
|
||||||
PK_LOOP_K(BLOCK)
|
PK_SMALL_NAME_DICT_LOOP(
|
||||||
#undef BLOCK
|
if(_keys[i] == key) return &_values[i];
|
||||||
|
)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool contains(K key) const {
|
bool contains(K key) const {
|
||||||
#define BLOCK(i) if(_keys[i] == key) return true;
|
|
||||||
#pragma unroll (PK_SMALL_NAME_DICT_CAPACITY)
|
#pragma unroll (PK_SMALL_NAME_DICT_CAPACITY)
|
||||||
PK_LOOP_K(BLOCK)
|
PK_SMALL_NAME_DICT_LOOP(
|
||||||
#undef BLOCK
|
if(_keys[i] == key) return true;
|
||||||
|
)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool del(K key){
|
bool del(K key){
|
||||||
#define BLOCK(i) if(_keys[i] == key){ _keys[i] = StrName(); _size--; return true; }
|
|
||||||
#pragma unroll (PK_SMALL_NAME_DICT_CAPACITY)
|
#pragma unroll (PK_SMALL_NAME_DICT_CAPACITY)
|
||||||
PK_LOOP_K(BLOCK)
|
PK_SMALL_NAME_DICT_LOOP(
|
||||||
#undef BLOCK
|
if(_keys[i] == key){ _keys[i] = StrName(); _size--; return true; }
|
||||||
|
)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
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]);
|
|
||||||
#pragma unroll (PK_SMALL_NAME_DICT_CAPACITY)
|
#pragma unroll (PK_SMALL_NAME_DICT_CAPACITY)
|
||||||
PK_LOOP_K(BLOCK)
|
PK_SMALL_NAME_DICT_LOOP(
|
||||||
#undef BLOCK
|
if(!_keys[i].empty()) func(_keys[i], _values[i]);
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear(){
|
void clear(){
|
||||||
#define BLOCK(i) _keys[i] = StrName();
|
|
||||||
#pragma unroll (PK_SMALL_NAME_DICT_CAPACITY)
|
#pragma unroll (PK_SMALL_NAME_DICT_CAPACITY)
|
||||||
PK_LOOP_K(BLOCK)
|
PK_SMALL_NAME_DICT_LOOP(
|
||||||
#undef BLOCK
|
_keys[i] = StrName();
|
||||||
|
)
|
||||||
_size = 0;
|
_size = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user