aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/ipmath/range_set.h
blob: d9e245182246cba9044e49f58097d277a7e15408 (plain) (blame)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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_;
};