2004-12-23 15:16:30 +02:00
|
|
|
#include "vstack.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
//#define DEBUG
|
|
|
|
#include "debug.h"
|
|
|
|
|
2005-01-21 22:42:33 +02:00
|
|
|
struct __vstack_t
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
2005-01-18 23:06:08 +02:00
|
|
|
cmph_uint32 pointer;
|
|
|
|
cmph_uint32 *values;
|
|
|
|
cmph_uint32 capacity;
|
2004-12-23 15:16:30 +02:00
|
|
|
};
|
|
|
|
|
2011-01-21 07:11:55 +02:00
|
|
|
vstack_t *vstack_new(void)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
2005-01-21 22:42:33 +02:00
|
|
|
vstack_t *stack = (vstack_t *)malloc(sizeof(vstack_t));
|
2004-12-23 15:16:30 +02:00
|
|
|
assert(stack);
|
|
|
|
stack->pointer = 0;
|
|
|
|
stack->values = NULL;
|
|
|
|
stack->capacity = 0;
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
2005-01-21 22:42:33 +02:00
|
|
|
void vstack_destroy(vstack_t *stack)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
|
|
|
assert(stack);
|
|
|
|
free(stack->values);
|
|
|
|
free(stack);
|
|
|
|
}
|
|
|
|
|
2005-01-21 22:42:33 +02:00
|
|
|
void vstack_push(vstack_t *stack, cmph_uint32 val)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
|
|
|
assert(stack);
|
2005-01-21 22:42:33 +02:00
|
|
|
vstack_reserve(stack, stack->pointer + 1);
|
2004-12-23 15:16:30 +02:00
|
|
|
stack->values[stack->pointer] = val;
|
|
|
|
++(stack->pointer);
|
|
|
|
}
|
2005-01-21 22:42:33 +02:00
|
|
|
void vstack_pop(vstack_t *stack)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
|
|
|
assert(stack);
|
|
|
|
assert(stack->pointer > 0);
|
|
|
|
--(stack->pointer);
|
|
|
|
}
|
|
|
|
|
2005-01-21 22:42:33 +02:00
|
|
|
cmph_uint32 vstack_top(vstack_t *stack)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
|
|
|
assert(stack);
|
|
|
|
assert(stack->pointer > 0);
|
|
|
|
return stack->values[(stack->pointer - 1)];
|
|
|
|
}
|
2005-01-21 22:42:33 +02:00
|
|
|
int vstack_empty(vstack_t *stack)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
|
|
|
assert(stack);
|
|
|
|
return stack->pointer == 0;
|
|
|
|
}
|
2005-01-21 22:42:33 +02:00
|
|
|
cmph_uint32 vstack_size(vstack_t *stack)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
|
|
|
return stack->pointer;
|
|
|
|
}
|
2005-01-21 22:42:33 +02:00
|
|
|
void vstack_reserve(vstack_t *stack, cmph_uint32 size)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
|
|
|
assert(stack);
|
|
|
|
if (stack->capacity < size)
|
|
|
|
{
|
2005-01-18 23:06:08 +02:00
|
|
|
cmph_uint32 new_capacity = stack->capacity + 1;
|
2004-12-23 15:16:30 +02:00
|
|
|
DEBUGP("Increasing current capacity %u to %u\n", stack->capacity, size);
|
|
|
|
while (new_capacity < size)
|
|
|
|
{
|
|
|
|
new_capacity *= 2;
|
|
|
|
}
|
2005-01-18 23:06:08 +02:00
|
|
|
stack->values = (cmph_uint32 *)realloc(stack->values, sizeof(cmph_uint32)*new_capacity);
|
2004-12-23 15:16:30 +02:00
|
|
|
assert(stack->values);
|
|
|
|
stack->capacity = new_capacity;
|
|
|
|
DEBUGP("Increased\n");
|
|
|
|
}
|
|
|
|
}
|