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


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


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

namespace uni {


namespace internal {


template<class T, class... Ts>
concept can_removable_priority_queue =
    sizeof...(Ts) == 0 &&
    requires (T pq, typename T::value_type v) {
        { pq.size() } -> std::same_as<typename T::size_type>;
        { pq.empty() } -> std::same_as<bool>;

        pq.top();
        pq.pop();

        pq.push(v);
        pq.emplace(v);
    };

} // namespace internal


template<class... Ts>
struct removable_priority_queue {};

template<class ValueType, class... Args>
    requires
        (!internal::can_removable_priority_queue<ValueType, Args...>) &&
        requires () {
            typename std::priority_queue<ValueType, Args...>;
        }
struct removable_priority_queue<ValueType, Args...> : removable_priority_queue<std::priority_queue<ValueType, Args...>> {
    using removable_priority_queue<std::priority_queue<ValueType, Args...>>::removable_priority_queue;
};

template<internal::can_removable_priority_queue PriorityQueue>
struct removable_priority_queue<PriorityQueue>
  : removable_priority_queue<PriorityQueue, internal::dummy>
{
    using removable_priority_queue<PriorityQueue, internal::dummy>::removable_priority_queue;
};

template<
    internal::can_removable_priority_queue PriorityQueue,
    class Multiset
>
struct removable_priority_queue<PriorityQueue, Multiset> : PriorityQueue {
    using base_type = PriorityQueue;
    using multiset_type = Multiset;

    using size_type = typename PriorityQueue::size_type;
    using value_type = typename PriorityQueue::value_type;
    using const_reference = typename PriorityQueue::const_reference;

  private:
    using self = removable_priority_queue<PriorityQueue, Multiset>;

    base_type _deleted;
    multiset_type _elements;

    static constexpr bool CHECK_EXISTANCE = !std::same_as<multiset_type, internal::dummy>;

    void _delete() noexcept(NO_EXCEPT) {
        while(!this->_deleted.empty() && this->_deleted.top() == this->base_type::top()) {
            this->base_type::pop();
            this->_deleted.pop();
        }
    }

  public:
    using base_type::base_type;


    size_type size() noexcept(NO_EXCEPT) {
        if constexpr(!self::CHECK_EXISTANCE) {
            assert(this->base_type::size() >= this->_deleted.size());
        }
        return this->base_type::size() - this->_deleted.size();
    }

    size_type empty() noexcept(NO_EXCEPT) {
        if constexpr(!self::CHECK_EXISTANCE) {
            assert(this->base_type::size() >= this->_deleted.size());
        }
        return this->base_type::size() == this->_deleted.size();
    }


    void push(const value_type& v) noexcept(NO_EXCEPT) {
        if constexpr(self::CHECK_EXISTANCE) this->_elements.insert(v);
        return this->base_type::push(v);
    }

    void push(value_type&& v) noexcept(NO_EXCEPT) {
        if constexpr(self::CHECK_EXISTANCE) this->_elements.insert(std::move(v));<--- Calling std::move(v)
        return this->base_type::push(std::move(v));<--- Access of moved variable 'v'.
    }

    template<class... Args>
    decltype(auto) emplace(Args&&... args) noexcept(NO_EXCEPT) {
        if constexpr(self::CHECK_EXISTANCE) this->_elements.emplace(args...);
        return this->base_type::emplace(std::forward<Args>(args)...);
    }


    std::conditional_t<self::CHECK_EXISTANCE, bool, void> remove(const value_type& v) noexcept(NO_EXCEPT) {
        if constexpr(self::CHECK_EXISTANCE) {
            if(!this->_elements.contains(v)) return false;
        }

        this->_deleted.push(v);

        if constexpr(self::CHECK_EXISTANCE) return true;<--- Found an exit path from function with non-void return type that has missing return statement
    }

    std::conditional_t<self::CHECK_EXISTANCE, bool, void> remove(value_type&& v) noexcept(NO_EXCEPT) {
        if constexpr(self::CHECK_EXISTANCE) {
            if(!this->_elements.contains(v)) return false;
        }

        this->_deleted.push(std::move(v));

        if constexpr(self::CHECK_EXISTANCE) return true;<--- Found an exit path from function with non-void return type that has missing return statement
    }

    template<class... Args>
    std::conditional_t<self::CHECK_EXISTANCE, bool, void> eliminate(Args&&... args) noexcept(NO_EXCEPT) {
        if constexpr(self::CHECK_EXISTANCE) {
            if(!this->_elements.contains({ args... })) return false;
        }

        this->_deleted.emplace(std::forward<Args>(args)...);

        if constexpr(self::CHECK_EXISTANCE) return true;<--- Found an exit path from function with non-void return type that has missing return statement
    }


    const_reference top() noexcept(NO_EXCEPT) {
        this->_delete();
        return this->base_type::top();
    }


    void pop() noexcept(NO_EXCEPT) {
        this->_delete();
        this->base_type::pop();
    }
};


} // namespace uni