Mercurial > hg > GearsTemplate
comparison src/parallel_execution/stack.c @ 116:f57e9ffa7960
add comment rb_tree
author | ikkun |
---|---|
date | Wed, 14 Sep 2016 20:35:21 +0900 |
parents | 9e139a340bd1 |
children | 2bb5e4f0fd35 |
comparison
equal
deleted
inserted
replaced
112:e3cba827d489 | 116:f57e9ffa7960 |
---|---|
1 #include <string.h> | 1 #include <string.h> |
2 #include "stack.h" | 2 #include "stack.h" |
3 | 3 |
4 stack_ptr stack_init(size_t size, int max) { | 4 stack_ptr stack_init(size_t size, int max) { |
5 stack_ptr stack_ptr; | 5 stack_ptr p; |
6 | 6 |
7 if ((stack_ptr = calloc(1, sizeof(stack))) == NULL) | 7 if ((p = calloc(1, sizeof(stack))) == NULL) |
8 return NULL; | 8 return NULL; |
9 | 9 |
10 if ((stack_ptr->data = calloc(max, size)) == NULL) { | 10 if ((p->data = calloc(max, size)) == NULL) { |
11 free(stack_ptr); | 11 free(p); |
12 return NULL; | 12 return NULL; |
13 } | 13 } |
14 | 14 |
15 stack_ptr->size = size; | 15 p->size = size; |
16 stack_ptr->max = max; | 16 p->max = max; |
17 stack_ptr->num = 0; | 17 p->num = 0; |
18 | 18 |
19 return stack_ptr; | 19 return p; |
20 } | 20 } |
21 | 21 |
22 stack_ptr stack_realloc(stack_ptr stack_ptr, int max) { | 22 stack_ptr stack_realloc(stack_ptr p, int max) { |
23 if (stack_ptr == NULL) | 23 if (p == NULL) |
24 return NULL; | 24 return NULL; |
25 | 25 |
26 if ((stack_ptr->data = realloc(stack_ptr->data, stack_ptr->size*max)) == NULL) | 26 if ((p->data = realloc(p->data, p->size*max)) == NULL) |
27 return NULL; | 27 return NULL; |
28 | 28 |
29 stack_ptr->max = max; | 29 p->max = max; |
30 | 30 |
31 return stack_ptr; | 31 return p; |
32 } | 32 } |
33 | 33 |
34 void stack_free(stack_ptr stack_ptr) { | 34 void stack_free(stack_ptr p) { |
35 if (stack_ptr != NULL && stack_ptr->data != NULL) { | 35 if (p != NULL && p->data != NULL) { |
36 free(stack_ptr->data); | 36 free(p->data); |
37 free(stack_ptr); | 37 free(p); |
38 } | 38 } |
39 } | 39 } |
40 | 40 |
41 int stack_push(stack_ptr stack_ptr, void* data) { | 41 int stack_push(stack_ptr p, void* data) { |
42 if (stack_ptr->max <= stack_ptr->num) | 42 if (p->max <= p->num) |
43 return -1; | 43 return -1; |
44 | 44 |
45 memcpy((char*)stack_ptr->data+stack_ptr->num*stack_ptr->size, data, stack_ptr->size); | 45 memcpy((char*)p->data+p->num*p->size, data, p->size); |
46 stack_ptr->num++; | 46 p->num++; |
47 | 47 |
48 return 0; | 48 return 0; |
49 } | 49 } |
50 | 50 |
51 int stack_pop(stack_ptr stack_ptr, void* data) { | 51 int stack_pop(stack_ptr p, void* data) { |
52 if (stack_ptr->num == 0) | 52 if (p->num == 0) |
53 return -1; | 53 return -1; |
54 | 54 |
55 stack_ptr->num--; | 55 p->num--; |
56 | 56 |
57 memcpy(data, (char*)stack_ptr->data+stack_ptr->num*stack_ptr->size, stack_ptr->size); | 57 memcpy(data, (char*)p->data+p->num*p->size, p->size); |
58 | 58 |
59 return 0; | 59 return 0; |
60 } | 60 } |
61 | 61 |
62 int isMax(const stack_ptr stack_ptr) { | 62 int isMax(const stack_ptr p) { |
63 return stack_ptr->max<=stack_ptr->num; | 63 return p->max<=p->num; |
64 } | 64 } |
65 | 65 |
66 int isEmpty(const stack_ptr stack_ptr) { | 66 int isEmpty(const stack_ptr p) { |
67 return stack_ptr->num<=0; | 67 return p->num<=0; |
68 } | 68 } |