Pixie
Loading...
Searching...
No Matches
sorted_vector.h
Go to the documentation of this file.
1#pragma once
2
7
8#include <pixie/permutations/permutation.h>
9
10#include <concepts>
11#include <cstddef>
12#include <cstdint>
13#include <functional>
14#include <limits>
15#include <ranges>
16#include <stdexcept>
17#include <type_traits>
18#include <utility>
19#include <vector>
20
21namespace pixie {
22
38template <class T = std::uint64_t,
39 class Compare = std::less<>,
40 class Index = std::uint64_t,
41 std::size_t StorageBits = 2048,
42 std::size_t Fanout = 8,
43 LengthLayout Layout = LengthLayout::cumulative>
45 static_assert(std::is_object_v<T> && !std::is_array_v<T> &&
46 std::same_as<T, std::remove_cv_t<T>> &&
47 !std::same_as<T, bool>,
48 "SortedPermutationVector requires a non-bool object type");
49
50 public:
54 using value_type = T;
56 using const_reference = const T&;
58 using size_type = std::size_t;
59
65 explicit SortedPermutationVector(Compare compare = {})
66 : compare_(std::move(compare)) {}
67
75
88 template <std::ranges::input_range Range>
89 requires std::
90 constructible_from<T, std::ranges::range_rvalue_reference_t<Range>>
91 static SortedPermutationVector from_range(Range&& range,
92 Compare compare = {}) {
93 SortedPermutationVector result(std::move(compare));
94 auto it = std::ranges::begin(range);
95 const auto end = std::ranges::end(range);
96 for (; it != end; ++it) {
97 result.insert(T(std::ranges::iter_move(it)));
98 }
99 return result;
100 }
101
103 size_type size() const noexcept { return permutation_.size(); }
105 bool empty() const noexcept { return permutation_.empty(); }
115 return values_[permutation_[position]];
116 }
117
126 void reserve(size_type count) { values_.reserve(count); }
127
136 template <class Key>
137 size_type lower_bound_index(const Key& key) const {
138 return permutation_.lower_bound(
139 [&](Index idx) { return !compare_(values_[idx], key); });
140 }
141
150 template <class Key>
151 size_type upper_bound_index(const Key& key) const {
152 return permutation_.lower_bound(
153 [&](Index idx) { return compare_(key, values_[idx]); });
154 }
155
170 size_type insert(T value) {
171 const size_type position = lower_bound_index(value);
172 if (values_.size() > std::numeric_limits<Index>::max()) {
173 throw std::length_error("SortedPermutationVector: index domain");
174 }
175 values_.push_back(std::move(value));
176 try {
177 permutation_.insert_at(position);
178 } catch (...) {
179 values_.pop_back();
180 throw;
181 }
182 return position;
183 }
184
192 size_type memory_usage_bytes() const noexcept {
193 const size_type maximum = std::numeric_limits<size_type>::max();
194 const size_type payload = values_.capacity() > maximum / sizeof(T)
195 ? maximum
196 : values_.capacity() * sizeof(T);
197 const size_type permutation =
198 permutation_.memory_usage_bytes() - sizeof(permutation_);
199 const size_type base =
200 payload > maximum - sizeof(*this) ? maximum : sizeof(*this) + payload;
201 return permutation > maximum - base ? maximum : base + permutation;
202 }
203
204 private:
205 std::vector<T> values_;
206 permutation_type permutation_;
207 [[no_unique_address]] Compare compare_;
208};
209
210} // namespace pixie
Exclusively owned permutation of the integers in [0, size()).
Definition permutation.h:45
Duplicate-preserving sorted view over owned vector payload and order.
Definition sorted_vector.h:44
SortedPermutationVector & operator=(SortedPermutationVector &&)=default
Replace vector payload, permutation, and comparator by move.
SortedPermutationVector & operator=(const SortedPermutationVector &)=delete
Exclusive ownership disallows copy assignment.
SortedPermutationVector(SortedPermutationVector &&)=default
Move vector payload, permutation, and comparator state.
const T & const_reference
Immutable read result borrowing the owned vector.
Definition sorted_vector.h:56
Permutation< Index, StorageBits, Fanout, Layout > permutation_type
Underlying order-index type.
Definition sorted_vector.h:52
const_reference operator[](size_type position) const
Read one sorted zero-based value.
Definition sorted_vector.h:114
T value_type
Stored value type.
Definition sorted_vector.h:54
size_type insert(T value)
Insert a value before existing equivalent values.
Definition sorted_vector.h:170
size_type memory_usage_bytes() const noexcept
Return explicit requested bytes owned by this wrapper.
Definition sorted_vector.h:192
SortedPermutationVector(Compare compare={})
Construct empty with a comparator.
Definition sorted_vector.h:65
size_type size() const noexcept
Return the number of sorted values.
Definition sorted_vector.h:103
bool empty() const noexcept
Return whether this owner has no values.
Definition sorted_vector.h:105
SortedPermutationVector(const SortedPermutationVector &)=delete
Exclusive ownership disallows copying.
size_type upper_bound_index(const Key &key) const
Return the first position whose value is ordered after key.
Definition sorted_vector.h:151
size_type lower_bound_index(const Key &key) const
Return the first position whose value is not ordered before key.
Definition sorted_vector.h:137
std::size_t size_type
Count and position type.
Definition sorted_vector.h:58
static SortedPermutationVector from_range(Range &&range, Compare compare={})
Build a sorted owner by consuming values one at a time.
Definition sorted_vector.h:91
void reserve(size_type count)
Reserve append-only payload capacity.
Definition sorted_vector.h:126
LengthLayout
Representation of tree child lengths.
Definition sequence_options.h:14