1 | #include <MultiProbe.hxx> |
---|
2 | #include <cmath> |
---|
3 | |
---|
4 | enum Bitpos { |
---|
5 | bit1 = 1, |
---|
6 | bit2 = 2, |
---|
7 | bit3 = 4, |
---|
8 | bit4 = 8, |
---|
9 | bit5 = 16, |
---|
10 | bit6 = 32, |
---|
11 | bit7 = 64, |
---|
12 | bit8 = 128 |
---|
13 | }; |
---|
14 | |
---|
15 | Bitvector::Bitvector(int bits) { |
---|
16 | int i; |
---|
17 | |
---|
18 | num_of_bits = bits; |
---|
19 | len = (bits%8) ? (bits/8+1) : (bits/8); |
---|
20 | vector = new char[len]; |
---|
21 | |
---|
22 | for (i=0; i<len; i++) |
---|
23 | vector[i] = vector[i] & 0; |
---|
24 | } |
---|
25 | |
---|
26 | Bitvector::~Bitvector() { |
---|
27 | delete [] vector; |
---|
28 | } |
---|
29 | |
---|
30 | |
---|
31 | int Bitvector::gen_id() { |
---|
32 | // generiere eine int Nummer fuer die Farbe aus dem Bitvector |
---|
33 | long num=0; |
---|
34 | for (int i=0; i<num_of_bits; i++) |
---|
35 | num += (int)(.5 + pow(2, i)*readbit(i)); |
---|
36 | return num; |
---|
37 | } |
---|
38 | Bitvector* Bitvector::merge(Bitvector* x) { |
---|
39 | int lthis, lx, lback; |
---|
40 | int i; // Zaehler |
---|
41 | |
---|
42 | lthis = num_of_bits; |
---|
43 | lx = x->get_num_of_bits(); |
---|
44 | lback = (lthis>lx) ? lthis : lx; |
---|
45 | Bitvector* back = new Bitvector(lback); |
---|
46 | |
---|
47 | for (i=0; i<lback; i++) |
---|
48 | if (this->readbit(i) || x->readbit(i)) |
---|
49 | back->setbit (i); |
---|
50 | |
---|
51 | return back; |
---|
52 | } |
---|
53 | |
---|
54 | int Bitvector::subset(Bitvector* Obermenge) { |
---|
55 | char* vector2 = Obermenge->get_vector(); |
---|
56 | |
---|
57 | for (int i=0; i<len; i++) { |
---|
58 | if ((vector[i] & vector2[i]) != vector[i]) { |
---|
59 | return 0; |
---|
60 | } |
---|
61 | } |
---|
62 | return 1; |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | void Bitvector::rshift() { |
---|
67 | long gemerkt=0; |
---|
68 | |
---|
69 | if (readbit(num_of_bits-1)) |
---|
70 | gemerkt=1; |
---|
71 | |
---|
72 | for (int i=len-1; i>-1; i--) { |
---|
73 | vector[i] = vector[i] << 1; |
---|
74 | if (readbit(8*i-1)) setbit(8*i); |
---|
75 | } |
---|
76 | if (gemerkt == 1) |
---|
77 | setbit(0); |
---|
78 | } |
---|
79 | |
---|
80 | void Bitvector::print() { |
---|
81 | int i; |
---|
82 | printf("Bitvektor: ("); |
---|
83 | for (i=0; i<num_of_bits; i++) |
---|
84 | printf("%d", readbit(i)); |
---|
85 | printf(")\n"); |
---|
86 | } |
---|
87 | |
---|
88 | int Bitvector::setbit(int pos) { |
---|
89 | int byte, idx, bitcode; |
---|
90 | if (pos > num_of_bits) |
---|
91 | return -1; |
---|
92 | byte = pos/8; |
---|
93 | idx = pos - byte*8; |
---|
94 | bitcode = (int) pow(2, idx); |
---|
95 | vector[byte] = vector[byte] | bitcode; |
---|
96 | return 0; |
---|
97 | } |
---|
98 | |
---|
99 | int Bitvector::delbit(int pos) { |
---|
100 | int byte, idx, bitcode; |
---|
101 | if (pos > num_of_bits) |
---|
102 | return -1; |
---|
103 | byte = pos/8; |
---|
104 | idx = pos - byte*8; |
---|
105 | bitcode = (int) pow(2, idx); |
---|
106 | if (readbit(pos)) |
---|
107 | vector[byte] = vector[byte] ^ bitcode; |
---|
108 | return 0; |
---|
109 | } |
---|
110 | |
---|
111 | int Bitvector::readbit(int pos) { |
---|
112 | int byte, idx, bitcode; |
---|
113 | if (pos > num_of_bits) |
---|
114 | return 0; |
---|
115 | byte = pos/8; |
---|
116 | idx = pos - byte*8; |
---|
117 | bitcode = (int) pow(2, idx); |
---|
118 | if (vector[byte] & bitcode) |
---|
119 | return 1; |
---|
120 | else |
---|
121 | return 0; |
---|
122 | } |
---|
123 | |
---|