Now measuring max mem usage.
[SXSI/TextCollection.git] / pos.h
1
2 /***************************************************************************
3  *   Copyright (C) 2006 by Wolfgang Gerlach   *
4  *   No object matches key 'wgerlach'.   *
5  *                                                                         *
6  *   This program is free software; you can redistribute it and/or modify  *
7  *   it under the terms of the GNU General Public License as published by  *
8  *   the Free Software Foundation; either version 2 of the License, or     *
9  *   (at your option) any later version.                                   *
10  *                                                                         *
11  *   This program is distributed in the hope that it will be useful,       *
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14  *   GNU General Public License for more details.                          *
15  *                                                                         *
16  *   You should have received a copy of the GNU General Public License     *
17  *   along with this program; if not, write to the                         *
18  *   Free Software Foundation, Inc.,                                       *
19  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20  ***************************************************************************/
21
22
23  //TODO check star color of the nodes pos and handle!
24
25 #ifndef Pos
26 #define Pos Pos
27
28 #ifndef uchar
29 #define uchar unsigned char
30 #endif
31 #ifndef ulong
32 #define ulong unsigned long
33 #endif
34   
35 #include "rbtree.h"
36 #include "handle.h"
37
38 class Handle;
39 class HandleNode;
40
41 void callPosUpdateCounters(RBNode *n, RBTree *T);
42 void callPosUpdateCountersOnPathToRoot(RBNode *n, RBTree *T);
43  
44 class PosNode : public RBNode {
45         public:
46         
47         HandleNode *handleNode;
48         ulong subtreeSize;
49         ulong textSize; // size including endmarker!
50         //ulong sumTextSize; // sum of textlength of all texts located in this subtree;
51         
52         PosNode() // NIL
53                 : RBNode(this),subtreeSize(0),textSize(0) { 
54         }
55
56         PosNode(PosNode *n, ulong textSize)
57                 : RBNode(n),subtreeSize(1),textSize(textSize) { 
58         }
59         
60                 
61         PosNode* getParent(){
62                 return ((PosNode*) ((RBNode*) this)->parent);
63         }
64
65         PosNode* getLeft(){
66                 return ((PosNode*) ((RBNode*) this)->left);
67         }
68
69         PosNode* getRight(){
70                 return ((PosNode*) ((RBNode*) this)->right);
71         }
72
73         void setParent(PosNode* n){
74                 ((RBNode*) this)->parent=(RBNode*)n;
75         }
76
77         void setLeft(PosNode* n){
78                 ((RBNode*) this)->left=(RBNode*)n;
79         }
80
81         void setRight(PosNode* n){
82                 ((RBNode*) this)->right=(RBNode*)n;
83         }       
84 };
85
86 class Pos: public RBTree
87 {
88         public:
89         
90         Handle* handle;
91         
92         ulong textNumber;
93         ulong matchPosition;
94
95         ulong sampleInterval;
96
97         Pos(ulong sampleInterval){
98                 setNil(new PosNode());
99                 setRoot(getNil());
100                 this->sampleInterval=sampleInterval;
101         }
102
103         void setRoot(PosNode* n){
104                 ((RBTree*) this)->root=(RBNode*)n;
105         }
106         
107         PosNode* getRoot(){
108                 return ((PosNode*) ((RBTree*) this)->root);
109         }
110
111         void setNil(PosNode* n){
112                 ((RBTree*) this)->nil=(RBNode*)n;
113         }
114
115         PosNode* getNil(){
116                 return ((PosNode*) ((RBTree*) this)->nil);
117         }
118
119         ulong getSize(){
120                 return (getRoot()!=getNil())?getRoot()->subtreeSize:0;
121         }
122
123
124         
125         ulong getPos(PosNode *n);
126
127
128         PosNode* getPosNode(ulong text);
129         ulong getTextSize(ulong pos);
130         void deleteText(ulong pos);
131         void deletePosNode(PosNode *n);
132         ulong appendText(ulong textSize);
133
134         void updateCountersOnPathToRoot(PosNode *n);
135         void updateCounters(PosNode *n);
136         
137 };
138
139 #endif