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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use crate::NibbleVec;
use endian_type::{BigEndian, LittleEndian};
use std::ffi::OsString;
use std::path::{Path, PathBuf};
pub trait TrieKey: PartialEq + Eq {
fn encode_bytes(&self) -> Vec<u8> {
panic!("implement this method or TrieKey::encode");
}
fn encode(&self) -> NibbleVec {
NibbleVec::from_byte_vec(self.encode_bytes())
}
}
#[derive(Debug)]
pub enum KeyMatch {
Partial(usize),
FirstPrefix,
SecondPrefix,
Full,
}
pub fn match_keys(start_idx: usize, first: &NibbleVec, second: &NibbleVec) -> KeyMatch {
let first_len = first.len() - start_idx;
let min_length = ::std::cmp::min(first_len, second.len());
for i in 0..min_length {
if first.get(start_idx + i) != second.get(i) {
return KeyMatch::Partial(i);
}
}
match (first_len, second.len()) {
(x, y) if x < y => KeyMatch::FirstPrefix,
(x, y) if x == y => KeyMatch::Full,
_ => KeyMatch::SecondPrefix,
}
}
pub fn check_keys<K: ?Sized>(key1: &K, key2: &K)
where
K: TrieKey,
{
if *key1 != *key2 {
panic!("multiple-keys with the same bit representation.");
}
}
impl TrieKey for Vec<u8> {
fn encode_bytes(&self) -> Vec<u8> {
self.clone()
}
}
impl TrieKey for [u8] {
fn encode_bytes(&self) -> Vec<u8> {
self.to_vec()
}
}
impl TrieKey for String {
fn encode_bytes(&self) -> Vec<u8> {
self.as_bytes().encode_bytes()
}
}
impl TrieKey for str {
fn encode_bytes(&self) -> Vec<u8> {
self.as_bytes().encode_bytes()
}
}
impl<'a, T: ?Sized + TrieKey> TrieKey for &'a T {
fn encode_bytes(&self) -> Vec<u8> {
(**self).encode_bytes()
}
}
impl<'a, T: ?Sized + TrieKey> TrieKey for &'a mut T {
fn encode_bytes(&self) -> Vec<u8> {
(**self).encode_bytes()
}
}
impl TrieKey for i8 {
fn encode_bytes(&self) -> Vec<u8> {
let mut v: Vec<u8> = Vec::with_capacity(1);
v.push(*self as u8);
v
}
}
impl TrieKey for u8 {
fn encode_bytes(&self) -> Vec<u8> {
let mut v: Vec<u8> = Vec::with_capacity(1);
v.push(*self);
v
}
}
#[cfg(unix)]
impl TrieKey for PathBuf {
fn encode_bytes(&self) -> Vec<u8> {
use std::os::unix::ffi::OsStringExt;
let str: OsString = self.clone().into();
str.into_vec()
}
}
#[cfg(unix)]
impl TrieKey for Path {
fn encode_bytes(&self) -> Vec<u8> {
use std::os::unix::ffi::OsStrExt;
self.as_os_str().as_bytes().encode_bytes()
}
}
impl<T> TrieKey for LittleEndian<T>
where
T: Eq + Copy,
{
fn encode_bytes(&self) -> Vec<u8> {
self.as_bytes().encode_bytes()
}
}
impl<T> TrieKey for BigEndian<T>
where
T: Eq + Copy,
{
fn encode_bytes(&self) -> Vec<u8> {
self.as_bytes().to_vec()
}
}
macro_rules! int_keys {
( $( $t:ty ),* ) => {
$(
impl TrieKey for $t {
fn encode_bytes(&self) -> Vec<u8> {
let be: BigEndian<$t> = From::from(*self);
be.encode_bytes()
}
}
)*
};
}
int_keys!(u16, u32, u64, i16, i32, i64, usize, isize);