Compare commits

...

3 Commits

Author SHA1 Message Date
0d422cd7e5 HashMapKeyValue.value -> value_ptr 2025-12-02 20:06:02 +05:00
17d2d1c38d added inline insertion sort and binary search 2025-12-02 19:24:46 +05:00
a57f05cfeb zeroStruct 2025-12-02 17:17:39 +05:00
6 changed files with 38 additions and 5 deletions

View File

@@ -0,0 +1,31 @@
#pragma once
#include "std.h"
/// USAGE insertionSort(list.data, list.len, .id)
#define insertionSort_inline(arr, n, field) \
for(i32 i = 1, j; i < (i32)n; i++) { \
j = i; \
while( j > 0 && arr[j - 1]field > arr[i]field){\
arr[j] = arr[j - 1]; \
j--; \
} \
arr[j] = arr[i]; \
} \
#define binarySearch_inline(arr, n, key, field, out_index) {\
i32 low = 0; \
i32 high = n - 1; \
while (low <= high) { \
i32 mid = low + (high - low) / 2; \
if (arr[mid]field == key) { \
out_index = mid; \
break; \
} \
/* choose left or right half */ \
if (arr[mid]field < key) \
low = mid + 1; \
else high = mid - 1; \
} \
out_index = -1; \
}

View File

@@ -43,7 +43,7 @@ typedef struct HashMapIter {
typedef struct HashMapKeyValue {
str key;
void* value;
void* value_ptr;
} HashMapKeyValue;
static inline HashMapIter HashMapIter_create(const HashMap_* table){

View File

@@ -21,7 +21,7 @@ static inline LLNode(T)* LLNode_##T##_createZero(){ \
LLNode(T)* n = (LLNode(T)*)malloc(sizeof(LLNode(T))); \
n->prev = NULL; \
n->next = NULL; \
memset(&n->value, 0, sizeof(T)); \
zeroStruct(&n->value); \
return n; \
} \
\

View File

@@ -153,4 +153,4 @@ typedef struct Result_ {
}\
} while(0)
#define try_assert(EXPR) if(!(EXPR)) { Return RESULT_ERROR(("try_assert(" #EXPR ")"), false); }
#define try_assert(EXPR) if(!(EXPR)) { Return RESULT_ERROR(("assertion must be true: " #EXPR), false); }

View File

@@ -76,7 +76,7 @@ typedef void (*Destructor_t)(void* self);
a48,a49,a50,a51,a52,a53,a54,a55, a56,a57,a58,a59,a60,a61,a62,a63, \
a64,...) a64
// Macro for counting variadic arguments (max 64)
// (see usage in kprint.h)
// (see usage in cptr.h)
#define count_args(ARGS...) __count_args(ARGS, \
64,63,62,61,60,59,58,57, 56,55,54,53,52,51,50,49, \
48,47,46,45,44,43,42,41, 40,39,38,37,36,35,34,33, \
@@ -85,6 +85,8 @@ typedef void (*Destructor_t)(void* self);
#define printfe(FORMAT, ...) fprintf(stderr, FORMAT ,##__VA_ARGS__)
#define zeroStruct(STRUCT_PTR) memset((STRUCT_PTR), 0, sizeof(*STRUCT_PTR));
/// @warning pointer can be null
#define NULLABLE(NAME) NAME

View File

@@ -225,6 +225,6 @@ bool HashMapIter_getCurrent(HashMapIter* self, HashMapKeyValue* kv){
return false;
kv->key = bu->key_hash_list.data[self->elem_n].key;
kv->value = (u8*)bu->value_list.data + self->map->value_t_size * self->elem_n;
kv->value_ptr = (u8*)bu->value_list.data + self->map->value_t_size * self->elem_n;
return true;
}