-
Notifications
You must be signed in to change notification settings - Fork 2
/
DynamicSortedSet.h
43 lines (34 loc) · 1.19 KB
/
DynamicSortedSet.h
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
#pragma once
#include "DynamicArrayOf.h"
#include "SliceOf.single.h"
#include <algorithm>
#include <stddef.h> // size_t
namespace array19 {
/// set of sorted of values
/// - useful for sets of ids
template<class T, class Less = std::less<>> struct DynamicSortedSet {
[[nodiscard]] auto isEmpty() const noexcept -> bool { return m.isEmpty(); }
[[nodiscard]] auto count() const -> size_t { return m.count(); }
[[nodiscard]] auto begin() const -> T const* { return m.begin(); }
[[nodiscard]] auto end() const -> T const* { return m.end(); }
[[nodiscard]] bool has(const T& v) const {
auto [b, e] = std::equal_range(m.begin(), m.end(), v, Less{});
return b != e;
}
void add(T const& v) {
auto [b, e] = std::equal_range(m.amendBegin(), m.amendEnd(), v, Less{});
if (b == e) {
m.splice(b, 0, array19::sliceOfSingle(v));
}
}
void remove(T const& v) {
auto [b, e] = std::equal_range(m.amendBegin(), m.amendEnd(), v, Less{});
if (b != e) {
m.remove(b, 1);
}
}
void clear() { m.clear(); }
private:
DynamicArrayOf<T> m;
};
} // namespace array19