BAM
Abstract Machine for Bottom-Up Evaluation with the Push Method
strtab.h
Go to the documentation of this file.
1 // ============================================================================
2 // Project: Deductive Database
3 // Filename: strtab.h
4 // Purpose: String Table
5 // Last Change: 04.08.2017
6 // Language: C++
7 // EMail: brass@informatik.uni-halle.de
8 // WWW: http://www.informatik.uni-halle.de/~brass/
9 // Address: Feldschloesschen 15, D-06120 Halle (Saale), GERMANY
10 // Copyright: (c) 2015-2017 by Stefan Brass
11 // License: See file "LICENSE" for copying conditions.
12 // Note: There is no warranty at all - this code may contain bugs.
13 // ============================================================================
14 
15 
27 //=============================================================================
28 // Include File Frame:
29 //=============================================================================
30 
31 #ifndef STRTAB_INCLUDED
32 #define STRTAB_INCLUDED
33 
34 //=============================================================================
35 // Used Types and Macros:
36 //=============================================================================
37 
38 #ifndef VER_INCLUDED
39 #include "../base/ver.h"
40 #endif
41 
42 #ifndef STR_INCLUDED
43 #include "../base/str.h"
44 #endif
45 
46 #ifndef CHECK_INCLUDED
47 #include "../base/check.h"
48 #endif
49 
50 #ifndef MPOOL_INCLUDED
51 #include "../mem/mpool.h"
52 #endif
53 
54 #ifndef FLEXARR_INCLUDED
55 #include "../bds/flexarr.h"
56 #endif
57 
58 #ifndef STRMEM_INCLUDED
59 #include "../dom/strmem.h"
60 #endif
61 
62 
63 //=============================================================================
64 // Private Constants:
65 //=============================================================================
66 
67 //-----------------------------------------------------------------------------
68 // STRTAB_MAGIC: Magic number (identifies objects of this class).
69 //-----------------------------------------------------------------------------
70 
71 static const long STRTAB_MAGIC = 0x5354420AL; // 'STB\n'
72 
73 
74 //=============================================================================
75 // Private Types:
76 //=============================================================================
77 
78 //-----------------------------------------------------------------------------
79 // strtab_node_c: Superclass of Nodes.
80 //-----------------------------------------------------------------------------
81 
82 class strtab_node_c;
83 
84 //-----------------------------------------------------------------------------
85 // strtab_leaf_c: Leaf node.
86 //-----------------------------------------------------------------------------
87 
88 class strtab_leaf_c;
89 
90 //-----------------------------------------------------------------------------
91 // strtab_small_c: Small branch node.
92 //-----------------------------------------------------------------------------
93 
94 class strtab_small_c;
95 
96 //-----------------------------------------------------------------------------
97 // strtab_full_s: Full branch node.
98 //-----------------------------------------------------------------------------
99 
100 class strtab_full_c;
101 
102 //=============================================================================
103 // Class for String Table (Saves Strings Without Duplicates, Assigns ID):
104 //=============================================================================
105 
106 class strtab_c {
107  public:
108 
109 
110 //-----------------------------------------------------------------------------
111 // Constructor, Destructor:
112 //-----------------------------------------------------------------------------
113 
114  // Constructor:
115  strtab_c(str_t tab_name);
116 
117  // Destructor:
118  ~strtab_c();
119 
120 //-----------------------------------------------------------------------------
121 // (Object) Methods:
122 //-----------------------------------------------------------------------------
123 
124  // put: Insert a string into this string memory pool, return ID.
125  int put(str_t start, str_t end);
126 
127  // put: As above, but with '\0'-terminated string:
128  int put(str_t str) {
129  CHECK_VALID("strtab_c::put(str_t)");
130  CHECK(str != STR_NULL, "strtab_c::put(str_t): str is null");
131  return put(str, str + str_len(str));
132  }
133 
134  // get: Get a string for a given ID.
135  str_t get(int id) const {
136  return StrArr.get(id);
137  }
138 
139  // Return name of this string table:
140  str_t name() const {
141  return MemPool.name();
142  }
143 
144  // Return number of strings in this table:
145  int num_strings() const {
146  return StrMem.num_strings();
147  }
148 
149  // Return number of leaf nodes:
150  int num_leaf_nodes() const {
151  return NumLeafNodes;
152  }
153 
154  // Return number of small nodes:
155  int num_small_nodes() const {
156  return NumSmallNodes;
157  }
158 
159  // Return number of full nodes:
160  int num_full_nodes() const {
161  return NumFullNodes;
162  }
163 
164  // Return number of memory pages used for storing nodes:
165  int num_node_pages() const {
166  return NumPages;
167  }
168 
169  // Return number of memory pages used for storing strings:
170  int num_str_pages() const {
171  return StrMem.num_pages();
172  }
173 
174  // Return number of memory pages used for the id->str array:
175  int num_array_pages() const {
176  return StrArr.num_pages();
177  }
178 
179 #if VER_DUMP
180  // dump: Show data structure.
181  void dump(str_t headline = STR_NULL) const;
182 #endif
183 
184 //-----------------------------------------------------------------------------
185 // Debugging Support:
186 //-----------------------------------------------------------------------------
187 
188 #if VER_DEBUG
189  // Integrity check:
190  public:
191  str_t check() const;
192 
193  // Magic number (identifies objects of this class for debugging).
194  private:
195  long Magic; // Must be "STRTAB_MAGIC".
196 #endif
197 
198 //-----------------------------------------------------------------------------
199 // Copy-constructor and assignment operator are not supported for this class:
200 //-----------------------------------------------------------------------------
201 
202  private:
203 
204  strtab_c(const strtab_c& obj); // Not implemented
205  strtab_c& operator=(const strtab_c& obj); // Not implemented
206 
207 //-----------------------------------------------------------------------------
208 // Auxiliary Methods:
209 //-----------------------------------------------------------------------------
210 
211  private:
212 
213  // make_leaf: Construct new leaf node.
214  strtab_leaf_c *make_leaf(str_t str, int id);
215 
216  // make_small: Construct small node with a single entry.
217  strtab_small_c* make_small(char c);
218 
219  // make_full: Construct full node with data of small node (upgrade).
220  strtab_full_c* make_full(strtab_small_c* small);
221 
222  // dump_node: Recursive method to show search tree data structure.
223 #if VER_DUMP
224  int dump_node(int depth, int ch, strtab_node_c *node,
225  int parent_no, int node_no) const;
226 #endif
227 
228 //-----------------------------------------------------------------------------
229 // Private Object Members:
230 //-----------------------------------------------------------------------------
231 
232  private:
233 
234  // Memory Pool for Allocating Storage Pages:
235  mpool_c MemPool;
236 
237  // String Memory (for saving/copying strings to own memory):
238  strmem_c StrMem;
239 
240  // Flexible Array of Pointers to Strings in StrMem:
241  flexarr_c<str_t> StrArr;
242 
243  // Current Page for Storing Index Tree Nodes:
244  page_t MemPage;
245 
246  // Pointer to next free position in current page:
247  page_u_t FreePtr;
248 
249  // Number of free memory units in current page:
250  int FreeUnits;
251 
252  // Free list for small nodes (which have been upgraded to full nodes):
253  strtab_small_c *FreeSmallNodes;
254 
255  // Number of memory pages used for storing tree nodes:
256  int NumPages;
257 
258  // Root of the radix tree for string search:
259  strtab_node_c *Root;
260 
261  // Node numbers of different types:
262  int NumLeafNodes;
263  int NumSmallNodes;
264  int NumFullNodes;
265 
266 //=============================================================================
267 // End of Class:
268 //=============================================================================
269 
270 };
271 
272 //-----------------------------------------------------------------------------
273 // Define pointer type:
274 //-----------------------------------------------------------------------------
275 
276 typedef strtab_c *strtab_t;
277 
278 //-----------------------------------------------------------------------------
279 // Define null pointer:
280 //-----------------------------------------------------------------------------
281 
282 #define STRTAB_NULL (static_cast<strtab_t>(0))
283 
284 //=============================================================================
285 // End of Include File:
286 //=============================================================================
287 
288 #endif
289 
Definition: strtab.cpp:106
Definition: strtab.h:106
Definition: mpool.h:103
int str_len(register str_t str)
Definition: str.cpp:34
#define CHECK_VALID(EX)
Definition: check.h:85
Definition: strmem.h:65
const char * str_t
Definition: str.h:41
Definition: strtab.cpp:54
Definition: strtab.cpp:67
Definition: page.h:78
#define STR_NULL
Definition: str.h:52
Definition: strtab.cpp:85
#define CHECK(EX, MSG)
Definition: check.h:69