tucontainable.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include "core/ast/param.hpp"
5#include <type_traits>
6
7namespace by {
8
9 class node;
10 template <typename T, typename R, typename RSquare> class tnucontainer;
11 template <typename T, typename TACTIC> class tnarr;
12 typedef class _nout tnarr<node, strTactic> narr;
13 template <typename K, typename V> class tbicontainable;
14
19 template <typename T, typename R = T*, typename RSquare = T&> class tucontainable {
20 BY_ME(tucontainable)
21 static constexpr nbool _IS_POINTER = std::is_pointer_v<R>;
22
23 public:
25
26 public:
27 virtual ~tucontainable();
28
29 // len:
30 virtual ncnt len() const = 0;
31
32 nbool in(const T& it) const;
33 nbool in(const T* it) const BY_SIDE_FUNC(in);
34 nbool in(std::function<nbool(const T&)> l) const;
35 template <typename T1> nbool in(std::function<nbool(const T1&)> l) const;
36
37 nbool isEmpty() const;
38
39 // get:
40 template <typename T1> std::conditional_t<_IS_POINTER, T1*, tmay<T1>> get(std::function<nbool(const T1&)> l) {
41 for(auto e = begin(); e; ++e) {
42 if constexpr(_IS_POINTER) {
43 T1& val = e->template cast<T1>() OR_CONTINUE;
44 if(!l(val)) continue;
45 return &val;
46 } else {
47 T1& val = e.get().template cast<T1>() OR_CONTINUE;
48 if(!l(val)) continue;
49 return tmay<T1>(val);
50 }
51 }
52
53 if constexpr(_IS_POINTER) return nullptr;
54 else return tmay<T1>();
55 }
56
57 std::conditional_t<_IS_POINTER, R, tmay<R>> get(std::function<nbool(const T&)> l) { return this->get<T>(l); }
58
59 template <typename T1>
60 std::conditional_t<_IS_POINTER, const T1*, tmay<T1>> get(std::function<nbool(const T1&)> l) const
61 BY_CONST_FUNC(get(l))
62 std::conditional_t<_IS_POINTER, const R, tmay<R>> get(std::function<nbool(const T&)> l) const
63 BY_CONST_FUNC(get(l))
64
65 template <typename T1> tnarr<T1, strTactic> getAll(std::function<nbool(const T1&)> l) const;
66 tnarr<T, strTactic> getAll(std::function<nbool(const T&)> l) const;
67
68 template <typename T1> void each(std::function<nbool(T1&)> l);
69 void each(std::function<nbool(T&)> l);
70 template <typename T1> void each(std::function<nbool(const T1&)> l) const BY_CONST_FUNC(each(l))
71 void each(std::function<nbool(const T&)> l) const BY_CONST_FUNC(each(l))
72
73 // iter:
74 iter begin() const;
75 iter rbegin() const;
76
77 virtual iter end() const;
78 virtual iter rend() const;
79
80 virtual iter last() const;
81
82 iter iterate(ncnt step) const;
83 iter iterate(const T& it) const;
84
85 iter riterate(ncnt step) const;
86 iter riterate(const T& it) const;
87
88 // set:
89 virtual nbool set(const iter& at, const T& new1) = 0;
90 virtual nbool set(const iter& at, const T* new1) BY_SIDE_FUNC(new1, set(at, *new1), false);
91
92 // add:
93 virtual nbool add(const iter& at, const T& new1) = 0;
94 nbool add(const iter& at, const T* new1) BY_SIDE_FUNC(new1, add(at, *new1), false);
95 nbool add(std::initializer_list<const T*> elems);
96 nbool add(const T& new1);
97 nbool add(const T* it) BY_SIDE_FUNC(add);
98 virtual void add(const iter& here, const iter& from, const iter& to) = 0;
99 void add(const iter& from, const iter& to);
100 void add(const iter& here, me& rhs);
101 void add(const me& rhs);
102 void add(const me* rhs) BY_SIDE_FUNC(rhs, add(*rhs), void());
103
104 template <typename E>
105 ncnt add(const typename tucontainable<E>::iter& from, const typename tucontainable<E>::iter& to) {
106 static_assert(areBaseOfT<T, E>::value, "given type 'E' is not subtype of 'T'");
107 int ret = 0;
108 for(auto e = from; e != to; ++e)
109 if(add(*e)) ret++;
110 return ret;
111 }
112
113 template <typename E> ncnt add(const tucontainable<E>& rhs) { return add(rhs.begin(), rhs.end()); }
114
115 template <typename K, typename V> ncnt add(const tbicontainable<K, V>& rhs);
116
117 // del:
119 nbool del();
120 nbool del(const T& it);
121 nbool del(const T* it) BY_SIDE_FUNC(del);
122 virtual nbool del(const iter& it) = 0;
123 virtual nbool del(const iter& from, const iter& end) = 0;
124 nbool del(const me& rhs);
125 nbool del(const me* rhs) BY_SIDE_FUNC(rhs, del(*rhs), false);
126
127 // etc:
128 virtual void rel() = 0;
129
130 protected:
131 virtual iteration* _onMakeIteration(ncnt step, nbool isReversed) const = 0;
132 };
133
135} // namespace by
Deferred execution utility like defer keyword in other languages.
Definition: end.hpp:14
Template bidirectional container interface.
Definition: tbicontainable.hpp:17
Optional value wrapper with byeol-style interface.
Definition: tmay.hpp:18
Definition: tnarr.hpp:9
Template unidirectional container interface.
Definition: tucontainable.hpp:19
nbool del()
delete last element if exists.
Bidirectional iterator for key-value containers.
Definition: biter.hpp:9
Definition: biteration.hpp:5