BAM
Abstract Machine for Bottom-Up Evaluation with the Push Method
set.h
Go to the documentation of this file.
1 // ============================================================================
2 // Project: Deductive Database
3 // Filename: set.h
4 // Purpose: Set of objects/tuples - relation with binding pattern b..b
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 
22 //=============================================================================
23 // Include File Frame:
24 //=============================================================================
25 
26 #ifndef SET_INCLUDED
27 #define SET_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 PAGE_INCLUDED
46 #include "../mem/page.h"
47 #endif
48 
49 #ifndef MPOOL_INCLUDED
50 #include "../mem/mpool.h"
51 #endif
52 
53 #ifndef RELSIZE_INCLUDED
54 #include "../rel/relsize.h"
55 #endif
56 
57 #ifndef REL_INCLUDED
58 #include "../rel/rel.h"
59 #endif
60 
61 
62 //=============================================================================
63 // Private Constants:
64 //=============================================================================
65 
66 //-----------------------------------------------------------------------------
67 // SET_MAXPAGES: Maximal hash table size in memory pages.
68 //-----------------------------------------------------------------------------
69 
70 static const int SET_MAXPAGES = 1024;
71 
72  // Since the hash table size is doubled each time more than 75%
73  // of the entries are used, SET_MAXPAGES must be a power of 2.
74 
75  // Additional pages will be used for entries in the hash table.
76  // The number of memory pages used for this purpose is unlimited.
77 
78 //-----------------------------------------------------------------------------
79 // SET_MAGIC: Magic number (identifies objects of this class).
80 //-----------------------------------------------------------------------------
81 
82 static const long SET_MAGIC = 0x5345540AL; // 'SET\n'
83 
84 
85 //=============================================================================
86 // Forward declarations:
87 //=============================================================================
88 
89 // Private class for nodes in the hash table chains:
90 // template<class T> class set_node_c;
91 
92 //=============================================================================
93 // Private Class for the Nodes in the Hash Table Chains:
94 //=============================================================================
95 
96 template<class T> class set_node_c {
97  public:
98  T Elem;
99  set_node_c<T> *Next;
100 };
101 
102 #define SET_NODE_T set_node_c<T>*
103 #define SET_NODE_NULL (static_cast<set_node_c<T> *>(0))
104 
105 
106 //=============================================================================
107 // Class Template for Sets (for Duplicate Check):
108 //=============================================================================
109 
110 template<class T> class set_c: public rel_c {
111  public:
112 
113 //-----------------------------------------------------------------------------
114 // Constructor, Destructor:
115 //-----------------------------------------------------------------------------
116 
117  // Constructor:
118  set_c(str_t set_name);
119 
120  // Destructor:
121  ~set_c();
122 
123 //-----------------------------------------------------------------------------
124 // Copy-constructor and assignment operator are not supported for this class:
125 //-----------------------------------------------------------------------------
126 
127  private:
128 
129  set_c(const set_c& obj); // Not implemented
130  set_c& operator=(const set_c& obj); // Not implemented
131 
132 
133 //=============================================================================
134 // (Object) Methods:
135 //=============================================================================
136 
137  public:
138 
139 //-----------------------------------------------------------------------------
140 // insert: Insert an element into the set (returns true if successful).
141 //-----------------------------------------------------------------------------
142 
143  // This method returns true if the element was not already in the set
144  // and was successfully inserted (i.e. if it is not a duplicate).
145  // It returns false if the element was already in the set (duplicate).
146  // It also returns false if it was impossible to insert the element
147  // because there was not enough memory.
148  // By pretending that it was a duplicate, recursion at least comes
149  // to an end and the program terminates.
150  // One should check the method mem_err() from time to time
151  // (at least at the end) to see whether this problem occurred
152  // or the result is reliable.
153  // A memory error is also logged in the class err_c.
154  // So even if one does not check mem_err() for every set object,
155  // one will notice the error if one at least checks the global error
156  // log or has enabled output of error messages.
157 
158  bool insert(T elem);
159 
160 //-----------------------------------------------------------------------------
161 // contains: Check whether an element is contained in the set.
162 //-----------------------------------------------------------------------------
163 
164  bool contains(T elem) const {
165  // Check this object:
166  CHECK_VALID("set_c::contains");
167 
168  // Search in hash table. First compute hash function:
169  int hash_val = elem.hash();
170  CHECK(hash_val >= 0, "set_c::contains: hash_val is negative");
171  hash_val %= HashSize;
172 
173  // Get start of linked list:
174  SET_NODE_T *hashtab_entry =
175  HashTab[hash_val / PAGE_SIZE(SET_NODE_T)];
176  hashtab_entry += (hash_val % PAGE_SIZE(SET_NODE_T));
177 
178  // Now search the linked list of atoms in that entry:
179  SET_NODE_T node;
180  for(node = *hashtab_entry; node != SET_NODE_NULL;
181  node = node->Next) {
182  if(node->Elem == elem)
183  return true;
184  }
185  return false;
186  }
187 
188 //-----------------------------------------------------------------------------
189 // hash_size: Size of the hash table.
190 //-----------------------------------------------------------------------------
191 
192  int hash_size() const {
193  CHECK_VALID("set_c::hash_size");
194  return HashSize;
195  }
196 
197 //-----------------------------------------------------------------------------
198 // entries_used: Number of hash table entries that are used (not NULL).
199 //-----------------------------------------------------------------------------
200 
201  int entries_used() const {
202  CHECK_VALID("set_c::entries_used");
203  return EntriesUsed;
204  }
205 
206 //-----------------------------------------------------------------------------
207 // size_value: Values of data-structure specific size measures.
208 //-----------------------------------------------------------------------------
209 
210  int size_value(relsize_t relsize) const {
211  CHECK_VALID("set_c::size_value");
212  CHECK(relsize_valid(relsize),
213  "set_c::size_value: invalid size measure");
214 
215  // Return value of selected size measure:
216  switch(relsize) {
217  case RELSIZE_OBJ_SIZE:
218  return static_cast<int>(sizeof(set_c<T>));
219  case RELSIZE_HASHTAB_SIZE:
220  return HashSize;
221  case RELSIZE_ENTRIES_USED:
222  return EntriesUsed;
223  default:
224  return -1;
225  }
226  }
227 
228 //=============================================================================
229 // Debugging Support:
230 //=============================================================================
231 
232 #if VER_DEBUG
233  public:
234  // Integrity check:
235  str_t check() const;
236 
237  private:
238  // Magic number (identifies objects of this class for debugging).
239  long Magic; // Must be "SET_MAGIC".
240 #endif
241 
242 #if VER_DUMP
243  public:
244  // Display data structure:
245  void dump(str_t headline = STR_NULL) const;
246 #endif
247 
248 
249 //=============================================================================
250 // Private Object Members:
251 //=============================================================================
252 
253  private:
254 
255  // Hash table size (in number of entries):
256  int HashSize;
257 
258  // Hash table size (in memory pages):
259  int HashPages;
260 
261  // Number of used entries in hash table:
262  int EntriesUsed;
263 
264  // Current Memory Page for Storing Tuple Objects:
265  page_t MemPage;
266 
267  // Pointer to next free place in current memory page:
268  set_node_c<T> *MemPtr;
269 
270  // Number of free places in current page:
271  int MemFree;
272 
273  // Hash table:
274  set_node_c<T> **HashTab[SET_MAXPAGES];
275 
276 //=============================================================================
277 // End of Class Template:
278 //=============================================================================
279 
280 };
281 
282 
283 //=============================================================================
284 // End of Include File:
285 //=============================================================================
286 
287 #endif
288 
Definition: rel.h:72
#define CHECK_VALID(EX)
Definition: check.h:85
const char * str_t
Definition: str.h:41
#define STR_NULL
Definition: str.h:52
Definition: set.h:96
#define CHECK(EX, MSG)
Definition: check.h:69
Definition: set.h:110