-
Notifications
You must be signed in to change notification settings - Fork 0
/
AttributeTranslator.cpp
78 lines (70 loc) · 2.41 KB
/
AttributeTranslator.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
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include "AttributeTranslator.h"
#include "provided.h"
using namespace std;
AttributeTranslator::AttributeTranslator() {
}
AttributeTranslator::~AttributeTranslator() {
}
bool AttributeTranslator::Load(string filename) {
ifstream attributeFile(filename);
string line;
if (attributeFile) {
while (!attributeFile.eof()) {
getline(attributeFile, line);
if (line.size() == 0) {
continue;
}
int pos = line.find(',');
string sourceaAttr = line.substr(0, pos); // source attribute
line = line.substr(pos+1);
pos = line.find(',');
string sourceValue = line.substr(0, pos); //source value;
string compatible = line.substr(pos+1); // compatible attribute + ',' + compatible value
string source = sourceaAttr + ',' + sourceValue;
vector<string>* ptr = m_pair.search(source); // search whether exist
if (ptr == nullptr) {
// if not exist create a new vector and insert
vector<string> attVal;
attVal.push_back(compatible);
m_pair.insert(source, attVal);
}
else {
// compatible attributes for a given attribute is less than 10, so O(10) = O(1)
bool flag = false; // repeated case
for (int i = 0; i < ptr->size(); i++) {
if ((*ptr)[i] == compatible) {
flag = true; // has repeat
break;
}
}
if (!flag) {
ptr->push_back(compatible); // no repeat push back
}
}
}
return true;
}
else {
return false;
}
}
vector<AttValPair> AttributeTranslator::FindCompatibleAttValPairs(const AttValPair& source) const {
vector<AttValPair> result;
string s = source.attribute + ',' + source.value;
vector<string>* ptr = m_pair.search(s);
if (ptr == nullptr) {
return result;
}
else {
for (int i = 0; i < ptr->size(); i++) {
int pos = (*ptr)[i].find(',');
// push back to the vector
result.push_back(AttValPair((*ptr)[i].substr(0, pos), (*ptr)[i].substr(pos + 1)));
}
}
return result;
}