Skip to content

Commit

Permalink
Improved DynamicSortedSet
Browse files Browse the repository at this point in the history
* added Less predicate
* added isEmpty()
* added [[nodiscard]] attributes
  • Loading branch information
arBmind committed Jun 14, 2023
1 parent 0b721e6 commit 148407b
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/array19.lib/array19/DynamicSortedSet.h
Expand Up @@ -9,25 +9,26 @@ namespace array19 {

/// set of sorted of values
/// - useful for sets of ids
template<class T> struct DynamicSortedSet {
auto count() const -> size_t { return m.count(); }
auto begin() const -> const T* { return m.begin(); }
auto end() const -> const T* { return m.end(); }

bool has(const T& v) const {
auto [b, e] = std::equal_range(m.begin(), m.end(), v);
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(const T& v) {
auto [b, e] = std::equal_range(m.amendBegin(), m.amendEnd(), v);
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(const T& v) {
auto [b, e] = std::equal_range(m.amendBegin(), m.amendEnd(), 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);
}
Expand Down

0 comments on commit 148407b

Please sign in to comment.