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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//! A generic k-bucket table implementation.

use std::collections::VecDeque;

use generic_array::{ArrayLength, GenericArray};

/// A key type trait used for [`KBucketTable`]
pub trait KBucketKey {
    type Length: ArrayLength;
    type Distance: KBucketDistance;

    /// Calculate the distance between two `keys`.
    fn distance(&self, rhs: &Self) -> Self::Distance
    where
        Self: Sized;

    /// Returns the uniquely determined key with the given distance to `self`.
    ///
    /// This implements the following equivalence:
    ///
    /// `self xor other = distance <==> other = self xor distance`
    fn longest_common_prefix(&self, rhs: &Self) -> usize
    where
        Self: Sized;

    /// Returns the longest common prefix length with `rhs`.
    fn for_distance(&self, distance: Self::Distance) -> Self;
}
/// k-bucket key distance associated type must implement this trait.
pub trait KBucketDistance {
    /// Returns the integer part of the base 2 logarithm of the `distance`.
    ///
    /// Returns `None` if the distance is zero.
    fn k_index(&self) -> Option<u32>;
}

/// Represents a k-bucket instance.
pub struct KBucket<Key, Value>(VecDeque<(Key, Value)>);

impl<Key, Value> Default for KBucket<Key, Value> {
    fn default() -> Self {
        Self(VecDeque::default())
    }
}

impl<Key, Value> KBucket<Key, Value> {
    fn len(&self) -> usize {
        self.0.len()
    }
    fn remove(&mut self, remove_key: &Key) -> Option<(Key, Value)>
    where
        Key: PartialEq,
    {
        let mut remove_index = None;

        for (index, (key, _)) in self.0.iter().enumerate() {
            if *key == *remove_key {
                remove_index = Some(index);
                break;
            }
        }

        if let Some(remove_index) = remove_index {
            self.0.remove(remove_index)
        } else {
            None
        }
    }

    fn get(&self, key: &Key) -> Option<&Value>
    where
        Key: PartialEq,
    {
        for (k, value) in self.0.iter() {
            if *key == *k {
                return Some(value);
            }
        }

        None
    }
}

pub struct KBucketTable<Key, Value, const K: usize>
where
    Key: KBucketKey,
{
    local_key: Key,
    count: usize,
    buckets: Vec<KBucket<Key, Value>>,
    k_index: GenericArray<Option<usize>, Key::Length>,
}

