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
#pragma once


#include <cmath><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <utility>
#include <functional><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <type_traits><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.


#include "snippet/iterations.hpp"

#include "internal/dev_env.hpp"
#include "internal/types.hpp"

#include "numeric/limits.hpp"


namespace uni {

namespace internal {

namespace boundary_seeker_impl {


template<class T>
struct integal {
  protected:
    std::function<bool(T)> _validate;

  public:
    integal(std::function<bool(T)> validate) noexcept(NO_EXCEPT) : _validate(validate) {}

    template<bool INVERT = false>
    T bound(const T _ok, const T _ng) const noexcept(NO_EXCEPT) {
        T ok = _ok, ng = _ng;
        if constexpr(INVERT) std::swap(ng, ok);

        while(std::abs(ok - ng) > 1) {
            T mid = ng + (ok - ng) / 2;
            (INVERT ^ this->_validate(mid) ? ok : ng) = mid;
        }

        return ok;
    }

    template<bool REVERSE = false, bool INVERT = false>
    T bound(const T ok) const noexcept(NO_EXCEPT) {
        assert(INVERT ^ this->_validate(ok));

        T ng = REVERSE ? -1 : 1;
        while(INVERT ^ this->_validate(ok + ng)) ng += ng;
        return this->bound<INVERT>(ok, ok + ng);
    }

    template<bool REVERSE = false, bool INVERT = false>
    T bound() const noexcept(NO_EXCEPT) {
        return this->bound<INVERT>(
            REVERSE ?
            numeric_limits<T>::arithmetic_infinity() / 2 :
            numeric_limits<T>::arithmetic_negative_infinity() / 2
        );
    }

    template<bool INVERT = false>
    T bound_or(const T ok, const T ng, const T proxy) const noexcept(NO_EXCEPT) {
        const T res = this->bound<INVERT>(ok, ng);
        return this->_validate(res) ? res : proxy;
    }

    template<bool REVERSE = false, bool INVERT = false>
    T bound_or(const T ok, const T proxy) const noexcept(NO_EXCEPT) {
        const T res = this->bound<REVERSE, INVERT>(ok);
        return this->_validate(res) ? res : proxy;
    }

    template<bool REVERSE = false, bool INVERT = false>
    T bound_or(const T proxy) const noexcept(NO_EXCEPT) {
        const T res = this->bound<REVERSE, INVERT>();
        return this->_validate(res) ? res : proxy;
    }
};

template<class T>
struct floating_point {
  protected:
    std::function<bool(T)> _validate;

  public:
    floating_point(std::function<bool(T)> validate) noexcept(NO_EXCEPT) : _validate(validate) {}

    template<bool INVERT = false, internal::size_t ITERATIONS = 200>
    T bound(const T _ok, const T _ng) const noexcept(NO_EXCEPT) {
        T ok = _ok, ng = _ng;
        if constexpr(INVERT) std::swap(ng, ok);

        REP(ITERATIONS) {
            T mid = ng + (ok - ng) / 2;
            (INVERT ^ this->_validate(mid) ? ok : ng) = mid;
        }
        return ok;
    }

    template<bool REVERSE = false, bool INVERT = false, internal::size_t ITERATIONS = 200>
    T bound(const T ok) const noexcept(NO_EXCEPT) {
        assert(INVERT ^ this->_validate(ok));

        T ng = REVERSE ? -1 : 1;
        while(INVERT ^ this->_validate(ok + ng)) ng += ng;
        return this->bound<INVERT>(ok, ok + ng);
    }

    template<bool REVERSE = false, bool INVERT = false, internal::size_t ITERATIONS = 200>
    T bound() const noexcept(NO_EXCEPT) {
        return this->bound<INVERT, ITERATIONS>(
            REVERSE ?
            numeric_limits<T>::arithmetic_infinity() / 2 :
            numeric_limits<T>::arithmetic_negative_infinity() / 2
        );
    }

    template<bool INVERT = false, internal::size_t ITERATIONS = 200>
    T bound_or(const T ok, const T ng, const T proxy) const noexcept(NO_EXCEPT) {
        const T res = this->bound<INVERT, ITERATIONS>(ok, ng);
        return this->_validate(res) ? res : proxy;
    }

    template<bool REVERSE = false, bool INVERT = false, internal::size_t ITERATIONS = 200>
    T bound_or(const T ok, const T proxy) const noexcept(NO_EXCEPT) {
        const T res = this->bound<REVERSE, INVERT, ITERATIONS>(ok);
        return this->_validate(res) ? res : proxy;
    }

    template<bool REVERSE = false, bool INVERT = false, internal::size_t ITERATIONS = 200>
    T bound_or(const T proxy) const noexcept(NO_EXCEPT) {
        const T res = this->bound<REVERSE, INVERT, ITERATIONS>();
        return this->_validate(res) ? res : proxy;
    }
};

template<class, bool>
struct seeker {};

template<class T>
struct seeker<T,true> : integal<T> {
    using integal<T>::integal;
};

template<class T>
struct seeker<T,false> : floating_point<T> {
    using floating_point<T>::floating_point;
};


} // namespace boundary_seeker_impl

} // namespace internal


template<class T>
using boundary_seeker = internal::boundary_seeker_impl::seeker<T,std::is_integral_v<T>>;


} // namespace uni