Orbiter 2022
Combinatorial Objects
bitvector.cpp
Go to the documentation of this file.
1/*
2 * bitvector.cpp
3 *
4 * Created on: Sep 28, 2020
5 * Author: betten
6 */
7
8
9
10#include "foundations.h"
11
12using namespace std;
13
14namespace orbiter {
15namespace layer1_foundations {
16namespace data_structures {
17
18
20{
21 data = NULL;
22 length = 0;
23 allocated_length = 0;
24}
25
27{
28 if (data) {
29 FREE_uchar(data);
30 data = NULL;
31 }
32}
33
34void bitvector::allocate(long int length)
35{
36 long int i;
37
38 bitvector::length = length;
39 allocated_length = (length + 7) >> 3;
40 data = NEW_uchar(allocated_length);
41 for (i = 0; i < allocated_length; i++) {
42 data[i] = 0;
43 }
44}
45
47{
48 return length;
49}
50
52{
53 return allocated_length;
54}
55
57{
58 return data;
59}
60
61void bitvector::m_i(long int i, int a)
62{
63 long int ii, bit;
64 uchar mask;
65
66 ii = i >> 3;
67 bit = i & 7;
68 mask = ((uchar) 1) << bit;
69 uchar &x = data[ii];
70
71 //cout << "before: x = " << (int) x << endl;
72 if (a == 0) {
73 uchar not_mask = ~mask;
74 x &= not_mask;
75 }
76 else {
77 x |= mask;
78 }
79 //cout << "after: x = " << (int) x << endl;
80}
81
82void bitvector::set_bit(long int i)
83{
84 long int ii, bit;
85 uchar mask;
86
87 ii = i >> 3;
88 bit = i & 7;
89 mask = ((uchar) 1) << bit;
90 uchar &x = data[ii];
91 x |= mask;
92}
93
94int bitvector::s_i(long int i)
95// returns 0 or 1
96{
97 long int ii, bit;
98 uchar mask;
99
100 ii = i >> 3;
101 bit = i & 7;
102 mask = ((uchar) 1) << bit;
103 uchar &x = data[ii];
104 if (x & mask) {
105 return 1;
106 }
107 else {
108 return 0;
109 }
110}
111
112void bitvector::save(ofstream &fp)
113{
114 fp.write((char*) &length, sizeof(long int));
115 fp.write((char*) &allocated_length, sizeof(long int));
116 fp.write((char*) data, allocated_length);
117}
118
119void bitvector::load(ifstream &fp)
120{
121 fp.read((char*) &length, sizeof(long int));
122 fp.read((char*) &allocated_length, sizeof(long int));
123 data = NEW_uchar(allocated_length);
124 fp.read((char*) data, allocated_length);
125}
126
128{
130 uint32_t h;
131
132 h = Data.char_vec_hash((char*) data, allocated_length);
133 return h;
134}
135
137{
138 long int i;
139 int a;
140
141 for (i = 0; i < length; i++) {
142 a = s_i(i);
143 cout << a;
144 }
145 cout << endl;
146}
147
148}}}
149
150
a catch-all container class for everything related to data structures
#define NEW_uchar(n)
Definition: foundations.h:634
#define FREE_uchar(p)
Definition: foundations.h:647
unsigned char uchar
Definition: foundations.h:204
the orbiter library for the classification of combinatorial objects