(working on the layer-stack)
parent
43d849606a
commit
e8bec2c0a2
|
@ -10,7 +10,7 @@
|
|||
* TODO: implement, document, everything
|
||||
* - this needs to be reorganized
|
||||
* - much of this should probably be split out into a generic flexable_array
|
||||
* type of some sort
|
||||
* type of some sort (unless the grow and shrink functions are really small)
|
||||
*/
|
||||
|
||||
|
||||
|
@ -20,6 +20,8 @@
|
|||
// ----------------------------------------------------------------------------
|
||||
|
||||
#define BLOCK_SIZE 5
|
||||
#define TYPE_SIZE sizeof(element_t)
|
||||
#define MARGIN 3
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
|
@ -29,38 +31,34 @@ typedef struct {
|
|||
} element_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t size_allocated;
|
||||
uint8_t size_filled;
|
||||
uint8_t allocated;
|
||||
uint8_t filled;
|
||||
element_t * data;
|
||||
} stack_t;
|
||||
|
||||
stack_t stack = { .size_allocated = BLOCK_SIZE;
|
||||
.size_filled = 0,
|
||||
.data = malloc( sizeof(element *) * BLOCK_SIZE ); };
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
uint8_t grow_stack(stack_t stack) {
|
||||
element_t * temp = realloc( stack.data, stack.size_allocated + BLOCK_SIZE );
|
||||
if (temp) {
|
||||
stack.data = temp;
|
||||
stack.size_allocated += BLOCK_SIZE;
|
||||
return 0;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
stack_t _stack = {
|
||||
.allocated = BLOCK_SIZE,
|
||||
.filled = 0,
|
||||
.data = (element_t *) malloc( TYPE_SIZE * BLOCK_SIZE ),
|
||||
};
|
||||
|
||||
uint8_t shrink_stack(stack_t stack) {
|
||||
if (stack.size_allocated == BLOCK_SIZE)
|
||||
return 0;
|
||||
|
||||
element_t * temp = realloc( stack.data, stack.size_allocated - BLOCK_SIZE );
|
||||
if (temp) {
|
||||
stack.data = temp;
|
||||
stack.size_allocated -= BLOCK_SIZE;
|
||||
return 0;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
// todo: this should not be the final function
|
||||
static uint8_t _resize_stack(stack_t stack) {
|
||||
uint8_t margin = stack.allocated - stack.filled;
|
||||
if (margin == 0 || margin > MARGIN) {
|
||||
uint8_t new_size = stack.allocated + TYPE_SIZE * (margin == 0)
|
||||
? BLOCK_SIZE
|
||||
: -BLOCK_SIZE;
|
||||
element_t * temp = (element_t *) realloc(stack.data, new_size);
|
||||
if (temp) {
|
||||
stack.data = temp;
|
||||
stack.allocated += (margin == 0) ? BLOCK_SIZE : -BLOCK_SIZE;
|
||||
return 0; // success
|
||||
} else {
|
||||
return 1; // error
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
|
Loading…
Reference in New Issue