BAM
Abstract Machine for Bottom-Up Evaluation with the Push Method
atomtab.h
Go to the documentation of this file.
1 // ============================================================================
2 // Project: Deductive Database
3 // Filename: atomtab.h
4 // Purpose: Atom Table (stores set of atoms, similar to Enumeration Type)
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) 2010-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 
22 //=============================================================================
23 // Include File Frame:
24 //=============================================================================
25 
26 #ifndef ATOMTAB_INCLUDED
27 #define ATOMTAB_INCLUDED
28 
29 //=============================================================================
30 // Used Types and Macros:
31 //=============================================================================
32 
33 #ifndef VER_INCLUDED
34 #include "../base/ver.h"
35 #endif
36 
37 #ifndef STR_INCLUDED
38 #include "../base/str.h"
39 #endif
40 
41 #ifndef CHECK_INCLUDED
42 #include "../base/check.h"
43 #endif
44 
45 #ifndef MPOOL_INCLUDED
46 #include "../mem/mpool.h"
47 #endif
48 
49 #ifndef FLEXARR_INCLUDED
50 #include "../bds/flexarr.h"
51 #endif
52 
53 #ifndef ATOM_INCLUDED
54 #include "atom.h"
55 #endif
56 
57 
58 //=============================================================================
59 // Private Constants:
60 //=============================================================================
61 
62 //-----------------------------------------------------------------------------
63 // ATOMTAB_MAGIC: Magic number (identifies objects of this class).
64 //-----------------------------------------------------------------------------
65 
66 static const long ATOMTAB_MAGIC = 0x4154420AL; // 'ATB\n'
67 
68 //-----------------------------------------------------------------------------
69 // ATOMTAB_HASHSIZE: Size of hash table.
70 //-----------------------------------------------------------------------------
71 
72 static const int ATOMTAB_HASHSIZE = 251; // Should be prime number
73 
74 //=============================================================================
75 // Class for Atom Table (stores set of atoms, similar to Enumeration Type):
76 //=============================================================================
77 
78 class atomtab_c {
79  public:
80 
81 //-----------------------------------------------------------------------------
82 // Constructor, Destructor:
83 //-----------------------------------------------------------------------------
84 
85  // Constructor:
86  atomtab_c(str_t tab_name);
87 
88  // Destructor:
89  ~atomtab_c();
90 
91 //-----------------------------------------------------------------------------
92 // (Object) Methods:
93 //-----------------------------------------------------------------------------
94 
95  // put: Search current token in atom table, insert if not found.
96  int put(str_t name_start, str_t name_end);
97 
98  // put: As above, but with '\0'-terminated string:
99  int put(str_t atom_name) {
100  CHECK_VALID("atomtab_c::put(str_t)");
101  CHECK(atom_name != STR_NULL,
102  "atomtab_c::put(str_t): atom_name is null");
103  return put(atom_name, atom_name + str_len(atom_name));
104  }
105 
106  // get: Return atom name for given id:
107  str_t get(int id);
108 
109  // name: Get name of this atom table (similar to enumeration type).
110  str_t name() const {
111  CHECK_VALID("atomtab_c::name");
112  return Name;
113  }
114 
115  // num_atoms: Get number of atoms in this table.
116  int num_atoms() const {
117  CHECK_VALID("atomtab_c::num_atoms");
118  return NumAtoms;
119  }
120 
121  // Return number of memory pages:
122  int num_pages() const {
123  return MemPool.num_alloc_pages();
124  }
125 
126 //-----------------------------------------------------------------------------
127 // Debugging Support:
128 //-----------------------------------------------------------------------------
129 
130 #if VER_DEBUG
131  public:
132  // Integrity check:
133  str_t check() const;
134 
135  private:
136  // Magic number (identifies objects of this class for debugging).
137  long Magic; // Must be "ATOMTAB_MAGIC".
138 #endif
139 
140 #if VER_DUMP
141  public:
142  // Display data structure:
143  void dump(str_t headline = STR_NULL) const;
144 #endif
145 
146 //-----------------------------------------------------------------------------
147 // Copy-constructor and assignment operator are not supported for this class:
148 //-----------------------------------------------------------------------------
149 
150  private:
151 
152  atomtab_c(const atomtab_c& obj); // Not implemented
153  atomtab_c& operator=(const atomtab_c& obj); // Not implemented
154 
155 //-----------------------------------------------------------------------------
156 // Private Object Members:
157 //-----------------------------------------------------------------------------
158 
159  private:
160 
161  // Memory Pool for Allocating Storage Pages:
162  mpool_c MemPool;
163 
164  // Current Memory Page for Storing Atom Objects:
165  page_t MemPage;
166 
167  // Pointer to next free place in current memory page:
168  atom_t MemPtr;
169 
170  // Number of free places in current page:
171  int MemFree;
172 
173  // Hash table:
174  atom_t HashTab[ATOMTAB_HASHSIZE];
175 
176  // Name of this atom table (domain, enumeration type):
177  str_t Name;
178 
179  // Current number of atoms (also Id of next atom):
180  int NumAtoms;
181 
182  // Find atom by id, small (standard) version:
183  atom_t AtomArr[ATOMTAB_HASHSIZE];
184 
185  // Find atom by id, large version:
186  flexarr_c<atom_t> LargeAtomArr;
187 
188 //=============================================================================
189 // End of Class:
190 //=============================================================================
191 
192 };
193 
194 //-----------------------------------------------------------------------------
195 // Define pointer type:
196 //-----------------------------------------------------------------------------
197 
198 typedef atomtab_c *atomtab_t;
199 
200 //-----------------------------------------------------------------------------
201 // Define null pointer:
202 //-----------------------------------------------------------------------------
203 
204 #define ATOMTAB_NULL (static_cast<atomtab_t>(0))
205 
206 //=============================================================================
207 // End of Include File:
208 //=============================================================================
209 
210 #endif
211 
Class Template for Flexible Arrays (Lists with Index Access)
Definition: flexarr.h:86
Definition: atom.h:61
Definition: atomtab.h:78
Definition: mpool.h:103
int str_len(register str_t str)
Definition: str.cpp:34
#define CHECK_VALID(EX)
Definition: check.h:85
const char * str_t
Definition: str.h:41
#define STR_NULL
Definition: str.h:52
Short Strings, Symbolic Constants.
#define CHECK(EX, MSG)
Definition: check.h:69