Compare commits
3 Commits
ea6c20f430
...
0d422cd7e5
| Author | SHA1 | Date | |
|---|---|---|---|
| 0d422cd7e5 | |||
| 17d2d1c38d | |||
| a57f05cfeb |
31
include/tlibc/algorithms.h
Normal file
31
include/tlibc/algorithms.h
Normal 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; \
|
||||||
|
}
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@ typedef struct HashMapIter {
|
|||||||
|
|
||||||
typedef struct HashMapKeyValue {
|
typedef struct HashMapKeyValue {
|
||||||
str key;
|
str key;
|
||||||
void* value;
|
void* value_ptr;
|
||||||
} HashMapKeyValue;
|
} HashMapKeyValue;
|
||||||
|
|
||||||
static inline HashMapIter HashMapIter_create(const HashMap_* table){
|
static inline HashMapIter HashMapIter_create(const HashMap_* table){
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ static inline LLNode(T)* LLNode_##T##_createZero(){ \
|
|||||||
LLNode(T)* n = (LLNode(T)*)malloc(sizeof(LLNode(T))); \
|
LLNode(T)* n = (LLNode(T)*)malloc(sizeof(LLNode(T))); \
|
||||||
n->prev = NULL; \
|
n->prev = NULL; \
|
||||||
n->next = NULL; \
|
n->next = NULL; \
|
||||||
memset(&n->value, 0, sizeof(T)); \
|
zeroStruct(&n->value); \
|
||||||
return n; \
|
return n; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
|
|||||||
@@ -153,4 +153,4 @@ typedef struct Result_ {
|
|||||||
}\
|
}\
|
||||||
} while(0)
|
} 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); }
|
||||||
|
|||||||
@@ -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, \
|
a48,a49,a50,a51,a52,a53,a54,a55, a56,a57,a58,a59,a60,a61,a62,a63, \
|
||||||
a64,...) a64
|
a64,...) a64
|
||||||
// Macro for counting variadic arguments (max 64)
|
// Macro for counting variadic arguments (max 64)
|
||||||
// (see usage in kprint.h)
|
// (see usage in cptr.h)
|
||||||
#define count_args(ARGS...) __count_args(ARGS, \
|
#define count_args(ARGS...) __count_args(ARGS, \
|
||||||
64,63,62,61,60,59,58,57, 56,55,54,53,52,51,50,49, \
|
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, \
|
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 printfe(FORMAT, ...) fprintf(stderr, FORMAT ,##__VA_ARGS__)
|
||||||
|
|
||||||
|
#define zeroStruct(STRUCT_PTR) memset((STRUCT_PTR), 0, sizeof(*STRUCT_PTR));
|
||||||
|
|
||||||
/// @warning pointer can be null
|
/// @warning pointer can be null
|
||||||
#define NULLABLE(NAME) NAME
|
#define NULLABLE(NAME) NAME
|
||||||
|
|
||||||
|
|||||||
@@ -225,6 +225,6 @@ bool HashMapIter_getCurrent(HashMapIter* self, HashMapKeyValue* kv){
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
kv->key = bu->key_hash_list.data[self->elem_n].key;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user