forked from sebastianbiallas/ht
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.h
1196 lines (1083 loc) · 32.4 KB
/
data.h
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* HT Editor
* data.h
*
* Copyright (C) 2002, 2003 Stefan Weyergraf ([email protected])
* Copyright (C) 2002, 2003 Sebastian Biallas ([email protected])
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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 General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef __DATA_H__
#define __DATA_H__
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "io/types.h"
#include <cstdlib>
typedef uint32 ObjectID;
typedef uint32 ID;
class ObjectStream;
struct BuildCtorArg {
};
template <typename T1, typename T2>
inline bool instanceOf(const T2 *o)
{
return (dynamic_cast<const T1*>(o) != NULL);
}
/*
* C style malloc support
*/
class HTMallocRes;
HTMallocRes ht_malloc(size_t);
class HTMallocRes
{
private:
friend HTMallocRes ht_malloc(size_t);
const size_t mSize;
HTMallocRes(size_t size)
: mSize(size)
{
}
HTMallocRes operator=(const HTMallocRes &); // not implemented
public:
template <typename T> operator T* () const
{
return static_cast<T*>(::malloc(mSize));
}
};
inline HTMallocRes ht_malloc(size_t size)
{
return HTMallocRes(size);
}
/**
* Macro for creating object build functions
*/
#define BUILDER(reg, obj, parent) Object *build_##obj(){BuildCtorArg a;return new obj(a);}
#define BUILDER2(reg, obj) Object *build_##obj(){BuildCtorArg a;return new obj(a);}
/**
* Registers builder function by object id.
*/
#define REGISTER(reg, obj) registerAtom(reg, (void*)build_##obj);
/**
* Unregisters builder function by object id.
*/
#define UNREGISTER(reg, obj) unregisterAtom(reg);
/* actually a str => bigendian-int */
/** used to define ObjectIDs */
#define MAGIC32(magic) (unsigned long)(((unsigned char)magic[0]<<24) | ((unsigned char)magic[1]<<16) | ((unsigned char)magic[2]<<8) | (unsigned char)magic[3])
/** No/invalid object */
#define OBJID_INVALID ((ObjectID)0)
/** A placeholder object id */
#define OBJID_TEMP ((ObjectID)-1)
#define OBJID_OBJECT MAGIC32("DAT\x00")
#define OBJID_ARRAY MAGIC32("DAT\x10")
#define OBJID_STACK MAGIC32("DAT\x11")
#define OBJID_BINARY_TREE MAGIC32("DAT\x20")
#define OBJID_AVL_TREE MAGIC32("DAT\x21")
#define OBJID_SET MAGIC32("DAT\x22")
#define OBJID_SLINKED_LIST MAGIC32("DAT\x30")
#define OBJID_QUEUE MAGIC32("DAT\x31")
#define OBJID_DLINKED_LIST MAGIC32("DAT\x32")
#define OBJID_KEYVALUE MAGIC32("DAT\x40")
#define OBJID_SINT MAGIC32("DAT\x41")
#define OBJID_SINT64 MAGIC32("DAT\x42")
#define OBJID_UINT MAGIC32("DAT\x43")
#define OBJID_UINT64 MAGIC32("DAT\x44")
#define OBJID_FLOAT MAGIC32("DAT\x45")
#define OBJID_MEMAREA MAGIC32("DAT\x48")
#define OBJID_STRING MAGIC32("DAT\x50")
#define OBJID_ISTRING MAGIC32("DAT\x51")
#define OBJID_AUTO_COMPARE MAGIC32("DAT\xc0")
/**
* This is THE base class.
*/
class Object {
public:
Object(BuildCtorArg&) {};
Object() {};
virtual ~Object() {};
void init() {};
virtual void done() {};
/* new */
/**
* Standard object duplicator.
* @returns copy of object
*/
virtual Object * clone() const;
/**
* Standard Object comparator.
* @param obj object to compare to
* @returns 0 for equality, negative number if |this<obj| and positive number if |this>obj|
*/
virtual int compareTo(const Object *obj) const;
/**
* Stringify object.
* Stringify object in string-buffer <i>s</i>. Never writes more than
* <i>maxlen</i> characters to <i>s</i>. If <i>maxlen</i> is > 0, a
* trailing zero-character is written.
*
* @param buf pointer to buffer that receives object stringification
* @param buflen size of buffer that receives object stringification
* @returns number of characters written to <i>s</i>, not including the trailing zero
*/
virtual int toString(char *buf, int buflen) const;
/**
* Standard Object idle function.
* Overwrite and register with htidle.cc::register_idle()
* (FIXME)
*
* @returns true if working, false if really idle
*/
virtual bool idle();
/**
* Load object from object stream.
*
* @param s object stream to load this object from
*/
virtual void load(ObjectStream &s);
/**
* @returns unique object id.
*/
virtual ObjectID getObjectID() const;
/**
* stores object.
*
* @param s object stream to store this object into
*/
virtual void store(ObjectStream &s) const;
};
typedef int (*Comparator)(const Object *a, const Object *b);
int autoCompare(const Object *a, const Object *b);
typedef void* ObjHandle;
const ObjHandle invObjHandle = NULL;
const uint invIdx = ((uint)-1);
/**
* An Enumerator.
*/
class Enumerator: public Object {
public:
Enumerator(BuildCtorArg&a): Object(a) {};
Enumerator() {};
/* extends Object */
virtual Enumerator * clone() const = 0;
virtual int toString(char *buf, int buflen) const;
/* new */
/**
* Count elements.
*
* @returns number of objects contained in this structure
* @throws NotImplementedException if counting of elements is not supported
*/
virtual uint count() const = 0;
/**
* Compare objects.
* Compare objects <i>a</i> and <i>b</i> and determine their (logical)
* linear order.
*
* @param a object a
* @param b object b
* @returns 0 if <i>a</i> equals <i>b</i>,
* a value >0 if <i>a</i> is greater than <i>b</i> and
* a value <0 if <i>a</i> is less than <i>b</i>
*/
virtual int compareObjects(const Object *a, const Object *b) const = 0;
/**
* Test if contained.
* Test if an object like <i>obj</i> is contained in this structure
*
* @param obj signature of object to find
* @returns true if an object like <i>obj</i> is contained, false otherwise
*/
inline bool contains(const Object *obj) const
{
return find(obj) != invObjHandle;
}
/**
* Test if empty.
* @returns true if empty
*/
inline bool isEmpty() const
{
return count() == 0;
}
/**
* Find equal object.
* Find first object equaling <i>obj</i> in this structure
* and if found return it's object handle.
*
* @param obj signature of object to find
* @returns object handle of found object or <i>invObjHandle</i> if not found
*/
virtual ObjHandle find(const Object *obj) const;
/**
* Find greater-or-equal object.
* Find lowest object being greater or equal compared to <i>obj</i> in this structure
* and if found return it's object handle. (lowest, greater and equal are
* defined via the compareTo method)
*
* @param obj signature of object to find
* @returns object handle of found object or <i>invObjHandle</i> if not found
*/
virtual ObjHandle findGE(const Object *obj) const;
/**
* Find greater object.
* Find lowest object being greater compared to <i>obj</i> in this structure
* and if found return it's object handle. (lowest and greater are
* defined via the compareTo method)
*
* @param obj signature of object to find
* @returns object handle of found object or <i>invObjHandle</i> if not found
*/
virtual ObjHandle findG(const Object *obj) const;
/**
* Find lower-or-equal object.
* Find greatest object being lower or equal compared to <i>obj</i> in this structure
* and if found return it's object handle. (greatest, lower and equal are
* defined via the compareTo method)
*
* @param obj signature of object to find
* @returns object handle of found object or <i>invObjHandle</i> if not found
*/
virtual ObjHandle findLE(const Object *obj) const;
/**
* Find lower object.
* Find greatest object being lower compared to <i>obj</i> in this structure
* and if found return it's object handle. (greatest and lower are
* defined via the compareTo method)
*
* @param obj signature of object to find
* @returns object handle of found object or <i>invObjHandle</i> if not found
*/
virtual ObjHandle findL(const Object *obj) const;
/**
* Find object's handle by index.
*
* @param i index of object to find
* @returns object handle of found object or <i>invObjHandle</i> if not found
*/
virtual ObjHandle findByIdx(int i) const = 0;
/**
* Find (logically) last element's object handle.
*
* @returns object handle of the last element or <i>invObjHandle</i>
* if the structure is empty
*/
virtual ObjHandle findLast() const = 0;
/**
* Find (logically) previous element's object handle.
* Find logically previous element (predecessor) of the object identified
* by <i>h</i>. Predecessor of "the invalid object" is the last element
* in this structure by definition. (ie. <i>findPrev(invObjHandle) :=
* findLast()</i>).
*
* @param h object handle to find a predecessor to
* @returns object handle of predecessor or <i>invObjHandle</i> if <i>h</i>
* identifies the first element.
*/
virtual ObjHandle findPrev(ObjHandle h) const = 0;
/**
* Find (logically) first element's object handle.
*
* @returns object handle of the first element or <i>invObjHandle</i>
* if this structure is empty
*/
virtual ObjHandle findFirst() const = 0;
/**
* Find (logically) next element's object handle.
* Find logically next element (successor) of the object, identified
* by <i>h</i>. Successor of "the invalid object" is the first element
* in this structure by definition. (ie. <i>findNext(invObjHandle) :=
* findFirst()</i>).
*
* @param h object handle to find a successor to
* @returns object handle of successor or <i>invObjHandle</i> if <i>h</i>
* identifies the last element.
*/
virtual ObjHandle findNext(ObjHandle h) const = 0;
/**
* Get object pointer from object handle.
*
* @param h object handle
* @returns object pointer if <i>h</i> is valid, <i>NULL</i> otherwise
*/
virtual Object * get(ObjHandle h) const = 0;
/**
* Get object's index from its handle.
*
* @param h object handle of object
* @returns index of object if <i>h</i> is valid, <i>InvIdx</i> otherwise.
*/
virtual uint getObjIdx(ObjHandle h) const = 0;
/**
* Get element by index.
* Get the element with index <i>idx</i> (if possible).
*
* @param idx index of element to get
* @returns pointer to the requested element or <i>NULL</i> if <i>idx</i>
* is invalid.
*/
Object * operator [] (int idx) const
{
return get(findByIdx(idx));
}
};
#define foreach(XTYPE, X, E, code...)\
for (ObjHandle temp0815 = (E).findFirst(); temp0815 != invObjHandle;) {\
XTYPE *X = (XTYPE*)(E).get(temp0815); \
temp0815 = (E).findNext(temp0815); \
{code;} \
}
#define foreachbwd(XTYPE, X, E, code...)\
for (ObjHandle temp0815 = (E).findLast(); temp0815 != invObjHandle;) {\
XTYPE *X = (XTYPE*)(E).get(temp0815); \
temp0815 = (E).findPrev(temp0815); \
{code;} \
}
#define firstThat(XTYPE, X, E, condition) \
{ \
XTYPE *Y = NULL; \
foreach(XTYPE, X, E, \
if (condition) { \
Y = X; \
break; \
} \
) \
X = Y; \
}
#define lastThat(XTYPE, X, E, condition) \
{ \
XTYPE *Y = NULL; \
foreachbwd(XTYPE, X, E, \
if (condition) { \
Y = X; \
break; \
} \
) \
X = Y; \
}
/**
* A Container.
*/
class Container: public Enumerator {
protected:
ObjectID hom_objid;
virtual void notifyInsertOrSet(const Object *o);
public:
Container(BuildCtorArg&a): Enumerator(a) {};
Container();
/* extends Enumerator */
virtual Container * clone() const = 0;
/* new */
/**
* Delete all objects. (ie. remove and free all objects)
*/
virtual void delAll() = 0;
/**
* Delete object.
* Delete (ie. remove and free) first object like <i>sig</i> in
* this structure (if possible).
*
* @param sig signature of object to delete (may be inserted in the structure)
* @returns true if an object has been deleted, false otherwise
*/
virtual bool delObj(Object *sig);
/**
* Delete object.
* Delete (ie. remove and free) object identified by <i>h</i>.
*
* @param h object handle of the object to delete
* @returns true if the object has been deleted, false otherwise
*/
virtual bool del(ObjHandle h) = 0;
/**
* Find or insert object.
* Find first object like <i>obj</i> and if that fails, insert <i>obj</i>.
* Ie. after call of this function it is guaranteed that <i>contains(obj)</i>.
*
* @param obj object to find similar one to or object to insert
* @param inserted indicates if <i>obj</i> has been inserted
* @returns object handle of existing or inserted object (never <i>invObjHandle</i>)
*/
virtual ObjHandle findOrInsert(Object *obj, bool &inserted);
/**
* Insert object.
* Insert <i>obj</i>
*
* @param obj object to insert
* @returns object handle of inserted object (never <i>invObjHandle</i>)
*/
virtual ObjHandle insert(Object *obj) = 0;
/**
* Remove object.
* Remove first object like <i>sig</i> from this structure.
* Returned object must be freed explicitly.
*
* @param sig signature of object to remove
* @returns removed object
*/
virtual Object * removeObj(const Object *sig);
/**
* Remove object.
* Remove object identified by <i>h</i>.
* Returned object must be freed explicitly.
*
* @param h object handle of object to remove
* @returns removed object
*/
virtual Object * remove(ObjHandle h) = 0;
/**
* Insert object. (operator-form)
*/
inline ObjHandle operator += (Object *obj) { return insert(obj); }
/**
* Delete object. (operator-form)
*/
inline bool operator -= (ObjHandle h) { return del(h); }
/**
* Delete object. (operator-form)
*/
inline bool operator -= (Object *sig) { return (*this -= find(sig)); }
/**
* Delete object by index.
*
* @param idx index of object to delete
* @returns true if the object has been deleted, false otherwise
*/
inline bool operator -= (int idx) { return (*this -= findByIdx(idx)); }
};
/**
* An abstract list
*/
class List: public Container {
public:
List(BuildCtorArg&a): Container(a) {};
List() {};
/* extends Enumerator */
virtual List * clone() const = 0;
/* new */
/**
* Insert object at position.
* Insert object <i>obj</i> at the position specified by <i>h</i>.
* if <i>h</i> does not specify a valid object handle (eg. invObjHandle),
* this function works like <i>insert(obj)</i>.
*
* @param h position to insert object at
* @param obj pointer to object to insert
* @returns true on success, false otherwise
*/
virtual void insertAt(ObjHandle h, Object *obj) = 0;
/**
* Move element.
* Move element from position <i>from</i> to position <i>to</i>.
*
* @param from position of element to move
* @param to position to move element to
* @returns true on success, false otherwise
*/
virtual bool moveTo(ObjHandle from, ObjHandle to) = 0;
/**
* Prepend object.
* Prepend object <i>obj</i>.
*
* @param obj pointer to object to prepend
* @returns object handle of inserted object (never <i>invObjHandle</i>)
*/
inline ObjHandle prepend(Object *obj)
{
insertAt(findFirst(), obj);
return findFirst();
}
/**
* Set element.
* Replace element at position <i>h</i> with object <i>obj</i>
* and delete replaced object.
*
* @param h object handle of element to replace
* @param obj object to replace element
* @returns true on success, false otherwise
*/
virtual bool set(ObjHandle h, Object *obj) = 0;
/**
* Force: Set element by index.
* Set element at index <i>i</i> to object <i>obj</i>
* and delete object previously located at this index if the index is valid.
* If the index <i>i</i> does not specify a valid list-index,
* the list is extended, so that <i>obj</i> will be the last element
* and the newly created entries in the list will be <i>NULL</i>s.
*
* @param i index at which to set
* @param obj object to set
*/
virtual void forceSetByIdx(int idx, Object *obj) = 0;
/**
* Swap two element.
* Swap element at position <i>h</i> with element at position <i>i</i>.
*
* @param h handle of one object
* @param i handle of the other object
* @returns true on success, false otherwise
*/
virtual bool swap(ObjHandle h, ObjHandle i) = 0;
};
#define ARRAY_CONSTR_ALLOC_DEFAULT 4
/**
* An array
*/
class Array: public List {
private:
bool own_objects;
uint ecount;
uint acount;
Object **elems;
virtual int calcNewBufferSize(int curbufsize, int min_newbufsize) const;
virtual void checkShrink();
virtual void freeObj(Object *obj);
void prepareWriteAccess(int i);
void realloc(int n);
inline bool validHandle(ObjHandle h) const
{
return (handleToNative(h) < ecount);
}
inline uint handleToNative(ObjHandle h) const
{
return (Object**)h-elems;
}
inline ObjHandle nativeToHandle(int i) const
{
return elems+i;
}
public:
Array(BuildCtorArg &a): List(a) {};
Array(bool own_objects, int prealloc = ARRAY_CONSTR_ALLOC_DEFAULT);
virtual ~Array();
/* extends Object */
virtual Array * clone() const;
virtual void load(ObjectStream &s);
virtual ObjectID getObjectID() const;
virtual void store(ObjectStream &s) const;
/* extends Enumerator */
virtual uint count() const
{
return ecount;
}
virtual int compareObjects(const Object *a, const Object *b) const;
virtual ObjHandle findByIdx(int i) const;
virtual ObjHandle findFirst() const;
virtual ObjHandle findLast() const;
virtual ObjHandle findNext(ObjHandle h) const;
virtual ObjHandle findPrev(ObjHandle h) const;
virtual Object * get(ObjHandle h) const;
virtual uint getObjIdx(ObjHandle h) const;
/* extends Container */
virtual void delAll();
virtual bool del(ObjHandle h);
virtual ObjHandle insert(Object *obj);
virtual Object * remove(ObjHandle h);
/* extends List */
virtual void forceSetByIdx(int idx, Object *obj);
virtual void insertAt(ObjHandle h, Object *obj);
virtual bool moveTo(ObjHandle from, ObjHandle to);
virtual bool set(ObjHandle h, Object *obj);
virtual bool swap(ObjHandle h, ObjHandle i);
/* new */
/**
* Delete range of objects. (ie. remove and free all objects)
* @return number of objects deleted
*/
virtual int delRange(int start, int end);
inline void insertBefore(int idx, Object *obj)
{
insertAt(findByIdx(idx), obj);
}
};
/**
* A stack
*/
class Stack: public Array {
public:
Stack(BuildCtorArg &a): Array(a) {};
Stack(bool own_objects);
/* new */
virtual Object * pop();
virtual void push(Object *obj);
virtual ObjectID getObjectID() const;
};
/**
* SLinkedList's node structure
*/
struct SLinkedListNode {
Object *obj;
SLinkedListNode *next;
};
/**
* A (simply) linked list
*/
class SLinkedList: public List {
private:
bool own_objects;
uint ecount;
SLinkedListNode *first, *last;
virtual SLinkedListNode *allocNode() const;
virtual void deleteNode(SLinkedListNode *node) const;
virtual void freeObj(Object *obj) const;
inline bool validHandle(ObjHandle h) const;
inline SLinkedListNode *handleToNative(ObjHandle h) const;
inline ObjHandle nativeToHandle(SLinkedListNode *n) const;
public:
SLinkedList(BuildCtorArg&a): List(a) {};
SLinkedList(bool own_objects);
virtual ~SLinkedList();
/* extends Object */
virtual SLinkedList * clone() const;
virtual void load(ObjectStream &s);
virtual ObjectID getObjectID() const;
virtual void store(ObjectStream &s) const;
/* extends Enumerator */
virtual uint count() const;
virtual int compareObjects(const Object *a, const Object *b) const;
virtual ObjHandle findByIdx(int i) const;
virtual ObjHandle findFirst() const;
virtual ObjHandle findLast() const;
virtual ObjHandle findNext(ObjHandle h) const;
virtual ObjHandle findPrev(ObjHandle h) const;
virtual Object * get(ObjHandle h) const;
virtual uint getObjIdx(ObjHandle h) const;
/* extends Container */
virtual void delAll();
virtual bool del(ObjHandle h);
virtual ObjHandle insert(Object *obj);
virtual Object * remove(ObjHandle h);
/* extends List */
virtual void forceSetByIdx(int idx, Object *obj);
virtual void insertAt(ObjHandle h, Object *obj);
virtual bool moveTo(ObjHandle from, ObjHandle to);
virtual bool set(ObjHandle h, Object *obj);
virtual bool swap(ObjHandle h, ObjHandle i);
};
/**
* A queue
*/
class Queue: public SLinkedList {
public:
Queue(BuildCtorArg&a): SLinkedList(a) {};
Queue(bool own_objects);
/* new */
/**
* De-queue element.
* Remove and return next element of the queue.
*
* @returns next element of the queue
*/
inline Object * deQueue()
{
return remove(findFirst());
}
/**
* En-queue element.
* Append element <i>obj</i> to the queue.
*
* @param obj pointer to object to en-queue
*/
inline void enQueue(Object *obj)
{
insert(obj);
}
virtual ObjectID getObjectID() const;
};
/**
* DLinkedList's node structure
*/
struct DLinkedListNode {
Object *obj;
DLinkedListNode *prev;
DLinkedListNode *next;
};
/**
* A (doubly) linked list
*/
class DLinkedList: public List {
private:
bool own_objects;
uint ecount;
DLinkedListNode *first, *last;
virtual DLinkedListNode *allocNode() const;
virtual void deleteNode(DLinkedListNode *node) const;
virtual void freeObj(Object *obj) const;
inline bool validHandle(ObjHandle h) const;
inline DLinkedListNode *handleToNative(ObjHandle h) const;
inline ObjHandle nativeToHandle(DLinkedListNode *n) const;
public:
DLinkedList(BuildCtorArg&a): List(a) {};
DLinkedList(bool own_objects);
virtual ~DLinkedList();
/* extends Object */
virtual DLinkedList * clone() const;
virtual void load(ObjectStream &s);
virtual ObjectID getObjectID() const;
virtual void store(ObjectStream &s) const;
/* extends Enumerator */
virtual uint count() const;
virtual int compareObjects(const Object *a, const Object *b) const;
virtual ObjHandle findByIdx(int i) const;
virtual ObjHandle findFirst() const;
virtual ObjHandle findLast() const;
virtual ObjHandle findNext(ObjHandle h) const;
virtual ObjHandle findPrev(ObjHandle h) const;
virtual Object * get(ObjHandle h) const;
virtual uint getObjIdx(ObjHandle h) const;
/* extends Container */
virtual void delAll();
virtual bool del(ObjHandle h);
virtual ObjHandle insert(Object *obj);
virtual Object * remove(ObjHandle h);
/* extends List */
virtual void forceSetByIdx(int idx, Object *obj);
virtual void insertAt(ObjHandle h, Object *obj);
virtual bool moveTo(ObjHandle from, ObjHandle to);
virtual bool set(ObjHandle h, Object *obj);
virtual bool swap(ObjHandle h, ObjHandle i);
};
/**
* BinaryTree's node structure
*/
struct BinTreeNode {
Object *key;
BinTreeNode *left, *right;
int unbalance;
};
/**
* A simple binary tree
*/
class BinaryTree: public Container {
protected:
bool own_objects;
uint ecount;
BinTreeNode *root;
Comparator compare;
BinTreeNode * allocNode() const;
void cloneR(BinTreeNode *node);
virtual void deleteNode(BinTreeNode *node) const;
BinTreeNode * findNode(BinTreeNode *node, const Object *obj) const;
BinTreeNode * findNodeG(BinTreeNode *node, const Object *obj) const;
BinTreeNode * findNodeGE(BinTreeNode *node, const Object *obj) const;
BinTreeNode * findNodeL(BinTreeNode *node, const Object *obj) const;
BinTreeNode * findNodeLE(BinTreeNode *node, const Object *obj) const;
BinTreeNode ** findNodePtr(BinTreeNode **nodeptr, const Object *obj) const;
void freeAll(BinTreeNode *n);
void freeObj(Object *obj) const;
BinTreeNode * getLeftmost(BinTreeNode *node) const;
BinTreeNode * getRightmost(BinTreeNode *node) const;
BinTreeNode ** getLeftmostPtr(BinTreeNode **nodeptr) const;
BinTreeNode ** getRightmostPtr(BinTreeNode **nodeptr) const;
ObjHandle findByIdxR(BinTreeNode *n, int &i) const;
ObjHandle insertR(BinTreeNode *&node, Object *obj);
void loadR(ObjectStream &s, BinTreeNode **n, int l, int r);
void storeR(ObjectStream &s, BinTreeNode *n) const;
virtual void setNodeIdentity(BinTreeNode *node, BinTreeNode *newident);
inline bool validHandle(ObjHandle h) const { return (h != invObjHandle); }
inline BinTreeNode * handleToNative(ObjHandle h) const { return (BinTreeNode*)h; }
inline ObjHandle nativeToHandle(BinTreeNode *n) const { return (ObjHandle*)n; }
public:
BinaryTree(BuildCtorArg&a): Container(a) {};
BinaryTree(bool own_objects, Comparator comparator = autoCompare);
virtual ~BinaryTree();
/* extends Object */
virtual BinaryTree * clone() const;
virtual void load(ObjectStream &s);
virtual ObjectID getObjectID() const;
virtual void store(ObjectStream &s) const;
/* extends Enumerator */
virtual void delAll();
virtual uint count() const;
virtual int compareObjects(const Object *a, const Object *b) const;
virtual ObjHandle find(const Object *obj) const;
virtual ObjHandle findG(const Object *obj) const;
virtual ObjHandle findGE(const Object *obj) const;
virtual ObjHandle findL(const Object *obj) const;
virtual ObjHandle findLE(const Object *obj) const;
virtual ObjHandle findByIdx(int i) const;
virtual ObjHandle findFirst() const;
virtual ObjHandle findLast() const;
virtual ObjHandle findNext(ObjHandle h) const;
virtual ObjHandle findPrev(ObjHandle h) const;
virtual Object * get(ObjHandle h) const;
virtual uint getObjIdx(ObjHandle h) const;
/* extends Container */
virtual bool del(ObjHandle h);
virtual ObjHandle insert(Object *obj);
virtual Object * remove(ObjHandle h);
};
/**
* A height-balanced binary tree (AVL)
*/
class AVLTree: public BinaryTree {
private:
void cloneR(BinTreeNode *node);
BinTreeNode * removeR(Object *key, BinTreeNode *&root, int &change, int cmp);
int loadR(ObjectStream &s, BinTreeNode *&n, int l, int r);
public:
AVLTree(BuildCtorArg&a): BinaryTree(a) {};
AVLTree(bool own_objects, Comparator comparator = autoCompare);
void debugOut();
bool expensiveCheck() const;
/* extends Object */
virtual AVLTree * clone() const;
virtual void load(ObjectStream &s);
virtual ObjectID getObjectID() const;
/* extends Container */
virtual ObjHandle insert(Object *obj);
virtual Object * remove(ObjHandle h);
};
/**
* MRU Cache's node structure
*/
struct MRUCacheNode: public BinTreeNode {
MRUCacheNode *moreRU, *lessRU;
};
/**
* A most-recently-used (MRU) cache
*/
class MRUCache: public AVLTree {
private:
MRUCacheNode * mostRU;
MRUCacheNode * leastRU;
virtual MRUCacheNode * allocNode() const;
void checkList() const;
virtual void deleteNode(BinTreeNode *node) const;
inline MRUCacheNode * handleToNative(ObjHandle h) const { return (MRUCacheNode*)h; }
inline ObjHandle nativeToHandle(MRUCacheNode *n) const { return (ObjHandle)n; }
virtual void setNodeIdentity(BinTreeNode *node, BinTreeNode *newident);
public:
MRUCache(bool own_objects, Comparator comparator = autoCompare);
/* extends Object */
virtual MRUCache * clone() const;
virtual void store(ObjectStream &s) const;
/* extends Container */
virtual ObjHandle insert(Object *obj);
virtual Object * remove(ObjHandle h);
/* extends AVLTree */
virtual void delAll();
/* new */
void propagate(ObjHandle h);
ObjHandle getLRU();
};
/**
* A finite set
*/
class Set: public AVLTree {
public:
Set(BuildCtorArg&a):AVLTree(a) {};
Set(bool own_objects);
/* new */
void intersectWith(Set *b);
void unionWith(Set *b);
inline bool operator &(Object *obj) const
{
return contains(obj);
}
virtual ObjectID getObjectID() const;
};
/*
* IntSet
*/
class IntSet: public Object {
protected:
uint mMaxSetSize; // in bits
uint mSetSize; // in bits
byte *mSet;
/* new */
inline uint idx2ByteOfs(uint i) const;
inline uint idx2BitMask(uint i) const;
void makeAccessible(uint i);
inline bool isAccessible(uint i) const;
public:
IntSet(uint aMaxSetSize);
virtual ~IntSet();
/* extends Object */
virtual IntSet *clone() const;
virtual int compareTo(const Object *obj) const;
virtual int toString(char *buf, int buflen) const;
/* new */
void assign(const IntSet &from);
bool contains(uint i) const;
void del(uint i);
void delAll();
bool findFirst(uint &i, bool set) const;
bool findNext(uint &i, bool set) const;
bool findPrev(uint &i, bool set) const;
void insert(uint i);
};
/**