Orbiter 2022
Combinatorial Objects
classify_bitvectors.cpp
Go to the documentation of this file.
1// classify_bitvectors.cpp
2//
3// Anton Betten
4//
5// December 23, 2017
6
7
8
9
10#include "foundations.h"
11
12using namespace std;
13
14namespace orbiter {
15namespace layer1_foundations {
16namespace data_structures {
17
18
19static int compare_func_for_bitvectors(void *a, void *b, void *data);
20
21
23{
24 null();
25}
26
28{
29 freeself();
30}
31
33{
34 nb_types = 0;
35 Type_data = NULL;
36 Type_rep = NULL;
37 Type_mult = NULL;
38 Type_extra_data = NULL;
39 rep_len = 0;
40 n = 0;
41 N = 0;
42 type_of = NULL;
43 C_type_of = NULL;
44 perm = NULL;
45}
46
48{
49 int i;
50
51 if (Type_data) {
52 for (i = 0; i < nb_types; i++) {
54 }
56 }
57 if (Type_extra_data) {
58 for (i = 0; i < nb_types; i++) {
59 //FREE_uchar(Type_data[i]);
60 }
62 }
63 if (Type_rep) {
65 }
66 if (Type_mult) {
68 }
69 if (type_of) {
71 }
72 if (C_type_of) {
74 }
75 if (perm) {
77 }
78 null();
79}
80
81
82void classify_bitvectors::init(int N, int rep_len, int verbose_level)
83{
84 int f_v = (verbose_level >= 1);
85 int i;
86
87 if (f_v) {
88 cout << "classify_bitvectors::init, N=" << N << endl;
89 }
96 type_of = NEW_int(N);
97 for (i = 0; i < N; i++) {
98 type_of[i] = -1;
99 }
100 nb_types = 0;
101 n = 0;
102 C_type_of = NULL;
103 perm = NULL;
104
105 if (f_v) {
106 cout << "classify_bitvectors::init done" << endl;
107 }
108}
109
111 int &idx, int verbose_level)
112{
113 int f_v = (verbose_level >= 1);
114 int ret;
115 sorting Sorting;
116
117 if (f_v) {
118 cout << "classify_bitvectors::search" << endl;
119 }
120 if (Sorting.vec_search((void **) Type_data,
121 compare_func_for_bitvectors, (void *) this,
122 nb_types, data, idx, 0 /*verbose_level - 1*/)) {
123 ret = TRUE;
124 }
125 else {
126 ret = FALSE;
127 }
128 if (f_v) {
129 cout << "classify_bitvectors::search done ret=" << ret << endl;
130 }
131 return ret;
132}
133
135 void *extra_data, int &f_found, int &idx, int verbose_level)
136{
137 int f_v = (verbose_level >= 1);
138 sorting Sorting;
139
140 if (f_v) {
141 cout << "classify_bitvectors::add rep_len=" << rep_len << endl;
142 }
143
144 if (n >= N) {
145 cout << "classify_bitvectors::add n >= N" << endl;
146 cout << "n=" << n << endl;
147 cout << "N=" << N << endl;
148 exit(1);
149 }
150 if (Sorting.vec_search((void **) Type_data,
151 compare_func_for_bitvectors, (void *) this,
152 nb_types, data, idx,
153 0 /*verbose_level - 2*/)) {
154 if (f_v) {
155 cout << "classify_bitvectors::add vec_search returns TRUE, idx=" << idx << endl;
156 }
157 type_of[n] = idx;
158 Type_mult[idx]++;
159 f_found = TRUE;
160 }
161 else {
162 if (f_v) {
163 cout << "classify_bitvectors::add vec_search returns FALSE, new bitvector, before add_at_idx" << endl;
164 }
165 add_at_idx(data, extra_data, idx, 0/*verbose_level*/);
166 if (f_v) {
167 cout << "classify_bitvectors::add vec_search returns FALSE, new bitvector, after add_at_idx" << endl;
168 }
169 f_found = FALSE;
170 }
171 n++;
172
173
174 if (f_v) {
175 cout << "classify_bitvectors::add done, nb_types="
176 << nb_types << endl;
177 }
178}
179
181{
182 int ret;
183
184 ret = compare_func_for_bitvectors((void **) Type_data[idx], data, (void *) this);
185 return ret;
186}
187
189 void *extra_data, int idx, int verbose_level)
190{
191 int f_v = (verbose_level >= 1);
192 int i;
193 sorting Sorting;
194
195 if (f_v) {
196 cout << "classify_bitvectors::add_at_idx" << endl;
197 }
198 for (i = nb_types; i > idx; i--) {
199 Type_data[i] = Type_data[i - 1];
201 Type_rep[i] = Type_rep[i - 1];
202 Type_mult[i] = Type_mult[i - 1];
203 }
205 for (i = 0; i < rep_len; i++) {
206 Type_data[idx][i] = data[i];
207 }
208 Type_extra_data[idx] = extra_data;
209 Type_rep[idx] = n;
210 Type_mult[idx] = 1;
211 nb_types++;
212 for (i = 0; i < n; i++) {
213 if (type_of[i] >= idx) {
214 type_of[i]++;
215 }
216 }
217 type_of[n] = idx;
218}
219
220
221void classify_bitvectors::finalize(int verbose_level)
222{
223 int f_v = (verbose_level >= 1);
224 sorting Sorting;
225
226 if (f_v) {
227 cout << "classify_bitvectors::finalize" << endl;
228 }
230
231 if (f_v) {
232 cout << "classify_bitvectors::finalize type_of=";
233 Int_vec_print(cout, type_of, N);
234 cout << endl;
235 }
237 if (f_v) {
238 cout << "classify_bitvectors::finalize classification:" << endl;
239 C_type_of->print(TRUE /* f_backwards*/);
240 cout << endl;
241 }
242
243 int *v;
244 int i;
245
247 v = NEW_int(nb_types);
248 for (i = 0; i < nb_types; i++) {
249 perm[i] = i;
250 v[i] = Type_rep[i];
251 }
253
254 FREE_int(v);
255
256 if (f_v) {
257 cout << "classify_bitvectors::finalize done" << endl;
258 }
259}
260
262{
263 int i;
264
265 cout << "We found " << nb_types << " types:" << endl;
266 for (i = 0; i < nb_types; i++) {
267 cout << i << " : " << Type_rep[i]
268 << " : " << Type_mult[i] << " : ";
269
270#if 0
271 for (j = 0; j < rep_len; j++) {
272 cout << (int) Type_data[i][j];
273 if (j < rep_len - 1) {
274 cout << ", ";
275 }
276 }
277#endif
278 cout << endl;
279 }
280}
281
283{
284 int i, j;
285
286 cout << "We found " << nb_types << " types:" << endl;
287 for (i = 0; i < nb_types; i++) {
288 cout << i << " : " << Type_rep[i]
289 << " : " << Type_mult[i] << " : ";
290
291 for (j = 0; j < rep_len; j++) {
292 cout << (int) Type_data[i][j];
293 if (j < rep_len - 1) {
294 cout << ", ";
295 }
296 }
297 cout << endl;
298 }
299}
300
302 std::string &prefix,
303 void (*encode_function)(void *extra_data,
304 long int *&encoding, int &encoding_sz, void *global_data),
305 void (*get_group_order_or_NULL)(void *extra_data,
306 ring_theory::longinteger_object &go, void *global_data),
307 void *global_data,
308 int verbose_level)
309{
310 int f_v = (verbose_level >= 1);
311 string fname_txt;
312 string fname_csv;
313 int i, j;
314
315 if (f_v) {
316 cout << "classify_bitvectors::save" << endl;
317 }
318
319 fname_txt.assign(prefix);
320 fname_txt.append("_iso.txt");
321 fname_csv.assign(prefix);
322 fname_csv.append("_iso.csv");
323
324
325 if (perm == NULL) {
326 cout << "classify_bitvectors::save perm == NULL" << endl;
327 exit(1);
328 }
329 long int *Reps = NULL; // [nb_types * sz]
330 int sz = 0;
331
332
333 if (f_v) {
334 cout << "classify_bitvectors::save writing file "
335 << fname_txt << endl;
336 }
337 {
338 ofstream fp(fname_txt);
339 int h;
340
341 for (i = 0; i < nb_types; i++) {
342 j = perm[i];
343
344 long int *encoding;
345 int encoding_sz;
346
347 if (f_v) {
348 cout << "classify_bitvectors::save " << i << " / "
349 << nb_types << " j=" << j
350 << " before encode_function" << endl;
351 }
352 (*encode_function)(Type_extra_data[j],
353 encoding, encoding_sz, global_data);
354 if (f_v) {
355 cout << "classify_bitvectors::save " << i
356 << " / " << nb_types
357 << " encoding_sz=" << encoding_sz << endl;
358 }
359 fp << encoding_sz;
360 for (h = 0; h < encoding_sz; h++) {
361 fp << " " << encoding[h];
362 }
363 if (get_group_order_or_NULL) {
365
366 (*get_group_order_or_NULL)(Type_extra_data[j], go, global_data);
367 fp << " ";
369 }
370 fp << endl;
371 if (i == 0) {
372 sz = encoding_sz;
373 Reps = NEW_lint(nb_types * sz);
374 Lint_vec_copy(encoding, Reps, sz);
375 }
376 else {
377 if (encoding_sz != sz) {
378 cout << "encoding_sz != sz" << endl;
379 exit(1);
380 }
381 Lint_vec_copy(encoding, Reps + i * sz, sz);
382 }
383 FREE_lint(encoding);
384 }
385 fp << "-1 " << nb_types << " " << N << endl;
386 }
388
389 if (f_v) {
390 cout << "classify_bitvectors::save Written "
391 "file " << fname_txt
392 << " of size " << Fio.file_size(fname_txt)
393 << " with " << nb_types
394 << " orbit representatives obtained from "
395 << N << " candidates, encoding size = " << sz << endl;
396 }
397
398 if (f_v) {
399 cout << "classify_bitvectors::save writing "
400 "file " << fname_csv << endl;
401 }
402 Fio.lint_matrix_write_csv(fname_csv, Reps, nb_types, sz);
403
404 if (f_v) {
405 cout << "classify_bitvectors::save "
406 "Written file " << fname_csv
407 << " of size " << Fio.file_size(fname_csv)
408 << " with " << nb_types
409 << " orbit representatives obtained from "
410 << N << " candidates, encoding size = " << sz << endl;
411 }
412
413 if (Reps) {
414 FREE_lint(Reps);
415 }
416
417 if (f_v) {
418 cout << "classify_bitvectors::save done" << endl;
419 }
420}
421
422static int compare_func_for_bitvectors(void *a, void *b, void *data)
423{
425 uchar *A = (uchar *) a;
426 uchar *B = (uchar *) b;
427 int i;
428
429 //cout << "compare_func_for_bitvectors CB->rep_len=" << CB->rep_len << endl;
430 for (i = 0; i < CB->rep_len; i++) {
431 //cout << "i = " << i << " A[i]=" << (int) A[i] << " B[i]=" << (int) B[i] << endl;
432 if (A[i] < B[i]) {
433 return -1;
434 }
435 if (A[i] > B[i]) {
436 return 1;
437 }
438 }
439 return 0;
440}
441
442}}}
443
444
445
classification of 0/1 matrices using canonical forms
void add_at_idx(uchar *data, void *extra_data, int idx, int verbose_level)
void search_and_add_if_new(uchar *data, void *extra_data, int &f_found, int &idx, int verbose_level)
void save(std::string &prefix, void(*encode_function)(void *extra_data, long int *&encoding, int &encoding_sz, void *global_data), void(*get_group_order_or_NULL)(void *extra_data, ring_theory::longinteger_object &go, void *global_data), void *global_data, int verbose_level)
a collection of functions related to sorted vectors
void int_vec_heapsort_with_log(int *v, int *w, int len)
Definition: sorting.cpp:1967
int vec_search(void **v, int(*compare_func)(void *a, void *b, void *data), void *data_for_compare, int len, void *a, int &idx, int verbose_level)
Definition: sorting.cpp:945
a statistical analysis of data consisting of single integers
void init(int *data, int data_length, int f_second, int verbose_level)
Definition: tally.cpp:72
void lint_matrix_write_csv(std::string &fname, long int *M, int m, int n)
Definition: file_io.cpp:1323
a class to represent arbitrary precision integers
Definition: ring_theory.h:366
#define Lint_vec_copy(A, B, C)
Definition: foundations.h:694
#define NEW_uchar(n)
Definition: foundations.h:634
#define FREE_uchar(p)
Definition: foundations.h:647
#define FREE_puchar(p)
Definition: foundations.h:649
#define FREE_int(p)
Definition: foundations.h:640
#define NEW_puchar(n)
Definition: foundations.h:636
unsigned char uchar
Definition: foundations.h:204
#define NEW_pvoid(n)
Definition: foundations.h:637
#define NEW_OBJECT(type)
Definition: foundations.h:638
#define FREE_OBJECT(p)
Definition: foundations.h:651
#define NEW_int(n)
Definition: foundations.h:625
#define FREE_pvoid(p)
Definition: foundations.h:650
#define TRUE
Definition: foundations.h:231
#define FALSE
Definition: foundations.h:234
#define FREE_lint(p)
Definition: foundations.h:642
#define NEW_lint(n)
Definition: foundations.h:628
#define Int_vec_print(A, B, C)
Definition: foundations.h:685
the orbiter library for the classification of combinatorial objects