aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/small_containers/compact_heap-inl.h
blob: 2c6b3507ba57a2b0e98639467715f076027eb34b (plain) (blame)
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
#ifndef COMPACT_HEAP_INL_H_
#error "Direct inclusion of this file is not allowed, include compact_heap.h"
// For the sake of sane code completion.
#include "compact_heap.h"
#endif

#include <library/cpp/yt/assert/assert.h>

#include <algorithm>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

template <class T, size_t N, class C>
TCompactHeap<T, N, C>::TCompactHeap(C comparator) noexcept
    : Comparator_(TReverseComparator(std::move(comparator)))
{ }

template <class T, size_t N, class C>
void TCompactHeap<T, N, C>::push(T value)
{
    bool wasInline = IsInline();
    Heap_.push_back(std::move(value));
    if (Y_UNLIKELY(!IsInline())) {
        if (wasInline) {
            std::make_heap(Heap_.begin(), Heap_.end(), Comparator_);
        } else {
            std::push_heap(Heap_.begin(), Heap_.end(), Comparator_);
        }
    }
}

template <class T, size_t N, class C>
void TCompactHeap<T, N, C>::pop()
{
    YT_ASSERT(!empty());

    if (Y_LIKELY(IsInline())) {
        auto minIt = std::max_element(Heap_.begin(), Heap_.end(), Comparator_);
        std::swap(*minIt, Heap_.back());
        Heap_.pop_back();
    } else {
        std::pop_heap(Heap_.begin(), Heap_.end(), Comparator_);
        Heap_.pop_back();
    }
}

template <class T, size_t N, class C>
auto TCompactHeap<T, N, C>::get_min() const -> const_reference
{
    YT_ASSERT(!empty());

    if (Y_LIKELY(IsInline())) {
        return *std::max_element(Heap_.begin(), Heap_.end(), Comparator_);
    } else {
        return Heap_.front();
    }
}

template <class T, size_t N, class C>
auto TCompactHeap<T, N, C>::extract_min() -> value_type
{
    YT_ASSERT(!empty());

    if (Y_LIKELY(IsInline())) {
        auto minIt = std::max_element(Heap_.begin(), Heap_.end(), Comparator_);
        std::swap(*minIt, Heap_.back());
        auto value = Heap_.back();
        Heap_.pop_back();

        return value;
    } else {
        std::pop_heap(Heap_.begin(), Heap_.end(), Comparator_);
        auto value = std::move(Heap_.back());
        Heap_.pop_back();

        return value;
    }
}

template <class T, size_t N, class C>
auto TCompactHeap<T, N, C>::begin() const -> const_iterator
{
    return Heap_.begin();
}

template <class T, size_t N, class C>
auto TCompactHeap<T, N, C>::end() const -> const_iterator
{
    return Heap_.end();
}

template <class T, size_t N, class C>
void TCompactHeap<T, N, C>::swap(TCompactHeap<T, N, C>& other)
{
    Heap_.swap(other.Heap_);
    std::swap(Comparator_, other.Comparator_);
}

template <class T, size_t N, class C>
size_t TCompactHeap<T, N, C>::size() const
{
    return Heap_.size();
}

template <class T, size_t N, class C>
size_t TCompactHeap<T, N, C>::capacity() const
{
    return Heap_.capacity();
}

template <class T, size_t N, class C>
size_t TCompactHeap<T, N, C>::max_size() const
{
    return Heap_.max_size();
}

template <class T, size_t N, class C>
bool TCompactHeap<T, N, C>::empty() const
{
    return Heap_.empty();
}

template <class T, size_t N, class C>
void TCompactHeap<T, N, C>::shrink_to_small()
{
    Heap_.shrink_to_small();
}

template <class T, size_t N, class C>
bool TCompactHeap<T, N, C>::IsInline() const
{
    return Heap_.capacity() == N;
}

////////////////////////////////////////////////////////////////////////////////

template <class T, size_t N, class C>
TCompactHeap<T, N, C>::TReverseComparator::TReverseComparator(C underlying)
    : Underlying_(std::move(underlying))
{ }

template <class T, size_t N, class C>
bool TCompactHeap<T, N, C>::TReverseComparator::operator()(const T& lhs, const T& rhs) const
{
    return Underlying_(rhs, lhs);
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT