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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use std::ops::Index;
use std::mem;

#[derive(Debug, Clone, PartialEq, Eq)]
enum Color {
    Red,
    Black,
}

impl Color {
    fn flip(&mut self) {
        match *self {
            Color::Red => *self = Color::Black,
            Color::Black => *self = Color::Red,
        }
    }
}

#[derive(Debug, PartialEq, Eq)]
pub struct LLRB<K: Ord, V> {
    color: Color,
    kv: Option<(K, V)>,
    left: Option<Box<LLRB<K, V>>>,
    right: Option<Box<LLRB<K, V>>>,
}

impl<K: Ord, V> Default for LLRB<K, V> {
    fn default() -> LLRB<K, V> {
        LLRB {
            color: Color::Red,
            kv: None,
            left: None,
            right: None,
        }
    }
}

impl<'a, K: Ord, V> Index<&'a K> for LLRB<K, V>
    where K: Ord
{
    type Output = V;

    #[inline]
    fn index(&self, key: &K) -> &V {
        self.get(key).expect("no entry found for key")
    }
}

impl<K: Ord, V> LLRB<K, V> {
    pub fn new() -> LLRB<K, V> {
        LLRB::default()
    }

    fn rotate_left(&mut self) {
        if !is_red(&self.right) {
            return;
        }

        let mut right = self.right.take().unwrap();
        self.color.flip();
        right.color.flip();
        self.right = right.left.take();
        mem::swap(self, &mut *right);
        self.left = Some(right);
    }

    fn rotate_right(&mut self) {
        if !is_red(&self.left) {
            return;
        }

        let mut left = self.left.take().unwrap();
        self.color.flip();
        left.color.flip();
        self.left = left.right.take();
        mem::swap(self, &mut *left);
        self.right = Some(left);
    }

    fn color_flip(&mut self) {
        self.color.flip();
        if let Some(ref mut r) = self.right {
            r.color.flip();
        }
        if let Some(ref mut l) = self.left {
            l.color.flip();
        }
    }

    fn fix_up(&mut self) {}

    fn move_red_right(&mut self) {}

    fn move_red_left(&mut self) {}

    fn is_double_left_red(&self) -> bool {
        if let Some(ref l) = self.left {
            if l.color == Color::Red && is_red(&l.left) {
                return true;
            }
        }
        false

    }


    /// Returns a reference to the value corresponding to the key.
    ///
    /// The key may be any borrowed form of the tree's key type, but the ordering
    /// on the borrowed form *must* match the ordering on the key type.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use llrb::LLRB;
    ///
    /// let mut tree = LLRB::new();
    /// tree.insert(1, "a");
    /// assert_eq!(tree.get(&1), Some(&"a"));
    /// assert_eq!(tree.get(&2), None);
    /// ```
    pub fn get(&self, k: &K) -> Option<&V> {
        if let Some(ref kv) = self.kv {
            if &kv.0 == k {
                Some(&kv.1)
            } else if k < &kv.0 {
                if let Some(ref right) = self.right {
                    right.get(k)
                } else {
                    None
                }
            } else {
                if let Some(ref left) = self.left {
                    left.get(k)
                } else {
                    None
                }
            }
        } else {
            None
        }
    }

    /// Returns true if the tree contains a value for the specified key.
    ///
    /// The key may be any borrowed form of the tree's key type, but the ordering
    /// on the borrowed form *must* match the ordering on the key type.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use llrb::LLRB;
    ///
    /// let mut tree = LLRB::new();
    /// tree.insert(1, "a");
    /// assert_eq!(tree.contains_key(&1), true);
    /// assert_eq!(tree.contains_key(&2), false);
    /// ```
    pub fn contains_key(&self, k: &K) -> bool {
        self.get(k).is_some()
    }

    /// Returns a mutable reference to the value corresponding to the key.
    ///
    /// The key may be any borrowed form of the tree's key type, but the ordering
    /// on the borrowed form *must* match the ordering on the key type.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use llrb::LLRB;
    ///
    /// let mut tree = LLRB::new();
    /// tree.insert(1, "a");
    /// if let Some(x) = tree.get_mut(&1) {
    ///     *x = "b";
    /// }
    /// assert_eq!(tree[&1], "b");
    /// ```
    pub fn get_mut(&mut self, k: &K) -> Option<&mut V> {
        if let Some(ref mut kv) = self.kv {
            if &kv.0 == k {
                Some(&mut kv.1)
            } else if k < &kv.0 {
                if let Some(ref mut right) = self.right {
                    right.get_mut(k)
                } else {
                    None
                }
            } else {
                if let Some(ref mut left) = self.left {
                    left.get_mut(k)
                } else {
                    None
                }
            }
        } else {
            None
        }
    }

