-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenotypeBit.cpp
85 lines (68 loc) · 1.4 KB
/
GenotypeBit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* Genotype.cpp
*
* Created on: 27-nov-2008
* Author: rafael
*/
#include "GenotypeBit.h"
#include "Util.h"
using namespace std;
GenotypeBit::GenotypeBit() {
//Empty
}
GenotypeBit::~GenotypeBit() {
}
GenotypeBit::GenotypeBit(int size) {
for (int i = 0; i < size; i++) {
genotype.push_back(Util::rand(2));
}
}
GenotypeBit::GenotypeBit(GenotypeBit * other) {
this->genotype = other->genotype;
}
void GenotypeBit::set(int index, bool value) {
genotype[index] = value;
}
void GenotypeBit::push_back(bool value) {
genotype.push_back(value);
}
bool GenotypeBit::at(int i) {
return genotype.at(i);
}
int GenotypeBit::size() {
return GenotypeBit::genotype.size();
}
vector<bool> GenotypeBit::grayToBinary() {
vector<bool> binary = genotype;
for (unsigned int i = 1; i < genotype.size(); i++) {
if (!genotype[i]) {
binary[i] = binary[i-1];
} else {
binary[i] = !binary[i-1];
}
}
return binary;
}
void GenotypeBit::binaryToGray() {
vector<bool> gray = genotype;
for (unsigned int i = 1; i < genotype.size(); i++) {
gray[i] = !(genotype[i-1] == genotype[i]);
}
genotype = gray;
}
ostream & operator<<(ostream & os, GenotypeBit g) {
for (int i = 0; i < g.size(); i++) {
os << g.at(i);
}
return os;
}
ostream & operator<<(ostream & os, GenotypeBit * g) {
if (g != NULL) {
for (int i = 0; i < g->size(); i++) {
os << g->at(i);
}
} else {
os << "null";
}
return os;
}