This commit is contained in:
Anton Ljungdahl 2025-01-16 23:12:28 +01:00
parent caf9a7f24b
commit bbc1b91772
5 changed files with 126 additions and 18 deletions

View File

@ -3,11 +3,14 @@
ctime -begin timeBuild.ctm
set CommonCompilerFlags=/nologo /Zi /FC /WX /W4 /wd4201 /wd4100 /wd4189 /wd4244 /wd4127 /wd4456
set Sources=../src/main.c
@rem set Sources=../src/main.c
@rem set OutputName=program.exe
set Sources=../src/algorithms_main.c
set OutputName=algorithms_test.exe
IF NOT EXIST .\build mkdir .\build
pushd .\build
cl %CommonCompilerFlags% %Sources% -Feprogram.exe
cl %CommonCompilerFlags% %Sources% -Fe%OutputName%
set LastError=%ERRORLEVEL%
popd

BIN
build/algorithms_test.rdi Normal file

Binary file not shown.

View File

@ -1,4 +1,5 @@
@echo off
pushd build
call program.exe
@rem call program.exe
call algorithms_test.exe
popd

104
src/algorithms_main.c Normal file
View File

@ -0,0 +1,104 @@
// header includes
#include "base/base_inc.h"
#include "os/os_inc.h"
// .c includes
#include "base/base_inc.c"
#include "os/os_inc.c"
#include "os/os_entry_point.c"
#define LOG DebugPrintf
function void
DebugPrintf(const char* format, ...)
{
char buffer[4096];
va_list args;
va_start(args, format);
vsprintf_s(buffer, sizeof(buffer), format, args);
OutputDebugString(buffer);
va_end(args);
}
typedef struct F64Node F64Node;
struct F64Node
{
F64Node *prev;
F64Node *next;
F64 value;
};
typedef struct F64List F64List;
struct F64List
{
F64Node *first;
F64Node *last;
U32 length;
};
F64Node g_f64_nil_node = {0};
function void
F64_push_node(F64List *list, F64Node *n)
{
if(list->first == 0)
{
list->first = n;
list->last = n;
} else {
list->last->next = n;
list->last = n;
}
list->length += 1;
}
function void
F64_push(Arena *arena, F64List *list, F64 value)
{
F64Node *node = PushArrayZero(arena, F64Node, 1);
node->value = value;
F64_push_node(list, node);
}
function void
print_F64_ll(F64List *list)
{
for(F64Node *n = list->first; n != 0; n = n->next)
{
LOG(" %2.f \n", n->value);
}
}
function void
reverse_linked_list_lol(void)
{
Arena *ll_arena = m_make_arena();
F64List list = {0};
for (U32 i = 0; i < 10; i++)
{
F64_push(ll_arena, &list, (F64)i);
}
print_F64_ll(&list);
F64Node *prev = 0;
F64Node *current = list.first;
F64Node *next_temp = 0;
while(current != 0)
{
next_temp = current->next;
current->next = prev;
prev = current;
current = next_temp;
}
list.first = prev;
print_F64_ll(&list);
}
void
EntryPoint(void)
{
reverse_linked_list_lol();
}

View File

@ -102,13 +102,13 @@
// f, l, n are "first", "last", "node" I think?
// DLL
// Doubly Linked List: Each node has a prev and next pointer. Operations: Push back, Push front, remove
#define DLLInsert_NPZ(f,l,p,n,next,prev,zchk,zset) \
(zchk(f) ? (((f) = (l) = (n)), zset((n)->next), zset((n)->prev)) :\
zchk(p) ? (zset((n)->prev), (n)->next = (f), (zchk(f) ? (0) : ((f)->prev = (n))), (f) = (n)) :\
#define DLLInsert_NPZ(f,l,p,n,next,prev,zchk,SetNull) \
(zchk(f) ? (((f) = (l) = (n)), SetNull((n)->next), SetNull((n)->prev)) :\
zchk(p) ? (SetNull((n)->prev), (n)->next = (f), (zchk(f) ? (0) : ((f)->prev = (n))), (f) = (n)) :\
((zchk((p)->next) ? (0) : (((p)->next->prev) = (n))), (n)->next = (p)->next, (n)->prev = (p), (p)->next = (n),\
((p) == (l) ? (l) = (n) : (0))))
#define DLLPushBack_NPZ(f,l,n,next,prev,zchk,zset) DLLInsert_NPZ(f,l,l,n,next,prev,zchk,zset)
#define DLLPushBack_NPZ(f,l,n,next,prev,zchk,SetNull) DLLInsert_NPZ(f,l,l,n,next,prev,zchk,SetNull)
#define DLLPushBack_NP(f, l, n, next, prev, zchk) \
(zchk(f) ? ((f)=(l)=(n),(n)->next=(n)->prev=0) : ((n)->prev=(l),(l)->next=(n),(l)=(n),(n)->next=0))
@ -124,10 +124,10 @@ zchk(p) ? (zset((n)->prev), (n)->next = (f), (zchk(f) ? (0) : ((f)->prev = (n)))
((n)->next->prev=(n)->prev, \
(n)->prev->next=(n)->next) ))
#define DLLRemove_NPZ(f,l,n,next,prev,zchk,zset) (((f)==(n))?\
((f)=(f)->next, (zchk(f) ? (zset(l)) : zset((f)->prev))):\
#define DLLRemove_NPZ(f,l,n,next,prev,zchk,SetNull) (((f)==(n))?\
((f)=(f)->next, (zchk(f) ? (SetNull(l)) : SetNull((f)->prev))):\
((l)==(n))?\
((l)=(l)->prev, (zchk(l) ? (zset(f)) : zset((l)->next))):\
((l)=(l)->prev, (zchk(l) ? (SetNull(f)) : SetNull((l)->next))):\
((zchk((n)->next) ? (0) : ((n)->next->prev=(n)->prev)),\
(zchk((n)->prev) ? (0) : ((n)->prev->next=(n)->next))))
@ -143,17 +143,17 @@ zchk(p) ? (zset((n)->prev), (n)->next = (f), (zchk(f) ? (0) : ((f)->prev = (n)))
////////////////
// Queue
// Queue has only a next pointer. But we can push from front also.
// zchk = zero check, zset = zero set
#define QueuePush_NZ(f, l, n, next, zchk, zset) (zchk(f)?\
(((f)=(l)=(n)), zset((n)->next)):\
((l)->next=(n),(l)=(n),zset((n)->next)))
// zchk = zero check, SetNull = zero set
#define QueuePush_NZ(f, l, n, next, zchk, SetNull) (zchk(f)?\
(((f)=(l)=(n)), SetNull((n)->next)):\
((l)->next=(n),(l)=(n),SetNull((n)->next)))
#define QueuePushFront_NZ(f, l, n, next, zchk, zset) ( zchk(f) ? \
((f)=(l)=(n)), zset((n)->next) : \
#define QueuePushFront_NZ(f, l, n, next, zchk, SetNull) ( zchk(f) ? \
((f)=(l)=(n)), SetNull((n)->next) : \
((n)->next = (f)), ((f) = (n)) )
#define QueuePop_NZ(f, l, next, zchk, zset) ( (f)==(l) ? \
(zset(f), zset(l)) : ((f)=(f)->next))
#define QueuePop_NZ(f, l, next, zchk, SetNull) ( (f)==(l) ? \
(SetNull(f), SetNull(l)) : ((f)=(f)->next))
#define QueuePush(f, l, n) QueuePush_NZ(f, l, n, next, CheckNull, SetNull)
#define QueuePushFront(f, l, n) QueuePushFront_NZ(f, l, n, next, CheckNull, SetNull)