forked from p4lang/behavioral-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lpm_trie.h
98 lines (77 loc) · 2.73 KB
/
lpm_trie.h
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
/* Copyright 2013-present Barefoot Networks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Antonin Bas (antonin@barefootnetworks.com)
*
*/
#ifndef BM_SIM_LPM_TRIE_H_
#define BM_SIM_LPM_TRIE_H_
#include <bf_lpm_trie/bf_lpm_trie.h>
#include <algorithm> // for std::swap
namespace bm {
static_assert(sizeof(value_t) == sizeof(uintptr_t),
"Invalid type sizes");
class LPMTrie {
public:
explicit LPMTrie(size_t key_width_bytes)
: key_width_bytes(key_width_bytes) {
trie = bf_lpm_trie_create(key_width_bytes, true);
}
/* Copy constructor */
LPMTrie(const LPMTrie& other) = delete;
/* Move constructor */
LPMTrie(LPMTrie&& other) noexcept
: key_width_bytes(other.key_width_bytes), trie(other.trie) {}
~LPMTrie() {
bf_lpm_trie_destroy(trie);
}
/* Copy assignment operator */
LPMTrie &operator=(const LPMTrie &other) = delete;
/* Move assignment operator */
LPMTrie &operator=(LPMTrie &&other) noexcept {
key_width_bytes = other.key_width_bytes;
std::swap(trie, other.trie);
return *this;
}
void insert_prefix(const ByteContainer &prefix, int prefix_length,
uintptr_t value) {
bf_lpm_trie_insert(trie, prefix.data(), prefix_length,
static_cast<value_t>(value));
}
bool delete_prefix(const ByteContainer &prefix, int prefix_length) {
return bf_lpm_trie_delete(trie, prefix.data(), prefix_length);
}
bool has_prefix(const ByteContainer &prefix, int prefix_length) const {
return bf_lpm_trie_has_prefix(trie, prefix.data(), prefix_length);
}
bool retrieve_value(const ByteContainer &prefix, int prefix_length,
uintptr_t *value) const {
return bf_lpm_trie_retrieve_value(trie, prefix.data(), prefix_length,
reinterpret_cast<value_t *>(value));
}
bool lookup(const ByteContainer &key, uintptr_t *value) const {
return bf_lpm_trie_lookup(trie, key.data(),
reinterpret_cast<value_t *>(value));
}
void clear() {
bf_lpm_trie_destroy(trie);
trie = bf_lpm_trie_create(key_width_bytes, true);
}
private:
size_t key_width_bytes{0};
bf_lpm_trie_t *trie{nullptr};
};
} // namespace bm
#endif // BM_SIM_LPM_TRIE_H_