-
Notifications
You must be signed in to change notification settings - Fork 2
/
symbols.c
212 lines (180 loc) · 5.36 KB
/
symbols.c
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* Emulator for LEGO RCX Brick, Copyright (C) 2003 Jochen Hoenicke.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; see the file COPYING.LESSER. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id: symbols.c 86 2004-05-13 11:54:37Z hoenicke $
*/
#include <stdlib.h>
#include <string.h>
#include "types.h"
#include "symbols.h"
struct symbol {
uint16 addr;
int16 type;
char * name;
struct symbol* left;
struct symbol* right;
};
static struct symbol *root;
/*
// top down splay routine.
//
// If name is in the tree with root t, it will be moved to the
// top of the tree and the element is returned.
//
// If name is not in the tree a neighbour of name is moved to the
// top of the tree and NULL is returned.
//
// If tree is empty NULL is returned.
*/
static struct symbol *splay (uint16 addr, int16 type, struct symbol ** root) {
struct symbol N, *t, *l, *r, *result;
t = *root;
if (t == NULL)
return t;
/* Fast path */
if (t->addr == addr && t->type == type)
return t;
/* Slow path: Need to reorder tree */
result = NULL;
N.left = N.right = NULL;
l = r = &N;
for (;;) {
if (addr < t->addr || (addr == t->addr && type < t->type)) {
if (t->left == NULL)
break;
if (addr < t->left->addr ||
(addr == t->left->addr && type < t->left->type)) {
struct symbol *y;
y = t->left; /* rotate right */
t->left = y->right;
y->right = t;
t = y;
if (t->left == NULL)
break;
}
r->left = t; /* link right */
r = t;
t = t->left;
} else if (addr > t->addr || (addr == t->addr && type > t->type)) {
if (t->right == NULL)
break;
if (addr > t->right->addr ||
(addr == t->right->addr && type > t->right->type)) {
struct symbol *y;
y = t->right; /* rotate left */
t->right = y->left;
y->left = t;
t = y;
if (t->right == NULL)
break;
}
l->right = t; /* link left */
l = t;
t = t->right;
} else {
/* we found the address */
result = t;
break;
}
}
l->right = t->left; /* assemble */
r->left = t->right;
t->left = N.right;
t->right = N.left;
*root = t;
return result;
}
void symbols_add(uint16 addr, int16 type, char* name) {
struct symbol *sym;
splay(addr, type, &root);
sym = malloc(sizeof(struct symbol));
sym->addr = addr;
sym->type = type;
if (root == NULL) {
sym->left = sym->right = NULL;
} else if (addr < root->addr ||
(addr == root->addr && type <= root->type)) {
sym->left = root->left;
sym->right = root;
root->left = NULL;
} else {
sym->right = root->right;
sym->left = root;
root->right = NULL;
}
root = sym;
sym->name = name;
}
char *symbols_get(uint16 addr, int16 type) {
struct symbol *sym = splay(addr, type, &root);
if (sym)
return sym->name;
else
return NULL;
}
static uint16 symbols_getaddr_subtree(char *name, struct symbol *sym) {
uint16 addr;
if (!sym)
return 0;
addr = symbols_getaddr_subtree(name, sym->left);
if (addr)
return addr;
if (strcmp(name, sym->name) == 0)
return sym->addr;
return symbols_getaddr_subtree(name, sym->right);
}
uint16 symbols_getaddr(char *name) {
return symbols_getaddr_subtree(name, root);
}
int symbols_remove(uint16 addr, int16 type) {
struct symbol *sym = splay(addr, type, &root);
if (sym) {
if (!sym->left) {
root = sym->right;
} else {
splay(addr, type, &sym->left);
root = sym->left;
root->right = sym->right;
}
free(sym->name);
free(sym);
return 1;
}
return 0;
}
void symbols_removeall() {
struct symbol * sym = root;
if (sym) {
root = sym->right;
symbols_removeall();
root = sym->left;
symbols_removeall();
free(sym->name);
free(sym);
root = NULL;
}
}
void symbols_iterate(symbols_iterate_func f, void *info) {
struct symbol * sym = root;
if (sym) {
root = sym->left;
symbols_iterate(f, info);
f(info, sym->addr, sym->type, sym->name);
root = sym->right;
symbols_iterate(f, info);
root = sym;
}
}