Orbiter 2022
Combinatorial Objects
bt_key.cpp
Go to the documentation of this file.
1// bt_key.cpp
2//
3// Anton Betten
4// 27.11.2000
5// moved from D2 to ORBI Nov 15, 2007
6
7#include <string.h> // strncmp
8
10#include "discreta.h"
11
12using namespace std;
13
14
15namespace orbiter {
16namespace layer2_discreta {
17
19{
20 k = BT_KEY;
21}
22
24 // copy constructor: this := x
25{
26 cout << "bt_key::copy constructor for object: "
27 << const_cast<discreta_base &>(x) << "\n";
28 const_cast<discreta_base &>(x).copyobject_to(*this);
29}
30
32 // copy assignment
33{
34 cout << "bt_key::operator = (copy assignment)" << endl;
35 copyobject(const_cast<discreta_base &>(x));
36 return *this;
37}
38
40{
41 OBJECTSELF s;
42
43 s = self;
44 new(this) bt_key;
45 self = s;
46 k = BT_KEY;
47}
48
50{
52}
53
55{
56 // cout << "group_selection::freeself_bt_key()\n";
58}
59
61{
62 return BT_KEY;
63}
64
66{
67#ifdef COPY_VERBOSE
68 cout << "bt_key::copyobject_to()\n";
69 print_as_vector(cout);
70#endif
73#ifdef COPY_VERBOSE
74 x.as_bt_key().print_as_vector(cout);
75#endif
76}
77
78ostream& bt_key::print(ostream& ost)
79{
80
81 return ost;
82}
83
84void bt_key::init(enum bt_key_kind type, int output_size, int field1, int field2)
85{
86 m_l_n(7);
88
93 int_vec_first() = 0;
94 int_vec_len() = 0;
95 f_ascending() = TRUE;
96
97}
98
99void bt_key::init_int4(int field1, int field2)
100{
102}
103
104void bt_key::init_int2(int field1, int field2)
105{
107}
108
109void bt_key::init_string(int output_size, int field1, int field2)
110{
112}
113
114void bt_key::init_int4_vec(int field1, int field2, int vec_fst, int vec_len)
115{
117 bt_key::int_vec_first() = vec_fst;
118 bt_key::int_vec_len() = vec_len;
119}
120
121void bt_key::init_int2_vec(int field1, int field2, int vec_fst, int vec_len)
122{
124 bt_key::int_vec_first() = vec_fst;
125 bt_key::int_vec_len() = vec_len;
126}
127
128int bt_lexicographic_cmp(char *p1, char *p2)
129{
130 return strcmp(p1, p2);
131}
132
133int bt_key_int_cmp(char *p1, char *p2)
134{
135 int_4 *p_l1, *p_l2;
136
137 p_l1 = (int_4 *) p1;
138 p_l2 = (int_4 *) p2;
139 if (*p_l1 < *p_l2) {
140 return -1;
141 }
142 if (*p_l1 > *p_l2) {
143 return 1;
144 }
145 return 0;
146}
147
148int bt_key_int2_cmp(char *p1, char *p2)
149{
150 int_4 *p_l1, *p_l2;
151
152 p_l1 = (int_4 *) p1;
153 p_l2 = (int_4 *) p2;
154 if (*p_l1 < *p_l2) {
155 return -1;
156 }
157 if (*p_l1 > *p_l2) {
158 return 1;
159 }
160 if (p_l1[1] < p_l2[1]) {
161 return -1;
162 }
163 if (p_l1[1] > p_l2[1]) {
164 return 1;
165 }
166 return 0;
167}
168
169void bt_key_print_int4(char **key, ostream& ost)
170{
171 int_4 i;
172 bt_key_get_int4(key, i);
173 ost << i;
174}
175
176void bt_key_print_int2(char **key, ostream& ost)
177{
178 int_2 i;
179 bt_key_get_int2(key, i);
180 ost << i;
181}
182
183void bt_key_print(char *key, Vector& V, ostream& ost)
184{
185 char *the_key = key;
186 char c;
187 int i, j, l1, output_size;
188 enum bt_key_kind k;
189
190 ost << "[";
191 for (i = 0; i < V.s_l(); i++) {
192 bt_key& Key = V[i].as_bt_key();
193 k = Key.type();
194 output_size = Key.output_size();
195 if (k == bt_key_int) {
196 if (output_size == 4) {
197 bt_key_print_int4(&the_key, ost);
198 }
199 else if (output_size == 2) {
200 bt_key_print_int2(&the_key, ost);
201 }
202 else {
203 cout << "bt_key_print() output_size not 2 or 4" << endl;
204 exit(1);
205 }
206 }
207 else if (k == bt_key_string) {
208 for (j = 0; j < output_size; j++) {
209 if (the_key[j] == 0) {
210 break;
211 }
212 }
213 l1 = j;
214 for (j = 0; j < output_size; j++) {
215 if (j < l1)
216 c = *the_key;
217 else
218 c = ' ';
219 ost << c;
220 the_key++;
221 }
222 // ost << ends;
223 }
224 else if (k == bt_key_int_vec) {
225 ost << "(";
226 for (j = 0; j < Key.int_vec_len(); j++) {
227 if (output_size == 4) {
228 bt_key_print_int4(&the_key, ost);
229 }
230 else if (output_size == 2) {
231 bt_key_print_int2(&the_key, ost);
232 }
233 else {
234 cout << "bt_key_print() output_size not 2 or 4" << endl;
235 exit(1);
236 }
237 if (j < Key.int_vec_len())
238 ost << ", ";
239 }
240 ost << ")";
241 }
242 else {
243 cout << "bt_key_print() unknown bt_key_kind" << endl;
244 exit(1);
245 }
246 if (i < V.s_l() - 1)
247 ost << " ";
248 }
249 ost << "]";
250}
251
252int bt_key_compare_int4(char **p_key1, char **p_key2)
253{
254 int_4 int1, int2;
255 int i;
256 char c;
257 char *pc_1 = (char *) &int1;
258 char *pc_2 = (char *) &int2;
259
260 for (i = 0; i < 4; i++) {
261 c = **p_key1;
262 (*p_key1)++;
263 pc_1[i] = c;
264 c = **p_key2;
265 (*p_key2)++;
266 pc_2[i] = c;
267 }
268 if (int1 < int2) {
269 return -1;
270 }
271 if (int1 > int2) {
272 return 1;
273 }
274 return 0;
275}
276
277int bt_key_compare_int2(char **p_key1, char **p_key2)
278{
279 int_2 int1, int2;
280 int i;
281 char c;
282 char *pc_1 = (char *) &int1;
283 char *pc_2 = (char *) &int2;
284
285 for (i = 0; i < 2; i++) {
286 c = **p_key1;
287 (*p_key1)++;
288 pc_1[i] = c;
289 c = **p_key2;
290 (*p_key2)++;
291 pc_2[i] = c;
292 }
293 if (int1 < int2) {
294 return -1;
295 }
296 if (int1 > int2) {
297 return 1;
298 }
299 return 0;
300}
301
302int bt_key_compare(char *key1, char *key2, Vector& V, int depth)
303{
304 char *the_key1 = key1;
305 char *the_key2 = key2;
306 int i, j, output_size, res;
307 enum bt_key_kind k;
308
309 if (depth == 0)
310 depth = V.s_l();
311 for (i = 0; i < depth; i++) {
312 bt_key& Key = V[i].as_bt_key();
313 k = Key.type();
314 output_size = Key.output_size();
315 if (k == bt_key_int) {
316 if (output_size == 4) {
317 res = bt_key_compare_int4(&the_key1, &the_key2);
318 if (res)
319 return res;
320 }
321 else if (output_size == 2) {
322 res = bt_key_compare_int2(&the_key1, &the_key2);
323 if (res)
324 return res;
325 }
326 else {
327 cout << "bt_key_compare() output_size not 2 or 4" << endl;
328 exit(1);
329 }
330 }
331 else if (k == bt_key_string) {
332 res = strncmp(the_key1, the_key2, output_size);
333 if (res)
334 return res;
335 the_key1 += output_size;
336 the_key2 += output_size;
337 }
338 else if (k == bt_key_int_vec) {
339 for (j = 0; j < Key.int_vec_len(); j++) {
340 if (output_size == 4) {
341 res = bt_key_compare_int4(&the_key1, &the_key2);
342 if (res)
343 return res;
344 }
345 else if (output_size == 2) {
346 res = bt_key_compare_int2(&the_key1, &the_key2);
347 if (res)
348 return res;
349 }
350 else {
351 cout << "bt_key_compare() output_size not 2 or 4" << endl;
352 exit(1);
353 }
354 }
355 }
356 else {
357 cout << "bt_key_compare() unknown bt_key_kind" << endl;
358 exit(1);
359 }
360 }
361 return 0;
362}
363
364void bt_key_fill_in_int4(char **p_key, discreta_base& key_op)
365{
366 if (key_op.s_kind() != INTEGER) {
367 cout << "bt_key_fill_in_int4 "
368 "object not an INTEGER" << endl;
369 exit(1);
370 }
371 integer& key_op_int = key_op.as_integer();
372 int_4 a = (int_4) key_op_int.s_i();
373 int i;
374 char *pc = (char *) &a;
375 char c;
376
377 for (i = 0; i < 4; i++) {
378 c = pc[i];
379 **p_key = c;
380 (*p_key)++;
381 }
382}
383
384void bt_key_fill_in_int2(char **p_key, discreta_base& key_op)
385{
386 if (key_op.s_kind() != INTEGER) {
387 cout << "bt_key_fill_in_int2 "
388 "object not an INTEGER" << endl;
389 exit(1);
390 }
391 integer& key_op_int = key_op.as_integer();
392 int_2 a = key_op_int.s_i();
393 int i;
394 char *pc = (char *) &a;
395 char c;
396
397 for (i = 0; i < 2; i++) {
398 c = pc[i];
399 **p_key = c;
400 (*p_key)++;
401 }
402}
403
404void bt_key_fill_in_string(char **p_key, int output_size, discreta_base& key_op)
405{
406 if (key_op.s_kind() != HOLLERITH) {
407 cout << "bt_key_fill_in_string "
408 "object not an HOLLERITH" << endl;
409 exit(1);
410 }
411 hollerith& h = key_op.as_hollerith();
412 strncpy(*p_key, h.s(), output_size);
413 *p_key += output_size;
414}
415
416void bt_key_fill_in(char *key, Vector& V, Vector& the_object)
417{
418 char *the_key = key;
419 int i, j, output_size;
420 enum bt_key_kind k;
421
422 for (i = 0; i < V.s_l(); i++) {
423
424 if (the_key - key > BTREEMAXKEYLEN) {
425 cout << "bt_key_fill_in the_key - key > BTREEMAXKEYLEN" << endl;
426 cout << "BTREEMAXKEYLEN=" << BTREEMAXKEYLEN << endl;
427 cout << "the_key - key=" << the_key - key << endl;
428 exit(1);
429 }
430
431 bt_key& Key = V[i].as_bt_key();
432 k = Key.type();
433 output_size = Key.output_size();
434 discreta_base& key_object = the_object.s_i(Key.field1());
435
436 if (k == bt_key_int) {
437 if (output_size == 4) {
438 bt_key_fill_in_int4(&the_key, key_object);
439 }
440 else if (output_size == 2) {
441 bt_key_fill_in_int2(&the_key, key_object);
442 }
443 else {
444 cout << "bt_key_fill_in() output_size not 2 or 4" << endl;
445 exit(1);
446 }
447 }
448 else if (k == bt_key_string) {
449 bt_key_fill_in_string(&the_key, output_size, key_object);
450 }
451 else if (k == bt_key_int_vec) {
452 int fst = Key.int_vec_first();
453 Vector& key_vec = key_object.as_vector();
454 discreta_base *key_object1;
455 integer null_ob;
456 null_ob.m_i(0);
457
458 for (j = 0; j < Key.int_vec_len(); j++) {
459 if (fst + j < key_vec.s_l())
460 key_object1 = &key_vec[fst + j];
461 else
462 key_object1 = &null_ob;
463 if (output_size == 4) {
464 bt_key_fill_in_int4(&the_key, *key_object1);
465 }
466 else if (output_size == 2) {
467 bt_key_fill_in_int2(&the_key, *key_object1);
468 }
469 else {
470 cout << "bt_key_fill_in output_size not 2 or 4" << endl;
471 exit(1);
472 }
473 }
474 }
475 else {
476 cout << "bt_key_fill_in unknown bt_key_kind" << endl;
477 exit(1);
478 }
479 }
480}
481
482void bt_key_get_int4(char **key, int_4 &i)
483{
484 char *pc = (char *)&i;
485
486 pc[0] = **key; (*key)++;
487 pc[1] = **key; (*key)++;
488 pc[2] = **key; (*key)++;
489 pc[3] = **key; (*key)++;
490}
491
492void bt_key_get_int2(char **key, int_2 &i)
493{
494 char *pc = (char *)&i;
495
496 pc[0] = **key; (*key)++;
497 pc[1] = **key; (*key)++;
498}
499
500}}
501
DISCRETA vector class for vectors of DISCRETA objects.
Definition: discreta.h:797
discreta_base & s_i(int i)
Definition: vector.cpp:202
void copyobject_to(discreta_base &x)
Definition: vector.cpp:72
DISCRETA class for databases.
Definition: discreta.h:1442
bt_key & operator=(const discreta_base &x)
Definition: bt_key.cpp:31
void init_string(int output_size, int field1, int field2)
Definition: bt_key.cpp:109
void init_int2_vec(int field1, int field2, int vec_fst, int vec_len)
Definition: bt_key.cpp:121
void copyobject_to(discreta_base &x)
Definition: bt_key.cpp:65
void init_int2(int field1, int field2)
Definition: bt_key.cpp:104
enum bt_key_kind & type()
Definition: discreta.h:1458
void init(enum bt_key_kind type, int output_size, int field1, int field2)
Definition: bt_key.cpp:84
void init_int4_vec(int field1, int field2, int vec_fst, int vec_len)
Definition: bt_key.cpp:114
std::ostream & print(std::ostream &)
Definition: bt_key.cpp:78
void init_int4(int field1, int field2)
Definition: bt_key.cpp:99
DISCRETA base class. All DISCRETA classes are derived from this class.
Definition: discreta.h:382
void copyobject(discreta_base &x)
Definition: base.cpp:194
DISCRETA string class.
Definition: discreta.h:626
DISCRETA integer class.
Definition: discreta.h:667
#define BTREEMAXKEYLEN
Definition: discreta.h:1490
int int_4
Definition: foundations.h:181
#define TRUE
Definition: foundations.h:231
short int_2
Definition: foundations.h:180
void bt_key_print_int4(char **key, ostream &ost)
Definition: bt_key.cpp:169
int bt_key_compare_int4(char **p_key1, char **p_key2)
Definition: bt_key.cpp:252
int bt_key_int2_cmp(char *p1, char *p2)
Definition: bt_key.cpp:148
int bt_lexicographic_cmp(char *p1, char *p2)
Definition: bt_key.cpp:128
void bt_key_get_int2(char **key, int_2 &i)
Definition: bt_key.cpp:492
int bt_key_int_cmp(char *p1, char *p2)
Definition: bt_key.cpp:133
void bt_key_print_int2(char **key, ostream &ost)
Definition: bt_key.cpp:176
void bt_key_fill_in(char *key, Vector &V, Vector &the_object)
Definition: bt_key.cpp:416
int bt_key_compare_int2(char **p_key1, char **p_key2)
Definition: bt_key.cpp:277
void bt_key_fill_in_int2(char **p_key, discreta_base &key_op)
Definition: bt_key.cpp:384
void bt_key_fill_in_int4(char **p_key, discreta_base &key_op)
Definition: bt_key.cpp:364
@ HOLLERITH
HOLLERITH.
Definition: discreta.h:71
void bt_key_fill_in_string(char **p_key, int output_size, discreta_base &key_op)
Definition: bt_key.cpp:404
void bt_key_get_int4(char **key, int_4 &i)
Definition: bt_key.cpp:482
void bt_key_print(char *key, Vector &V, ostream &ost)
Definition: bt_key.cpp:183
int bt_key_compare(char *key1, char *key2, Vector &V, int depth)
Definition: bt_key.cpp:302
the orbiter library for the classification of combinatorial objects
DISCRETA internal class.
Definition: discreta.h:353