|
| 1 | +template<class S, class T, class R> |
| 2 | + class UmlRsdsOcl { |
| 3 | + public: |
| 4 | + |
| 5 | + static T iterate(vector<S>* _s, T initialValue, std::function<T(S,T)> _f) |
| 6 | + { T acc = initialValue; |
| 7 | + for (auto _pos = _s->begin(); _pos != _s->end(); ++_pos) |
| 8 | + { S _x = *_pos; |
| 9 | + acc = _f(_x,acc); |
| 10 | + } |
| 11 | + return acc; |
| 12 | + } |
| 13 | + |
| 14 | + static map<S,T>* includingMap(map<S,T>* m, S src, T trg) |
| 15 | + { map<S,T>* copy = new map<S,T>(); |
| 16 | + auto iter = m->begin(); |
| 17 | + for ( ; iter != m->end(); ++iter) |
| 18 | + { S key = iter->first; |
| 19 | + (*copy)[key] = iter->second; |
| 20 | + } |
| 21 | + (*copy)[src] = trg; |
| 22 | + return copy; |
| 23 | + } |
| 24 | + |
| 25 | + |
| 26 | + static map<S,T>* unionMap(map<S,T>* m1, map<S,T>* m2) |
| 27 | + { map<S,T>* res = new map<S,T>(); |
| 28 | + auto iter = m1->begin(); |
| 29 | + for ( ; iter != m1->end(); ++iter) |
| 30 | + { S key = iter->first; |
| 31 | + if (m2->count(key) == 0) |
| 32 | + { (*res)[key] = iter->second; } |
| 33 | + } |
| 34 | + for (iter = m2->begin(); iter != m2->end(); ++iter) |
| 35 | + { S key = iter->first; |
| 36 | + (*res)[key] = iter->second; |
| 37 | + } |
| 38 | + return res; |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + static bool includesAllMap(map<S,T>* sup, map<S,T>* sub) |
| 43 | + { auto iter = sub->begin(); |
| 44 | + for ( ; iter != sub->end(); ++iter) |
| 45 | + { S key = iter->first; |
| 46 | + auto f = sup->find(key); |
| 47 | + if (f != sup->end()) |
| 48 | + { if (iter->second == f->second) {} |
| 49 | + else |
| 50 | + { return false; } |
| 51 | + } |
| 52 | + else |
| 53 | + { return false; } |
| 54 | + } |
| 55 | + return true; |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + static bool excludesAllMap(map<S,T>* sup, map<S,T>* sub) |
| 60 | + { auto iter = sub->begin(); |
| 61 | + for ( ; iter != sub->end(); ++iter) |
| 62 | + { S key = iter->first; |
| 63 | + auto f = sup->find(key); |
| 64 | + if (f != sup->end()) |
| 65 | + { if (iter->second == f->second) |
| 66 | + { return false; } |
| 67 | + } |
| 68 | + } |
| 69 | + return true; |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + static map<S,T>* excludeAllMap(map<S,T>* m1, map<S,T>* m2) |
| 74 | + { map<S,T>* res = new map<S,T>(); |
| 75 | + auto iter = m1->begin(); |
| 76 | + for ( ; iter != m1->end(); ++iter) |
| 77 | + { S key = iter->first; |
| 78 | + auto f = m2->find(key); |
| 79 | + if (f != m2->end()) |
| 80 | + { if (iter->second == f->second) { } |
| 81 | + else |
| 82 | + { (*res)[key] = iter->second; } |
| 83 | + } |
| 84 | + else |
| 85 | + { (*res)[key] = iter->second; } |
| 86 | + } |
| 87 | + return res; |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + static map<S,T>* excludingMapKey(map<S,T>* m, string k) |
| 92 | + { // m - { k |-> m(k) } |
| 93 | + map<S,T>* res = new map<S,T>(); |
| 94 | + auto iter = m->begin(); |
| 95 | + for ( ; iter != m->end(); ++iter) |
| 96 | + { S key = iter->first; |
| 97 | + if (key == k) {} |
| 98 | + else |
| 99 | + { (*res)[key] = iter->second; } |
| 100 | + } |
| 101 | + return res; |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + static map<S,T>* excludingMapValue(map<S,T>* m, T v) |
| 106 | + { // m - { k |-> v } |
| 107 | + map<S,T>* res = new map<S,T>(); |
| 108 | + auto iter = m->begin(); |
| 109 | + for ( ; iter != m->end(); ++iter) |
| 110 | + { S key = iter->first; |
| 111 | + if (iter->second == v) {} |
| 112 | + else |
| 113 | + { (*res)[key] = iter->second; } |
| 114 | + } |
| 115 | + return res; |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + static map<S,T>* intersectionMap(map<S,T>* m1, map<S,T>* m2) |
| 122 | + { map<S,T>* res = new map<S,T>(); |
| 123 | + auto iter = m1->begin(); |
| 124 | + for ( ; iter != m1->end(); ++iter) |
| 125 | + { S key = iter->first; |
| 126 | + if (m2->count(key) > 0) |
| 127 | + { if (m2->at(key) == iter->second) |
| 128 | + { (*res)[key] = iter->second; } |
| 129 | + } |
| 130 | + } |
| 131 | + return res; |
| 132 | + } |
| 133 | + |
| 134 | + static std::set<S>* keys(map<S,T>* s) |
| 135 | + { auto iter = s->begin(); |
| 136 | + std::set<S>* res = new std::set<S>(); |
| 137 | + |
| 138 | + for ( ; iter != s->end(); ++iter) |
| 139 | + { S key = iter->first; |
| 140 | + res->insert(key); |
| 141 | + } |
| 142 | + return res; |
| 143 | + } |
| 144 | + |
| 145 | + |
| 146 | + static vector<T>* values(map<S,T>* s) |
| 147 | + { auto iter = s->begin(); |
| 148 | + vector<T>* res = new vector<T>(); |
| 149 | + |
| 150 | + for ( ; iter != s->end(); ++iter) |
| 151 | + { T value = iter->second; |
| 152 | + res->push_back(value); |
| 153 | + } |
| 154 | + return res; |
| 155 | + } |
| 156 | + |
| 157 | + |
| 158 | + static map<S,T>* restrict(map<S,T>* m1, std::set<S>* ks) |
| 159 | + { map<S,T>* res = new map<S,T>(); |
| 160 | + auto iter = m1->begin(); |
| 161 | + for ( ; iter != m1->end(); ++iter) |
| 162 | + { S key = iter->first; |
| 163 | + if (ks->find(key) != ks->end()) |
| 164 | + { (*res)[key] = iter->second; } |
| 165 | + } |
| 166 | + return res; |
| 167 | + } |
| 168 | + |
| 169 | + static map<S,T>* antirestrict(map<S,T>* m1, std::set<S>* ks) |
| 170 | + { map<S,T>* res = new map<S,T>(); |
| 171 | + auto iter = m1->begin(); |
| 172 | + for ( ; iter != m1->end(); ++iter) |
| 173 | + { S key = iter->first; |
| 174 | + if (ks->find(key) == ks->end()) |
| 175 | + { (*res)[key] = iter->second; } |
| 176 | + } |
| 177 | + return res; |
| 178 | + } |
| 179 | + |
| 180 | + static map<S,T>* selectMap(map<S,T>* m1, std::function<bool(T)> f) |
| 181 | + { map<S,T>* res = new map<S,T>(); |
| 182 | + auto iter = m1->begin(); |
| 183 | + for ( ; iter != m1->end(); ++iter) |
| 184 | + { S key = iter->first; |
| 185 | + T val = iter->second; |
| 186 | + if (f(val)) |
| 187 | + { (*res)[key] = val; } |
| 188 | + } |
| 189 | + return res; |
| 190 | + } |
| 191 | + |
| 192 | + static map<S,T>* rejectMap(map<S,T>* m1, std::function<bool(T)> f) |
| 193 | + { map<S,T>* res = new map<S,T>(); |
| 194 | + auto iter = m1->begin(); |
| 195 | + for ( ; iter != m1->end(); ++iter) |
| 196 | + { S key = iter->first; |
| 197 | + T val = iter->second; |
| 198 | + if (f(val)) { } |
| 199 | + else |
| 200 | + { (*res)[key] = val; } |
| 201 | + } |
| 202 | + return res; |
| 203 | + } |
| 204 | + |
| 205 | + static map<S,R>* collectMap(map<S,T>* m1, |
| 206 | + std::function<R(T)> f) |
| 207 | + { map<S,R>* res = new map<S,R>(); |
| 208 | + auto iter = m1->begin(); |
| 209 | + for ( ; iter != m1->end(); ++iter) |
| 210 | + { S key = iter->first; |
| 211 | + T val = iter->second; |
| 212 | + |
| 213 | + (*res)[key] = f(val); |
| 214 | + } |
| 215 | + return res; |
| 216 | + } |
| 217 | + |
| 218 | + |
| 219 | + }; |
| 220 | + |
| 221 | + |
0 commit comments