1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
#include "bowl.h"
#include "array.h"
#include <stdlib.h>
#include <memory.h>
#include <stdbool.h>
#define ARRAY_START_SIZE 2
#define HASH_START_SIZE 24
void *_get_data(struct bowl *ptr)
{
switch(ptr->type) {
case ARRAY | HASH: return ptr->_pl.array;
case LINKED: return ptr->_pl.linked;
case LEAF: return ptr->_pl.data;
default: return NULL;
}
}
err_t bowl_malloc(struct bowl **ptr, bowl_t type)
{
CHECK((type != 0), INVALID_PARAMS)
(*ptr) = malloc(sizeof(struct bowl));
CHECK(*ptr, MALLOC_FAILED)
memset(*ptr, 0, sizeof(struct bowl));
(*ptr)->type = type;
switch((*ptr)->type) {
case LEAF: return OK; // No further allocation needed
case ARRAY: return array_malloc(*ptr, ARRAY_START_SIZE);
default: return INVALID_STATE;
}
return OK;
}
err_t bowl_insert(struct bowl *self, struct bowl *new)
{
CHECK(self, INVALID_PARAMS)
CHECK(new, INVALID_PARAMS)
switch(self->type) {
case LEAF: return INVALID_PARAMS;
case ARRAY: return array_insert(self, new);
default: return INVALID_STATE;
}
return OK;
}
err_t bowl_insert_key(struct bowl *self, size_t idx, struct bowl *child)
{
CHECK(self, INVALID_PARAMS)
CHECK(child, INVALID_PARAMS)
CHECK((idx >= 0), INVALID_PARAMS)
switch(self->type) {
case LEAF: return INVALID_PARAMS;
case ARRAY: return array_insert_key(self, idx, child);
default: return INVALID_STATE;
}
}
err_t bowl_swap_key(struct bowl *self, size_t idx, struct bowl *child, struct bowl **prev)
{
CHECK(self, INVALID_PARAMS)
CHECK(child, INVALID_PARAMS)
CHECK((idx >= 0), INVALID_PARAMS)
switch(self->type) {
case LEAF: return INVALID_PARAMS;
case ARRAY: return array_swap_key(self, idx, child, prev);
default: return INVALID_STATE;
}
}
err_t bowl_remove(struct bowl *self, struct bowl *child)
{
CHECK(self, INVALID_PARAMS)
CHECK(child, INVALID_PARAMS)
switch(self->type) {
case LEAF: return INVALID_PARAMS;
case ARRAY: return array_remove(self, child);
default: return INVALID_STATE;
}
}
err_t bowl_remove_key(struct bowl *self, size_t idx, struct bowl **prev)
{
CHECK(self, INVALID_PARAMS)
CHECK((idx >= 0), INVALID_PARAMS)
switch(self->type) {
case LEAF: return INVALID_PARAMS;
case ARRAY: return array_remove_key(self, idx, prev);
default: return INVALID_STATE;
}
}
err_t bowl_free(struct bowl *self)
{
CHECK(self, INVALID_PARAMS)
err_t e;
switch(self->type) {
case LEAF: e = data_free(self->_pl.data); break;
case ARRAY: e = array_free(self); break;
default: e = INVALID_STATE; break;
}
if(e) return e;
free(self);
return OK;
}
|