-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfastree.h
More file actions
205 lines (158 loc) · 5.23 KB
/
fastree.h
File metadata and controls
205 lines (158 loc) · 5.23 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
/*
IL : Intlog Language
Object Oriented Prolog Project
Copyright (C) 1992-2021 - Ing. Capelli Carlo
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, 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 General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef FASTREE_H_
#define FASTREE_H_
//////////////////////////////////
// fast tree construction,
// to be used when parsing terms
// to store source positions
//
#include "stack.h"
#include "iafx.h"
#include <cassert>
#include <iostream>
using namespace std;
// forward decl
class FastTree;
struct NodeLevel;
// data types
typedef unsigned long NodeData;
typedef unsigned NodeIndex;
#define INVALID_NODE NodeIndex(-1)
// vector elements
class NodeDefinition {
public:
// store your data (term...)
NodeData m_data;
private:
// tree construction info
NodeIndex m_nFirstSon, m_nBrother;
friend class FastTree;
};
class IAFX_API FastTree : vect<NodeDefinition> {
public:
FastTree() {
m_nRoot = INVALID_NODE;
m_nTop = 0;
}
FastTree(const FastTree& toCopy) {
Assign(toCopy);
}
FastTree& operator=(const FastTree& toCopy) {
Assign(toCopy);
return *this;
}
virtual ~FastTree() {}
// formatted display indented
virtual int DisplayNode(ostream &s, int nline, const NodeLevel & nd) const;
void Display(ostream &s, unsigned off = 1) const;
void DisplayConnected(ostream &s, unsigned char connLines[5], unsigned off = 1) const;
// type connection character options
enum selConn { V1H1, V1H2, V2H1, V2H2 };
enum selChar { CH_V, CH_H, CH_BR, CH_VR, CH_SP };
static void CharConnections(unsigned char connLines[5], selConn select = V1H1);
// release all elements indexs
void RemoveAll() {
m_nRoot = INVALID_NODE;
m_nTop = 0;
}
// access a node (debug validate index)
NodeDefinition &operator[](NodeIndex allocated) {
return *GetAt(allocated);
}
NodeDefinition *GetAt(NodeIndex allocated) const {
assert(/*allocated >= 0 && */allocated < m_nTop);
return getptr(allocated);
}
// tree construction
void SetSon(NodeIndex father, NodeIndex firstSon) {
assert(GetAt(father)->m_nFirstSon == INVALID_NODE);
assert(GetAt(firstSon)->m_nBrother == INVALID_NODE);
GetAt(father)->m_nFirstSon = firstSon;
}
void CatSon(NodeIndex father, NodeIndex firstSon) {
assert(GetAt(father)->m_nFirstSon == INVALID_NODE);
assert(GetAt(firstSon) != nullptr);
GetAt(father)->m_nFirstSon = firstSon;
}
void AddSon(NodeIndex father, NodeIndex newSon) {
NodeIndex lastBrother = LastSon(father);
if (lastBrother != INVALID_NODE)
CatBrother(lastBrother, newSon);
else
CatSon(father, newSon);
}
void SetBrother(NodeIndex firstSon, NodeIndex secSon) {
assert(GetAt(firstSon)->m_nBrother == INVALID_NODE);
assert(GetAt(secSon)->m_nBrother == INVALID_NODE);
GetAt(firstSon)->m_nBrother = secSon;
}
void CatBrother(NodeIndex firstSon, NodeIndex secSon) {
assert(GetAt(firstSon)->m_nBrother == INVALID_NODE);
assert(GetAt(secSon) != nullptr);
GetAt(firstSon)->m_nBrother = secSon;
}
void SetRoot(NodeIndex root) {
assert(m_nRoot == INVALID_NODE);
assert(GetAt(root) != nullptr);
m_nRoot = root;
}
NodeIndex UnlinkBrother(NodeIndex son) {
NodeIndex brother = GetAt(son)->m_nBrother;
GetAt(son)->m_nBrother = INVALID_NODE;
return brother;
}
// reserve a slot to be given to GetAt, SetSon, CatSon, ...
NodeIndex AllocNode();
// tree visit
NodeIndex Root() const {
return m_nRoot;
}
NodeIndex FirstSon(NodeIndex father) const {
return GetAt(father)->m_nFirstSon;
}
NodeIndex LastSon(NodeIndex father) const {
NodeIndex s = FirstSon(father), l = INVALID_NODE;
while (s != INVALID_NODE) {
l = s;
s = GetAt(s)->m_nBrother;
}
return l;
}
NodeIndex NextSon(NodeIndex brother) const {
return GetAt(brother)->m_nBrother;
}
unsigned NumSons(NodeIndex father) const;
NodeIndex NthSon(NodeIndex father, unsigned index) const;
void PushSons(stack<NodeLevel> &v, const NodeLevel &q) const;
// compute dimension from required node
unsigned GetSize(NodeIndex node) const;
unsigned TotalSize() const {
return m_nTop;
}
protected:
void Assign(const FastTree& toCopy);
// save free allocation
NodeIndex m_nTop;
NodeIndex m_nRoot;
};
// used to fast visit term
struct NodeLevel {
NodeIndex n;
unsigned l;
};
#endif