BAM
Abstract Machine for Bottom-Up Evaluation with the Push Method
row_2.h
Go to the documentation of this file.
1 // ============================================================================
2 // Project: Deductive Database
3 // Filename: row_2.h
4 // Purpose: Tuple/Table Row with two integer columns.
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 ROW_2_INCLUDED
27 #define ROW_2_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 
46 //=============================================================================
47 // Private Constants:
48 //=============================================================================
49 
50 //-----------------------------------------------------------------------------
51 // ROW_2_PRIME: Some large prime number (needed for hash function).
52 //-----------------------------------------------------------------------------
53 
54 static const long ROW_2_PRIME = 1073806763;
55 
56 //-----------------------------------------------------------------------------
57 // ROW_2_MAGIC: Magic number (identifies objects of this class).
58 //-----------------------------------------------------------------------------
59 
60 static const long ROW_2_MAGIC = 0x525F320AL; // 'R_2\n'
61 
62 //=============================================================================
63 // Class for Relation with two integer columns, no index:
64 //=============================================================================
65 
66 class row_2_c {
67  public:
68 
69 //-----------------------------------------------------------------------------
70 // Constants:
71 //-----------------------------------------------------------------------------
72 
73  static const int NUM_COLS = 2;
74 
75 //-----------------------------------------------------------------------------
76 // Constructor, Destructor:
77 //-----------------------------------------------------------------------------
78 
79  // Constructor:
80  row_2_c(int col1, int col2) : C1(col1), C2(col2) {
81 
82  // Set Magic number:
83  CHECK_CODE(Magic = ROW_2_MAGIC);
84  }
85 
86  // Destructor:
87  ~row_2_c() {
88  // Check this object:
89  CHECK_VALID("row_2_c::destructor");
90 
91  // Make this object invalid:
92  CHECK_CODE(Magic = 0);
93  }
94 
95  // Copy-constructor:
96  row_2_c(const row_2_c& row) : C1(row.C1), C2(row.C2) {
97 
98  // Set Magic number:
99  CHECK_CODE(Magic = ROW_2_MAGIC);
100  }
101 
102 //-----------------------------------------------------------------------------
103 // (Object) Methods:
104 //-----------------------------------------------------------------------------
105 
106  // c1: Get value of first column.
107  int c1() const {
108  // Check this object:
109  CHECK_VALID("row_2_c::c1");
110 
111  // Return value of column 1:
112  return C1;
113  }
114 
115  // c2: Get value of second column.
116  int c2() const {
117  // Check this object:
118  CHECK_VALID("row_2_c::c2");
119 
120  // Return value of column 2:
121  return C2;
122  }
123 
124  // hash: Compute a hash value.
125  int hash() const {
126  // Check this object:
127  CHECK_VALID("row_2_c::hash");
128 
129  // Compute hash value:
130  long hash_val = C1 ^ (C2 << 11) ^ (C2 << 7);
131  if(hash_val < 0)
132  hash_val = -(hash_val+1);
133  CHECK(hash_val >= 0,
134  "row_2_c::hash: hash_val is negative");
135  return static_cast<int>(hash_val % ROW_2_PRIME);
136  }
137 
138 
139  // ==: equality test.
140  bool operator==(const row_2_c& other) const {
141  // Check this object:
142  CHECK_VALID("row_2_c::==");
143 
144  // Objects are equal if both columns have the same value:
145  return C1 == other.C1 && C2 == other.C2;
146  }
147 
148  // =: Assignment Operator.
149  row_2_c& operator=(const row_2_c& other) {
150  // Copy data values:
151  C1 = other.C1;
152  C2 = other.C2;
153 
154  // Set Magic number:
155  CHECK_CODE(Magic = ROW_2_MAGIC);
156 
157  // Return this object (standard interface of assignment op):
158  return *this;
159  }
160 
161  // Create a string representation in a given buffer:
162  void to_string(char *buf, int buf_len) const;
163 
164 //-----------------------------------------------------------------------------
165 // Debugging Support:
166 //-----------------------------------------------------------------------------
167 
168 #if VER_DEBUG
169  public:
170  // Integrity check:
171  str_t check() const;
172 
173  private:
174  // Magic number (identifies objects of this class for debugging).
175  long Magic; // Must be "ROW_2_MAGIC".
176 #endif
177 
178 #if VER_DUMP
179  public:
180  // Display data structure:
181  void dump(str_t headline = STR_NULL) const;
182 #endif
183 
184 
185 //-----------------------------------------------------------------------------
186 // Private Object Members:
187 //-----------------------------------------------------------------------------
188 
189  private:
190 
191  // Column values:
192  int C1;
193  int C2;
194 
195 //=============================================================================
196 // End of Class:
197 //=============================================================================
198 
199 };
200 
201 //-----------------------------------------------------------------------------
202 // Define pointer type:
203 //-----------------------------------------------------------------------------
204 
205 typedef row_2_c *row_2_t;
206 
207 //-----------------------------------------------------------------------------
208 // Define null pointer:
209 //-----------------------------------------------------------------------------
210 
211 #define ROW_2_NULL (static_cast<row_2_t>(0))
212 
213 //=============================================================================
214 // End of Include File:
215 //=============================================================================
216 
217 #endif
218 
#define CHECK_CODE(CODE)
Definition: check.h:167
#define CHECK_VALID(EX)
Definition: check.h:85
const char * str_t
Definition: str.h:41
Definition: row_2.h:66
#define STR_NULL
Definition: str.h:52
#define CHECK(EX, MSG)
Definition: check.h:69