BAM
Abstract Machine for Bottom-Up Evaluation with the Push Method
set_tt.h
Go to the documentation of this file.
1 // ============================================================================
2 // Project: Deductive Database
3 // Filename: set_tt.h
4 // Purpose: Set with two columns with range [0..1023] (ten bits)
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_TT_INCLUDED
27 #define SET_TT_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 REL_INCLUDED
50 #include "../rel/rel.h"
51 #endif
52 
53 
54 //=============================================================================
55 // Private Constants:
56 //=============================================================================
57 
58 //-----------------------------------------------------------------------------
59 // SET_TT_MAGIC: Magic number (identifies objects of this class).
60 //-----------------------------------------------------------------------------
61 
62 static const long SET_TT_MAGIC = 0x5354540AL; // 'STT\n'
63 
64 //-----------------------------------------------------------------------------
65 // SET_TT_C1_SIZE: Number of possible values in column 1 (array size):
66 //-----------------------------------------------------------------------------
67 
68 static const int SET_TT_C1_SIZE = 1024;
69 
70 //-----------------------------------------------------------------------------
71 // SET_TT_C2_SIZE: Number of possible values in column 2 (bitmap size):
72 //-----------------------------------------------------------------------------
73 
74 static const int SET_TT_C2_SIZE = 1024;
75 
76 
77 //=============================================================================
78 // Local Types:
79 //=============================================================================
80 
81 //-----------------------------------------------------------------------------
82 // SET_TT_BITMAP_SIZE: Size of a bitmap for column 2 in "unsigned int" units:
83 //-----------------------------------------------------------------------------
84 
85 #define SET_TT_BITMAP_SIZE \
86  ((SET_TT_C2_SIZE + (VER_UINT_BITS-1)) / VER_UINT_BITS)
87 
88 //-----------------------------------------------------------------------------
89 // Bitmap used for second column:
90 //-----------------------------------------------------------------------------
91 
93  private:
94  unsigned int BitMap[SET_TT_BITMAP_SIZE];
95  friend class set_tt_c;
96 };
97 
98 //-----------------------------------------------------------------------------
99 // Pointer type for bitmaps:
100 //-----------------------------------------------------------------------------
101 
103 
104 //-----------------------------------------------------------------------------
105 // Null pointer for bitmaps:
106 //-----------------------------------------------------------------------------
107 
108 #define SET_TT_BITMAP_NULL (static_cast<set_tt_bitmap_t>(0))
109 
110 
111 //=============================================================================
112 // Class for Set with two Integer Columns with Range [0..1023] (ten bits):
113 //=============================================================================
114 
115 // Non-negative integers are used as ID for strings and for atoms.
116 // This version has an index on the first column.
117 // It is assumed that the first column is dense, so we can basically use
118 // an array as index. However, it is also assumed that a fixed-size array
119 // is not sufficient, so the relation can be quite big or of an unknown size.
120 
121 class set_tt_c : public rel_c {
122  public:
123 
124 //-----------------------------------------------------------------------------
125 // Constructor, Destructor:
126 //-----------------------------------------------------------------------------
127 
128  // Constructor:
129  set_tt_c(str_t rel_name);
130 
131  // Destructor:
132  ~set_tt_c();
133 
134 //-----------------------------------------------------------------------------
135 // (Object) Methods:
136 //-----------------------------------------------------------------------------
137 
138  // insert: Insert tuple into relation.
139  bool insert(int c1, int c2);
140 
141 //-----------------------------------------------------------------------------
142 // contains: Check whether an element is contained in the set.
143 //-----------------------------------------------------------------------------
144 
145  inline bool contains(int c1, int c2) const {
146  // Check this object:
147  CHECK_VALID("set_tt_c::contains");
148 
149  // Check parameters:
150  CHECK(c1 >= 0, "set_tt_c::contains: c1 is negative");
151  CHECK(c2 >= 0, "set_tt_c::contains: c2 is negative");
152  CHECK(c1 < SET_TT_C1_SIZE,
153  "set_tt_c::contains: c1 is too large");
154  CHECK(c2 < SET_TT_C2_SIZE,
155  "set_tt_c::contains: c2 is too large");
156 
157  // Check whether bitmap for c1 exists.
158  // If not, there is no such row:
159  set_tt_bitmap_t bitmap = Arr[c1];
160  if(bitmap == SET_TT_BITMAP_NULL)
161  return false;
162 
163  // Search bit for c2 in bitmap:
164  int index = c2 / VER_UINT_BITS;
165  int bit_no = c2 % VER_UINT_BITS;
166  unsigned int *ptr = bitmap->BitMap + index;
167  unsigned int mask = 1 << bit_no;
168  return (*ptr & mask) != 0;
169  }
170 
171 //-----------------------------------------------------------------------------
172 // Debugging Support:
173 //-----------------------------------------------------------------------------
174 
175 #if VER_DEBUG
176  public:
177  // Integrity check:
178  str_t check() const;
179 
180  private:
181  // Magic number (identifies objects of this class for debugging).
182  long Magic; // Must be "SET_TT_MAGIC".
183 #endif
184 
185 #if VER_DUMP
186  public:
187  // Display data structure:
188  void dump(str_t headline = STR_NULL) const;
189 #endif
190 
191 //-----------------------------------------------------------------------------
192 // Copy-constructor and assignment operator are not supported for this class:
193 //-----------------------------------------------------------------------------
194 
195  private:
196 
197  set_tt_c(const set_tt_c& obj); // Not implemented
198  set_tt_c& operator=(const set_tt_c& obj); // Not implemented
199 
200 //-----------------------------------------------------------------------------
201 // Private Object Members:
202 //-----------------------------------------------------------------------------
203 
204  private:
205 
206  // Array of pointers to bitmaps:
207  set_tt_bitmap_t Arr[SET_TT_C1_SIZE];
208 
209  // Current Memory Page for Storing Bitmaps:
210  page_t MemPage;
211 
212  // Pointer to next free place in current memory page:
213  set_tt_bitmap_t MemPtr;
214 
215  // Number of free places in current page (in set_tt_bitmap_c units):
216  int MemFree;
217 
218 
219 //=============================================================================
220 // End of Class:
221 //=============================================================================
222 
223 };
224 
225 //-----------------------------------------------------------------------------
226 // Define pointer type:
227 //-----------------------------------------------------------------------------
228 
229 typedef set_tt_c *set_tt_t;
230 
231 //-----------------------------------------------------------------------------
232 // Define null pointer:
233 //-----------------------------------------------------------------------------
234 
235 #define SET_TT_NULL (static_cast<set_tt_t>(0))
236 
237 //=============================================================================
238 // End of Include File:
239 //=============================================================================
240 
241 #endif
242 
Definition: set_tt.h:92
#define VER_UINT_BITS
Definition: ver.h:798
Definition: rel.h:72
#define CHECK_VALID(EX)
Definition: check.h:85
const char * str_t
Definition: str.h:41
Definition: set_tt.h:121
#define STR_NULL
Definition: str.h:52
#define CHECK(EX, MSG)
Definition: check.h:69