Orbiter 2022
Combinatorial Objects
algorithms.cpp
Go to the documentation of this file.
1/*
2 * algorithms.cpp
3 *
4 * Created on: Jan 14, 2022
5 * Author: betten
6 */
7
8
9
10
11#include "foundations.h"
12
13using namespace std;
14
15
16
17//#include "pstdint.h" /* Replace with <stdint.h> if appropriate */
18#include <stdint.h>
19
20
21namespace orbiter {
22namespace layer1_foundations {
23namespace data_structures {
24
25
26
27
29{
30
31}
32
34{
35
36}
37
38
39//#define HASH_PRIME ((int) 1 << 30 - 1)
40#define HASH_PRIME 174962718
41
42int algorithms::hashing(int hash0, int a)
43{
44 int h = hash0; // a1 = a;
45
46 do {
47 h <<= 1;
48 if (ODD(a)){
49 h++;
50 }
51 h = h % HASH_PRIME; // h %= HASH_PRIME;
52 a >>= 1;
53 } while (a);
54 //cout << "hashing: " << hash0 << " + " << a1 << " = " << h << endl;
55 return h;
56}
57
58int algorithms::hashing_fixed_width(int hash0, int a, int bit_length)
59{
60 int h = hash0;
61 int a1 = a;
62 int i;
63
64 for (i = 0; i < bit_length; i++) {
65 h <<= 1;
66 if (ODD(a)){
67 h++;
68 }
69 h = h % HASH_PRIME; // h %= HASH_PRIME;
70 a >>= 1;
71 }
72 if (a) {
73 cout << "algorithms::hashing_fixed_width a is not zero" << endl;
74 cout << "a=" << a1 << endl;
75 cout << "bit_length=" << bit_length << endl;
76 exit(1);
77 }
78 //cout << "hashing: " << hash0 << " + " << a1 << " = " << h << endl;
79 return h;
80}
81
82void algorithms::uchar_print_bitwise(std::ostream &ost, uchar u)
83{
84 uchar mask;
85 int i;
86
87 for (i = 0; i < 8; i++) {
88 mask = ((uchar) 1) << i;
89 if (u & mask) {
90 ost << "1";
91 }
92 else {
93 ost << "0";
94 }
95 }
96}
97
98void algorithms::uchar_move(uchar *p, uchar *q, int len)
99{
100 int i;
101
102 for (i = 0; i < len; i++) {
103 *q++ = *p++;
104 }
105}
106
107void algorithms::int_swap(int& x, int& y)
108{
109 int z;
110
111 z = x;
112 x = y;
113 y = z;
114}
115
116void algorithms::print_pointer_hex(std::ostream &ost, void *p)
117{
118 void *q = p;
119 uchar *pp = (uchar *)&q;
120 int i, a, low, high;
121
122 ost << "0x";
123 for (i = (int)sizeof(pvoid) - 1; i >= 0; i--) {
124 a = (int)pp[i];
125 //cout << " a=" << a << " ";
126 low = a % 16;
127 high = a / 16;
128 print_hex_digit(ost, high);
129 print_hex_digit(ost, low);
130 }
131}
132
133void algorithms::print_hex_digit(std::ostream &ost, int digit)
134{
135 if (digit < 10) {
136 ost << (char)('0' + digit);
137 }
138 else if (digit < 16) {
139 ost << (char)('a' + (digit - 10));
140 }
141 else {
142 cout << "algorithms::print_hex_digit illegal digit " << digit << endl;
143 exit(1);
144 }
145}
146
147void algorithms::print_repeated_character(std::ostream &ost, char c, int n)
148{
149 int i;
150
151 for (i = 0; i < n; i++) {
152 ost << c;
153 }
154}
155
156uint32_t algorithms::root_of_tree_uint32_t (uint32_t* S, uint32_t i)
157{
158 while (S[i] != i) {
159 i = S[i];
160 }
161 return i;
162}
163
165 int nb_rows, int nb_cols, int nb_needed,
166 int f_has_Rhs, int *Rhs,
167 long int *&Solutions, int &nb_sol, long int &nb_backtrack, int &dt,
168 int f_DLX,
169 int verbose_level)
170// allocates Solutions[nb_sol * nb_needed]
171{
172 int f_v = (verbose_level >= 1);
175 int t0 = Os.os_ticks();
176
177 if (f_v) {
178 cout << "algorithms::solve_diophant nb_rows=" << nb_rows << " nb_cols="
179 << nb_cols << " f_has_Rhs=" << f_has_Rhs
180 << " verbose_level=" << verbose_level << endl;
181 cout << "f_DLX=" << f_DLX << endl;
182 //int_matrix_print(Inc, nb_rows, nb_cols);
183 }
185
186 if (f_has_Rhs) {
188 nb_cols, Inc, nb_needed,
189 Rhs,
190 0 /* verbose_level */);
191 }
192 else {
193 Dio->init_problem_of_Steiner_type(nb_rows,
194 nb_cols, Inc, nb_needed,
195 0 /* verbose_level */);
196 }
197
198 if (FALSE /*f_v4*/) {
199 Dio->print();
200 }
201
202 if (f_DLX && !f_has_Rhs) {
203 Dio->solve_all_DLX(0 /* verbose_level*/);
204 nb_backtrack = Dio->nb_steps_betten;
205 }
206 else {
207 Dio->solve_all_mckay(nb_backtrack, INT_MAX, verbose_level - 2);
208 }
209
210 nb_sol = Dio->_resultanz;
211 if (nb_sol) {
212 Dio->get_solutions(Solutions, nb_sol, 1 /* verbose_level */);
213 if (FALSE /*f_v4*/) {
214 cout << "Solutions:" << endl;
215 Lint_matrix_print(Solutions, nb_sol, nb_needed);
216 }
217 }
218 else {
219 Solutions = NULL;
220 }
221 FREE_OBJECT(Dio);
222 int t1 = Os.os_ticks();
223 dt = t1 - t0;
224 if (f_v) {
225 cout << "algorithms::solve_diophant done nb_sol=" << nb_sol
226 << " nb_backtrack=" << nb_backtrack << " dt=" << dt << endl;
227 }
228}
229
230#undef get16bits
231#if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \
232 || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
233#define get16bits(d) (*((const uint16_t *) (d)))
234#endif
235
236#if !defined (get16bits)
237#define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
238 +(uint32_t)(((const uint8_t *)(d))[0]) )
239#endif
240
241
242uint32_t algorithms::SuperFastHash (const char * data, int len)
243{
244uint32_t hash = len, tmp;
245int rem;
246
247 if (len <= 0 || data == 0) return 0;
248
249 rem = len & 3;
250 len >>= 2;
251
252 /* Main loop */
253 for (;len > 0; len--) {
254 hash += get16bits (data);
255 tmp = (get16bits (data+2) << 11) ^ hash;
256 hash = (hash << 16) ^ tmp;
257 data += 2*sizeof (uint16_t);
258 hash += hash >> 11;
259 }
260
261 /* Handle end cases */
262 switch (rem) {
263 case 3: hash += get16bits (data);
264 hash ^= hash << 16;
265 hash ^= ((signed char)data[sizeof (uint16_t)]) << 18;
266 hash += hash >> 11;
267 break;
268 case 2: hash += get16bits (data);
269 hash ^= hash << 11;
270 hash += hash >> 17;
271 break;
272 case 1: hash += (signed char)*data;
273 hash ^= hash << 10;
274 hash += hash >> 1;
275 }
276
277 /* Force "avalanching" of final 127 bits */
278 hash ^= hash << 3;
279 hash += hash >> 5;
280 hash ^= hash << 4;
281 hash += hash >> 17;
282 hash ^= hash << 25;
283 hash += hash >> 6;
284
285 return hash;
286}
287
288
289
290}}}
291
#define HASH_PRIME
Definition: algorithms.cpp:40
#define get16bits(d)
Definition: algorithms.cpp:237
uint32_t SuperFastHash(const char *data, int len)
Definition: algorithms.cpp:242
int hashing_fixed_width(int hash0, int a, int bit_length)
Definition: algorithms.cpp:58
void uchar_print_bitwise(std::ostream &ost, uchar u)
Definition: algorithms.cpp:82
uint32_t root_of_tree_uint32_t(uint32_t *S, uint32_t i)
Definition: algorithms.cpp:156
void print_pointer_hex(std::ostream &ost, void *p)
Definition: algorithms.cpp:116
void print_repeated_character(std::ostream &ost, char c, int n)
Definition: algorithms.cpp:147
void print_hex_digit(std::ostream &ost, int digit)
Definition: algorithms.cpp:133
void solve_diophant(int *Inc, int nb_rows, int nb_cols, int nb_needed, int f_has_Rhs, int *Rhs, long int *&Solutions, int &nb_sol, long int &nb_backtrack, int &dt, int f_DLX, int verbose_level)
Definition: algorithms.cpp:164
diophantine systems of equations (i.e., linear systems over the integers)
Definition: solvers.h:209
void get_solutions(long int *&Sol, int &nb_sol, int verbose_level)
Definition: diophant.cpp:1062
void init_problem_of_Steiner_type_with_RHS(int nb_rows, int nb_cols, int *Inc, int nb_to_select, int *Rhs, int verbose_level)
Definition: diophant.cpp:375
int solve_all_mckay(long int &nb_backtrack_nodes, int maxresults, int verbose_level)
Definition: diophant.cpp:1435
void init_problem_of_Steiner_type(int nb_rows, int nb_cols, int *Inc, int nb_to_select, int verbose_level)
Definition: diophant.cpp:405
#define Lint_matrix_print(A, B, C)
Definition: foundations.h:708
unsigned char uchar
Definition: foundations.h:204
#define NEW_OBJECT(type)
Definition: foundations.h:638
#define FREE_OBJECT(p)
Definition: foundations.h:651
#define FALSE
Definition: foundations.h:234
#define ODD(x)
Definition: foundations.h:222
void * pvoid
Definition: foundations.h:206
the orbiter library for the classification of combinatorial objects