Orbiter 2022
Combinatorial Objects
lint_vec.cpp
Go to the documentation of this file.
1/*
2 * lint_vec.cpp
3 *
4 * Created on: Apr 14, 2021
5 * Author: betten
6 */
7
8
9
10#include "foundations.h"
11
12
13using namespace std;
14
15
16namespace orbiter {
17namespace layer1_foundations {
18namespace data_structures {
19
20
22{
23
24}
25
27{
28
29}
30
31
32void lint_vec::apply(long int *from, long int *through, long int *to, int len)
33{
34 int i;
35
36 for (i = 0; i < len; i++) {
37 to[i] = through[from[i]];
38 }
39}
40
41void lint_vec::take_away(long int *v, int &len,
42 long int *take_away, int nb_take_away)
43 // v must be sorted
44{
45 int i, j, idx;
46 sorting Sorting;
47
48 for (i = 0; i < nb_take_away; i++) {
49 if (!Sorting.lint_vec_search(v, len, take_away[i], idx, 0)) {
50 continue;
51 }
52 for (j = idx; j < len; j++) {
53 v[j] = v[j + 1];
54 }
55 len--;
56 }
57}
58
59
60void lint_vec::zero(long int *v, long int len)
61{
62 int i;
63 long int *p;
64
65 for (p = v, i = 0; i < len; p++, i++) {
66 *p = 0;
67 }
68}
69
70void lint_vec::mone(long int *v, long int len)
71{
72 int i;
73 long int *p;
74
75 for (p = v, i = 0; i < len; p++, i++) {
76 *p = -1;
77 }
78}
79
80void lint_vec::copy(long int *from, long int *to, long int len)
81{
82 int i;
83 long int *p, *q;
84
85 for (p = from, q = to, i = 0; i < len; p++, q++, i++) {
86 *q = *p;
87 }
88}
89
90void lint_vec::copy_to_int(long int *from, int *to, long int len)
91{
92 int i;
93 long int *p;
94 int *q;
95
96 for (p = from, q = to, i = 0; i < len; p++, q++, i++) {
97 *q = *p;
98 }
99}
100
101void lint_vec::complement(long int *v, long int *w, int n, int k)
102// computes the complement of v[k] in the set {0,...,n-1} to w[n - k]
103{
104 long int j1, j2, i;
105
106 j1 = 0;
107 j2 = 0;
108 for (i = 0; i < n; i++) {
109 if (j1 < k && v[j1] == i) {
110 j1++;
111 continue;
112 }
113 w[j2] = i;
114 j2++;
115 }
116 if (j2 != n - k) {
117 cout << "lint_vec::complement j2 != n - k" << endl;
118 exit(1);
119 }
120}
121
122
123
124long int lint_vec::minimum(long int *v, int len)
125{
126 long int i, m;
127
128 if (len == 0) {
129 cout << "lint_vec::minimum len == 0" << endl;
130 exit(1);
131 }
132 m = v[0];
133 for (i = 1; i < len; i++) {
134 if (v[i] < m) {
135 m = v[i];
136 }
137 }
138 return m;
139}
140
141long int lint_vec::maximum(long int *v, int len)
142{
143 long int m, i;
144
145 if (len == 0) {
146 cout << "lint_vec::maximum len == 0" << endl;
147 exit(1);
148 }
149 m = v[0];
150 for (i = 1; i < len; i++) {
151 if (v[i] > m) {
152 m = v[i];
153 }
154 }
155 return m;
156}
157
158
159void lint_vec::matrix_print_width(std::ostream &ost,
160 long int *p, int m, int n, int dim_n, int w)
161{
162 int i, j;
163
164 for (i = 0; i < m; i++) {
165 for (j = 0; j < n; j++) {
166 ost << setw((int) w) << p[i * dim_n + j];
167 if (w) {
168 ost << " ";
169 }
170 }
171 ost << endl;
172 }
173}
174
175
176int lint_vec::matrix_max_log_of_entries(long int *p, int m, int n)
177{
178 int i, j;
179 long a, w = 1, w1;
181
182 for (i = 0; i < m; i++) {
183 for (j = 0; j < n; j++) {
184 a = p[i * n + j];
185 if (a > 0) {
186 w1 = NT.lint_log10(a);
187 }
188 else if (a < 0) {
189 w1 = NT.lint_log10(-a) + 1;
190 }
191 else {
192 w1 = 1;
193 }
194 w = MAXIMUM(w, w1);
195 }
196 }
197 return w;
198}
199
200
201
202
203void lint_vec::matrix_print(long int *p, int m, int n)
204{
205 int w;
206
207 w = matrix_max_log_of_entries(p, m, n);
208 matrix_print(p, m, n, w);
209}
210
211void lint_vec::matrix_print(long int *p, int m, int n, int w)
212{
213 int i, j;
214
215 for (i = 0; i < m; i++) {
216 for (j = 0; j < n; j++) {
217 cout << setw((int) w) << p[i * n + j];
218 if (w) {
219 cout << " ";
220 }
221 }
222 cout << endl;
223 }
224}
225
226void lint_vec::set_print(long int *v, int len)
227{
228 set_print(cout, v, len);
229}
230
231
232void lint_vec::set_print(std::ostream &ost, long int *v, int len)
233{
234 int i;
235
236 ost << "{ ";
237 for (i = 0; i < len; i++) {
238 ost << v[i];
239 if (i < len - 1) {
240 ost << ", ";
241 }
242 }
243 ost << " }";
244}
245
246void lint_vec::print(std::ostream &ost, long int *v, int len)
247{
248 int i;
249
250 if (len > 50) {
251 ost << "( ";
252 for (i = 0; i < 50; i++) {
253 ost << v[i];
254 if (i < len - 1) {
255 ost << ", ";
256 }
257 }
258 ost << "...";
259 for (i = len - 3; i < len; i++) {
260 ost << v[i];
261 if (i < len - 1) {
262 ost << ", ";
263 }
264 }
265 ost << " )";
266 }
267 else {
268 print_fully(ost, v, len);
269 }
270}
271
272void lint_vec::print(std::ostream &ost, std::vector<long int> &v)
273{
274 int i, len;
275
276 len = v.size();
277 if (len > 50) {
278 ost << "( ";
279 for (i = 0; i < 50; i++) {
280 ost << v[i];
281 if (i < len - 1) {
282 ost << ", ";
283 }
284 }
285 ost << "...";
286 for (i = len - 3; i < len; i++) {
287 ost << v[i];
288 if (i < len - 1) {
289 ost << ", ";
290 }
291 }
292 ost << " )";
293 }
294 else {
295 print_fully(ost, v);
296 }
297}
298
299void lint_vec::print_as_table(std::ostream &ost, long int *v, int len, int width)
300{
301 int i;
302
303 for (i = 0; i < len; i++) {
304 ost << v[i];
305 if (i < len - 1) {
306 ost << ", ";
307 }
308 if (((i + 1) % 10) == 0) {
309 ost << endl;
310 }
311 }
312 ost << endl;
313}
314
315void lint_vec::print_bare_fully(std::ostream &ost, long int *v, int len)
316{
317 int i;
318
319 //ost << "( ";
320 for (i = 0; i < len; i++) {
321 ost << v[i];
322 if (i < len - 1) {
323 ost << ",";
324 }
325 }
326 //ost << " )";
327}
328
329void lint_vec::print_fully(std::ostream &ost, long int *v, int len)
330{
331 int i;
332
333 ost << "( ";
334 for (i = 0; i < len; i++) {
335 ost << v[i];
336 if (i < len - 1) {
337 ost << ", ";
338 }
339 }
340 ost << " )";
341}
342
343void lint_vec::print_fully(std::ostream &ost, std::vector<long int> &v)
344{
345 int i, len;
346
347 len = v.size();
348 ost << "( ";
349 for (i = 0; i < len; i++) {
350 ost << v[i];
351 if (i < len - 1) {
352 ost << ", ";
353 }
354 }
355 ost << " )";
356}
357
358
359void lint_vec::scan(std::string &s, long int *&v, int &len)
360{
361 istringstream ins(s);
362 scan_from_stream(ins, v, len);
363}
364
365
366void lint_vec::scan(const char *s, long int *&v, int &len)
367{
368 istringstream ins(s);
369 scan_from_stream(ins, v, len);
370}
371
372void lint_vec::scan_from_stream(istream & is, long int *&v, int &len)
373{
374 int verbose_level = 0;
375 int f_v = (verbose_level >= 1);
376 long int a;
377 char s[10000], c;
378 int l, h;
379
380 if (f_v) {
381 cout << "lint_vec::scan_from_stream" << endl;
382 }
383 len = 20;
384 v = NEW_lint(len);
385 h = 0;
386 l = 0;
387
388 while (TRUE) {
389 if (!is) {
390 len = h;
391 if (f_v) {
392 cout << "lint_vec::scan_from_stream done" << endl;
393 }
394 return;
395 }
396 l = 0;
397 if (is.eof()) {
398 //cout << "breaking off because of eof" << endl;
399 len = h;
400 return;
401 }
402 is >> c;
403 //c = get_character(is, verbose_level - 2);
404 if (c == 0) {
405 len = h;
406 if (f_v) {
407 cout << "lint_vec::scan_from_stream done" << endl;
408 }
409 return;
410 }
411 while (TRUE) {
412 // read digits:
413 //cout << "int_vec_scan_from_stream: \"" << c
414 //<< "\", ascii=" << (int)c << endl;
415 l = 0;
416 while (c != 0) {
417 if (c == ' ') {
418 is >> c;
419 if (f_v) {
420 cout << "lint_vec::scan_from_stream skipping space" << endl;
421 }
422 continue;
423 }
424 if (c == '-') {
425 //cout << "c='" << c << "'" << endl;
426 if (is.eof()) {
427 //cout << "breaking off because of eof" << endl;
428 break;
429 }
430 s[l++] = c;
431 is >> c;
432 //c = get_character(is, verbose_level - 2);
433 }
434 else if (c >= '0' && c <= '9') {
435 if (f_v) {
436 cout << "c='" << c << "'" << endl;
437 }
438 if (is.eof()) {
439 //cout << "breaking off because of eof" << endl;
440 break;
441 }
442 s[l++] = c;
443 is >> c;
444 //c = get_character(is, verbose_level - 2);
445 }
446 else {
447 //cout << "breaking off because c='" << c << "'" << endl;
448 break;
449 }
450 if (c == 0) {
451 break;
452 }
453 //cout << "int_vec_scan_from_stream inside loop: \""
454 //<< c << "\", ascii=" << (int)c << endl;
455 }
456 s[l] = 0;
457 if (f_v) {
458 cout << "lint_vec::scan_from_stream reading " << s << endl;
459 }
460 a = atol(s);
461 if (FALSE) {
462 cout << "digit as string: " << s
463 << ", numeric: " << a << endl;
464 }
465 if (h == len) {
466 len += 20;
467 long int *v2;
468
469 v2 = NEW_lint(len);
470 copy(v, v2, h);
471 FREE_lint(v);
472 v = v2;
473 }
474 v[h++] = a;
475 l = 0;
476 if (!is) {
477 len = h;
478 return;
479 }
480 if (c == 0) {
481 len = h;
482 return;
483 }
484 if (is.eof()) {
485 //cout << "breaking off because of eof" << endl;
486 len = h;
487 return;
488 }
489 is >> c;
490 //c = get_character(is, verbose_level - 2);
491 if (c == 0) {
492 len = h;
493 if (f_v) {
494 cout << "lint_vec::scan_from_stream done" << endl;
495 }
496 return;
497 }
498 }
499 }
500}
501
502void lint_vec::print_to_str(char *str, long int *data, int len)
503{
504 long int i, a;
505
506 str[0] = 0;
507 strcat(str, "\" ");
508 for (i = 0; i < len; i++) {
509 a = data[i];
510 sprintf(str + strlen(str), "%ld", a);
511 if (i < len - 1) {
512 strcat(str, ", ");
513 }
514 }
515 strcat(str, "\"");
516}
517
518void lint_vec::print_to_str_naked(char *str, long int *data, int len)
519{
520 long int i, a;
521
522 str[0] = 0;
523 for (i = 0; i < len; i++) {
524 a = data[i];
525 sprintf(str + strlen(str), "%ld", a);
526 if (i < len - 1) {
527 strcat(str, ", ");
528 }
529 }
530}
531
532
533void lint_vec::create_string_with_quotes(std::string &str, long int *v, int len)
534{
535 ostringstream s;
536 int i;
537
538 s << "\"";
539 for (i = 0; i < len; i++) {
540 s << v[i];
541 if (i < len - 1) {
542 s << ",";
543 }
544 }
545 s << "\"";
546 str.assign(s.str());
547}
548
549
550}}}
551
552
void copy_to_int(long int *from, int *to, long int len)
Definition: lint_vec.cpp:90
void matrix_print_width(std::ostream &ost, long int *p, int m, int n, int dim_n, int w)
Definition: lint_vec.cpp:159
void print_to_str(char *str, long int *data, int len)
Definition: lint_vec.cpp:502
void complement(long int *v, long int *w, int n, int k)
Definition: lint_vec.cpp:101
void scan(std::string &s, long int *&v, int &len)
Definition: lint_vec.cpp:359
void apply(long int *from, long int *through, long int *to, int len)
Definition: lint_vec.cpp:32
void print_bare_fully(std::ostream &ost, long int *v, int len)
Definition: lint_vec.cpp:315
void take_away(long int *v, int &len, long int *take_away, int nb_take_away)
Definition: lint_vec.cpp:41
int matrix_max_log_of_entries(long int *p, int m, int n)
Definition: lint_vec.cpp:176
void print_as_table(std::ostream &ost, long int *v, int len, int width)
Definition: lint_vec.cpp:299
void copy(long int *from, long int *to, long int len)
Definition: lint_vec.cpp:80
void scan_from_stream(std::istream &is, long int *&v, int &len)
Definition: lint_vec.cpp:372
void create_string_with_quotes(std::string &str, long int *v, int len)
Definition: lint_vec.cpp:533
void print_to_str_naked(char *str, long int *data, int len)
Definition: lint_vec.cpp:518
void print(std::ostream &ost, long int *v, int len)
Definition: lint_vec.cpp:246
void matrix_print(long int *p, int m, int n)
Definition: lint_vec.cpp:203
void print_fully(std::ostream &ost, long int *v, int len)
Definition: lint_vec.cpp:329
a collection of functions related to sorted vectors
int lint_vec_search(long int *v, int len, long int a, int &idx, int verbose_level)
Definition: sorting.cpp:1157
#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 MAXIMUM(x, y)
Definition: foundations.h:217
the orbiter library for the classification of combinatorial objects