-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringtree.cpp
More file actions
295 lines (244 loc) · 9.72 KB
/
stringtree.cpp
File metadata and controls
295 lines (244 loc) · 9.72 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//------------------------------------------------------------------------------
#include "stringtree.h"
#include "dfprintf.h"
#include "macros.h"
#include <stdio.h>
//==============================================================================
StringTree::StringTree()
// Empty constructor
{
root = 0;
Clear(); // Guess
}
//------------------------------------------------------------------------------
StringTree::StringTree(string * ps)
// Constructor from string
{
root = 0;
UnStream(*ps);
}
//------------------------------------------------------------------------------
StringTree::StringTree(StringTree & rs)
// Copy constructor
{
root = 0;
string buffer;
rs.Stream(&buffer);
UnStream(buffer); // And why not?
}
//------------------------------------------------------------------------------
StringTree::~StringTree(void)
// Recursive deletion of node tree
{
if (root!=0) delete root;
}
//------------------------------------------------------------------------------
void * StringTree::Add(string str,int im,void * pd)
// Create the root image/string node
{
if (root!=0) delete root; // Trash any existing
root = new Node(this,str,im,pd); // New root node
return (void *)root; // And out it goes
}
//------------------------------------------------------------------------------
void * StringTree::Add(void * n,string str,int im,void * pd)
// Add a child node to "n", return the new node address
{
Node * pN = new Node(this,str,im,pd); // New node
Node * pn = static_cast<Node *>(n); // Type-force putative parent
pn->child.push_back(pN); // Parent->child link
pN->par = pn; // Child->parent link
return (void *)pN;
}
//------------------------------------------------------------------------------
void StringTree::Clear()
{
if (root!=0) delete root; // Lose any tree
root = 0; // No tree root
count = 0; // No nodes ... obviously
pstream = 0; // No streaming string
smap.clear(); // No streaming map
umap.clear(); // No unstreaming map
nfc = 0; // Next free byte pointer
}
//------------------------------------------------------------------------------
void StringTree::Dump()
{
printf("================\n");
printf("StringTree dump:\n");
printf("BytesPerPointer = %u\n",BPP); // Bytes per pointer
printf("BytesPerIinteger = %u\n",BPI); // Bytes per integer
printf("BytesPerUnsigned = %u\n",BPU); // Bytes per unsigned
printf("count = %d\n",count);
printf("root = %#010x\n",root);
if (root!=0) root->Dump();
printf("nfc = %u\n",nfc);
printf("\nStream map:\n");
WALKMAP(Node *,unsigned,smap,i) printf("%#010x : %010u\n",(*i).first,(*i).second);
printf("\nUnstream map:\n");
WALKMAP(unsigned,Node *,umap,i) printf("%010u : %#010x\n",(*i).first,(*i).second);
printf("\n");
printf("================\n");
}
//------------------------------------------------------------------------------
void StringTree::Get(void * p,string & rs,int & ri,void *& rpv)
// Routine to extract the data from a node
{
Node * pN = static_cast<Node *>(p); // Node handle
if (pN==0) pN = root; // 0 => use root
if (pN==0) return; // No root => no tree
rs = pN->str;
ri = pN->image;
rpv = pN->pdata;
}
//------------------------------------------------------------------------------
void StringTree::Get(void * p,void(*pcb)(StringTree*,void *))
// Routine to walk the children of a node, passing each handle to the callback
{
if (pcb==0) return; // No function to call
Node * pN = static_cast<Node *>(p); // Node handle
if (pN==0) pN = root; // 0 => use root
if (pN==0) return; // No root => no tree
WALKVECTOR(Node *,pN->child,i) pcb(this,*i);
}
//------------------------------------------------------------------------------
StringTree::Node * StringTree::Root()
// I gave in with encapsulation bit. This gives the user-side complete access
// to the tree. Don't break it.
{
return root;
}
//------------------------------------------------------------------------------
void StringTree::Stream(string * buffer)
// Write the entire data structure out into a linear string, so it can be
// passed around (and even sent via MPI...)
{
if (buffer==0) return; // Empty call
if (root==0) return; // If we're empty, nothing to do
buffer->clear(); // If it wasn't empty, it should have bin
pstream = buffer;
smap.clear(); // Clear the streaming address map
nfc = 0; // Next free cell in buffer
WalkStream(root); // Build string
pstream = 0; // Disconnect it
}
//------------------------------------------------------------------------------
string StringTree::STint2str(int i)
// Turns an integer into a byte string
{
bodge.i = i; // Copy value to the bodging union
string s;
s.assign(bodge.cc,BPI); // Have to use assign() 'cos +=
return s; // truncates at the first '\0'
}
//------------------------------------------------------------------------------
int StringTree::STstr2int(string & s,unsigned & n)
// Turns a substring (starting at position n) string into an integer
{
// Copy the bytes
for(unsigned i=n;i<n+BPI;i++) bodge.cc[i-n] = s[i];
n += BPI; // Increment the next free byte pointer
return bodge.i;
}
//------------------------------------------------------------------------------
string StringTree::STstr2str(string & s,unsigned & n)
// Pulls out a substring and increments the next free byte pointer.
// In the buffer, the substring is preceded by an unsigned length.
{
unsigned len = STstr2unsigned(s,n); // Length of substring
string a = s.substr(n,len); // Hoik it out
n += a.size(); // Increment pointer
return a;
}
//------------------------------------------------------------------------------
unsigned StringTree::STstr2unsigned(string & s,unsigned & n)
// Turns a substring (starting at position n) string into an unsigned
{
for(unsigned i=n;i<n+BPU;i++) bodge.cc[i-n] = s[i];
n += BPU;
return bodge.u;
}
//------------------------------------------------------------------------------
void * StringTree::STstr2voidp(string & s,unsigned & n)
// Turns a substring (starting at position n) string into a void *
{
for(unsigned i=n;i<n+BPP;i++) bodge.cc[i-n] = s[i];
n += BPP;
return bodge.p;
}
//------------------------------------------------------------------------------
string StringTree::STunsigned2str(unsigned u)
// Turns an unsigned into a byte string
{
bodge.u = u;
string s;
s.assign(bodge.cc,BPU);
return s;
}
//------------------------------------------------------------------------------
string StringTree::STvoidp2str(void * p)
// Turns a void * into a byte string
{
bodge.p = p;
string s;
s.assign(bodge.cc,BPP);
return s;
}
//------------------------------------------------------------------------------
void StringTree::UnStream(string & s)
// Unpack - in a single pass - the tree structure packed into the string.
{
Clear(); // Clear any existing data structure
while (nfc<s.size()) { // Walk the string
unsigned tmp = nfc; // Save the start byte position
unsigned par_ = STstr2unsigned(s,nfc); // Parent offset
int image_ = STstr2int(s,nfc); // Tree data
void * pdata_ = STstr2voidp(s,nfc);
string str_ = STstr2str(s,nfc);
void * pN; // Create the new node
if (umap.empty()) pN = Add(str_,image_,pdata_);// New root
else pN = Add(umap[par_],str_,image_,pdata_); // New not-root
umap[tmp] = (Node *)pN; // Map the start byte to the node
}
}
//------------------------------------------------------------------------------
void StringTree::WalkStream(Node * pN)
// Recursive walk to pack the stream string
{
smap[pN] = nfc; // Map the node to its start byte
*pstream += STunsigned2str(smap[pN->par]); // Write the offset
*pstream += STint2str(pN->image); // Write the node data
*pstream += STvoidp2str(pN->pdata);
*pstream += STunsigned2str(pN->str.size());
*pstream += pN->str;
nfc = pstream->size(); // Update the next free byte
WALKVECTOR(Node *,pN->child,i) WalkStream(*i); // Down we go....
}
//==============================================================================
StringTree::Node::Node(StringTree * p,string s,int im,void * pd)
{
str = s; // String
image = im; // Image index
par = 0; // Parent node
cnt = p->count++; // Node count
pdata = pd; // Hanger for outside use
pST = p; // Enclosing object
}
//------------------------------------------------------------------------------
StringTree::Node::~Node()
{
WALKVECTOR(Node *,child,i) delete *i;
}
//------------------------------------------------------------------------------
void StringTree::Node::Dump(int off)
{
string B;
for (int i=0;i<off;i++) dprintf(B," ");
dprintf(B,"this = %#010x, parent = %#010x, pdata = %#010x, image = %3d, string = %s\n",
this,par,pdata,image,str.c_str());
printf("%s",B.c_str());
WALKVECTOR(Node *,child,i)
if ((*i)->par!=this) printf("Node child->par link fault\n");
WALKVECTOR(Node *,child,i) (*i)->Dump(off+2);
}
//==============================================================================