range_set.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #pragma once
  2. #include "ipmath.h"
  3. #include <util/generic/set.h>
  4. #include <util/ysaveload.h>
  5. /// @brief Maintains a disjoint set of added ranges. Allows for efficient membership queries
  6. /// for an address in a set of IP ranges.
  7. class TIpRangeSet {
  8. struct TRangeLess {
  9. bool operator()(const TIpAddressRange& lhs, const TIpAddressRange& rhs) const;
  10. };
  11. using TTree = TSet<TIpAddressRange, TRangeLess>;
  12. public:
  13. using iterator = TTree::iterator;
  14. using const_iterator = TTree::const_iterator;
  15. using value_type = TTree::value_type;
  16. using TIterator = TTree::iterator;
  17. using TConstIterator = TTree::const_iterator;
  18. TIpRangeSet();
  19. ~TIpRangeSet();
  20. void Add(TIpAddressRange range);
  21. template <typename TContainer>
  22. void Add(TContainer&& addrs) {
  23. using T = typename std::decay<TContainer>::type::value_type;
  24. static_assert(std::is_convertible<T, TIpAddressRange>::value);
  25. for (auto&& addr : addrs) {
  26. Add(addr);
  27. }
  28. }
  29. TIpAddressRange::TIpType Type() const;
  30. bool IsEmpty() const;
  31. bool Contains(TIpv6Address addr) const;
  32. TConstIterator Find(TIpv6Address addr) const;
  33. TConstIterator Begin() const {
  34. return Ranges_.begin();
  35. }
  36. TConstIterator End() const {
  37. return Ranges_.end();
  38. }
  39. TConstIterator begin() const {
  40. return Begin();
  41. }
  42. TConstIterator end() const {
  43. return End();
  44. }
  45. Y_SAVELOAD_DEFINE(Ranges_);
  46. private:
  47. TTree Ranges_;
  48. };