-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cc
466 lines (427 loc) · 12.7 KB
/
test.cc
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#include <tcl.h>
#include <stdio.h>
#include "interval_tree.h"
#include "RedBlackTree.h"
#include "sets.H"
static SetDatabaseHandle<char*,IntervalTree *>
global_IntervalTreesInTcl(TCL_STRING_KEYS);
static SetDatabaseHandle<char*,RedBlackTree *>
global_RedBlackTreesInTcl(TCL_STRING_KEYS);
class SimpleInterval : public Interval {
public:
SimpleInterval(const int low,const int high)
:_low(low), _high(high)
{ }
int GetLowPoint() const { return _low;}
int GetHighPoint() const { return _high;}
IntervalTreeNode * GetNode() { return _node;}
void SetNode(IntervalTreeNode * node) {_node = node;}
protected:
int _low;
int _high;
IntervalTreeNode * _node;
};
class SimpleKey : public RedBlackEntry {
public:
SimpleKey(const int key)
:_key(key) {
}
int GetKey() const { return _key;}
protected:
int _key;
};
int CreateIntervalTreeInTcl(ClientData, Tcl_Interp * interp, int argc,
char ** argv)
{
if (argc != 2) {
Tcl_SetResult(interp,
"Wrong # arguments.\nUsage: intTreeCreate nameOfTree\n",
TCL_STATIC);
return TCL_ERROR;
} else if (NULL != global_IntervalTreesInTcl.Search(argv[1])) {
Tcl_SetResult(interp,
"An interval tree with that name already exists!\n",
TCL_STATIC);
return TCL_ERROR;
} else {
global_IntervalTreesInTcl.Add(argv[1],new IntervalTree);
return TCL_OK;
}
}
int DeleteIntervalTreeInTcl(ClientData, Tcl_Interp * interp, int argc,
char ** argv)
{
IntervalTree * treeToDelete;
if (argc != 2) {
Tcl_SetResult(interp,
"Wrong # arguments.\nUsage: intTreeDelete nameOfTree\n",
TCL_STATIC);
return TCL_ERROR;
} else if (NULL == (treeToDelete =
global_IntervalTreesInTcl.Search(argv[1]))) {
Tcl_SetResult(interp,
"An interval tree with that name doesn't exist!\n",
TCL_STATIC);
return TCL_ERROR;
} else {
delete treeToDelete;
global_IntervalTreesInTcl.Delete(argv[1]);
return TCL_OK;
}
}
int QueryIntervalTreeInTcl(ClientData, Tcl_Interp * interp, int argc,
char ** argv)
{
int low;
int high;
IntervalTree * tree;
if (argc != 4) {
Tcl_SetResult(interp,
"Wrong # arguments.\nUsage: intTreeQuery name low high \n",
TCL_STATIC);
return TCL_ERROR;
} else if (NULL == (tree = global_IntervalTreesInTcl.Search(argv[1]))) {
Tcl_SetResult(interp,
"An interval tree with that name doesn't exist!\n",
TCL_STATIC);
return TCL_ERROR;
} else {
if ( (TCL_OK != Tcl_GetInt(interp,argv[2],&low)) ||
(TCL_OK != Tcl_GetInt(interp,argv[3],&high)) )
return TCL_ERROR;
else {
TemplateStack<void *> * queryResults = tree->Enumerate(low,high);
Tcl_Obj * listResult = Tcl_NewListObj(0,NULL);
for (int i=0, last = queryResults->Size(); i < last; i++) {
char buffer[100];
Interval * currentInterval = (Interval *) (*queryResults)[i];
Tcl_Obj * intervalList = Tcl_NewListObj(0,NULL);
sprintf(buffer,"%i",currentInterval->GetLowPoint());
Tcl_ListObjAppendElement(interp,intervalList,
Tcl_NewStringObj(buffer,-1));
sprintf(buffer,"%i",currentInterval->GetHighPoint());
Tcl_ListObjAppendElement(interp,intervalList,
Tcl_NewStringObj(buffer,-1));
Tcl_ListObjAppendElement(interp,listResult,intervalList);
}
Tcl_SetObjResult(interp,listResult);
return TCL_OK;
}
}
}
int AddIntervalToTreeInTcl(ClientData, Tcl_Interp * interp, int argc,
char ** argv)
{
int low;
int high;
IntervalTree * tree;
if (argc != 4) {
Tcl_SetResult(interp,
"Wrong # arguments.\nUsage: intTreeAdd name low high \n",
TCL_STATIC);
return TCL_ERROR;
} else if (NULL == (tree = global_IntervalTreesInTcl.Search(argv[1]))) {
Tcl_SetResult(interp,
"An interval tree with that name doesn't exist!\n",
TCL_STATIC);
return TCL_ERROR;
} else {
if ( (TCL_OK != Tcl_GetInt(interp,argv[2],&low)) ||
(TCL_OK != Tcl_GetInt(interp,argv[3],&high)) )
return TCL_ERROR;
else {
SimpleInterval * newSimpleInterval = new SimpleInterval(low,high);
newSimpleInterval->SetNode(tree->Insert(newSimpleInterval));
return TCL_OK;
}
}
}
int IntervalTreeRemoveNode(ClientData, Tcl_Interp * interp, int argc,
char ** argv)
{
int low;
int high;
IntervalTree * tree;
if (argc != 4) {
Tcl_SetResult(interp,
"Wrong # args.\nUsage: intTreeRemoveNode name low high \n",
TCL_STATIC);
return TCL_ERROR;
} else if (NULL == (tree = global_IntervalTreesInTcl.Search(argv[1]))) {
Tcl_SetResult(interp,
"An interval tree with that name doesn't exist!\n",
TCL_STATIC);
return TCL_ERROR;
} else {
if ( (TCL_OK != Tcl_GetInt(interp,argv[2],&low)) ||
(TCL_OK != Tcl_GetInt(interp,argv[3],&high)) )
return TCL_ERROR;
else {
TemplateStack<void *> * queryResults = tree->Enumerate(low,high);
for (int i=0, last = queryResults->Size(); i < last; i++) {
SimpleInterval * currentInterval =
(SimpleInterval *) (*queryResults)[i];
if ( (currentInterval->GetLowPoint() == low) &&
(currentInterval->GetHighPoint() == high) ) {
tree->DeleteNode(currentInterval->GetNode());
delete currentInterval;
return TCL_OK;
}
}
Tcl_SetResult(interp,"Interval not found in tree!\n",TCL_STATIC);
return TCL_ERROR;
}
}
}
// red black tree stuff follows:
int CreateRedBlackTreeInTcl(ClientData, Tcl_Interp * interp, int argc,
char ** argv)
{
if (argc != 2) {
Tcl_SetResult(interp,
"Wrong # arguments.\nUsage: rbTreeCreate nameOfTree\n",
TCL_STATIC);
return TCL_ERROR;
} else if (NULL != global_RedBlackTreesInTcl.Search(argv[1])) {
Tcl_SetResult(interp,
"A red black tree with that name already exists!\n",
TCL_STATIC);
return TCL_ERROR;
} else {
global_RedBlackTreesInTcl.Add(argv[1],new RedBlackTree);
return TCL_OK;
}
}
int DeleteRedBlackTreeInTcl(ClientData, Tcl_Interp * interp, int argc,
char ** argv)
{
RedBlackTree * treeToDelete;
if (argc != 2) {
Tcl_SetResult(interp,
"Wrong # arguments.\nUsage: rbTreeDelete nameOfTree\n",
TCL_STATIC);
return TCL_ERROR;
} else if (NULL == (treeToDelete =
global_RedBlackTreesInTcl.Search(argv[1]))) {
Tcl_SetResult(interp,
"A red black tree with that name doesn't exist!\n",
TCL_STATIC);
return TCL_ERROR;
} else {
delete treeToDelete;
global_RedBlackTreesInTcl.Delete(argv[1]);
return TCL_OK;
}
}
int AddToRedBlackTreeInTcl(ClientData, Tcl_Interp * interp, int argc,
char ** argv)
{
int key;
RedBlackTree * tree;
if (argc != 3) {
Tcl_SetResult(interp,
"Wrong # arguments.\nUsage: rbTreeAdd name key \n",
TCL_STATIC);
return TCL_ERROR;
} else if (NULL == (tree = global_RedBlackTreesInTcl.Search(argv[1]))) {
Tcl_SetResult(interp,
"A red black tree with that name doesn't exist!\n",
TCL_STATIC);
return TCL_ERROR;
} else {
if (TCL_OK != Tcl_GetInt(interp,argv[2],&key))
return TCL_ERROR;
else {
SimpleKey * newSimpleKey = new SimpleKey(key);
tree->Insert(newSimpleKey);
return TCL_OK;
}
}
}
int QueryRedBlackTreeInTcl(ClientData, Tcl_Interp * interp, int argc,
char ** argv)
{
int low;
int high;
RedBlackTree * tree;
if (argc != 4) {
Tcl_SetResult(interp,
"Wrong # arguments.\nUsage: rbTreeQuery name low high \n",
TCL_STATIC);
return TCL_ERROR;
} else if (NULL == (tree = global_RedBlackTreesInTcl.Search(argv[1]))) {
Tcl_SetResult(interp,
"A red black tree with that name doesn't exist!\n",
TCL_STATIC);
return TCL_ERROR;
} else {
if ( (TCL_OK != Tcl_GetInt(interp,argv[2],&low)) ||
(TCL_OK != Tcl_GetInt(interp,argv[3],&high)) )
return TCL_ERROR;
else {
TemplateStack<RedBlackTreeNode *> * queryResults =
tree->Enumerate(low,high);
Tcl_Obj * listResult = Tcl_NewListObj(0,NULL);
for (int i=0, last = queryResults->Size(); i < last; i++) {
char buffer[100];
RedBlackEntry * currentEntry = (*queryResults)[i]->GetEntry();
sprintf(buffer,"%i",currentEntry->GetKey());
Tcl_ListObjAppendElement(interp,listResult,
Tcl_NewStringObj(buffer,-1));
}
Tcl_SetObjResult(interp,listResult);
return TCL_OK;
}
}
}
int RedBlackTreeRemoveNode(ClientData, Tcl_Interp * interp, int argc,
char ** argv)
{
int low;
int high;
RedBlackTree * tree;
if (argc != 4) {
Tcl_SetResult(interp,
"Wrong # args.\nUsage: rbTreeRemoveNode name low high \n",
TCL_STATIC);
return TCL_ERROR;
} else if (NULL == (tree = global_RedBlackTreesInTcl.Search(argv[1]))) {
Tcl_SetResult(interp,
"A red black tree with that name doesn't exist!\n",
TCL_STATIC);
return TCL_ERROR;
} else {
if ( (TCL_OK != Tcl_GetInt(interp,argv[2],&low)) ||
(TCL_OK != Tcl_GetInt(interp,argv[3],&high)) )
return TCL_ERROR;
else {
TemplateStack<RedBlackTreeNode *> * queryResults =
tree->Enumerate(low,high);
for (int i=0, last = queryResults->Size(); i < last; i++) {
RedBlackEntry * curEntry = (*queryResults)[i]->GetEntry();
if ((low <= curEntry->GetKey()) && (curEntry->GetKey() <= high)) {
tree->DeleteNode( (*queryResults)[i]);
delete curEntry;
return TCL_OK;
}
}
Tcl_SetResult(interp,"no matching keys found in tree!\n",TCL_STATIC);
return TCL_ERROR;
}
}
}
#ifdef TCL_XT_TEST
#include <X11/Intrinsic.h>
#endif
#ifdef TCL_TEST
EXTERN int TclObjTest_Init _ANSI_ARGS_((Tcl_Interp *interp));
EXTERN int Tcltest_Init _ANSI_ARGS_((Tcl_Interp *interp));
#endif /* TCL_TEST */
#ifdef TCL_XT_TEST
EXTERN int Tclxttest_Init _ANSI_ARGS_((Tcl_Interp *interp));
#endif
/*
*----------------------------------------------------------------------
*
* main --
*
* This is the main program for the application.
*
* Results:
* None: Tcl_Main never returns here, so this procedure never
* returns either.
*
* Side effects:
* Whatever the application does.
*
*----------------------------------------------------------------------
*/
int main(int argc, char ** argv)
{
#ifdef TCL_XT_TEST
XtToolkitInitialize();
#endif
Tcl_Main(argc, argv, Tcl_AppInit);
return 0; /* Needed only to prevent compiler warning. */
}
/*
*----------------------------------------------------------------------
*
* Tcl_AppInit --
*
* This procedure performs application-specific initialization.
* Most applications, especially those that incorporate additional
* packages, will have their own version of this procedure.
*
* Results:
* Returns a standard Tcl completion code, and leaves an error
* message in interp->result if an error occurs.
*
* Side effects:
* Depends on the startup script.
*
*----------------------------------------------------------------------
*/
int Tcl_AppInit(Tcl_Interp * interp)
{
if (Tcl_Init(interp) == TCL_ERROR) {
return TCL_ERROR;
}
#ifdef TCL_TEST
#ifdef TCL_XT_TEST
if (Tclxttest_Init(interp) == TCL_ERROR) {
return TCL_ERROR;
}
#endif
if (Tcltest_Init(interp) == TCL_ERROR) {
return TCL_ERROR;
}
Tcl_StaticPackage(interp, "Tcltest", Tcltest_Init,
(Tcl_PackageInitProc *) NULL);
if (TclObjTest_Init(interp) == TCL_ERROR) {
return TCL_ERROR;
}
#endif /* TCL_TEST */
/*
* Call the init procedures for included packages. Each call should
* look like this:
*
* if (Mod_Init(interp) == TCL_ERROR) {
* return TCL_ERROR;
* }
*
* where "Mod" is the name of the module.
*/
/*
* Call Tcl_CreateCommand for application-specific commands, if
* they weren't already created by the init procedures called above.
*/
Tcl_CreateCommand(interp, "intTreeCreate",
CreateIntervalTreeInTcl, NULL, NULL);
Tcl_CreateCommand(interp, "intTreeDelete",
DeleteIntervalTreeInTcl, NULL, NULL);
Tcl_CreateCommand(interp, "intTreeAdd",
AddIntervalToTreeInTcl, NULL, NULL);
Tcl_CreateCommand(interp, "intTreeQuery",
QueryIntervalTreeInTcl, NULL, NULL);
Tcl_CreateCommand(interp, "intTreeRemoveNode",
IntervalTreeRemoveNode, NULL, NULL);
Tcl_CreateCommand(interp, "rbTreeCreate",
CreateRedBlackTreeInTcl, NULL, NULL);
Tcl_CreateCommand(interp, "rbTreeDelete",
DeleteRedBlackTreeInTcl, NULL, NULL);
Tcl_CreateCommand(interp, "rbTreeAdd",
AddToRedBlackTreeInTcl, NULL, NULL);
Tcl_CreateCommand(interp, "rbTreeQuery",
QueryRedBlackTreeInTcl, NULL, NULL);
Tcl_CreateCommand(interp, "rbTreeRemoveNode",
RedBlackTreeRemoveNode, NULL, NULL);
/*
* Specify a user-specific startup file to invoke if the application
* is run interactively. Typically the startup file is "~/.apprc"
* where "app" is the name of the application. If this line is deleted
* then no user-specific startup file will be run under any conditions.
*/
Tcl_SetVar(interp, "tcl_rcFileName", "~/.tclshrc", TCL_GLOBAL_ONLY);
return TCL_OK;
}