-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecord.h
More file actions
169 lines (145 loc) · 5.68 KB
/
Record.h
File metadata and controls
169 lines (145 loc) · 5.68 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
#ifndef RECORD_H
#define RECORD_H
#include <any>
#include <iostream>
#include <vector>
#include <string.h>
#include <variant>
using namespace std;
class Record {
private:
class Attribute {
public:
string name;
string type;
any value;
Attribute(string name, string type, string strVal) : name(name), type(type) {
if (type == "Double")
value = stod(strVal);
else if (type == "Integer")
value = stoi(strVal);
else {
type = "String"; // Force to String if user provides unknown type
value = strVal;
}
}
friend ostream& operator<< (ostream& os, const Attribute& a) {
if (a.type == "Double")
os << any_cast<double>(a.value);
else if (a.type == "Integer")
os << any_cast<int>(a.value);
else
os << any_cast<string>(a.value);
return os;
}
bool evaluate_cond(string Sel_val, char Sel_op) {
if (type == "String") {
string recValue = any_cast<string>(value);
string selValue = Sel_val;
if(Sel_op == '='){return recValue == selValue;}
else if(Sel_op == '!'){return recValue != selValue;}
}
else if (type == "Double") {
double recValue = any_cast<double>(value);
double selValue = stod(Sel_val);
if(Sel_op == '='){return recValue == selValue;}
else if(Sel_op == '!'){return recValue != selValue;}
else if(Sel_op == '<'){return recValue < selValue;}
else if(Sel_op == '>'){return recValue > selValue;}
}
else if (type == "Integer") {
int recValue = any_cast<int>(value);
int selValue = stoi(Sel_val);
if(Sel_op == '='){return recValue == selValue;}
else if(Sel_op == '!'){return recValue != selValue;}
else if(Sel_op == '<'){return recValue < selValue;}
else if(Sel_op == '>'){return recValue > selValue;}
}
else {
cout << "BAD" << endl;
}
return false;
}
};
public:
vector<Attribute> attributes;
Record() {}
Record(const vector<string>& names, const vector<string>& types, const vector<string>& vals) {
for(size_t i=0; i < names.size(); i++) {
addAttribute(names[i], types[i], vals[i]);
}
}
void addAttribute(const string& name, const string& type, const string& value) {
attributes.emplace_back(name, type, value);
}
void addAttribute(const Attribute& attr) {
attributes.push_back(attr);
}
vector<string> getAttributenames(){
vector<string> attrNames;
for(const auto& attr : attributes){
attrNames.push_back(attr.name);
}
return attrNames;
}
Attribute getAttribute(const string& name) const {
size_t i;
for(i=0; i < attributes.size(); i++) {
if (name == attributes[i].name)
break;
}
return attributes[i];
}
vector<string> getAttributetypes(){
vector<string> attrTypes;
for(const auto& attr : attributes){
attrTypes.push_back(attr.type);
}
return attrTypes;
}
friend ostream& operator<< (ostream& os, const Record& r) {
bool first = true;
for(Attribute attr : r.attributes) {
if (first) first = false;
else cout << " ";
cout << attr;
}
return os;
}
bool evaluate_cond(const string& Sel_attr, const string& Sel_val, char Sel_op){
for(int i = 0; i < attributes.size(); i++){
if(attributes[i].name == Sel_attr){
return attributes[i].evaluate_cond(Sel_val, Sel_op);
}
}
return false;
}
Record concatonaterecs(const Record& r2){
Record conc;
for(const auto& attr : attributes){
conc.addAttribute(attr);
}
for(const auto& attr : r2.attributes){
conc.addAttribute(attr);
}
return conc;
}
bool attributes_match(const Record& other, const string& Attr1Name, const string& Attr2Name) {
Attribute Attr1 = getAttribute(Attr1Name);
Attribute Attr2 = other.getAttribute(Attr2Name);
if (Attr1.type != Attr2.type) {
return false;
}
if (Attr1.type == "String") {
return any_cast<string>(Attr1.value) == any_cast<string>(Attr2.value);
}
else if (Attr1.type == "Integer") {
return any_cast<int>(Attr1.value) == any_cast<int>(Attr2.value);
}
else if (Attr1.type == "Double") {
return any_cast<double>(Attr1.value) == any_cast<double>(Attr2.value);
}
return false;
}
};
#endif