~ubuntu-branches/ubuntu/trusty/gobject-introspection/trusty

« back to all changes in this revision

Viewing changes to girepository/cmph/vstack.c

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2011-03-22 00:32:36 UTC
  • mfrom: (1.4.1 upstream) (3.3.33 multiarch)
  • Revision ID: james.westby@ubuntu.com-20110322003236-4spdgfk1vai6xay1
Tags: 0.10.4-2
Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "vstack.h"
 
2
 
 
3
#include <stdlib.h>
 
4
#include <assert.h>
 
5
 
 
6
//#define DEBUG
 
7
#include "debug.h"
 
8
 
 
9
struct __vstack_t
 
10
{
 
11
        cmph_uint32 pointer;
 
12
        cmph_uint32 *values;
 
13
        cmph_uint32 capacity;
 
14
};
 
15
 
 
16
vstack_t *vstack_new(void)
 
17
{
 
18
        vstack_t *stack = (vstack_t *)malloc(sizeof(vstack_t));
 
19
        assert(stack);
 
20
        stack->pointer = 0;
 
21
        stack->values = NULL;
 
22
        stack->capacity = 0;
 
23
        return stack;
 
24
}
 
25
 
 
26
void vstack_destroy(vstack_t *stack)
 
27
{
 
28
        assert(stack);
 
29
        free(stack->values);
 
30
        free(stack);
 
31
}
 
32
 
 
33
void vstack_push(vstack_t *stack, cmph_uint32 val)
 
34
{
 
35
        assert(stack);
 
36
        vstack_reserve(stack, stack->pointer + 1);
 
37
        stack->values[stack->pointer] = val;
 
38
        ++(stack->pointer);
 
39
}
 
40
void vstack_pop(vstack_t *stack)
 
41
{
 
42
        assert(stack);
 
43
        assert(stack->pointer > 0);
 
44
        --(stack->pointer);
 
45
}
 
46
 
 
47
cmph_uint32 vstack_top(vstack_t *stack)
 
48
{
 
49
        assert(stack);
 
50
        assert(stack->pointer > 0);
 
51
        return stack->values[(stack->pointer - 1)];
 
52
}
 
53
int vstack_empty(vstack_t *stack)
 
54
{
 
55
        assert(stack);
 
56
        return stack->pointer == 0;
 
57
}
 
58
cmph_uint32 vstack_size(vstack_t *stack)
 
59
{
 
60
        return stack->pointer;
 
61
}
 
62
void vstack_reserve(vstack_t *stack, cmph_uint32 size)
 
63
{
 
64
        assert(stack);
 
65
        if (stack->capacity < size)
 
66
        {
 
67
                cmph_uint32 new_capacity = stack->capacity + 1;
 
68
                DEBUGP("Increasing current capacity %u to %u\n", stack->capacity, size);
 
69
                while (new_capacity     < size)
 
70
                {
 
71
                        new_capacity *= 2;
 
72
                }
 
73
                stack->values = (cmph_uint32 *)realloc(stack->values, sizeof(cmph_uint32)*new_capacity);
 
74
                assert(stack->values);
 
75
                stack->capacity = new_capacity;
 
76
                DEBUGP("Increased\n");
 
77
        }
 
78
}
 
79