Compare commits
2
Commits
ac50b21178
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8d0a3968f1 | ||
|
|
901d7a6637 |
@@ -3,7 +3,7 @@
|
|||||||
sonuva is a c helper single header library
|
sonuva is a c helper single header library
|
||||||
|
|
||||||
feat:
|
feat:
|
||||||
[ ] Log
|
[X] Log (Has three levels of Log levels)
|
||||||
|
[ ] HashMap
|
||||||
[ ] String Tokenizer
|
[ ] String Tokenizer
|
||||||
[ ] File I/O
|
[ ] File I/O
|
||||||
|
|
||||||
|
|||||||
@@ -5,44 +5,32 @@
|
|||||||
#define SONUVA_IMPLEMENTATION
|
#define SONUVA_IMPLEMENTATION
|
||||||
#include "sonuva.h"
|
#include "sonuva.h"
|
||||||
|
|
||||||
typedef struct Vector {
|
|
||||||
size_t size;
|
|
||||||
size_t capacity;
|
|
||||||
int *arr;
|
|
||||||
} Vector;
|
|
||||||
|
|
||||||
void vec_push(Vector *v, int value) {
|
|
||||||
if (v) {
|
|
||||||
if (v->size >= v->capacity) {
|
|
||||||
size_t new_capacity = v->capacity * 2;
|
|
||||||
if(new_capacity == 0) new_capacity = 8;
|
|
||||||
int *bigger_arr = realloc(v->arr, (sizeof(int) * new_capacity));
|
|
||||||
v->arr = bigger_arr;
|
|
||||||
v->capacity = new_capacity;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
v = malloc(sizeof(Vector) * 2);
|
|
||||||
v->capacity = sizeof(Vector) * 2;
|
|
||||||
v->size = 0;
|
|
||||||
}
|
|
||||||
v->arr[v->size] = value;
|
|
||||||
v->size++;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Vector *v = malloc(sizeof(Vector));
|
Vector *v = malloc(sizeof(Vector));
|
||||||
vec_push(v, 10);
|
vec_push(v, 10);
|
||||||
vec_push(v, 20);
|
vec_push(v, 20);
|
||||||
for (int i =0; i< v->capacity; i++) {
|
vec_swap(v, 0, 1);
|
||||||
printf("%d\n", v->arr[i]);
|
LOG("Values in Vector");
|
||||||
|
for (int i =0; i< v->size; i++) {
|
||||||
|
printf("%d : %d\n", i, v->arr[i]);
|
||||||
}
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
LOG("Vector Remove", "todo");
|
||||||
/* vec_rem(v, 10); */
|
/* vec_rem(v, 10); */
|
||||||
|
|
||||||
|
LOG("Vector Delete", "todo");
|
||||||
/* vec_delete(v, 10); */
|
/* vec_delete(v, 10); */
|
||||||
|
|
||||||
|
LOG("Vector purge", "todo");
|
||||||
/* vec_purge(v, 10); */
|
/* vec_purge(v, 10); */
|
||||||
/* vec_swap(v, 1, 2); */
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
printf("\n\n\033[31;1;4mTypes of Logs:");
|
||||||
|
printf("\n");
|
||||||
LOG("This is a Log");
|
LOG("This is a Log");
|
||||||
LOG("This is a Warning", "warn");
|
LOG("This is a Warning", "warn");
|
||||||
LOG("This is a TODO", "todo");
|
LOG("This is a TODO", "todo");
|
||||||
|
|||||||
@@ -35,7 +35,58 @@ void LOG2(char *str, char *level) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Vector
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct Vector {
|
||||||
|
size_t size;
|
||||||
|
size_t capacity;
|
||||||
|
int *arr;
|
||||||
|
} Vector;
|
||||||
|
|
||||||
|
|
||||||
|
void vec_push(Vector *v, int value) {
|
||||||
|
if (v) {
|
||||||
|
if (v->size >= v->capacity) {
|
||||||
|
size_t new_capacity = v->capacity * 2;
|
||||||
|
if(new_capacity == 0) new_capacity = 8;
|
||||||
|
int *bigger_arr = realloc(v->arr, (sizeof(int) * new_capacity));
|
||||||
|
v->arr = bigger_arr;
|
||||||
|
v->capacity = new_capacity;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
v = malloc(sizeof(Vector) * 2);
|
||||||
|
v->capacity = sizeof(Vector) * 2;
|
||||||
|
v->size = 0;
|
||||||
|
}
|
||||||
|
v->arr[v->size] = value;
|
||||||
|
v->size++;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void vec_swap(Vector *v, int pos1, int pos2) {
|
||||||
|
int temp = v->arr[pos1];
|
||||||
|
v->arr[pos1] = v->arr[pos2];
|
||||||
|
v->arr[pos2] = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Hashmap
|
||||||
|
|
||||||
|
typedef struct HashMapElem {
|
||||||
|
char* key,
|
||||||
|
char* value;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct HashMap {
|
||||||
|
size_t size;
|
||||||
|
size_t capacity;
|
||||||
|
HashMapElem *arr;
|
||||||
|
} HashMap;
|
||||||
|
|
||||||
|
void hs_put() {}
|
||||||
|
|
||||||
|
void hs_get() {}
|
||||||
|
|
||||||
|
|
||||||
#endif // SONUVA_IMPLEMENTATION
|
#endif // SONUVA_IMPLEMENTATION
|
||||||
|
|||||||
Reference in New Issue
Block a user