| 1 | #include <MultiProbe.hxx> |
|---|
| 2 | #include <stdio.h> |
|---|
| 3 | #include <stdlib.h> |
|---|
| 4 | #include <string.h> |
|---|
| 5 | #include <math.h> |
|---|
| 6 | |
|---|
| 7 | typedef enum { |
|---|
| 8 | bit1 = 1, |
|---|
| 9 | bit2 = 2, |
|---|
| 10 | bit3 = 4, |
|---|
| 11 | bit4 = 8, |
|---|
| 12 | bit5 = 16, |
|---|
| 13 | bit6 = 32, |
|---|
| 14 | bit7 = 64, |
|---|
| 15 | bit8 = 128 |
|---|
| 16 | } Bitpos; |
|---|
| 17 | |
|---|
| 18 | Bitvector::Bitvector(int bits) |
|---|
| 19 | { |
|---|
| 20 | int i; |
|---|
| 21 | |
|---|
| 22 | num_of_bits = bits; |
|---|
| 23 | len = (bits%8) ? (bits/8+1) : (bits/8); |
|---|
| 24 | vector = new char[len]; |
|---|
| 25 | |
|---|
| 26 | for (i=0;i<len;i++) |
|---|
| 27 | vector[i] = vector[i] & 0; |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | Bitvector::~Bitvector() |
|---|
| 31 | { |
|---|
| 32 | delete [] vector; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | int Bitvector::gen_id() |
|---|
| 37 | { |
|---|
| 38 | // generiere eine int Nummer fuer die Farbe aus dem Bitvector |
|---|
| 39 | long num=0; |
|---|
| 40 | for (int i=0; i<num_of_bits; i++) |
|---|
| 41 | num += (int)(.5 + pow(2,i)*readbit(i)); |
|---|
| 42 | return num; |
|---|
| 43 | } |
|---|
| 44 | Bitvector* Bitvector::merge(Bitvector* x) |
|---|
| 45 | { |
|---|
| 46 | int lthis, lx, lback; |
|---|
| 47 | int i; // Zaehler |
|---|
| 48 | |
|---|
| 49 | lthis = num_of_bits; |
|---|
| 50 | lx = x->get_num_of_bits(); |
|---|
| 51 | lback = (lthis>lx) ? lthis : lx; |
|---|
| 52 | Bitvector* back = new Bitvector(lback); |
|---|
| 53 | |
|---|
| 54 | for (i=0 ;i<lback; i++) |
|---|
| 55 | if ( this->readbit(i) || x->readbit(i) ) |
|---|
| 56 | back->setbit (i); |
|---|
| 57 | |
|---|
| 58 | return back; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | int Bitvector::subset(Bitvector* Obermenge) |
|---|
| 62 | { |
|---|
| 63 | char* vector2 = Obermenge->get_vector(); |
|---|
| 64 | |
|---|
| 65 | for (int i=0; i<len; i++) |
|---|
| 66 | { |
|---|
| 67 | if ( (vector[i] & vector2[i]) != vector[i] ) |
|---|
| 68 | return 0; |
|---|
| 69 | } |
|---|
| 70 | //delete vector2; |
|---|
| 71 | return 1; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | void Bitvector::rshift() |
|---|
| 76 | { |
|---|
| 77 | long gemerkt=0; |
|---|
| 78 | |
|---|
| 79 | if (readbit(num_of_bits-1)) |
|---|
| 80 | gemerkt=1; |
|---|
| 81 | |
|---|
| 82 | for (int i=len-1; i>-1 ; i--) |
|---|
| 83 | { |
|---|
| 84 | vector[i] = vector[i] <<1; |
|---|
| 85 | if (readbit(8*i-1)) |
|---|
| 86 | setbit(8*i); |
|---|
| 87 | } |
|---|
| 88 | if (gemerkt == 1) |
|---|
| 89 | setbit(0); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | void Bitvector::print() |
|---|
| 93 | { |
|---|
| 94 | int i; |
|---|
| 95 | printf("Bitvektor: ("); |
|---|
| 96 | for (i=0;i<num_of_bits;i++) |
|---|
| 97 | printf("%d",readbit(i)); |
|---|
| 98 | printf(")\n"); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | int Bitvector::setbit(int pos) |
|---|
| 102 | { |
|---|
| 103 | int byte,idx,bitcode; |
|---|
| 104 | if (pos > num_of_bits) |
|---|
| 105 | return -1; |
|---|
| 106 | byte = pos/8; |
|---|
| 107 | idx = pos - byte*8; |
|---|
| 108 | bitcode = (int) pow(2,idx); |
|---|
| 109 | vector[byte] = vector[byte] | bitcode; |
|---|
| 110 | return 0; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | int Bitvector::delbit(int pos) |
|---|
| 114 | { |
|---|
| 115 | int byte,idx,bitcode; |
|---|
| 116 | if (pos > num_of_bits) |
|---|
| 117 | return -1; |
|---|
| 118 | byte = pos/8; |
|---|
| 119 | idx = pos - byte*8; |
|---|
| 120 | bitcode = (int) pow(2,idx); |
|---|
| 121 | if (readbit(pos)) |
|---|
| 122 | vector[byte] = vector[byte] ^ bitcode; |
|---|
| 123 | return 0; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | int Bitvector::readbit(int pos) |
|---|
| 127 | { |
|---|
| 128 | int byte,idx,bitcode; |
|---|
| 129 | if (pos > num_of_bits) |
|---|
| 130 | return 0; |
|---|
| 131 | byte = pos/8; |
|---|
| 132 | idx = pos - byte*8; |
|---|
| 133 | bitcode = (int) pow(2,idx); |
|---|
| 134 | if (vector[byte] & bitcode) |
|---|
| 135 | return 1; |
|---|
| 136 | else |
|---|
| 137 | return 0; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | void permutation(int k,int n) |
|---|
| 143 | { |
|---|
| 144 | int h,i,j; |
|---|
| 145 | int c[1000]; |
|---|
| 146 | |
|---|
| 147 | c[0] = -1; |
|---|
| 148 | for ( i=1; i<k+1; i++ ) |
|---|
| 149 | c[i] = i; |
|---|
| 150 | |
|---|
| 151 | j = 1; |
|---|
| 152 | |
|---|
| 153 | while (j != 0) |
|---|
| 154 | { |
|---|
| 155 | for (h=1;h<k+1;h++) |
|---|
| 156 | printf("%d ",c[h]); |
|---|
| 157 | printf("\n"); |
|---|
| 158 | |
|---|
| 159 | j = k; |
|---|
| 160 | while (c[j] == n-k+j) |
|---|
| 161 | j = j-1; |
|---|
| 162 | c[j] = c[j]+1; |
|---|
| 163 | for (i=j+1; i< k+1;i++) |
|---|
| 164 | c[i] = c[i-1] + 1; |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | void permute (int k, int n) |
|---|
| 169 | { |
|---|
| 170 | int i; |
|---|
| 171 | for (i=1; i<k+1;i++) |
|---|
| 172 | { |
|---|
| 173 | printf("\nPermutation k aus n, k = %d, n = %d\n",i,n); |
|---|
| 174 | permutation(i,n); |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | } |
|---|
| 178 | /* |
|---|
| 179 | int main(int argc, char** argv) |
|---|
| 180 | { |
|---|
| 181 | int i; |
|---|
| 182 | |
|---|
| 183 | Bitvector *a = new Bitvector(10); |
|---|
| 184 | Bitvector *b = new Bitvector(15); |
|---|
| 185 | Bitvector *c ; |
|---|
| 186 | |
|---|
| 187 | a->setbit(2); |
|---|
| 188 | a->setbit(3); |
|---|
| 189 | a->setbit(4); |
|---|
| 190 | a->print(); |
|---|
| 191 | |
|---|
| 192 | b->setbit(6); |
|---|
| 193 | b->setbit(7); |
|---|
| 194 | b->setbit(12); |
|---|
| 195 | b->setbit(14); |
|---|
| 196 | |
|---|
| 197 | b->print(); |
|---|
| 198 | |
|---|
| 199 | if (b->subset(a)) |
|---|
| 200 | printf("B ist subset von a\n"); |
|---|
| 201 | else |
|---|
| 202 | printf("B ist kein subset von a\n"); |
|---|
| 203 | |
|---|
| 204 | c->setbit(3); |
|---|
| 205 | c->setbit(4); |
|---|
| 206 | c->setbit(5); |
|---|
| 207 | c->print(); |
|---|
| 208 | if (c->subset(a)) |
|---|
| 209 | printf("C ist subset von a\n"); |
|---|
| 210 | else |
|---|
| 211 | printf("C ist kein subset von a\n"); |
|---|
| 212 | |
|---|
| 213 | printf("Rightshift von A:\n"); |
|---|
| 214 | a->rshift(); |
|---|
| 215 | a->print(); |
|---|
| 216 | |
|---|
| 217 | if (c->subset(a)) |
|---|
| 218 | printf("C ist subset von a\n"); |
|---|
| 219 | else |
|---|
| 220 | printf("C ist kein subset von a\n"); |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | c = b->merge(a); |
|---|
| 224 | c->print(); |
|---|
| 225 | |
|---|
| 226 | delete a; |
|---|
| 227 | delete b; |
|---|
| 228 | delete c; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | */ |
|---|