-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHash.h
128 lines (108 loc) · 2.66 KB
/
Hash.h
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
#include <iostream>
#include <stdio.h>
#include <sstream>
#include <string>
#include <regex.h>
#include <map>
#include <vector>
#include <typeinfo>
#include <stdexcept>
using namespace std;
namespace pl {
#define JOIN2(a,b) a##b
#define JOIN(a,b) JOIN2(a,b)
#define foreach(a) for(bool JOIN(flag,__LINE__)=a.first();JOIN(flag,__LINE__);JOIN(flag,__LINE__)=a.next())
class kv_list : public vector<string> {
protected:
kv_list::iterator it;
public:
//foreach helpers
bool first() { it = begin(); return it != end(); }
bool next() { ++it; return it != end(); }
string current(){ return *it; }
};
class Hash : public map<string,scalar> {
private:
kv_list ary;//used in keys, values
protected:
Hash::iterator it;
public:
//foreach helpers
bool first() { it = begin(); return it != end(); }
bool next() { ++it; return it != end(); }
pair<string,scalar> current(){ return *it; }
string key() { return it->first; }
scalar value() { return it->second; }
void value(const scalar& s) { it->second = s; }
bool exists(const string& key) {
if (this->find(key) != this->end()) return true;
return false;
}
const kv_list& keys() {
ary.clear();
foreach((*this)) {
ary.push_back( this->current().first );
};
return ary;
}
void dump() {
//we use (*this) instead of just *this, so that unpacking foreach macro generates
// (*this).begin(), (*this).end(), etc...
foreach((*this)) {
cout << this->key() << " => " << this->value() << nl;
}
}
// void dump() { cout << dump_as_str() << nl; }
bool match(const string& str) {
foreach((*this)) {
if (this->value() ^= str) return true;
}
return false;
}
bool equal(const scalar& s) {
foreach((*this)) {
if (this->value() == s) return true;
}
return false;
}
//hyper operators
bool operator ^= (const string& str) {
return match(str);
}
bool operator == (const scalar& s) {
return equal(s);
}
Hash& operator += (const scalar& rhs) {
foreach((*this)) it->second += rhs;
return *this;
}
Hash& operator -= (const scalar& rhs) {
foreach((*this)) it->second -= rhs;
return *this;
}
Hash& operator *= (const scalar& rhs) {
foreach((*this)) it->second *= rhs;
return *this;
}
Hash& operator /= (const scalar& rhs) {
foreach((*this)) it->second /= rhs;
return *this;
}
Hash& operator ++ () {
foreach((*this)) ++it->second;
return *this;
}
Hash& operator ++ (int) {
foreach((*this)) it->second++;
return *this;
}
Hash& operator -- () {
foreach((*this)) --it->second;
return *this;
}
Hash& operator -- (int) {
foreach((*this)) it->second--;
return *this;
}
};
};//end namespace