로딩중...
검색중...
일치하는것 없음
tbicontainable.inl
1#pragma once
2
3#include "core/ast/node.hpp"
4#include "core/builtin/container/iter/biter.inl"
5#include "core/builtin/container/native/tnarr.inl"
7
8namespace by {
9
10#define TEMPL template <typename K, typename V>
11#define ME tbicontainable<K, V>
12
13 TEMPL
14 ME::~tbicontainable() {}
15
16 TEMPL
17 V& ME::operator[](const K& key) { return *get(key); }
18
19 TEMPL
20 const V& ME::operator[](const K& key) const { return *get(key); }
21
22 TEMPL
23 ncnt ME::isEmpty() const { return len() <= 0; }
24
25 TEMPL
26 nbool ME::in(const V& val) const {
27 return get([&](const K&, const V& elem) { return &elem == &val; });
28 }
29
30 TEMPL
31 nbool ME::in(std::function<nbool(const K& key, const V& val)> l) const { return in<V>(l); }
32
33 TEMPL
34 template <typename V1> nbool ME::in(std::function<nbool(const K& key, const V1& val)> l) const {
35 for(auto e = begin(); e; ++e) {
36 V1& val = e->template cast<V1>() OR_CONTINUE;
37 const K& key = e.getKey() OR_CONTINUE;
38 WHEN(l(key, val)) .ret(true);
39 }
40 return false;
41 }
42
43 TEMPL
44 template <typename V1> V1* ME::get() {
45 return get<V1>([](const K&, const V1&) { return true; });
46 }
47
48 TEMPL
49 template <typename V1> V1* ME::get(const K& key) { return get(key) TO(template cast<V1>()); }
50
51 TEMPL
52 template <typename V1> V1* ME::get(std::function<nbool(const K&, const V1&)> l) {
53 for(auto e = begin(); e; ++e) {
54 V1& val = e.getVal() TO(template cast<V1>()) OR_CONTINUE;
55 const K& key = e.getKey() OR_CONTINUE;
56 if(!l(key, val)) continue;
57 return &val;
58 }
59
60 return nullptr;
61 }
62
63 TEMPL
64 V* ME::get(std::function<nbool(const K&, const V&)> l) { return this->get<V>(l); }
65
66 TEMPL
67 tnarr<V> ME::getAll(const K& key) const {
68 narr ret;
69 _getAll(key, ret);
70 return ret;
71 }
72
73 TEMPL
74 template <typename V1> tnarr<V1> ME::getAll() const {
75 return getAll<V1>([](const K&, const V1&) { return true; });
76 }
77
78 TEMPL
79 template <typename V1> tnarr<V1> ME::getAll(std::function<nbool(const K&, const V1&)> l) const {
80 tnarr<V1> ret;
81 for(auto e = begin(); e; ++e) {
82 const V1& val = e.getVal() TO(template cast<V1>()) OR_CONTINUE;
83 const K& key = e.getKey() OR_CONTINUE;
84 if(!l(key, val)) continue;
85
86 ret.add(val);
87 }
88
89 return ret;
90 }
91
92 TEMPL
93 tnarr<V> ME::getAll(std::function<nbool(const K&, const V&)> l) const { return this->getAll<V>(l); }
94
95 TEMPL
96 template <typename V1> void ME::each(std::function<nbool(const K&, V1&)> l) {
97 for(auto e = begin(); e; ++e) {
98 V1& val = e.getVal() TO(template cast<V1>()) OR_CONTINUE;
99 const K& key = e.getKey() OR_CONTINUE;
100 if(!l(key, val)) break;
101 }
102 }
103
104 TEMPL
105 void ME::each(std::function<nbool(const K&, V&)> l) { this->each<V>(l); }
106
107 TEMPL
108 typename ME::iter ME::begin() const { return iterate(0, true); }
109
110 TEMPL
111 typename ME::iter ME::begin(const K& key) const { return iterate(key, true); }
112
113 TEMPL
114 typename ME::iter ME::rbegin() const { return riterate(0, true); }
115
116 TEMPL
117 typename ME::iter ME::rbegin(const K& key) const { return riterate(key, true); }
118
119 TEMPL
120 typename ME::iter ME::end() const { return iterate(len(), true); }
121
122 TEMPL
123 typename ME::iter ME::rend() const { return riterate(len(), true); }
124
125 TEMPL
126 typename ME::iter ME::last() const { return iterate(len() - 1); }
127
128 TEMPL
129 typename ME::iter ME::iterate(ncnt step) const { return iterate(step, false); }
130
131 TEMPL
132 typename ME::iter ME::iterate(ncnt step, nbool isBoundary) const {
133 return iter(_onMakeIteration(nullptr, false, step, isBoundary));
134 }
135
136 TEMPL
137 typename ME::iter ME::iterate(const K& key) const { return iterate(key, false); }
138
139 TEMPL
140 typename ME::iter ME::iterate(const K& key, nbool isBoundary) const {
141 auto* e = _onMakeIteration(&key, false, 0, isBoundary);
142 const K* eKey = e->getKey();
143 if(!e->isEnd() && (!eKey || *eKey != key)) e->next(1);
144
145 return iter(e);
146 }
147
148 TEMPL
149 typename ME::iter ME::riterate(ncnt step) const { return riterate(step, false); }
150
151 TEMPL
152 typename ME::iter ME::riterate(ncnt step, nbool isBoundary) const {
153 return iter(_onMakeIteration(nullptr, true, step, isBoundary));
154 }
155
156 TEMPL
157 typename ME::iter ME::riterate(const K& key) const { return riterate(key, false); }
158
159 TEMPL
160 typename ME::iter ME::riterate(const K& key, nbool isBoundary) const {
161 auto* e = _onMakeIteration(&key, true, 0, isBoundary);
162 if(!e->isEnd() && *e->getKey() != key) e->next(1);
163
164 return iter(e);
165 }
166
167 TEMPL
168 ncnt ME::add(const iter& from, const iter& to) {
169 int ret = 0;
170 for(iter e = from; e != to; ++e)
171 if(add(e.getKey(), e.getVal())) ret++;
172 return ret;
173 }
174
175 TEMPL
176 ncnt ME::add(const tbicontainable& rhs) { return add(rhs.begin(), rhs.end()); }
177
178 TEMPL
179 nbool ME::del(const tbicontainable& rhs) { return del(rhs.begin(), rhs.end()); }
180
181#undef ME
182#undef TEMPL
183} // namespace by
Bidirectional iterator for key-value containers
Definition biter.hpp:10
to Top