Skip to content

Commit f6982bd

Browse files
committed
added DynamicSorteSet and SortedStrongSet
1 parent 0b2ed6b commit f6982bd

4 files changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#pragma once
2+
#include "DynamicArrayOf.h"
3+
#include "SliceOf.single.h"
4+
5+
#include <algorithm>
6+
7+
namespace array19 {
8+
9+
/// set of sorted of values
10+
/// - useful for sets of ids
11+
template<class T> struct DynamicSortedSet {
12+
auto count() const -> size_t { return m.count(); }
13+
auto begin() const -> const T* { return m.begin(); }
14+
auto end() const -> const T* { return m.end(); }
15+
16+
bool has(const T& v) const {
17+
auto [b, e] = std::equal_range(m.begin(), m.end(), v);
18+
return b != e;
19+
}
20+
21+
void add(const T& v) {
22+
auto [b, e] = std::equal_range(m.amendBegin(), m.amendEnd(), v);
23+
if (b == e) {
24+
m.splice(b, 0, array19::sliceOfSingle(v));
25+
}
26+
}
27+
28+
void remove(const T& v) {
29+
auto [b, e] = std::equal_range(m.amendBegin(), m.amendEnd(), v);
30+
if (b != e) {
31+
m.remove(b, 1);
32+
}
33+
}
34+
35+
void clear() { m.clear(); }
36+
37+
private:
38+
DynamicArrayOf<T> m;
39+
};
40+
41+
} // namespace array19

src/array19.lib/array19/array19.qbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Product {
1818
"DynamicArrayOf.equals.h",
1919
"DynamicArrayOf.h",
2020
"DynamicArrayOf.ostream.h",
21+
"DynamicSortedSet.h",
2122
"MoveSliceOf.h",
2223
"MoveSliceOf.single.h",
2324
"SliceOf.carray.h",
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#pragma once
2+
#include "Strong.less.h"
3+
#include "array19/DynamicArrayOf.h"
4+
#include "array19/SliceOf.single.h"
5+
6+
#include <algorithm>
7+
8+
namespace strong19 {
9+
10+
/// set of sorted strong values
11+
/// - useful for sets of ids
12+
template<class T> struct SortedStrongSet {
13+
auto count() const -> size_t { return m.count(); }
14+
auto begin() const -> const T* { return m.begin(); }
15+
auto end() const -> const T* { return m.end(); }
16+
17+
bool has(T v) const {
18+
auto [b, e] = std::equal_range(m.begin(), m.end(), v, StrongLess<T>{});
19+
return b != e;
20+
}
21+
22+
void add(T v) {
23+
auto [b, e] = std::equal_range(m.amendBegin(), m.amendEnd(), v, StrongLess<T>{});
24+
if (b == e) {
25+
m.splice(b, 0, array19::sliceOfSingle(v));
26+
}
27+
}
28+
29+
void remove(T v) {
30+
auto [b, e] = std::equal_range(m.amendBegin(), m.amendEnd(), v, StrongLess<T>{});
31+
if (b != e) {
32+
m.remove(b, 1);
33+
}
34+
}
35+
36+
void clear() { m.clear(); }
37+
38+
private:
39+
array19::DynamicArrayOf<T> m;
40+
};
41+
42+
} // namespace strong19

src/strong19.lib/strong19/strong19.qbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Product {
33
Depends { name: "string19" }
44
Depends { name: "fmt"; required: false }
5+
Depends { name: "array19"; required: false }
56

67
Export {
78
Depends { name: "cpp" }
@@ -19,6 +20,7 @@ Product {
1920
files: [
2021
"ADL.h",
2122
"Macro.h",
23+
"SortedStrongSet.h",
2224
"Strong.extras.ostream.h",
2325
"Strong.fmt.h",
2426
"Strong.h",

0 commit comments

Comments
 (0)