liblash

A bianry tree implementation used for game development from scratch
git clone git://holbrook.no/liblash.git
Log | Files | Refs | LICENSE

commit 6b3a0d194ee155ae409b9c9347234d6240a6edb6
parent fcea9c6b10611e1ded2c338bc0559877f18fe34e
Author: nolash <dev@holbrook.no>
Date:   Mon, 13 Jan 2020 12:11:15 +0100

Remove redundant double pointer in hex2bin, archive old tests

Diffstat:
MMakefile.am | 6++++++
Afromhex.c | 24++++++++++++++++++++++++
Mhex.c | 10++++++----
Minclude/hex.h | 2+-
Rtests/debuglog.c -> old/tests/debuglog.c | 0
Rtests/debuglogmaster.c -> old/tests/debuglogmaster.c | 0
Rtests/debugtimer.c -> old/tests/debugtimer.c | 0
Aold/tests/old/tree.c | 110+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aold/tests/old/tree2.c | 40++++++++++++++++++++++++++++++++++++++++
Aold/tests/old/tree2_console.c | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aold/tests/old/tree2_shifttest.c | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aold/tests/old/tree_console.c | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aold/tests/old/tree_shifttest.c | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aold/tests/old/tree_x.c | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aold/tests/old/treedump.c | 39+++++++++++++++++++++++++++++++++++++++
Rtests/tree3.c -> old/tests/tree3.c | 0
Rtests/tree3_console.c -> old/tests/tree3_console.c | 0
Rtests/tree3_shifttest.c -> old/tests/tree3_shifttest.c | 0
18 files changed, 542 insertions(+), 5 deletions(-)

