liblash

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

tree.c (3461B)


      1 // test tree insert, remove search
      2 // without args, selects 10 random numbers for test
      3 // with 1 args, selects [arg] random numbers for test
      4 // with 2+ args, inserts args as numbers for test
      5 
      6 // #include "../lash_tree_old.h"
      7 #include "../lash_tree.h"
      8 #include "../lash_tree_dump.h"
      9 #include <stdio.h>
     10 #include <stdlib.h>
     11 #include <time.h>
     12 #include <math.h>
     13 
     14 int main(int argc, char *argv[]) {
     15 	
     16 	long int *nums;
     17 	int i;
     18 	lash_tree_t *tree;
     19 	clock_t test_clock_insert_total = 0.f;
     20 	clock_t test_clock_remove_total = 0.f;
     21 	clock_t test_clock_searchlocal_total = 0.f;
     22 	unsigned int tmpcount = 0;
     23 	if (argc > 2) {
     24 		tmpcount = argc - 1;
     25 		nums = (long int*)malloc(sizeof(long int)*tmpcount);
     26 		for (i = 1; i < argc; i++) {
     27 			*(nums + i - 1) = (long int)atoi(argv[i]);
     28 		}
     29 	} else if (argc == 2) {
     30 		tmpcount = (int)atoi(argv[1]);
     31 		nums = (long int*)malloc(sizeof(long int)*tmpcount);
     32 	}
     33 	
     34 	if (tmpcount == 0) {
     35 		tmpcount = 10;
     36 		nums = (long int*)malloc(sizeof(long int)*10);
     37 	}
     38 	
     39 	const unsigned int insert_count = tmpcount;
     40 	
     41 	if (argc <= 2) {
     42 		srand(clock());
     43 		for (i = 0; i < insert_count; i++) {
     44 			*(nums + i) = (long int)rand() % 100;
     45 		}
     46 	}
     47 	
     48 	tree = lash_treeInit(tree, insert_count);
     49 	if (tree == NULL)
     50 		return 1;
     51 	
     52 	lash_treeDumpInit(1);
     53 	lash_treeDumpAdd(tree, "heap");
     54 		
     55 	for (i = 0; i < insert_count; i++) {
     56 		long int n = *(nums + i);
     57 		printf("Inserting %li ... ", n);
     58 		int pushresult;
     59 		clock_t begin = clock();
     60 		pushresult = lash_treePush(tree, n, -1);
     61 		//pushresult = lash_treeInsert(tree, n, 0);
     62 		test_clock_insert_total += clock() - begin;
     63 		if (pushresult == -1)
     64 			printf("FAILED\n");
     65 		else
     66 			printf("OK in position %d\n", pushresult);
     67 		lash_treeDump(tree, NULL);
     68 	}
     69 	
     70 	unsigned int localrandomidx = rand() % (insert_count - 1);
     71 	unsigned int localrandomval = rand();
     72 	unsigned int localrandompos = *(tree->idx + localrandomidx);
     73 	*(tree->local+localrandompos) = localrandomval;
     74 	clock_t begin = clock();
     75 	unsigned int localpos;
     76 	localpos = lash_treeFindByLocal(tree, localrandomval);
     77 	test_clock_searchlocal_total += clock() - begin;
     78 	char randomresult[255];
     79 	sprintf(randomresult, "When filled, inserted random value %d at local pos %d(idx pos %d), lashTreeFindByLocal returned index %d\n", localrandomval, localrandompos, localrandomidx, localpos);
     80 	
     81 	long int n;
     82 	unsigned int l;
     83 	unsigned int c = 1;
     84 	long int lastresult = 0;
     85 	int shiftresult = tree->position;
     86 	printf("Sorted output: ");
     87 	while (shiftresult) {
     88 		clock_t begin = clock();
     89 		shiftresult = lash_treeShift(tree, &n, &l);
     90 		//shiftresult = lash_treeRemove(tree, &n, 0);
     91 		test_clock_remove_total += clock() - begin;
     92 		printf("(%u)%li", c, n);
     93 		if (lastresult > n && c > 1)
     94 			printf(" <<< SORTERROR!");
     95 		printf("\n");
     96 		lash_treeDump(tree, NULL);
     97 		lastresult = n;
     98 		c++;
     99 	}
    100 	printf("\n%s\n", randomresult);
    101 	
    102 	
    103 	
    104 	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));
    105 	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));
    106 	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));
    107 	
    108 	lash_treeFree(tree);
    109 	return 0;
    110 }