From eae3c69f85e17a06a5503986482faa05b5dc6f21 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 15 Jun 2024 11:56:04 +0800 Subject: [PATCH] backup --- include/pocketpy/common/algorithm.h | 23 +++++++++++++++++++++++ include/pocketpy/common/vector.h | 18 ++++++++++++++++++ include/pocketpy/common/vector.hpp | 5 +++-- src/common/algorithm.c | 20 ++++++++++++++++++++ src/common/vector.c | 3 +++ src/compiler/lexer.cpp | 4 ++-- src/modules/random.cpp | 2 +- 7 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 include/pocketpy/common/algorithm.h create mode 100644 src/common/algorithm.c diff --git a/include/pocketpy/common/algorithm.h b/include/pocketpy/common/algorithm.h new file mode 100644 index 00000000..f5a56509 --- /dev/null +++ b/include/pocketpy/common/algorithm.h @@ -0,0 +1,23 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +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 + 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 \ No newline at end of file diff --git a/include/pocketpy/common/vector.h b/include/pocketpy/common/vector.h index 1620520d..a3c4abf3 100644 --- a/include/pocketpy/common/vector.h +++ b/include/pocketpy/common/vector.h @@ -7,6 +7,8 @@ extern "C" { #include #include +#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 \ No newline at end of file diff --git a/include/pocketpy/common/vector.hpp b/include/pocketpy/common/vector.hpp index ac7d4640..4d05c4cf 100644 --- a/include/pocketpy/common/vector.hpp +++ b/include/pocketpy/common/vector.hpp @@ -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; } diff --git a/src/common/algorithm.c b/src/common/algorithm.c new file mode 100644 index 00000000..0db7d07c --- /dev/null +++ b/src/common/algorithm.c @@ -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; +} \ No newline at end of file diff --git a/src/common/vector.c b/src/common/vector.c index b6212672..b28b75fe 100644 --- a/src/common/vector.c +++ b/src/common/vector.c @@ -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; +} diff --git a/src/compiler/lexer.cpp b/src/compiler/lexer.cpp index 8fd748e6..e87ce605 100644 --- a/src/compiler/lexer.cpp +++ b/src/compiler/lexer.cpp @@ -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 { diff --git a/src/modules/random.cpp b/src/modules/random.cpp index 6dca0d54..131aa298 100644 --- a/src/modules/random.cpp +++ b/src/modules/random.cpp @@ -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));