BAM
Abstract Machine for Bottom-Up Evaluation with the Push Method
gen_cur2.h
Go to the documentation of this file.
1 // ============================================================================
2 // Project: Deductive Database
3 // Filename: gen_cur2.h
4 // Purpose: Cursor over generated two-column relations (for tests).
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 
22 //=============================================================================
23 // Include File Frame:
24 //=============================================================================
25 
26 #ifndef GEN_CUR2_INCLUDED
27 #define GEN_CUR2_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 STACK_INCLUDED
46 #include "../bds/stack.h"
47 #endif
48 
49 //=============================================================================
50 // Private Constants:
51 //=============================================================================
52 
53 //-----------------------------------------------------------------------------
54 // GEN_CUR2_MAGIC: Magic number (identifies objects of this class).
55 //-----------------------------------------------------------------------------
56 
57 static const long GEN_CUR2_MAGIC = 0x4743320AL; // 'GC2\n'
58 
59 //=============================================================================
60 // Class for Cursor to Generated Relation with Two Integer Columns (for Tests):
61 //=============================================================================
62 
63 class gen_cur2_c {
64  public:
65 
66 //-----------------------------------------------------------------------------
67 // Constructor, Destructor:
68 //-----------------------------------------------------------------------------
69 
70  // Constructor 1 with full parameter selection:
71  gen_cur2_c(int size, int c1_start, int c1_step, int c1_max,
72  int c2_start, int c2_step, int c2_max);
73 
74  // Constructor 2 with simplified relation ID:
75  gen_cur2_c(int rel_id);
76 
77  // Destructor:
78  ~gen_cur2_c();
79 
80 //-----------------------------------------------------------------------------
81 // Methods:
82 //-----------------------------------------------------------------------------
83 
84  // open: Open the cursor.
85  void open();
86 
87  // fetch: Get next tuple, returns false if there is no further tuple.
88  bool fetch() {
89 
90  // Check this object:
91  CHECK_VALID("gen_cur2_c::fetch");
92 
93  // Check state:
94  CHECK(TupleNo != -1,
95  "gen_cur2_c::fetch: cursor not open");
96  CHECK(TupleNo <= Size,
97  "gen_cur2_c::fetch: fetch beyond end");
98 
99  // Increase tuple number (counts number of fetches):
100  TupleNo++;
101 
102  // Check whether we are at the end of the relation:
103  if(TupleNo > Size)
104  return false;
105 
106  // Initialization (first fetch):
107  if(TupleNo == 1) {
108  C1 = Start1;
109  C2 = Start2;
110  }
111 
112  // Compute next tuple:
113  else {
114  // Compute next value of column 1:
115  if(C1 <= Max1 - Step1)
116  C1 += Step1;
117  else if(Iter1 < Step1) {
118  C1 = Start1 + Iter1;
119  Iter1++;
120  }
121  else {
123  "gen_cur2_c::fetch: No more values 1");
124  C1 = -1;
125  C2 = -1;
126  return false;
127  }
128 
129  // Compute next value of column 2:
130  if(C2 <= Max2 - Step2)
131  C2 += Step2;
132  else if(Iter2 < Step2) {
133  C2 = Start2 + Iter2;
134  Iter2++;
135  }
136  else {
138  "gen_cur2_c::fetch: No more values 2");
139  C1 = -1;
140  C2 = -1;
141  return false;
142  }
143  }
144 
145  // Done:
146  return true;
147  }
148 
149  // c1: Return value of column 1 to which the cursor points.
150  int c1() {
151 
152  // Check this object:
153  CHECK_VALID("gen_cur2_c::c1");
154 
155  // Check state:
156  CHECK(TupleNo != -1,
157  "gen_cur2_c::c1: cursor not open");
158  CHECK(TupleNo > 0,
159  "gen_cur2_c::c1: fetch must be called first");
160  CHECK(TupleNo <= Size,
161  "gen_cur2_c::c1: fetch beyond end");
162 
163  // Return current value for column 1:
164  return C1;
165  }
166 
167  // c2: Return value of column 2 to which the cursor points.
168  int c2() {
169 
170  // Check this object:
171  CHECK_VALID("gen_cur2_c::c2");
172 
173  // Check state:
174  CHECK(TupleNo != -1,
175  "gen_cur2_c::c2: cursor not open");
176  CHECK(TupleNo > 0,
177  "gen_cur2_c::c2: fetch must be called first");
178  CHECK(TupleNo <= Size,
179  "gen_cur2_c::c2: fetch beyond end");
180 
181  // Return current value for column 2:
182  return C2;
183  }
184 
185  // close: Close cursor.
186  void close() {
187 
188  // Check this object:
189  CHECK_VALID("gen_cur2_c::close");
190 
191  // Check state:
192  CHECK(TupleNo >= 0,
193  "gen_cur2_c::close: cursor not open");
194 
195  // Close cursor:
196  TupleNo = -1;
197  C1 = -1;
198  C2 = -1;
199  }
200 
201  // push: Save the cursor state on a stack.
202  bool push(stack_c<int> *stack) {
203 
204  // Check this object:
205  CHECK_VALID("gen_cur2_c::push");
206 
207  // Save changeable attributes:
208  if(!stack->push(TupleNo))
209  return false;
210  if(!stack->push(C1))
211  return false;
212  if(!stack->push(C2))
213  return false;
214  if(!stack->push(Iter1))
215  return false;
216  if(!stack->push(Iter2))
217  return false;
218 
219  // Ok:
220  return true;
221  }
222 
223  // pop: Restore the cursor state from a stack.
224  void pop(stack_c<int> *stack) {
225 
226  // Check this object:
227  CHECK_VALID("gen_cur2_c::pop");
228 
229  // Restore changeable attributes (in opposite order):
230  Iter2 = stack->pop();
231  Iter1 = stack->pop();
232  C2 = stack->pop();
233  C1 = stack->pop();
234  TupleNo = stack->pop();
235  }
236 
237 //-----------------------------------------------------------------------------
238 // Debugging Support:
239 //-----------------------------------------------------------------------------
240 
241 #if VER_DEBUG
242  public:
243  // Integrity check:
244  str_t check() const;
245 
246  private:
247  // Magic number (identifies objects of this class for debugging).
248  long Magic; // Must be "GEN_CUR2_MAGIC".
249 #endif
250 
251 #if VER_DUMP
252  public:
253  // Display data structure:
254  void dump(str_t headline = STR_NULL) const;
255 #endif
256 
257 //-----------------------------------------------------------------------------
258 // Copy-constructor and assignment operator are not supported for this class:
259 //-----------------------------------------------------------------------------
260 
261  private:
262 
263  gen_cur2_c(const gen_cur2_c& obj); // Not implemented
264  gen_cur2_c& operator=(const gen_cur2_c& obj); // Not implemented
265 
266 //-----------------------------------------------------------------------------
267 // Private Object Members:
268 //-----------------------------------------------------------------------------
269 
270  private:
271 
272  // Relation size (number of tuples to be generated):
273  int Size;
274 
275  // Start value of column 1:
276  int Start1;
277 
278  // Step distance for column 1:
279  int Step1;
280 
281  // Maximal Value that can be used for column 1:
282  int Max1;
283 
284  // Start value of column 2:
285  int Start2;
286 
287  // Step distance for column 2:
288  int Step2;
289 
290  // Maximal Value that can be used for column 2:
291  int Max2;
292 
293  // Current tuple number:
294  int TupleNo;
295 
296  // Current value for column 1 in the tuple sequence:
297  int C1;
298 
299  // Current value for column 2 in the tuple sequence:
300  int C2;
301 
302  // Current iteration counter for column 1 (number of restarts - 1):
303  // The restart is at Start1 + Iter1 (to generate distinct values).
304  // If Iter1 >= Step1, no further restart is possible.
305  int Iter1;
306 
307  // Current iteration counter for column 2 (number of restarts - 1):
308  int Iter2;
309 
310 
311 //-----------------------------------------------------------------------------
312 // Auxiliary Methods:
313 //-----------------------------------------------------------------------------
314 
315  // Initialize sequence generator attributes:
316  void init();
317 
318 //=============================================================================
319 // End of Class:
320 //=============================================================================
321 
322 };
323 
324 //-----------------------------------------------------------------------------
325 // Define pointer type:
326 //-----------------------------------------------------------------------------
327 
328 typedef gen_cur2_c *gen_cur2_t;
329 
330 //-----------------------------------------------------------------------------
331 // Define null pointer:
332 //-----------------------------------------------------------------------------
333 
334 #define GEN_CUR2_NULL (static_cast<gen_cur2_t>(0))
335 
336 //=============================================================================
337 // End of Include File:
338 //=============================================================================
339 
340 #endif
341 
#define CHECK_IMPOSSIBLE(MSG)
Definition: check.h:151
#define CHECK_VALID(EX)
Definition: check.h:85
const char * str_t
Definition: str.h:41
Definition: gen_cur2.h:63
#define STR_NULL
Definition: str.h:52
#define CHECK(EX, MSG)
Definition: check.h:69