Pixie
Loading...
Searching...
No Matches
sorted_sequence.h
Go to the documentation of this file.
1#pragma once
2
7
8#include <pixie/permutations/sequence.h>
9
10#include <concepts>
11#include <cstddef>
12#include <cstdint>
13#include <functional>
14#include <limits>
15#include <ranges>
16#include <utility>
17
18namespace pixie {
19
36template <class T = std::uint64_t,
37 class Compare = std::less<>,
38 ElementStorage Storage = ElementStorage::automatic,
39 std::size_t StorageBits = 2048,
40 std::size_t Fanout = 8,
41 LengthLayout Layout = LengthLayout::cumulative,
42 std::size_t ChunkBytes = 4096>
44 public:
49 using value_type = T;
53 using size_type = std::size_t;
54
61 explicit SortedPermutableSequence(Compare compare = {})
62 : compare_(std::move(compare)) {}
63
71
84 template <std::ranges::input_range Range>
85 requires std::
86 constructible_from<T, std::ranges::range_rvalue_reference_t<Range>>
88 Compare compare = {}) {
89 SortedPermutableSequence result(std::move(compare));
90 auto it = std::ranges::begin(range);
91 const auto end = std::ranges::end(range);
92 for (; it != end; ++it) {
93 result.insert(T(std::ranges::iter_move(it)));
94 }
95 return result;
96 }
97
99 size_type size() const noexcept { return sequence_.size(); }
101 bool empty() const noexcept { return sequence_.empty(); }
111 return sequence_[position];
112 }
113
122 template <class Key>
123 size_type lower_bound_index(const Key& key) const {
124 return sequence_.lower_bound([&](const T& v) { return !compare_(v, key); });
125 }
126
135 template <class Key>
136 size_type upper_bound_index(const Key& key) const {
137 return sequence_.lower_bound([&](const T& v) { return compare_(key, v); });
138 }
139
150 size_type insert(T value) {
151 const size_type position = lower_bound_index(value);
152 sequence_.insert_at(position, std::move(value));
153 return position;
154 }
155
163 size_type memory_usage_bytes() const noexcept {
164 const size_type nested = sequence_.memory_usage_bytes() - sizeof(sequence_);
165 return nested > std::numeric_limits<size_type>::max() - sizeof(*this)
166 ? std::numeric_limits<size_type>::max()
167 : sizeof(*this) + nested;
168 }
169
170 private:
171 sequence_type sequence_;
172 [[no_unique_address]] Compare compare_;
173};
174
175} // namespace pixie
Owning, move-only tree sequence with immutable element reads.
Definition sequence.h:74
typename Traits::const_reference const_reference
Definition sequence.h:108
Duplicate-preserving sorted sequence backed by PermutableSequence.
Definition sorted_sequence.h:43
SortedPermutableSequence & operator=(const SortedPermutableSequence &)=delete
Exclusive ownership disallows copy assignment.
T value_type
Stored value type.
Definition sorted_sequence.h:49
static SortedPermutableSequence from_range(Range &&range, Compare compare={})
Build a sorted owner by consuming values one at a time.
Definition sorted_sequence.h:87
bool empty() const noexcept
Return whether this owner is empty.
Definition sorted_sequence.h:101
SortedPermutableSequence(const SortedPermutableSequence &)=delete
Exclusive ownership disallows copying.
std::size_t size_type
Count and position type.
Definition sorted_sequence.h:53
SortedPermutableSequence(Compare compare={})
Construct empty with a comparator.
Definition sorted_sequence.h:61
SortedPermutableSequence & operator=(SortedPermutableSequence &&)=default
Replace ownership and comparator state by move.
typename sequence_type::const_reference const_reference
Immutable indexed result inherited from the sequence policy.
Definition sorted_sequence.h:51
SortedPermutableSequence(SortedPermutableSequence &&)=default
Move ownership and comparator state.
PermutableSequence< T, Storage, StorageBits, Fanout, Layout, ChunkBytes > sequence_type
Underlying owning sequence type.
Definition sorted_sequence.h:46
size_type memory_usage_bytes() const noexcept
Return explicit requested bytes owned by this wrapper.
Definition sorted_sequence.h:163
size_type lower_bound_index(const Key &key) const
Return the first position whose value is not ordered before key.
Definition sorted_sequence.h:123
size_type size() const noexcept
Return the number of sorted values.
Definition sorted_sequence.h:99
size_type upper_bound_index(const Key &key) const
Return the first position whose value is ordered after key.
Definition sorted_sequence.h:136
const_reference operator[](size_type position) const
Read one sorted zero-based value.
Definition sorted_sequence.h:110
size_type insert(T value)
Insert a value before existing equivalent values.
Definition sorted_sequence.h:150
ElementStorage
Compile-time element storage selection.
Definition permutable_sequence.h:23
LengthLayout
Representation of tree child lengths.
Definition sequence_options.h:14