Compare commits
2 Commits
ea6c20f430
...
17d2d1c38d
| Author | SHA1 | Date | |
|---|---|---|---|
| 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 < 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; \
|
||||
}
|
||||
|
||||
@@ -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; \
|
||||
} \
|
||||
\
|
||||
|
||||
@@ -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); }
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user