Pārlūkot izejas kodu

feat(set): add public Set data structure

Danilo Gómez 1 gadu atpakaļ
vecāks
revīzija
606aa1c1e8
2 mainītis faili ar 29 papildinājumiem un 0 dzēšanām
  1. 2 0
      src/lib.rs
  2. 27 0
      src/set.rs

+ 2 - 0
src/lib.rs

@@ -1 +1,3 @@
 mod node;
+mod set;
+pub use set::Set;

+ 27 - 0
src/set.rs

@@ -0,0 +1,27 @@
+use crate::node::Node;
+
+#[derive(Debug)]
+pub struct Set<T: Ord> {
+    root: Option<Box<Node<T>>>,
+}
+
+impl<T: Ord> Default for Set<T> {
+    fn default() -> Self {
+        Set { root: None }
+    }
+}
+
+impl<T: Ord> Set<T> {
+    pub fn new() -> Self {
+        Default::default()
+    }
+
+    pub fn insert(&mut self, value: T) -> bool {
+        if let Some(ref mut node) = self.root {
+            node.insert(value)
+        } else {
+            self.root = Some(Box::new(Node::new(value)));
+            true
+        }
+    }
+}