    /// Inserts a key-value pair into the tree.
    ///
    /// If the tree did not have this key present, `None` is returned.
    ///
    /// If the tree did have this key present, the value is updated, and the old
    /// value is returned. The key is not updated, though; this matters for
    /// types that can be `==` without being identical.
    ///
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use llrb::LLRB;
    ///
    /// let mut tree = LLRB::new();
    /// assert_eq!(tree.insert(37, "a"), None);
    /// assert_eq!(tree.is_empty(), false);
    ///
    /// tree.insert(37, "b");
    /// assert_eq!(tree.insert(37, "c"), Some("b"));
    /// assert_eq!(tree[&37], "c");
    /// ```
    pub fn insert(&mut self, k: K, v: V) -> Option<V> {
        if self.kv.is_none() {
            self.kv = Some((k, v));
            return None;
        }

        if is_red(&self.right) && is_red(&self.left) {
            self.color_flip();
        }

        let old = if let Some((ref key, ref mut val)) = self.kv {
            if key == &k {
                let old = mem::replace(val, v);
                Some(old)
            } else if &k < key {
                populate_insert(&mut self.left, k, v)
            } else {
                populate_insert(&mut self.right, k, v)
            }
        } else {
            None
        };

        if is_red(&self.right) {
            self.rotate_left();
        }

        if self.is_double_left_red() {
            self.rotate_right();
        }

        old
    }

    /// Removes a key from the tree, returning the value at the key if the key
    /// was previously in the tree.
    ///
    /// The key may be any borrowed form of the tree's key type, but the ordering
    /// on the borrowed form *must* match the ordering on the key type.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use llrb::LLRB;
    ///
    /// let mut tree = LLRB::new();
    /// tree.insert(1, "a");
    /// assert_eq!(tree.remove(&1), Some("a"));
    /// assert_eq!(tree.remove(&1), None);
    /// ```
    pub fn remove(&mut self, k: &K) -> Option<V> {
        let mut old = None;
        let left_is_red = is_red(&self.left);
        let double_left_red = self.is_double_left_red();
        let right_is_red = is_red(&self.left);
        if let Some((ref mut key, ref mut val)) = self.kv {
            if k > key {
                if !left_is_red && !double_left_red {
                    // self.move_red_left();
                }
                if let Some(ref mut left) = self.left {
                    old = left.remove(k);
                }
            }
        }
        self.fix_up();
        old
    }

    fn min(&self) -> &LLRB<K, V> {
        self
    }

    /// Moves all elements from `other` into `Self`, leaving `other` empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use llrb::LLRB;
    ///
    /// let mut a = LLRB::new();
    /// a.insert(1, "a");
    /// a.insert(2, "b");
    /// a.insert(3, "c");
    ///
    /// let mut b = LLRB::new();
    /// b.insert(3, "d");
    /// b.insert(4, "e");
    /// b.insert(5, "f");
    ///
    /// a.append(&mut b);
    ///
    /// assert_eq!(a.len(), 5);
    /// assert_eq!(b.len(), 0);
    ///
    /// assert_eq!(a[&1], "a");
    /// assert_eq!(a[&2], "b");
    /// assert_eq!(a[&3], "d");
    /// assert_eq!(a[&4], "e");
    /// assert_eq!(a[&5], "f");
    /// ```
    pub fn append(&mut self, other: &mut Self) {}

    /// Returns the number of elements in the tree.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use llrb::LLRB;
    ///
    /// let mut a = LLRB::new();
    /// assert_eq!(a.len(), 0);
    /// a.insert(1, "a");
    /// assert_eq!(a.len(), 1);
    /// ```
    pub fn len(&self) -> usize {
        // TODO store this in metadata encapsulating struct
        if self.kv.is_none() {
            0
        } else {
            let llen = if let Some(ref l) = self.left {
                l.len()
            } else {
                0
            };
            let rlen = if let Some(ref r) = self.right {
                r.len()
            } else {
                0
            };
            1 + llen + rlen
        }
    }

    /// Returns true if the tree contains no elements.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use llrb::LLRB;
    ///
    /// let mut a = LLRB::new();
    /// assert!(a.is_empty());
    /// a.insert(1, "a");
    /// assert!(!a.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

fn is_red<K: Ord, V>(node: &Option<Box<LLRB<K, V>>>) -> bool {
    if let &Some(ref n) = node {
        if n.color == Color::Red {
            return true;
        }
    }
    false
}

fn populate_insert<K: Ord, V>(child_ref: &mut Option<Box<LLRB<K, V>>>, k: K, v: V) -> Option<V> {
    let mut child = child_ref.take().unwrap_or_else(|| Box::new(LLRB::default()));
    let ret = child.insert(k, v);
    *child_ref = Some(child);
    ret
}

#[cfg(test)]
mod tests {
    // TODO property: even black height

    // TODO property: never two reds in a row

    // TODO property: never right-leaning representation of 3-Node
}