mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
backup
This commit is contained in:
parent
e7e8b9141e
commit
eae3c69f85
23
include/pocketpy/common/algorithm.h
Normal file
23
include/pocketpy/common/algorithm.h
Normal file
@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
void *c11__lower_bound(const void *key, const void *ptr, int count, int size,
|
||||
bool (*less)(const void *, const void *));
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
namespace pkpy{
|
||||
template<typename T>
|
||||
T* lower_bound(T* begin, T* end, const T& value){
|
||||
return (T*)c11__lower_bound(&value, begin, end - begin, sizeof(T), [](const void* a, const void* b){
|
||||
return *(T*)a < *(T*)b;
|
||||
});
|
||||
}
|
||||
} // namespace pkpy
|
||||
#endif
|
@ -7,6 +7,8 @@ extern "C" {
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "pocketpy/common/algorithm.h"
|
||||
|
||||
typedef struct c11_array{
|
||||
void* data;
|
||||
int count;
|
||||
@ -30,6 +32,7 @@ void c11_vector__dtor(c11_vector* self);
|
||||
c11_vector c11_vector__copy(const c11_vector* self);
|
||||
void* c11_vector__at(c11_vector* self, int index);
|
||||
void c11_vector__reserve(c11_vector* self, int capacity);
|
||||
void c11_vector__clear(c11_vector* self);
|
||||
|
||||
#define c11__getitem(T, self, index) ((T*)(self)->data)[index]
|
||||
#define c11__setitem(T, self, index, value) ((T*)(self)->data)[index] = value;
|
||||
@ -53,6 +56,21 @@ void c11_vector__reserve(c11_vector* self, int capacity);
|
||||
(self)->count += (size); \
|
||||
}while(0)
|
||||
|
||||
|
||||
#define c11_vector__insert(T, self, index, elem) \
|
||||
do{ \
|
||||
if((self)->count == (self)->capacity) c11_vector__reserve((self), (self)->capacity*2); \
|
||||
memmove((T*)(self)->data + (index) + 1, (T*)(self)->data + (index), ((self)->count - (index)) * sizeof(T)); \
|
||||
((T*)(self)->data)[index] = (elem); \
|
||||
(self)->count++; \
|
||||
}while(0)
|
||||
|
||||
#define c11_vector__erase(T, self, index) \
|
||||
do{ \
|
||||
memmove((T*)(self)->data + (index), (T*)(self)->data + (index) + 1, ((self)->count - (index) - 1) * sizeof(T)); \
|
||||
(self)->count--; \
|
||||
}while(0)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
@ -8,6 +8,7 @@
|
||||
|
||||
#include "pocketpy/common/traits.hpp"
|
||||
#include "pocketpy/common/types.hpp"
|
||||
#include "pocketpy/common/algorithm.h"
|
||||
|
||||
namespace pkpy {
|
||||
|
||||
@ -427,13 +428,13 @@ struct small_map {
|
||||
Item* data() const { return _data.data(); }
|
||||
|
||||
void insert(const K& key, const V& value) {
|
||||
Item* it = std::lower_bound(_data.begin(), _data.end(), key);
|
||||
Item* it = lower_bound(_data.begin(), _data.end(), key);
|
||||
assert(it == _data.end() || it->first != key);
|
||||
_data.insert(it, {key, value});
|
||||
}
|
||||
|
||||
V* try_get(const K& key) const {
|
||||
auto it = std::lower_bound(_data.begin(), _data.end(), key);
|
||||
auto it = lower_bound(_data.begin(), _data.end(), key);
|
||||
if(it == _data.end() || it->first != key) return nullptr;
|
||||
return &it->second;
|
||||
}
|
||||
|
20
src/common/algorithm.c
Normal file
20
src/common/algorithm.c
Normal file
@ -0,0 +1,20 @@
|
||||
#include "pocketpy/common/algorithm.h"
|
||||
|
||||
void *c11__lower_bound(const void *key, const void *ptr, int count, int size,
|
||||
bool (*less)(const void *, const void *)) {
|
||||
char* __first = (char*)ptr;
|
||||
int __len = count;
|
||||
|
||||
while(__len != 0){
|
||||
int __l2 = (int)((unsigned int)__len >> 1);
|
||||
char* __m = __first + __l2 * size;
|
||||
if(less(__m, key)){
|
||||
__first = __m;
|
||||
__m += size;
|
||||
__len -= __l2 + 1;
|
||||
}else{
|
||||
__len = __l2;
|
||||
}
|
||||
}
|
||||
return __first;
|
||||
}
|
@ -60,3 +60,6 @@ void c11_vector__reserve(c11_vector* self, int capacity){
|
||||
self->data = realloc(self->data, self->elem_size * self->capacity);
|
||||
}
|
||||
|
||||
void c11_vector__clear(c11_vector* self){
|
||||
self->count = 0;
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ static bool is_possible_number_char(char c) noexcept{
|
||||
static bool is_unicode_Lo_char(uint32_t c) noexcept{
|
||||
// open a hole for carrot
|
||||
if(c == U'🥕') return true;
|
||||
auto index = std::lower_bound(kLoRangeA, kLoRangeA + 476, c) - kLoRangeA;
|
||||
auto index = lower_bound(kLoRangeA, kLoRangeA + 476, c) - kLoRangeA;
|
||||
if(c == kLoRangeA[index]) return true;
|
||||
index -= 1;
|
||||
if(index < 0) return false;
|
||||
@ -161,7 +161,7 @@ Error* Lexer::eat_name() noexcept{
|
||||
const auto KW_BEGIN = kTokens + TK("False");
|
||||
const auto KW_END = kTokens + kTokenCount;
|
||||
|
||||
auto it = std::lower_bound(KW_BEGIN, KW_END, name);
|
||||
auto it = lower_bound(KW_BEGIN, KW_END, name);
|
||||
if(it != KW_END && *it == name) {
|
||||
add_token(it - kTokens);
|
||||
} else {
|
||||
|
@ -199,7 +199,7 @@ struct Random {
|
||||
List result(k);
|
||||
for(int i = 0; i < k; i++) {
|
||||
f64 r = self.gen.uniform(0.0, cum_weights[size - 1]);
|
||||
int idx = std::lower_bound(cum_weights.begin(), cum_weights.end(), r) - cum_weights.begin();
|
||||
int idx = lower_bound(cum_weights.begin(), cum_weights.end(), r) - cum_weights.begin();
|
||||
result[i] = data[idx];
|
||||
}
|
||||
return VAR(std::move(result));
|
||||
|
Loading…
x
Reference in New Issue
Block a user