BAM
Abstract Machine for Bottom-Up Evaluation with the Push Method
set_s.h
Go to the documentation of this file.
1 // ============================================================================
2 // Project: Deductive Database
3 // Filename: set_s.h
4 // Purpose: Set of tuples (binding pattern b..b): Using C++ Standard T.Lib.
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) 2016-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 
23 //=============================================================================
24 // Include File Frame:
25 //=============================================================================
26 
27 #ifndef SET_S_INCLUDED
28 #define SET_S_INCLUDED
29 
30 //=============================================================================
31 // Used Types and Macros:
32 //=============================================================================
33 
34 #ifndef VER_INCLUDED
35 #include "../base/ver.h"
36 #endif
37 
38 #ifndef STR_INCLUDED
39 #include "../base/str.h"
40 #endif
41 
42 #ifndef CHECK_INCLUDED
43 #include "../base/check.h"
44 #endif
45 
46 #ifndef PAGE_INCLUDED
47 #include "../mem/page.h"
48 #endif
49 
50 #ifndef MPOOL_INCLUDED
51 #include "../mem/mpool.h"
52 #endif
53 
54 #ifndef RELSIZE_INCLUDED
55 #include "relsize.h"
56 #endif
57 
58 #ifndef REL_INCLUDED
59 #include "rel.h"
60 #endif
61 
62 #include <stddef.h>
63 #include <unordered_set>
64 
65 
66 //=============================================================================
67 // Private Constants:
68 //=============================================================================
69 
70 
71 //-----------------------------------------------------------------------------
72 // SET_S_MAGIC: Magic number (identifies objects of this class).
73 //-----------------------------------------------------------------------------
74 
75 static const long SET_S_MAGIC = 0x5354530AL; // 'STS\n'
76 
77 
78 //=============================================================================
79 // Helper Classes, Parameters to the C++11 unordered_set:
80 //=============================================================================
81 
82 template<class T> class set_s_hash_c {
83  public:
84  size_t operator() (T const & x) const noexcept
85  {
86  // My row types all have a hash function:
87  return x.hash();
88  }
89 };
90 
91 //=============================================================================
92 // Class Template for Sets (for Duplicate Check) - Using C++11 unordered_set:
93 //=============================================================================
94 
95 
96 template<class T> class set_s_c: public rel_c {
97  public:
98 
99 //-----------------------------------------------------------------------------
100 // Constructor, Destructor:
101 //-----------------------------------------------------------------------------
102 
103  // Constructor:
104  set_s_c(str_t set_s_name);
105 
106  // Destructor:
107  ~set_s_c();
108 
109 //-----------------------------------------------------------------------------
110 // Copy-constructor and assignment operator are not supported for this class:
111 //-----------------------------------------------------------------------------
112 
113  private:
114 
115  set_s_c(const set_s_c& obj); // Not implemented
116  set_s_c& operator=(const set_s_c& obj); // Not implemented
117 
118 
119 //=============================================================================
120 // (Object) Methods:
121 //=============================================================================
122 
123  public:
124 
125 //-----------------------------------------------------------------------------
126 // insert: Insert an element into the set (returns true if successful).
127 //-----------------------------------------------------------------------------
128 
129  // This method returns true if the element was not already in the set
130  // and was successfully inserted (i.e. if it is not a duplicate).
131  // It returns false if the element was already in the set (duplicate).
132  // It also returns false if it was impossible to insert the element
133  // because there was not enough memory.
134  // By pretending that it was a duplicate, recursion at least comes
135  // to an end and the program terminates.
136  // One should check the method mem_err() from time to time
137  // (at least at the end) to see whether this problem occurred
138  // or the result is reliable.
139  // A memory error is also logged in the class err_c.
140  // So even if one does not check mem_err() for every set object,
141  // one will notice the error if one at least checks the global error
142  // log or has enabled output of error messages.
143 
144  bool insert(T elem) {
145 
146  // Check this object:
147  CHECK_VALID("set_s_c::insert");
148 
149  // Insert:
150  //std::pair<std::unordered_set<T, set_s_hash_c<T> >::iterator,
151  // bool> result =
152  // Set.insert(elem);
153 
154  // Was the element actually inserted?
155  //if(result.second) {
156  if(Set.insert(elem).second) {
157  NumRows++;
158  return true;
159  }
160  else
161  return false;
162  }
163 
164 //-----------------------------------------------------------------------------
165 // contains: Check whether an element is contained in the set.
166 //-----------------------------------------------------------------------------
167 
168  bool contains(T elem) const {
169 
170  // Check this object:
171  CHECK_VALID("set_s_c::contains");
172 
173  // Return result:
174  return Set.count(elem) > 0;
175 
176  // Alternative:
177  // std::unordered_set<T>::const_iterator it = Set.find(elem);
178  // return it != Set.end();
179  }
180 
181 //-----------------------------------------------------------------------------
182 // size_value: Values of data-structure specific size measures.
183 //-----------------------------------------------------------------------------
184 
185  int size_value(relsize_t relsize) const {
186  CHECK_VALID("set_s_c::size_value");
187  CHECK(relsize_valid(relsize),
188  "set_s_c::size_value: invalid size measure");
189 
190  // Return value of selected size measure:
191  switch(relsize) {
192  case RELSIZE_OBJ_SIZE:
193  return static_cast<int>(sizeof(set_s_c<T>));
194  default:
195  return -1;
196  }
197  }
198 
199 //=============================================================================
200 // Debugging Support:
201 //=============================================================================
202 
203 #if VER_DEBUG
204  public:
205  // Integrity check:
206  str_t check() const;
207 
208  private:
209  // Magic number (identifies objects of this class for debugging).
210  long Magic; // Must be "SET_S_MAGIC".
211 #endif
212 
213 #if VER_DUMP
214  public:
215  // Display data structure:
216  void dump(str_t headline = STR_NULL) const;
217 #endif
218 
219 
220 //=============================================================================
221 // Private Object Members:
222 //=============================================================================
223 
224  private:
225 
226  // C++11 Standard Library Object:
227  std::unordered_set<T, set_s_hash_c<T> > Set;
228 
229 
230 //=============================================================================
231 // End of Class Template:
232 //=============================================================================
233 
234 };
235 
236 
237 //=============================================================================
238 // End of Include File:
239 //=============================================================================
240 
241 #endif
242 
Size measures for relation data structures.
Definition: set_s.h:82
Definition: rel.h:72
#define CHECK_VALID(EX)
Definition: check.h:85
Definition: set_s.h:96
const char * str_t
Definition: str.h:41
#define STR_NULL
Definition: str.h:52
Superclass of all data structures for relations.
#define CHECK(EX, MSG)
Definition: check.h:69