Skip to content

Instantly share code, notes, and snippets.

@andresilva
Last active December 4, 2018 06:00
Show Gist options
  • Save andresilva/dc51b73ecac7224b8117 to your computer and use it in GitHub Desktop.
Save andresilva/dc51b73ecac7224b8117 to your computer and use it in GitHub Desktop.
rust pattern match rc
use std::rc::Rc;
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Color {
Black,
Red
}
use Color::{Black, Red};
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum RedBlackTree<T> {
Tip,
Node(Color, Rc<RedBlackTree<T>>, T, Rc<RedBlackTree<T>>)
}
use RedBlackTree::{Tip, Node};
fn balance<T>(c: Color, l: Rc<RedBlackTree<T>>, v: T, r: Rc<RedBlackTree<T>>) -> RedBlackTree<T> {
match (c, *l, v, *r) {
(Black, Node(Red, Node(Red, a, x, b), y, c), z, d) |
(Black, Node(Red, a, x, Node(Red, b, y, c)), z, d) |
(Black, a, x, Node(Red, Node(Red, b, y, c), z, d)) |
(Black, a, x, Node(Red, b, y, Node(Red, c, z, d))) =>
Node(Red, Node(Black, a, x, b), y, Node(Black, c, z, d)),
_ => Node(c, l, v, r)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment