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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#pragma once


#include <compare><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <iterator><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.


#include "snippet/aliases.hpp"

#include "internal/dummy.hpp"
#include "internal/uncopyable.hpp"

#include "action/base.hpp"
#include "data_structure/internal/basic_tree_concept.hpp"

#include "debugger/debug.hpp"




namespace uni {

namespace internal {

namespace dynamic_tree_impl {

namespace internal {



template<class T>
consteval auto to_val() {
    if constexpr(actions::internal::operatable_action<T>) return typename T::operand{};
    else return T{};
}

template<class T>
consteval auto to_acc() {
    if constexpr(actions::internal::operatable_action<T>) return typename T::operand{};
    else return dummy{};
}

template<class T>
consteval auto to_lazy() {
    if constexpr(actions::internal::effective_action<T>) return typename T::operation{};
    else return dummy{};
}


template<class T, bool LEAF_ONLY, bool MAY_BE_LAZY = true>
struct data_type {
    using val_t = decltype(to_val<T>());
    using acc_t = decltype(to_acc<T>());
    using lazy_t = decltype(to_lazy<T>());

    val_t val;
    [[no_unique_address]] acc_t acc;
    [[no_unique_address]] std::conditional_t<MAY_BE_LAZY, lazy_t, dummy> lazy;

    bool rev = false;

    data_type() noexcept = default;
    data_type(const val_t& _val) noexcept(NO_EXCEPT) : val(_val) {}

    auto _debug() const { return this->val; }


    friend bool operator==(const data_type& lhs, const data_type& rhs) noexcept(NO_EXCEPT) {
        return lhs.val == rhs.val;
    }

    friend auto operator<=>(const data_type& lhs, const data_type& rhs) noexcept(NO_EXCEPT) {
        return lhs.val <=> rhs.val;
    }
};



template<class ActionOrValue, class Derived, class Context>
struct basic_core
  : Context::substance<Derived, internal::data_type<ActionOrValue, Context::LEAF_ONLY>>
{
    using data_type = internal::data_type<ActionOrValue, Context::LEAF_ONLY>;

  private:
    using base = typename Context::substance<Derived, data_type>;
    static_assert(basic_tree<base>);

  public:
    using base::base;

    using node_handler = typename base::node_handler;
    using node_pointer = typename base::node_pointer;

    using size_type = typename base::size_type;

    using operand = data_type::val_t;
    using operation = data_type::lazy_t;


    inline auto val(const node_pointer& node) const noexcept(NO_EXCEPT) {
        if constexpr(Context::LEAF_ONLY) {
            if(node->is_leaf()) return node->size * node->data.val;
            return node->data.val;
        }
        else {
            return node->data.acc;
        }
    }


    using base::split;
    using base::merge;


    inline void split(const node_pointer tree, const size_type l, const size_type r, node_pointer& t0, node_pointer& t1, node_pointer& t2) noexcept(NO_EXCEPT) {
        // See: https://twitter.com/KakurenboUni/status/1784576244321018209
        this->split(tree, l, t0, t1);
        this->split(t1, r - l, t1, t2);
    }

    inline void split(
        const node_pointer tree,
        const size_type l, const size_type m, const size_type r,
        node_pointer& t0, node_pointer& t1, node_pointer& t2, node_pointer& t3
    ) noexcept(NO_EXCEPT) {
        // See: https://twitter.com/KakurenboUni/status/1784576244321018209
        this->split(tree, l, m, t0, t1, t2);
        this->split(t2, r - m, t2, t3);
    }

    inline void merge(node_pointer& tree, node_pointer t0, const node_pointer t1, const node_pointer t2) noexcept(NO_EXCEPT) {
        this->merge(t0, t0, t1);
        this->merge(tree, t0, t2);
    }



    void erase(node_pointer& tree, const size_type l, const size_type r) noexcept(NO_EXCEPT) {
        assert(l <= r);
        node_pointer t0, t1, t2;

        this->split(tree, l, r, t0, t1, t2);
        this->dispose(t1);
        this->merge(tree, t0, t2);
    }


    auto pop(node_pointer& tree, const size_type pos, const size_type count = 1) noexcept(NO_EXCEPT) {
        assert(0 <= count);

        if(count == 0) return operand{};

        node_pointer t0, t1, t2;

        this->split(tree, pos, pos + count, t0, t1, t2);

        const auto res = this->val(t1);

        this->dispose(t1);
        this->merge(tree, t0, t2);

        return res;
    }


    operand get(node_pointer tree, const size_type pos) noexcept(NO_EXCEPT) {
        if(tree == node_handler::nil || pos < 0 || pos >= tree->size) return {};

        this->base::push(tree);

        const auto lower_bound = tree->left->size;
        const auto upper_bound = tree->size - tree->right->size;

        if(pos < lower_bound) {
            return this->get(tree->left, pos);
        }
        else if(pos >= upper_bound) {
            return this->get(tree->right, pos - upper_bound);
        }
        else {
            return tree->data.val;
        }
    }



    template<std::forward_iterator I>
        requires std::output_iterator<I, operand>
    void enumerate(node_pointer tree, I& itr) noexcept(NO_EXCEPT) {
        if(tree == node_handler::nil) return;

        this->base::push(tree);

        this->enumerate(tree->left, itr);

        if constexpr(Context::LEAF_ONLY) {
            if(tree->is_leaf()) {
                REP(tree->size) *(itr++) = tree->data.val;
            }
        }
        else {
            REP(tree->length) *(itr++) = tree->data.val;
        }

        this->enumerate(tree->right, itr);
    }

    auto fold(node_pointer& tree, size_type l, size_type r) noexcept(NO_EXCEPT) {
        assert(l <= r);
        if(l == r) return operand{};

        node_pointer t0, t1, t2;

        this->split(tree, l, r, t0, t1, t2);

        const operand res = this->val(t1);

        this->merge(tree, t0, t1, t2);

        return res;
    }
};


} // namespace internal

} // namespace dynamic_tree_impl

} // namespace internal

} // namespace uni