diff --git a/Makefile.am b/Makefile.am @@ -1,3 +1,5 @@ +AM_CFLAGS = -I$(top_srcdir)/include + lib_LTLIBRARIES = liblash.la libbtree.la libdebug.la liblash_la_SOURCES = hex.c endian.c tree.c liblash_la_CFLAGS = -I./include @@ -7,3 +9,7 @@ libbtree_la_CFLAGS = -I./include libdebug_la_SOURCES = debug_log.c debug_timer.c libdebug_la_CFLAGS = -I./include + +bin_PROGRAMS = fromhex +fromhex_SOURCES = fromhex.c +fromhex_LDADD = liblash.la diff --git a/fromhex.c b/fromhex.c @@ -0,0 +1,24 @@ +#include <stdlib.h> +#include <string.h> +#include <stdio.h> + +#include "hex.h" + +int main(int argc, void *argv[]) { + int i; + int c; + unsigned char *bin; + + if (argc < 2) { + return 1; + } + + c = strlen(argv[1]) / 2; + bin = malloc(c); + hex2bin(argv[1], bin); + for (i = 0; i < c; i++) { + putchar(bin[i]); + } + free(bin); + return 0; +} diff --git a/hex.c b/hex.c @@ -42,7 +42,7 @@ int hexchr2bin(const char hex, char *out) { return 1; } -size_t hex2bin(const char *hex, unsigned char **out) { +size_t hex2bin(const char *hex, unsigned char *out) { size_t len; char b1; char b2; @@ -56,13 +56,15 @@ size_t hex2bin(const char *hex, unsigned char **out) { return 0; len /= 2; - *out = malloc(len); - memset(*out, 'A', len); + //*out = malloc(len); + //memset(*out, 'A', len); + memset(out, 'A', len); for (i=0; i<len; i++) { if (!hexchr2bin(hex[i*2], &b1) || !hexchr2bin(hex[i*2+1], &b2)) { return 0; } - (*out)[i] = (b1 << 4) | b2; + //(*out)[i] = (b1 << 4) | b2; + *(out+i) = (b1 << 4) | b2; } return len; } diff --git a/include/hex.h b/include/hex.h @@ -3,6 +3,6 @@ int toHex(const unsigned char *data, size_t l, unsigned char *zHex, size_t *z); int hexchr2bin(const char hex, char *out); -size_t hex2bin(const char *hex, unsigned char **out); +size_t hex2bin(const char *hex, unsigned char *out); #endif // LASH_HEX_H_ diff --git a/tests/debuglog.c b/old/tests/debuglog.c diff --git a/tests/debuglogmaster.c b/old/tests/debuglogmaster.c diff --git a/tests/debugtimer.c b/old/tests/debugtimer.c diff --git a/old/tests/old/tree.c b/old/tests/old/tree.c @@ -0,0 +1,110 @@ +// test tree insert, remove search +// without args, selects 10 random numbers for test +// with 1 args, selects [arg] random numbers for test +// with 2+ args, inserts args as numbers for test + +// #include "../lash_tree_old.h" +#include "../lash_tree.h" +#include "../lash_tree_dump.h" +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <math.h> + +int main(int argc, char *argv[]) { + + long int *nums; + int i; + lash_tree_t *tree; + clock_t test_clock_insert_total = 0.f; + clock_t test_clock_remove_total = 0.f; + clock_t test_clock_searchlocal_total = 0.f; + unsigned int tmpcount = 0; + if (argc > 2) { + tmpcount = argc - 1; + nums = (long int*)malloc(sizeof(long int)*tmpcount); + for (i = 1; i < argc; i++) { + *(nums + i - 1) = (long int)atoi(argv[i]); + } + } else if (argc == 2) { + tmpcount = (int)atoi(argv[1]); + nums = (long int*)malloc(sizeof(long int)*tmpcount); + } + + if (tmpcount == 0) { + tmpcount = 10; + nums = (long int*)malloc(sizeof(long int)*10); + } + + const unsigned int insert_count = tmpcount; + + if (argc <= 2) { + srand(clock()); + for (i = 0; i < insert_count; i++) { + *(nums + i) = (long int)rand() % 100; + } + } + + tree = lash_treeInit(tree, insert_count); + if (tree == NULL) + return 1; + + lash_treeDumpInit(1); + lash_treeDumpAdd(tree, "heap"); + + for (i = 0; i < insert_count; i++) { + long int n = *(nums + i); + printf("Inserting %li ... ", n); + int pushresult; + clock_t begin = clock(); + pushresult = lash_treePush(tree, n, -1); + //pushresult = lash_treeInsert(tree, n, 0); + test_clock_insert_total += clock() - begin; + if (pushresult == -1) + printf("FAILED\n"); + else + printf("OK in position %d\n", pushresult); + lash_treeDump(tree, NULL); + } + + unsigned int localrandomidx = rand() % (insert_count - 1); + unsigned int localrandomval = rand(); + unsigned int localrandompos = *(tree->idx + localrandomidx); + *(tree->local+localrandompos) = localrandomval; + clock_t begin = clock(); + unsigned int localpos; + localpos = lash_treeFindByLocal(tree, localrandomval); + test_clock_searchlocal_total += clock() - begin; + char randomresult[255]; + sprintf(randomresult, "When filled, inserted random value %d at local pos %d(idx pos %d), lashTreeFindByLocal returned index %d\n", localrandomval, localrandompos, localrandomidx, localpos); + + long int n; + unsigned int l; + unsigned int c = 1; + long int lastresult = 0; + int shiftresult = tree->position; + printf("Sorted output: "); + while (shiftresult) { + clock_t begin = clock(); + shiftresult = lash_treeShift(tree, &n, &l); + //shiftresult = lash_treeRemove(tree, &n, 0); + test_clock_remove_total += clock() - begin; + printf("(%u)%li", c, n); + if (lastresult > n && c > 1) + printf(" <<< SORTERROR!"); + printf("\n"); + lash_treeDump(tree, NULL); + lastresult = n; + c++; + } + printf("\n%s\n", randomresult); + + + + printf("Total insert time (%d longints): %lu ticks @ %li pr sec = %.7f secs\n\n", insert_count, (long)test_clock_insert_total, CLOCKS_PER_SEC, ((double)test_clock_insert_total / CLOCKS_PER_SEC)); + printf("Total removal time (%d longints): %lu ticks @ %li pr sec = %.7f secs\n\n", insert_count, (long)test_clock_remove_total, CLOCKS_PER_SEC, ((double)test_clock_remove_total / CLOCKS_PER_SEC)); + printf("Total searchlocal time (%d longints): %lu ticks @ %li pr sec = %.7f secs\n\n", insert_count, (long)test_clock_searchlocal_total, CLOCKS_PER_SEC, ((double)test_clock_searchlocal_total / CLOCKS_PER_SEC)); + + lash_treeFree(tree); + return 0; +} diff --git a/old/tests/old/tree2.c b/old/tests/old/tree2.c @@ -0,0 +1,40 @@ +#include "../lash_tree2.h" +#include "../lash_tree2_dump.h" +#include <stdio.h> + +int main() { + lash_tree_t *tree; + char *teststring = (char*)malloc(sizeof(char) * 255); + char *teststring2 = (char*)malloc(sizeof(char) * 255); + char *teststring3 = (char*)malloc(sizeof(char) * 255); + char *resultstring; + long int value; + sprintf(teststring, "one"); + sprintf(teststring2, "two"); + sprintf(teststring3, "three"); + + tree = lash_treeInit(tree, 10); + if (tree == NULL) + return 1; + + lash_treeDumpInit(1); + lash_treeDumpAdd(tree, "tree"); + + + lash_treePush(tree, 10, (void*)teststring); + lash_treeDump(NULL, NULL); + lash_treePush(tree, 20, (void*)teststring2); + lash_treeDump(NULL, NULL); + lash_treeShift(tree, &value, &resultstring); + printf("Shift got %li and %s\n", value, resultstring); + lash_treeDump(NULL, NULL); + lash_treePush(tree, 30, (void*)teststring3); + lash_treeDump(NULL, NULL); + lash_treeShift(tree, &value, &resultstring); + printf("Shift got %li and %s\n", value, resultstring); + lash_treeDump(NULL, NULL); + free(teststring); + lash_treeFree(tree); + + return 0; +} diff --git a/old/tests/old/tree2_console.c b/old/tests/old/tree2_console.c @@ -0,0 +1,61 @@ +#include "../lash_tree2.h" +#include "../lash_tree2_dump.h" +#include <stdlib.h> +#include <stdio.h> + +int main(int argc, char **argv) { + int run = 1; + int dump = 1; + char buf[1024]; + + lash_tree_t *tree; + tree = lash_treeInit(tree, 100); + if (tree == NULL) + return 1; + + lash_treeDumpInit(1); + lash_treeDumpAdd(tree, "tree"); + + while (run) { + char cmd; + long int val1 = -1; + char *val2 = NULL; + unsigned int pos = 0; + printf(">> "); + fflush(stdout); + fgets(buf, 1024, stdin); + sscanf(buf, "%c", &cmd); + + switch(cmd) { + case 'p': + printf("value: "); + fflush(stdout); + fgets(buf, 1024, stdin); + sscanf(buf, "%li", &val1); + if (val1 != -1) { + pos = lash_treePush(tree, val1, val2); + printf("pos: %u\n", pos); + } + break; + case 's': + if (tree->count == 0) { + printf("Nothing to shift\n"); + } else { + lash_treeShift(tree, &val1, &val2); + printf("val1: %li, val2: %p\n", val1, val2); + } + break; + case 'd': + dump ^= 1; + printf(dump ? "(Dump on)\n" : "(Dump off)\n"); + break; + case 'q': + run = 0; + break; + } + + if (dump) + lash_treeDump(tree, NULL); + } + return 0; +} diff --git a/old/tests/old/tree2_shifttest.c b/old/tests/old/tree2_shifttest.c @@ -0,0 +1,66 @@ +#include "../lash_tree2.h" +#include "../lash_tree2_dump.h" +#include <stdlib.h> +#include <stdio.h> +#include <limits.h> +#include <time.h> + +#define TREE_CAPACITY 100 +#define HEAP_RANGE 1000 + +int main(int argc, char **argv) { + time_t t; + lash_tree_t *tree; + int i; + int j; + char pushvaluestring[255]; + char *local; + long int lastnumber; + + srand((unsigned) time(&t)); + + tree = lash_treeInit(tree, TREE_CAPACITY); + if (tree == NULL) + return 1; + + lash_treeDumpInit(1); + lash_treeDumpAdd(tree, "tree"); + + i = 0; + for (j = 0; j < TREE_CAPACITY; j++) { + long int pushvalue = -1; + if (j > 0) + pushvalue = rand() % HEAP_RANGE; + + if (i % 2 == 0 && i > 0) { + long int number; + unsigned int local; + fprintf(stderr, "Shifting!\n"); + lash_treeShift(tree, &number, &local); + fprintf(stderr, "Shift yield n%li l%d\n", number, local); + j--; + } + + sprintf(pushvaluestring, "before push [%li]", pushvalue); + fprintf(stderr, "pushing %li\n", pushvalue); + //lash_treeDump(tree, pushvaluestring); + lash_treePush(tree, pushvalue, 0); + lash_treeDump(tree, "after push"); + i++; + } + + printf("Unpacking\n"); + + lastnumber = -1; + for (i = 0; i < TREE_CAPACITY; i++) { + long int number; + lash_treeShift(tree, &number, &local); + printf("%li ", number); + if (number < lastnumber) + printf("SORTERROR! "); + lastnumber = number; + } + printf("\n"); + + return 0; +} diff --git a/old/tests/old/tree_console.c b/old/tests/old/tree_console.c @@ -0,0 +1,60 @@ +#include "../lash_tree.h" +#include "../lash_tree_dump.h" +#include <stdlib.h> +#include <stdio.h> + +int main(int argc, char **argv) { + int run = 1; + int dump = 1; + char buf[1024]; + + lash_tree_t *tree; + tree = lash_treeInit(tree, 100); + if (tree == NULL) + return 1; + lash_treeDumpInit(1); + lash_treeDumpAdd(tree, "tree"); + + while (run) { + char cmd; + long int val1 = -1; + unsigned int val2 = 0; + unsigned int pos = 0; + printf(">> "); + fflush(stdout); + fgets(buf, 1024, stdin); + sscanf(buf, "%c", &cmd); + + switch(cmd) { + case 'p': + printf("value: "); + fflush(stdout); + fgets(buf, 1024, stdin); + sscanf(buf, "%li", &val1); + if (val1 != -1) { + pos = lash_treePush(tree, val1, val2); + printf("pos: %u\n", pos); + } + break; + case 's': + if (tree->position == 0) { + printf("Nothing to shift\n"); + } else { + lash_treeShift(tree, &val1, &val2); + printf("val1: %li, val2: %u\n", val1, val2); + } + break; + case 'd': + dump ^= 1; + printf(dump ? "(Dump on)\n" : "(Dump off)\n"); + break; + case 'q': + run = 0; + break; + } + + if (dump) + lash_treeDump(NULL, NULL); + } + return 0; +} diff --git a/old/tests/old/tree_shifttest.c b/old/tests/old/tree_shifttest.c @@ -0,0 +1,59 @@ +#include "../lash_tree.h" +#include "../lash_tree_dump.h" +#include <stdlib.h> +#include <stdio.h> +#include <limits.h> +#include <time.h> + +#define TREE_CAPACITY 10 +#define HEAP_RANGE 100 + +int main(int argc, char **argv) { + time_t t; + lash_tree_t *tree; + int i; + int j; + char pushvaluestring[255]; + + srand((unsigned) time(&t)); + + tree = lash_treeInit(tree, TREE_CAPACITY); + if (tree == NULL) + return 1; + + lash_treeDumpInit(1); + lash_treeDumpAdd(tree, "tree"); + + i = 0; + for (j = 0; j < TREE_CAPACITY; j++) { + long int pushvalue = -1; + if (j > 0) + pushvalue = rand() % HEAP_RANGE; + + if (i % 2 == 0 && i > 0) { + long int number; + unsigned int local; + fprintf(stderr, "Shifting!\n"); + lash_treeShift(tree, &number, &local); + fprintf(stderr, "Shift yield n%li l%d\n", number, local); + j--; + } + + sprintf(pushvaluestring, "before push [%li]", pushvalue); + fprintf(stderr, "pushing %li\n", pushvalue); + //lash_treeDump(tree, pushvaluestring); + lash_treePush(tree, pushvalue, 0); + lash_treeDump(tree, "after push"); + i++; + } + + printf("Unpacking\n"); + for (i = 0; i < TREE_CAPACITY; i++) { + long int number; + lash_treeShift(tree, &number, NULL); + printf("%li ", number); + } + printf("\n"); + + return 0; +} diff --git a/old/tests/old/tree_x.c b/old/tests/old/tree_x.c @@ -0,0 +1,70 @@ +// tree replay step routine +// input string is output from tree.c + +#include "../lash_tree.h" +#include "../lash_tree_dump.h" +#include <regex.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + + +int main() { + + int count = 0; + char *input = "(i1,h6)37 (i2,h8)46 (i3,h2)40 (i4,h3)61 (i5,h9)46 (i6,h1)92 (i7,h5)86 (i8,h0)95 (i9,h4)83 \0"; + char *cursor = input; + //strcat(cursor, "\0"); + long int *tmpheap = (long int*)malloc(sizeof(long int) * 255); + unsigned int *tmpidx = (unsigned int*)malloc(sizeof(unsigned int) * 255); + lash_tree_t *tree; + tree = lash_treeInit(tree, 255); + if (tree == NULL) + return 1; + lash_treeDumpInit(1); + lash_treeDumpAdd(tree, "heap"); + + int rc = 100; + regex_t rx; + int re = 0; + + regmatch_t rp[rc]; + char *rb = (char*)malloc(255); + //re = regcomp(&rx, "/\(i([:digit:]+),h([:digit:]+))[:digit:]+ "); + re = regcomp(&rx, "i([0-9]+),h([0-9]+))([0-9]+) ", REG_EXTENDED); + + while (re != REG_NOMATCH) { + unsigned int result_h, result_i = 0; + long int result_r = 0; + char *tmpstring = (char*)malloc(16); + re = regexec(&rx, cursor, rc, rp, 0); + regerror (re, &rx, rb, 255); + if (re == REG_NOMATCH) + continue; + strncpy(tmpstring, cursor + rp[2].rm_so, rp[2].rm_eo - rp[2].rm_so); + *(tmpstring + rp[2].rm_eo - rp[2].rm_so) = 0; + result_h = (unsigned int)atoi(tmpstring); + strncpy(tmpstring, cursor + rp[1].rm_so, rp[1].rm_eo - rp[1].rm_so); + *(tmpstring + rp[1].rm_eo - rp[1].rm_so) = 0; + result_i = (unsigned int)atoi(tmpstring); + strncpy(tmpstring, cursor + rp[3].rm_so, rp[3].rm_eo - rp[3].rm_so); + *(tmpstring + rp[3].rm_eo - rp[3].rm_so) = 0; + result_r = (long int)atoi(tmpstring); + + *(tmpidx + result_i - 1) = result_h; + *(tmpheap + result_h) = result_r; + cursor += rp[0].rm_eo; + count++; + printf("%d: %i %i (n%li), cursor is now '%s'\n", count, (int)rp[0].rm_so, (int)rp[0].rm_eo, result_r, cursor); + } + + tree->idx = tmpidx; + tree->heap = tmpheap; + tree->position = count; + + long int n = -1; + unsigned int l = 0; + lash_treeRemove(tree, 0, &n, &l); + lash_treeDump(NULL, NULL); + return 0; +} diff --git a/old/tests/old/treedump.c b/old/tests/old/treedump.c @@ -0,0 +1,39 @@ +#include "../lash_tree.h" +#include "../lash_tree_dump.h" +#include <stdlib.h> +#include <stdio.h> + +int main(int argc, char **argv) { + + lash_tree_t *tree1; + lash_tree_t *tree2; + lash_tree_t *tree3; + + tree1 = lash_treeInit(tree1, 10); + if (tree1 == NULL) + return 1; + + tree2 = lash_treeInit(tree2, 13); + if (tree2 == NULL) + return 1; + + tree3 = lash_treeInit(tree3, 12); + if (tree3 == NULL) + return 1; + + printf("Tree 1 has cap %d\n", tree1->capacity); + printf("Tree 2 has cap %d\n", tree2->capacity); + printf("Tree 3 has cap %d\n", tree3->capacity); + lash_treeDumpInit(2); + lash_treeDumpAdd(tree1, "test1"); + lash_treeDumpAdd(tree3, "test3"); + lash_treePush(tree1, 10, 0); + lash_treePush(tree1, 15, 0); + lash_treePush(tree1, 20, 0); + lash_treePush(tree3, 30, 0); + lash_treePush(tree3, 40, 2); + lash_treeDump(NULL, NULL); + lash_treeDump(tree1, NULL); + lash_treeDump(tree3, "test of comment"); + return 0; +} diff --git a/tests/tree3.c b/old/tests/tree3.c diff --git a/tests/tree3_console.c b/old/tests/tree3_console.c diff --git a/tests/tree3_shifttest.c b/old/tests/tree3_shifttest.c