mirror of
https://github.com/pocketpy/pocketpy
synced 2025-12-07 18:50:19 +00:00
some optimize
This commit is contained in:
parent
e765e9f9bd
commit
563ae74a19
@ -13,7 +13,7 @@ extern "C" {
|
|||||||
T* __first = ptr; \
|
T* __first = ptr; \
|
||||||
int __len = count; \
|
int __len = count; \
|
||||||
while(__len != 0) { \
|
while(__len != 0) { \
|
||||||
int __l2 = (int)((unsigned int)__len / 2); \
|
int __l2 = __len >> 1; \
|
||||||
T* __m = __first + __l2; \
|
T* __m = __first + __l2; \
|
||||||
if(less((*__m), (key))) { \
|
if(less((*__m), (key))) { \
|
||||||
__first = ++__m; \
|
__first = ++__m; \
|
||||||
|
|||||||
@ -64,14 +64,21 @@ void SMALLMAP_METHOD(set)(SMALLMAP* self, K key, V value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
V* SMALLMAP_METHOD(try_get)(const SMALLMAP* self, K key) {
|
V* SMALLMAP_METHOD(try_get)(const SMALLMAP* self, K key) {
|
||||||
int index;
|
// use `bsearch` which is faster than `lower_bound`
|
||||||
c11__lower_bound(KV, self->data, self->count, key, less, &index);
|
int low = 0;
|
||||||
KV* it = c11__at(KV, self, index);
|
int high = self->count - 1;
|
||||||
if(index != self->count && equal(it->key, key)) {
|
KV* a = self->data;
|
||||||
return &it->value;
|
while(low <= high){
|
||||||
|
int mid = (low + high) / 2;
|
||||||
|
if(equal(a[mid].key, key)){
|
||||||
|
return &a[mid].value;
|
||||||
|
} else if(less(a[mid], key)){
|
||||||
|
low = mid + 1;
|
||||||
} else {
|
} else {
|
||||||
return NULL;
|
high = mid - 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
V SMALLMAP_METHOD(get)(const SMALLMAP* self, K key, V default_value) {
|
V SMALLMAP_METHOD(get)(const SMALLMAP* self, K key, V default_value) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user