-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompositeKey.cc
More file actions
203 lines (177 loc) · 5.09 KB
/
Copy pathCompositeKey.cc
File metadata and controls
203 lines (177 loc) · 5.09 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "CompositeKey.h"
using namespace std;
CompositeKey::CompositeKey() : numVars(0) { }
CompositeKey::~CompositeKey(){ }
void CompositeKey::addInt(int _addMe) {
MultiTypeVar var;
var.type = Integer; var.valInt = _addMe;
multiTypeList[numVars++] = var;
}
void CompositeKey::addDbl(double _addMe) {
MultiTypeVar var;
var.type = Float; var.valDbl = _addMe;
multiTypeList[numVars++] = var;
}
void CompositeKey::addStr(char* _addMe) {
MultiTypeVar var;
var.type = String; var.valStr = string(_addMe);
multiTypeList[numVars++] = var;
}
void CompositeKey::extractRecord(char*& _bits, CNF& _predicate, bool& _isLeft) {
int whichAtt;
for (int i = 0; i < _predicate.numAnds; i++) {
if(_isLeft ^ (_predicate.andList[i].operand1 == Left)) { // XOR
whichAtt = _predicate.andList[i].whichAtt2;
} else {
whichAtt = _predicate.andList[i].whichAtt1;
}
switch(_predicate.andList[i].attType) {
case Integer:
addInt(*((int*) (_bits + ((int*) _bits)[whichAtt + 1])));
break;
case Float:
addDbl(*((double*) (_bits + ((int*) _bits)[whichAtt + 1])));
break;
case String:
addStr(_bits + ((int*) _bits)[whichAtt + 1]);
break;
default:
cerr << "ERROR: Unknown type '" << _predicate.andList[i].attType << "'." << endl << endl;
break;
}
}
}
void CompositeKey::extractRecord(Record& _rec, Schema& _schema) {
char* bits = _rec.GetBits();
int pointer;
size_t n = _schema.GetNumAtts();
for (size_t i = 0; i < n; i++) {
pointer = ((int *) bits)[i + 1];
switch(_schema.GetAtts()[i].type) {
case Integer:
addInt(*(int *) &(bits[pointer]));
break;
case Float:
addDbl(*(double *) &(bits[pointer]));
break;
case String:
addStr(&(bits[pointer]));
break;
}
}
}
void CompositeKey::extractRecord(Record& _rec, Schema& _schema, int _whichAtt) {
char* bits = _rec.GetBits();
int pointer = ((int *) bits)[_whichAtt + 1];
switch(_schema.GetAtts()[_whichAtt].type) {
case Integer:
addInt(*(int *) &(bits[pointer]));
break;
case Float:
addDbl(*(double *) &(bits[pointer]));
break;
case String:
addStr(&(bits[pointer]));
break;
}
}
// int CompositeKey::getNumVars() const {
// return numVars;
// }
// Type CompositeKey::getTypeAt(int _pos) const {
// return multiTypeList[_pos].type;
// }
// int CompositeKey::getIntAt(int _pos) const {
// return multiTypeList[_pos].valInt;
// }
// double CompositeKey::getDblAt(int _pos) const {
// return multiTypeList[_pos].valDbl;
// }
// string CompositeKey::getStrAt(int _pos) const {
// return multiTypeList[_pos].valStr;
// }
bool CompositeKey::operator==(const CompositeKey& _other) const {
for(int i = 0; i < numVars; i++) {
switch(multiTypeList[i].type) {
case Integer:
if(multiTypeList[i].valInt != _other.multiTypeList[i].valInt)
return false;
break;
case Float:
if(multiTypeList[i].valDbl != _other.multiTypeList[i].valDbl)
return false;
break;
case String:
if(multiTypeList[i].valStr.compare(_other.multiTypeList[i].valStr) != 0)
return false;
break;
default:
cerr << "ERROR: Unknown type '" << multiTypeList[i].type << "'." << endl << endl;
return false;
}
}
return true;
}
bool CompositeKey::operator<(const CompositeKey& _other) const {
for(int i = 0; i < numVars; i++) {
switch(multiTypeList[i].type) {
case Integer:
if(multiTypeList[i].valInt != _other.multiTypeList[i].valInt)
return multiTypeList[i].valInt < _other.multiTypeList[i].valInt;
break;
case Float:
if(multiTypeList[i].valDbl != _other.multiTypeList[i].valDbl)
return multiTypeList[i].valDbl < _other.multiTypeList[i].valDbl;
break;
case String:
{
int ret = multiTypeList[i].valStr.compare(_other.multiTypeList[i].valStr);
if(ret != 0)
return ret < 0 ? true : false;
}
break;
default:
cerr << "ERROR: Unknown type '" << multiTypeList[i].type << "'." << endl << endl;
return false;
}
}
return false; // equal
}
bool CompositeKey::operator>(const CompositeKey& _other) const {
for(int i = 0; i < numVars; i++) {
switch(multiTypeList[i].type) {
case Integer:
if(multiTypeList[i].valInt != _other.multiTypeList[i].valInt)
return multiTypeList[i].valInt > _other.multiTypeList[i].valInt;
break;
case Float:
if(multiTypeList[i].valDbl != _other.multiTypeList[i].valDbl)
return multiTypeList[i].valDbl > _other.multiTypeList[i].valDbl;
break;
case String:
{
int ret = multiTypeList[i].valStr.compare(_other.multiTypeList[i].valStr);
if(ret != 0)
return ret > 0 ? true : false;
}
break;
default:
cerr << "ERROR: Unknown type '" << multiTypeList[i].type << "'." << endl << endl;
return false;
}
}
return false; // equal
}
ostream& operator<<(ostream& _out, CompositeKey& _this) {
_out << "(";
for(int i = 0; i < _this.numVars; i++) {
if(i > 0) { _out << ", "; }
switch(_this.multiTypeList[i].type) {
case Integer: _out << _this.multiTypeList[i].valInt; break;
case Float: _out << _this.multiTypeList[i].valDbl; break;
case String: _out << "\"" << _this.multiTypeList[i].valStr << "\""; break;
default: break;
}
}
return _out << ")";
}