impl<Key, Value, const K: usize> KBucketTable<Key, Value, K>
where
    Key: KBucketKey,
{
    /// Returns the const k value of the k-bucket.
    pub fn const_k() -> usize {
        K
    }
    /// Create a new KBucketsTable instance with local key value.
    pub fn new(local_key: Key) -> Self {
        assert!(K > 0, "the k must greater than zero");
        Self {
            local_key,
            count: 0,
            buckets: Default::default(),
            k_index: Default::default(),
        }
    }

    /// Return the parameter `K` of k-bucket.
    pub fn k(&self) -> usize {
        K
    }

    /// Returns the local node key.
    pub fn local_key(&self) -> &Key {
        &self.local_key
    }

    /// Get the count of stored keys.
    pub fn len(&self) -> usize {
        self.count
    }

    /// Returns value of given key.
    pub fn get(&self, key: &Key) -> Option<&Value>
    where
        Key: PartialEq,
    {
        let k_index = self.k_index_of(key);

        if let Some(bucket) = self.bucket(k_index) {
            bucket.get(key)
        } else {
            None
        }
    }

    /// Push value into k-bucket's bottom. and pop the LRU value from the k-bucket's top if the k-bucket is full.
    ///
    /// This function has no side effects if called with `local_key`.
    pub fn insert<Q>(&mut self, key: Q, value: Value) -> Option<(Key, Value)>
    where
        Key: From<Q>,
        Key: PartialEq,
    {
        let key = Key::from(key);

        let k_index = self.k_index_of(&key);

        let k_bucket = if let Some(index) = self.k_index[k_index] {
            self.buckets.get_mut(index).unwrap()
        } else {
            self.buckets.push(KBucket::default());
            self.k_index[k_index] = Some(self.buckets.len() - 1);
            self.buckets.last_mut().unwrap()
        };

        // the key is in the k-bucket, replace old value.
        if let Some((key, _)) = k_bucket.remove(&key) {
            k_bucket.0.push_back((key, value));
            return None;
        }

        // k-bucket is full
        if k_bucket.0.len() == K {
            let removed = k_bucket.0.pop_front().expect("K > 0");

            k_bucket.0.push_back((key, value));

            return Some(removed);
        } else {
            k_bucket.0.push_back((key, value));

            self.count += 1;

            return None;
        }
    }

    /// Remove the keypair from the table.
    ///
    /// Returns None, if the key is not exists.
    pub fn remove(&mut self, key: &Key) -> Option<Value>
    where
        Key: PartialEq,
    {
        let k_index = self.k_index_of(&key);

        if let Some(index) = self.k_index[k_index] {
            let bucket = self.buckets.get_mut(index).expect("Consistency Guarantee");

            bucket.remove(key).map(|(_, value)| value)
        } else {
            None
        }
    }

    /// Returns an iterator of up to `k` keys closest to `target`.
    pub fn closest_k(&self, target: &Key) -> KBucketTableIter<'_, Key, Value, K> {
        let k_index = self.k_index_of(target);

        let bucket_len = if let Some(bucket) = self.bucket(k_index) {
            if bucket.len() == K {
                return KBucketTableIter {
                    table: self,
                    k_count: 0,
                    k_row_from: k_index,
                    k_col_offset: 0,
                    k_max_count: K,
                };
            }

            bucket.len()
        } else {
            0
        };

        let mut k_offset = k_index;
        let mut k_end_offset = k_index;

        let mut k_inner_offset = 0;

        let mut nodes = bucket_len;

        while nodes < K {
            if k_offset > 0 {
                k_offset -= 1;
                if let Some(bucket) = self.bucket(k_offset) {
                    nodes += bucket.len();

                    if nodes >= K {
                        k_inner_offset = nodes - K;
                        // nodes = self.const_k;
                        break;
                    } else {
                        k_inner_offset = 0;
                    }
                } else {
                    k_inner_offset = 0;
                }
            } else if k_end_offset == self.k_index.len() {
                break;
            }

            if k_end_offset + 1 < self.k_index.len() {
                k_end_offset += 1;

                if let Some(bucket) = self.bucket(k_end_offset) {
                    nodes += bucket.len();

                    if nodes >= K {
                        break;
                    }
                }
            } else if k_offset == 0 {
                break;
            }
        }

        if nodes > K {
            nodes = K;
        }

        return KBucketTableIter {
            table: self,
            k_count: 0,
            k_row_from: k_offset,
            k_col_offset: k_inner_offset,
            k_max_count: nodes,
        };
    }

    /// Returns an iterator over all inserted keys.
    pub fn iter(&self) -> KBucketTableIter<'_, Key, Value, K> {
        return KBucketTableIter {
            table: self,
            k_count: 0,
            k_row_from: 0,
            k_col_offset: 0,
            k_max_count: self.count,
        };
    }

    /// Get bucket of one key.
    pub fn bucket_of_key(&self, key: &Key) -> Option<&KBucket<Key, Value>> {
        self.bucket(self.k_index_of(key))
    }

    fn k_index_of(&self, key: &Key) -> usize {
        let k_index = key
            .distance(&self.local_key)
            .k_index()
            .expect("Get local key index") as usize;

        assert!(k_index < self.k_index.len());

        k_index
    }

    fn bucket(&self, index: usize) -> Option<&KBucket<Key, Value>> {
        self.k_index[index].map(|index| &self.buckets[index])
    }
}

/// An immutable iterator over [`KBucketTable`]
pub struct KBucketTableIter<'a, Key, Value, const K: usize>
where
    Key: KBucketKey,
{
    table: &'a KBucketTable<Key, Value, K>,
    k_count: usize,
    k_row_from: usize,
    k_max_count: usize,
    k_col_offset: usize,
}

impl<'a, Key, Value, const K: usize> Iterator for KBucketTableIter<'a, Key, Value, K>
where
    Key: KBucketKey,
{
    type Item = &'a (Key, Value);

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if self.k_count == self.k_max_count {
                return None;
            }

            let k_bucket_offset = match self.table.k_index[self.k_row_from] {
                Some(k_bucket_offset) => k_bucket_offset,
                None => {
                    self.k_row_from += 1;
                    self.k_col_offset = 0;
                    continue;
                }
            };

            let k_bucket = &self.table.buckets[k_bucket_offset];

            //  In the bucket, iterate in MRU order
            let item = &k_bucket.0[k_bucket.0.len() - self.k_col_offset - 1];

            self.k_col_offset += 1;
            self.k_count += 1;

            if self.k_col_offset == k_bucket.0.len() {
                self.k_col_offset = 0;
                self.k_row_from += 1;
            }

            return Some(item);
        }
    }
}