-
Notifications
You must be signed in to change notification settings - Fork 0
/
unordered_set.cpp
82 lines (67 loc) · 4.56 KB
/
unordered_set.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
// 09 Apr 2014
// - Crate Hazard structure, has members max, min, ppa, sop.
// - Add constructor `Hazard()` for Hazard structure, inlcuding distributing vector<string> to each memeber.
// - Define hash and equality functions as lambdas, derived from default `std::hash<string>()(h.locator1)` function.
#include <iostream>
#include <set>
#include <unordered_set>
#include <vector>
using namespace std;
struct Hazard{
std::string loc1;
std::string loc2;
float sop;
std::vector<double> max;
std::vector<double> min;
std::vector<double> ppa;
Hazard(const std::vector<std::string>& s, int n){
// static_assert( static_cast<int>(s.size()) != 3*n + 3, "incorrect size of s");
loc1 = s[0];
loc2 = s[1];
for (int i = 0; i < (s.size() - 3)/3; i++){
max.push_back( stod(s[2+i]));
min.push_back( stod(s[2+i+n]));
ppa.push_back( stod(s[2+i+2*n]));
}
sop = stof(*(s.end() - 1));
}
};
template <typename T>
void printElements (const T& coll)
{
for (const auto& elem : coll) {
std::cout << elem << "\t";
}
}
int main(){
auto hf = [](const Hazard& h){ return hash<string>()(h.loc1)^hash<string>()(h.loc2); };
auto eq = [](const Hazard& h1, const Hazard& h2){ return (h1.loc1 == h2.loc1) & (h1.loc2 == h2.loc2); };
unordered_set<Hazard, decltype(hf), decltype(eq)> h(100, hf, eq);
vector< vector<string>> tmp = {
{"CO12","4NX","0.000000","0.737635","0.826935","1.008770","1.348565","0.00000","0.156005","0.237145","0.194680","0.105875","0.00000000","0.26783357","0.26783357","0.28038827","0.32642217","2"},
{"CO12","4NZ","0.179625","0.358305","0.388260","0.443950","0.527180","0.05955","0.096350","0.077030","0.070275","0.039705","0.08775628","0.20476465","0.21939069","0.23401674","0.32177301","2"},
{"CO12","4PA","0.000000","0.259000","0.330160","0.505160","0.898500","0.00000","0.002000","0.010100","0.015560","0.154480","0.00000000","0.39616468","0.53311049","0.75809290","0.97818439","2"},
{"CO12","4PB","0.000000","0.124225","0.153350","0.272000","0.529400","0.00000","0.004400","0.001450","0.006530","0.007430","0.00000000","0.02568185","0.04708340","0.11556835","0.37238689","2"},
{"CO12","4PD","0.000000","0.200880","0.263330","0.424885","0.803880","0.00000","0.016345","0.005125","0.009830","0.068410","0.00000000","0.11265830","0.27359873","0.41844512","0.82884322","2"},
{"CO12","4PF","0.000000","0.034860","0.101050","0.205285","0.477300","0.00000","0.001375","0.004075","0.006925","0.007700","0.00000000","0.02785548","0.04178322","0.09749419","0.42479611","2"},
{"CO12","4PH","0.000000","0.000000","0.000000","0.000000","0.000000","0.00000","0.000000","0.000000","0.000000","0.000000","0.00000000","0.00000000","0.00000000","0.00000000","0.00000000","2"},
{"CO12","4PJ","0.000000","0.000000","0.000000","0.000000","0.000000","0.00000","0.000000","0.000000","0.000000","0.000000","0.00000000","0.00000000","0.00000000","0.00000000","0.00000000","2"},
{"CO12","4PL","0.000000","0.000000","0.000000","0.000000","0.000000","0.00000","0.000000","0.000000","0.000000","0.000000","0.00000000","0.00000000","0.00000000","0.00000000","0.00000000","2"},
{"CO12","4PP","0.000000","0.000000","0.000000","0.000000","0.000000","0.00000","0.000000","0.000000","0.000000","0.000000","0.00000000","0.00000000","0.00000000","0.00000000","0.00000000","2"},
{"CO12","4PQ","0.000000","0.000000","0.000000","0.017330","0.390600","0.00000","0.000000","0.000000","0.003830","0.002400","0.00000000","0.00000000","0.00000000","0.01076971","0.07538799","2"},
{"CO12","4PR","0.000000","0.000000","0.000000","0.000000","0.183200","0.00000","0.000000","0.000000","0.000000","0.009770","0.00000000","0.00000000","0.00000000","0.00000000","0.01687856","2"},
{"CO12","4PS","0.000000","0.000000","0.000000","0.000000","0.000000","0.00000","0.000000","0.000000","0.000000","0.000000","0.00000000","0.00000000","0.00000000","0.00000000","0.00000000","2"},
{"CO12","4PT","0.654395","0.701255","0.759115","0.942470","1.299930","0.00444","0.040270","0.058050","0.074335","0.129230","0.21452918","0.73744405","0.76426020","0.84662408","0.94814236","2"}
};
for (auto v : tmp){
h.emplace(Hazard(v, 5));
}
cout << "\ntest data import and set object:" << std::endl;
for (auto v : h){
cout << v.loc1 << '\t' << v.loc2 << '\t';
printElements(v.max); printElements(v.min); printElements(v.ppa);
cout << v.sop << endl;
}
cout << "END OF TEST\n";
return 0;
}