-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchema.cc
More file actions
169 lines (129 loc) · 3.34 KB
/
Copy pathSchema.cc
File metadata and controls
169 lines (129 loc) · 3.34 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
#include <iostream>
#include "Config.h"
#include "Swap.h"
#include "Schema.h"
using namespace std;
Attribute::Attribute() : name(""), type(Name), noDistinct(0) {}
Attribute::Attribute(const Attribute& _other) :
name(_other.name), type(_other.type), noDistinct(_other.noDistinct) {}
Attribute& Attribute::operator=(const Attribute& _other) {
// handle self-assignment first
if (this == &_other) return *this;
name = _other.name;
type = _other.type;
noDistinct = _other.noDistinct;
return *this;
}
void Attribute::Swap(Attribute& _other) {
STL_SWAP(name, _other.name);
SWAP(type, _other.type);
SWAP(noDistinct, _other.noDistinct);
}
Schema::Schema(vector<string>& _attributes, vector<string>& _attributeTypes,
vector<unsigned int>& _distincts) {
for (int i = 0; i < _attributes.size(); i++) {
Attribute a;
a.name = _attributes[i];
a.noDistinct = _distincts[i];
if (_attributeTypes[i] == "INTEGER") a.type = Integer;
else if (_attributeTypes[i] == "FLOAT") a.type = Float;
else if (_attributeTypes[i] == "STRING") a.type = String;
atts.push_back(a);
}
}
Schema::Schema(const Schema& _other) {
for (int i = 0; i < _other.atts.size(); i++) {
Attribute a; a = _other.atts[i];
atts.push_back(a);
}
}
Schema& Schema::operator=(const Schema& _other) {
// handle self-assignment first
if (this == &_other) return *this;
for (int i = 0; i < _other.atts.size(); i++) {
Attribute a; a = _other.atts[i];
atts.push_back(a);
}
return *this;
}
void Schema::Swap(Schema& _other) {
atts.swap(_other.atts);
}
int Schema::Append(Schema& _other) {
for (int i = 0; i < _other.atts.size(); i++) {
int pos = Index(_other.atts[i].name);
if (pos != -1) return -1;
}
for (int i = 0; i < _other.atts.size(); i++) {
Attribute a; a = _other.atts[i];
atts.push_back(a);
}
return 0;
}
int Schema::Index(string& _attName) {
for (int i = 0; i < atts.size(); i++) {
if (_attName == atts[i].name) return i;
}
// if we made it here, the attribute was not found
return -1;
}
Type Schema::FindType(string& _attName) {
int pos = Index(_attName);
if (pos == -1) return Integer;
return atts[pos].type;
}
int Schema::GetDistincts(string& _attName) {
int pos = Index(_attName);
if (pos == -1) return -1;
return atts[pos].noDistinct;
}
int Schema::RenameAtt(string& _oldName, string& _newName) {
int pos = Index(_newName);
if (pos != -1) return -1;
pos = Index(_oldName);
if (pos == -1) return -1;
atts[pos].name = _newName;
return 0;
}
int Schema::Project(vector<int>& _attsToKeep) {
int numAttsToKeep = _attsToKeep.size();
int numAtts = atts.size();
// too many attributes to keep
if (numAttsToKeep > numAtts) return -1;
vector<Attribute> copy; atts.swap(copy);
for (int i=0; i<numAttsToKeep; i++) {
int index = _attsToKeep[i];
if ((index >= 0) && (index < numAtts)) {
Attribute a; a = copy[index];
atts.push_back(a);
}
else {
atts.swap(copy);
copy.clear();
return -1;
}
}
copy.clear();
return 0;
}
ostream& operator<<(ostream& _os, Schema& _c) {
for(int i=0; i<_c.atts.size(); i++) {
_os << '\t' << _c.atts[i].name << '\t';
switch(_c.atts[i].type) {
case Integer:
_os << "INTEGER";
break;
case Float:
cout << "FLOAT";
break;
case String:
cout << "STRING";
break;
default:
cout << "UNKNOWN";
break;
}
_os << "\t" << _c.atts[i].noDistinct << endl;
}
return _os;
}