Mercurial > hg > GearsTemplate
changeset 25:390cf0523ea7
add file
author | Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 01 May 2015 05:35:10 +0900 |
parents | 7494c0b87ec4 |
children | 06fcbe45e85c |
files | src/llrb/llrb.c src/llrb/stack.h |
diffstat | 2 files changed, 77 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/src/llrb/llrb.c Fri May 01 05:20:47 2015 +0900 +++ b/src/llrb/llrb.c Fri May 01 05:35:10 2015 +0900 @@ -266,12 +266,13 @@ } __code code5(struct Context* context) { - c2 = clock(); - //printf("%ld %ld\n", context->data[1]->count-1, (long)c2-c1); puts("---prev---"); print_tree(pre, 0); puts("---follow---"); print_tree(context->data[Tree]->tree.root, 0); + puts("--Number of Data--"); + printf("%d\n", context->dataNum); + stack_free(pstack); goto meta(context, Exit); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/llrb/stack.h Fri May 01 05:35:10 2015 +0900 @@ -0,0 +1,74 @@ +#include <string.h> + +typedef struct { + size_t size; + int max; + int num; + void* data; +} stack, *stack_ptr; + +stack_ptr stack_init(size_t size, int max) { + stack_ptr stack_ptr; + + if ((stack_ptr = calloc(1, sizeof(stack))) == NULL) + return NULL; + + if ((stack_ptr->data = calloc(max, size)) == NULL) { + free(stack_ptr); + return NULL; + } + + stack_ptr->size = size; + stack_ptr->max = max; + stack_ptr->num = 0; + + return stack_ptr; +} + +stack_ptr stack_realloc(stack_ptr stack_ptr, int max) { + if (stack_ptr == NULL) + return NULL; + + if ((stack_ptr->data = realloc(stack_ptr->data, stack_ptr->size*max)) == NULL) + return NULL; + + stack_ptr->max = max; + + return stack_ptr; +} + +void stack_free(stack_ptr stack_ptr) { + if (stack_ptr != NULL && stack_ptr->data != NULL) { + free(stack_ptr->data); + free(stack_ptr); + } +} + +int stack_push(stack_ptr stack_ptr, void* data) { + if (stack_ptr->max <= stack_ptr->num) + return -1; + + memcpy((char*)stack_ptr->data+stack_ptr->num*stack_ptr->size, data, stack_ptr->size); + stack_ptr->num++; + + return 0; +} + +int stack_pop(stack_ptr stack_ptr, void* data) { + if (stack_ptr->num == 0) + return -1; + + stack_ptr->num--; + + memcpy(data, (char*)stack_ptr->data+stack_ptr->num*stack_ptr->size, stack_ptr->size); + + return 0; +} + +int isMax(const stack_ptr stack_ptr) { + return stack_ptr->max<=stack_ptr->num; +} + +int isEmpty(const stack_ptr stack_ptr) { + return stack_ptr->num<=0; +}