Orbiter 2022
Combinatorial Objects
vector_hashing.cpp
Go to the documentation of this file.
1// vector_hashing.cpp
2//
3// Anton Betten
4//
5// started: October 14, 2008
6
7
8#include "foundations.h"
9
10using namespace std;
11
12
13namespace orbiter {
14namespace layer1_foundations {
15namespace data_structures {
16
17
19{
20 data_size = 0;
21 N = 0;
22 bit_length = 0;
23 vector_data = NULL;
24 H = NULL;
25 H_sorted = NULL;
26 perm = NULL;
27 perm_inv = NULL;
28 nb_types = 0;
29 type_first = NULL;
30 type_len = NULL;
31 type_value = NULL;
32
33}
34
36{
37 if (vector_data) {
39 vector_data = NULL;
40 }
41 if (H) {
42 FREE_int(H);
43 H = NULL;
44 }
45 if (H_sorted) {
47 H_sorted = NULL;
48 }
49 if (perm) {
51 perm = NULL;
52 }
53 if (perm_inv) {
55 perm_inv = NULL;
56 }
57 if (type_first) {
59 type_first = NULL;
60 }
61 if (type_len) {
63 type_len = NULL;
64 }
65 if (type_value) {
67 type_value = NULL;
68 }
69}
70
71void vector_hashing::allocate(int data_size, int N, int bit_length)
72{
77 H = NEW_int(N);
78}
79
80void vector_hashing::compute_tables(int verbose_level)
81{
82 int f_v = (verbose_level >= 1);
83 int f_vv = (verbose_level >= 2);
84 int i, j, idx;
85 sorting Sorting;
86
87
88 if (f_v) {
89 cout << "vector_hashing::compute_tables" << endl;
90 }
91 for (i = 0; i < N; i++) {
95 }
96#if 0
97 cout << "H:" << endl;
98 int_vec_print(cout, H, N);
99 cout << endl;
100#endif
101
104
105 if (f_v) {
106 cout << "N =" << N << endl;
107 cout << "nb_types=" << nb_types << endl;
108 }
110
111 for (i = 0; i < nb_types; i++) {
112 idx = type_first[i] + 0;
113 type_value[i] = H_sorted[idx];
114 }
115
116 //int_vec_sorting_permutation(H, N, perm, perm_inv,
117 //TRUE /* f_increasingly */);
118
119
120#if 0
121 for (i = 0; i < N; i++) {
122 H_sorted[perm[i]] = H[i];
123 }
124#endif
125
126
127#if 0
128 cout << "H sorted:" << endl;
129 int_vec_print(cout, H_sorted, N);
130 cout << endl;
131#endif
132 if (f_vv) {
133 cout << "vector_hashing::compute_tables() N=" << N
134 << " nb_types=" << nb_types << endl;
135 for (i = 0; i < nb_types; i++) {
136 if (type_len[i] == 1)
137 continue;
138 cout << i << " : "
139 << type_first[i] << " : "
140 << type_len[i]
141 << " : " << H_sorted[type_first[i]] << " : " << endl;
142 for (j = 0; j < type_len[i]; j++) {
143 idx = perm_inv[type_first[i] + j];
144 cout << "j=" << j << " index " << idx << endl;
145 cout << idx << " : ";
147 cout << " : " << H[idx] << endl;
148 }
149 }
150 }
151}
152
154{
155 int i, j, idx;
156
157 cout << "vector_hashing N=" << N << " nb_types=" << nb_types << endl;
158 cout << "data:" << endl;
159 for (i = 0; i < N; i++) {
160 cout << i << " : ";
162 cout << " : " << H[i] << endl;
163 }
164
165 cout << "H sorted:" << endl;
166 Int_vec_print(cout, H_sorted, N);
167 cout << endl;
168
169 cout << "types:" << endl;
170 for (i = 0; i < nb_types; i++) {
171 //if (type_len[i] == 1)
172 //continue;
173 cout << i << " : "
174 << type_first[i] << " : "
175 << type_len[i]
176 << " : " << H_sorted[type_first[i]] << " : " << endl;
177 for (j = 0; j < type_len[i]; j++) {
178 idx = perm_inv[type_first[i] + j];
179 cout << "j=" << j << " index " << idx << endl;
180 cout << idx << " : ";
182 cout << " : " << H[idx] << endl;
183 }
184 }
185 cout << "type_value:" << endl;
186 for (i = 0; i < nb_types; i++) {
187 cout << setw(4) << i << " : " << setw(10) << type_value[i] << endl;
188 }
189}
190
192{
193 int h, idx, f, l, i, I;
194 sorting Sorting;
195
197 if (!Sorting.int_vec_search(type_value, nb_types, h, idx)) {
198 cout << "vector_hashing::rank did not "
199 "find hash value h=" << h << endl;
200 exit(1);
201 }
202 f = type_first[idx];
203 l = type_len[idx];
204 for (i = 0; i < l; i++) {
205 I = f + i;
206 idx = perm_inv[I];
207 if (Sorting.int_vec_compare(vector_data + idx * data_size,
208 data, data_size) == 0) {
209 return idx;
210 }
211 }
212 cout << "vector_hashing::rank did not find "
213 "data f=" << f << " l=" << l << endl;
214 cout << "data:" << endl;
215 Int_vec_print(cout, data, data_size);
216 cout << endl;
217 cout << "hash h=" << h << endl;
218 cout << "idx=" << idx << endl;
219 for (i = 0; i < l; i++) {
220 I = f + i;
221 idx = perm_inv[I];
222 cout << I << " : " << idx << " : ";
224 cout << endl;
225 }
226 cout << endl;
227
228 print();
229
230 exit(1);
231
232}
233
234void vector_hashing::unrank(int rk, int *data)
235{
236 int i;
237
238 for (i = 0; i < data_size; i++) {
239 data[i] = vector_data[rk * data_size + i];
240 }
241}
242
243}}}
244
int hash(int *v, int len, int bit_length)
Definition: int_vec.cpp:1074
a collection of functions related to sorted vectors
int int_vec_search(int *v, int len, int a, int &idx)
Definition: sorting.cpp:1094
void int_vec_classify(int length, int *the_vec, int *&the_vec_sorted, int *&sorting_perm, int *&sorting_perm_inv, int &nb_types, int *&type_first, int *&type_len)
Definition: sorting.cpp:1503
void allocate(int data_size, int N, int bit_length)
#define FREE_int(p)
Definition: foundations.h:640
#define NEW_int(n)
Definition: foundations.h:625
#define Int_vec_print(A, B, C)
Definition: foundations.h:685
orbiter_kernel_system::orbiter_session * Orbiter
global Orbiter session
the orbiter library for the classification of combinatorial objects