-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefixes.cpp
144 lines (111 loc) · 3.58 KB
/
prefixes.cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**/
#include "prefixes.hpp"
#include "mem.hpp"
/**/
#include <new>
/**/
namespace {
constexpr auto kMemCount = 100000;
constexpr auto kBits32 = 32;
///
struct BitIp {
int bit_{}; // NOLINT(misc-non-private-member-variables-in-classes)
bool added_{}; // NOLINT(misc-non-private-member-variables-in-classes)
BitIp* up_{}; // NOLINT(misc-non-private-member-variables-in-classes)
std::unique_ptr<BitIp> true_{}; // NOLINT(misc-non-private-member-variables-in-classes)
std::unique_ptr<BitIp> false_{}; // NOLINT(misc-non-private-member-variables-in-classes)
~BitIp() = default;
BitIp(int bit = kBits32, BitIp* parent = nullptr) : bit_(bit), up_(parent) {}
BitIp(BitIp const&) = delete;
BitIp(BitIp&&) = default;
auto operator=(BitIp const&) = delete;
auto operator=(BitIp &&) -> BitIp& = default;
static auto operator new(size_t /*count*/) -> void* { return mem().Malloc(); }
static void operator delete(void* ptr) { mem().Free(ptr); }
private:
static auto mem() -> Mem<BitIp, kMemCount>& {
static Mem<BitIp, kMemCount> mem_;
return mem_;
}
};
///
constexpr auto BitMin(int mask) {
if (0 > mask) {
mask = 0;
} else if (kBits32 < mask) {
mask = kBits32;
}
return kBits32 - mask;
}
///
auto Closest(BitIp* bit_ip, int ip, int bit_min) {
for (int bit = kBits32 - 1; bit >= bit_min; bit--) {
if (0 == (ip & (1 << bit))) {
if (!bit_ip->false_) { return bit_ip; }
bit_ip = bit_ip->false_.get();
continue;
}
if (!bit_ip->true_) { return bit_ip; }
bit_ip = bit_ip->true_.get();
}
return bit_ip;
}
///
void Add(BitIp* closest, int ip, int bit_min) {
for (int bit = closest->bit_ - 1; bit >= bit_min; bit--) {
if (0 == (ip & (1 << bit))) {
closest->false_ = std::make_unique<BitIp>(bit, closest);
closest = closest->false_.get();
continue;
}
closest->true_ = std::make_unique<BitIp>(bit, closest);
closest = closest->true_.get();
}
closest->added_ = true;
}
///
void Delete(BitIp* closest, int bit_min) {
if (closest->bit_ > bit_min) { return; }
closest->added_ = false;
while (nullptr != closest->up_ && !closest->added_ && nullptr == closest->true_ && !closest->false_) {
BitIp* up = closest->up_;
if (up->true_.get() == closest) {
up->true_.reset();
} else {
up->false_.reset();
}
closest = up;
}
}
///
auto Find(BitIp* closest) -> BitIp* {
while (nullptr != closest && !closest->added_) { closest = closest->up_; }
return closest;
}
///
auto Mask(BitIp* bit_ip) -> int {
if (nullptr == bit_ip) { return kMaskNotFound; }
return kBits32 - bit_ip->bit_;
}
} // namespace
/**/
struct Prefixes::Opaque {
std::unique_ptr<BitIp> base{std::make_unique<BitIp>()};
};
/**/
Prefixes::Prefixes() : opaque_(new Opaque) {}
/**/
Prefixes::~Prefixes() = default;
/**/
void Prefixes::Add(int ip, int mask) {
auto bit_min = BitMin(mask);
::Add(::Closest(opaque_->base.get(), ip, bit_min), ip, bit_min);
}
/**/
void Prefixes::Del(int ip, int mask) {
auto bit_min = BitMin(mask);
::Delete(::Closest(opaque_->base.get(), ip, bit_min), bit_min);
}
/**/
auto Prefixes::Check(int ip) -> int { return ::Mask(::Find(::Closest(opaque_->base.get(), ip, BitMin(kBits32)))); }
/**/