Loading...
Searching...
No Matches
tucontainable.inl
1#pragma once
2
3#include "core/ast/node.hpp"
4#include "core/builtin/container/iter/uiter.inl"
8
9namespace by {
10
11#define TEMPL template <typename T, typename R, typename RSquare>
12#define ME tucontainable<T, R, RSquare>
13
14 TEMPL
15 ME::~tucontainable() {}
16
17 TEMPL
18 nbool ME::in(const T& t) const {
19 return in([&](const T& elem) -> nbool { return elem == t; });
20 }
21
22 TEMPL
23 template <typename T1> nbool ME::in(std::function<nbool(const T1&)> l) const {
24 for(auto e = begin(); e; ++e) {
25 if constexpr(_IS_POINTER) {
26 const T1& val = e->template cast<T1>() OR_CONTINUE;
27 WHEN(l(val)) .ret(true);
28 } else {
29 const T1& val = e.get().template cast<T1>() OR_CONTINUE;
30 WHEN(l(val)) .ret(true);
31 }
32 }
33 return false;
34 }
35
36 TEMPL
37 nbool ME::in(std::function<nbool(const T&)> l) const { return this->in<T>(l); }
38
39 TEMPL
40 nbool ME::isEmpty() const { return len() <= 0; }
41
42 TEMPL
43 template <typename K, typename V> ncnt ME::add(const tbicontainable<K, V>& rhs) {
44 static_assert(areBaseOfT<T, V>::value, "given type 'V' is not subtype of 'T'");
45 ncnt ret = 0;
46 for(auto e = rhs.begin(); e; ++e)
47 ret += add(*e);
48 return ret;
49 }
50
51 TEMPL
52 template <typename T1> tnarr<T1> ME::getAll(std::function<nbool(const T1&)> l) const {
53 tnarr<T1> ret;
54 for(auto e = begin(); e; ++e) {
55 if constexpr(_IS_POINTER) {
56 const T1& val = e->template cast<T1>() OR_CONTINUE;
57 if(!l(val)) continue;
58 ret.add(val);
59 } else {
60 const T1& val = e.get().template cast<T1>() OR_CONTINUE;
61 if(!l(val)) continue;
62 ret.add(val);
63 }
64 }
65
66 return ret;
67 }
68
69 TEMPL
70 tnarr<T> ME::getAll(std::function<nbool(const T&)> l) const { return this->getAll<T>(l); }
71
72 TEMPL
73 template <typename T1> void ME::each(std::function<nbool(T1&)> l) {
74 for(auto e = begin(); e; ++e) {
75 if constexpr(_IS_POINTER) {
76 T1& val = e->template cast<T1>() OR_CONTINUE;
77 if(!l(val)) break;
78 } else {
79 T1& val = e.get().template cast<T1>() OR_CONTINUE;
80 if(!l(val)) break;
81 }
82 }
83 }
84
85 TEMPL
86 void ME::each(std::function<nbool(T&)> l) { this->each<T>(l); }
87
88 TEMPL
89 typename ME::iter ME::begin() const {
90 static iter (me::*specifier)(ncnt) const = &me::iterate;
91 return (this->*specifier)(0);
92 }
93
94 TEMPL
95 typename ME::iter ME::rbegin() const {
96 static iter (me::*specifier)(ncnt) const = &me::riterate;
97 return (this->*specifier)(0);
98 }
99
100 TEMPL
101 typename ME::iter ME::end() const {
102 // why do you need specifier here?:
103 // please refer nseq. it requires tucontainable to contain nint as type parameter.
104 // in this case, we have very similar 'iterate' funcs.
105 // - iterate(ncnt step)
106 // - iterate(const nint& it)
107 //
108 // to avoid ambigious error, I used specifier.
109 static iter (me::*specifier)(ncnt) const = &me::iterate;
110 return (this->*specifier)(len());
111 }
112
113 TEMPL
114 typename ME::iter ME::rend() const {
115 static iter (me::*specifier)(ncnt) const = &me::riterate;
116 return (this->*specifier)(len());
117 }
118
119 TEMPL
120 typename ME::iter ME::last() const {
121 WHEN(len() <= 0) .ret(end());
122 static iter (me::*specifier)(ncnt) const = &me::iterate;
123 return (this->*specifier)(len() - 1);
124 }
125
126 TEMPL
127 typename ME::iter ME::iterate(ncnt step) const { return iter(_onMakeIteration(step, false)); }
128
129 TEMPL
130 typename ME::iter ME::iterate(const T& it) const {
131 for(iter e = begin(); e; ++e)
132 if constexpr(_IS_POINTER) {
133 WHEN(e.get() == &it) .ret(iter(e));
134 } else {
135 WHEN(e.get() == it) .ret(iter(e));
136 }
137
138 return end();
139 }
140
141 TEMPL
142 typename ME::iter ME::riterate(ncnt step) const { return iter(_onMakeIteration(step, true)); }
143
144 TEMPL
145 typename ME::iter ME::riterate(const T& it) const {
146 for(iter e = rbegin(); e; ++e)
147 if constexpr(_IS_POINTER) {
148 WHEN(e.get() == &it) .ret(iter(e));
149 } else {
150 WHEN(e.get() == it) .ret(iter(e));
151 }
152
153 return rend();
154 }
155
156 TEMPL
157 nbool ME::add(std::initializer_list<const T*> elems) {
158 nbool ret = false;
159 for(auto* elem: elems)
160 ret = add(elem);
161 return ret;
162 }
163
164 TEMPL
165 nbool ME::add(const T& new1) { return add(end(), new1); }
166
167 TEMPL
168 void ME::add(const iter& from, const iter& to) { return add(end(), from, to); }
169
170 TEMPL
171 void ME::add(const iter& here, me& rhs) { return add(here, rhs.begin(), rhs.end()); }
172
173 TEMPL
174 void ME::add(const me& rhs) { return add(end(), rhs.begin(), rhs.end()); }
175
176 TEMPL
177 nbool ME::del() {
178 static iter (me::*specifier)(ncnt) const = &me::iterate;
179 return del((this->*specifier)(len() - 1));
180 }
181
182 TEMPL
183 nbool ME::del(const T& it) {
184 static iter (me::*specifier)(const T&) const = &me::iterate;
185 return del((this->*specifier)(it));
186 }
187
188 TEMPL
189 nbool ME::del(const tucontainable& rhs) { return del(rhs.begin(), rhs.end()); }
190
191#undef ME
192#undef TEMPL
193} // namespace by
Bidirectional iterator for key-value containers.
Definition biter.hpp:10
to Top