aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/left_right_heap.hpp
blob: cd69bd7834b9d3f27595695ce6f3ba344eca8012 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <algorithm>

namespace vespalib {

namespace { // left/right heap operations (code duplicated since we use old gcc)

template <typename T, typename C>
void left_heap_insert(T *heap, size_t pos, T value, C cmp) {
    size_t parent = (pos - 1) >> 1;
    while (pos != 0 && cmp(value, *(heap + parent))) {
        *(heap + pos) = std::move(*(heap + parent));
        pos = parent;
        parent = (parent - 1) >> 1;
    }
    *(heap + pos) = std::move(value);
}

template <typename T, typename C>
void right_heap_insert(T *heap, size_t pos, T value, C cmp) {
    size_t parent = (pos - 1) >> 1;
    while (pos != 0 && cmp(value, *(heap - parent))) {
        *(heap - pos) = std::move(*(heap - parent));
        pos = parent;
        parent = (parent - 1) >> 1;
    }
    *(heap - pos) = std::move(value);
}

template <typename T, typename C>
void left_heap_adjust(T *heap, size_t len, T value, C cmp) {
    size_t pos = 0;
    size_t child2 = 2;
    while (child2 < len) {
        if (cmp(*(heap + child2 - 1), *(heap + child2))) {
            --child2;
        }
        *(heap + pos) = std::move(*(heap + child2));
        pos = child2;
        child2 = (pos << 1) + 2;
    }
    if (child2 == len) {
        *(heap + pos) = std::move(*(heap + child2 - 1));
        pos = child2 - 1;
    }
    left_heap_insert<T,C>(heap, pos, std::move(value), cmp);
}

template <typename T, typename C>
void right_heap_adjust(T *heap, size_t len, T value, C cmp) {
    size_t pos = 0;
    size_t child2 = 2;
    while (child2 < len) {
        if (cmp(*(heap - child2 + 1), *(heap - child2))) {
            --child2;
        }
        *(heap - pos) = std::move(*(heap - child2));
        pos = child2;
        child2 = (pos << 1) + 2;
    }
    if (child2 == len) {
        *(heap - pos) = std::move(*(heap - child2 + 1));
        pos = child2 - 1;
    }
    right_heap_insert<T,C>(heap, pos, std::move(value), cmp);
}

template <typename T, typename C>
void left_heap_remove(T *heap, size_t len, T value, C cmp) {
    *(heap + len) = std::move(*heap);
    left_heap_adjust<T,C>(heap, len, std::move(value), cmp);
}

template <typename T, typename C>
void right_heap_remove(T *heap, size_t len, T value, C cmp) {
    *(heap - len) = std::move(*heap);
    right_heap_adjust<T,C>(heap, len, std::move(value), cmp);
}

} // namespace vespalib::<unnamed>

//-----------------------------------------------------------------------------

template <typename T, typename C>
void LeftHeap::push(T *begin, T *end, C cmp) {
    left_heap_insert<T,C>(begin, (end - begin - 1), std::move(*(end - 1)), cmp);
}

template <typename T, typename C>
void LeftHeap::pop(T *begin, T *end, C cmp) {
    left_heap_remove<T,C>(begin, (end - begin - 1), std::move(*(end - 1)), cmp);
}

template <typename T, typename C>
void LeftHeap::adjust(T *begin, T *end, C cmp) {
    left_heap_adjust<T,C>(begin, (end - begin), std::move(*begin), cmp);
}

//-----------------------------------------------------------------------------

template <typename T, typename C>
void RightHeap::push(T *begin, T *end, C cmp) {
    right_heap_insert<T,C>((end - 1), (end - begin - 1), std::move(*begin), cmp);
}

template <typename T, typename C>
void RightHeap::pop(T *begin, T *end, C cmp) {
    right_heap_remove<T,C>((end - 1), (end - begin - 1), std::move(*begin), cmp);
}

template <typename T, typename C>
void RightHeap::adjust(T *begin, T *end, C cmp) {
    right_heap_adjust<T,C>((end - 1), (end - begin), std::move(*(end - 1)), cmp);
}

//-----------------------------------------------------------------------------

namespace { // inverted comparator helper class

template <typename T, typename C> struct InvCmp {
    C cmp;
    InvCmp(C c) : cmp(c) {}
    bool operator()(const T &a, const T &b) const {
        return cmp(b, a);
    }
};

} // namespace vespalib::<unnamed>

//-----------------------------------------------------------------------------

template <typename T, typename C>
void LeftArrayHeap::push(T *begin, T *end, C cmp) {
    T value = std::move(*--end);
    while ((begin != end) && cmp(*(end - 1), value)) {
        *end = std::move(*(end - 1));
        --end;
    }
    *end = std::move(value);
}

//-----------------------------------------------------------------------------

template <typename T, typename C>
void RightArrayHeap::push(T *begin, T *end, C cmp) {
    T value = std::move(*begin++);
    while ((begin != end) && cmp(*begin, value)) {
        *(begin - 1) = std::move(*begin);
        ++begin;
    }
    *(begin - 1) = std::move(value);
}

//-----------------------------------------------------------------------------

template <typename T, typename C>
void LeftStdHeap::push(T *begin, T *end, C cmp) {
    std::push_heap(begin, end, InvCmp<T,C>(cmp));
}

template <typename T, typename C>
void LeftStdHeap::pop(T *begin, T *end, C cmp) {
    std::pop_heap(begin, end, InvCmp<T,C>(cmp));
}

template <typename T, typename C>
void LeftStdHeap::adjust(T *begin, T *end, C cmp) {
    std::pop_heap(begin, end, InvCmp<T,C>(cmp));
    std::push_heap(begin, end, InvCmp<T,C>(cmp));
}

//-----------------------------------------------------------------------------

} // namespace vespalib