aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/ipmath/range_set.h
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/ipmath/range_set.h
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/ipmath/range_set.h')
-rw-r--r--library/cpp/ipmath/range_set.h66
1 files changed, 66 insertions, 0 deletions
diff --git a/library/cpp/ipmath/range_set.h b/library/cpp/ipmath/range_set.h
new file mode 100644
index 00000000000..d9e24518224
--- /dev/null
+++ b/library/cpp/ipmath/range_set.h
@@ -0,0 +1,66 @@
+#pragma once
+
+#include "ipmath.h"
+
+#include <util/generic/set.h>
+#include <util/ysaveload.h>
+
+
+/// @brief Maintains a disjoint set of added ranges. Allows for efficient membership queries
+/// for an address in a set of IP ranges.
+class TIpRangeSet {
+ struct TRangeLess {
+ bool operator()(const TIpAddressRange& lhs, const TIpAddressRange& rhs) const;
+ };
+
+ using TTree = TSet<TIpAddressRange, TRangeLess>;
+
+public:
+ using iterator = TTree::iterator;
+ using const_iterator = TTree::const_iterator;
+ using value_type = TTree::value_type;
+ using TIterator = TTree::iterator;
+ using TConstIterator = TTree::const_iterator;
+
+ TIpRangeSet();
+ ~TIpRangeSet();
+
+ void Add(TIpAddressRange range);
+
+ template <typename TContainer>
+ void Add(TContainer&& addrs) {
+ using T = typename std::decay<TContainer>::type::value_type;
+ static_assert(std::is_convertible<T, TIpAddressRange>::value);
+
+ for (auto&& addr : addrs) {
+ Add(addr);
+ }
+ }
+
+ TIpAddressRange::TIpType Type() const;
+
+ bool IsEmpty() const;
+ bool Contains(TIpv6Address addr) const;
+ TConstIterator Find(TIpv6Address addr) const;
+
+ TConstIterator Begin() const {
+ return Ranges_.begin();
+ }
+
+ TConstIterator End() const {
+ return Ranges_.end();
+ }
+
+ TConstIterator begin() const {
+ return Begin();
+ }
+
+ TConstIterator end() const {
+ return End();
+ }
+
+ Y_SAVELOAD_DEFINE(Ranges_);
+
+private:
+ TTree Ranges_;
+};