pub fn insert(&mut self, node: ReorgNode<K, M>, most_valuable: Option<bool>) {
        // if new node older than we search, we don't care about it
        if node.height <= self.allowed_oldest() {
            return;
        }
        // if new nodes parent isn't stored already and it's height isn't greater than
        // what we know the newest to be, we don't care about it
        if !self.nodes_by_key.contains_key(&node.parent) && node.height <= self.height {
            return;
        }
        // when the root nodes depth reaches the threshold we predetermined
        if self.root.height == self.allowed_oldest() {
            match self.root.children.len() {
                0 => {}
                1 => {
                    // In case the root has only one child, the child becomes the new node.
                    // If this fails that means the children of the root were already removed.
                    self.root = self.nodes_by_key.remove(&self.root.children[0]).unwrap();
                }
                _ => {
                    // In case the root has multiple children we determine the longest branch.
                    let remove = self.root.children.clone();
                    // We replace the current root with its child that heirs the longest lineage.
                    // If this fails that means that the branch has already been removed.
                    self.root =
                        self.nodes_by_key
                            .remove(&self.find_longest_branch(Some(
                                most_valuable.unwrap_or(self.value_based),
                            )))
                            .unwrap();
                    for dead_branch in remove {
                        // we delete every branch stemming from the root other than the longest one
                        if dead_branch != self.root.key {
                            self.delete_children(&dead_branch);
                        }
                    }
                }
            }
        }
        // Retrieving the inserted nodes parent to append said node to the
        // parents list of children. If neither ifs trigger than parent is not part
        // of the system, and we put the node into the buffer.
        if let Some(parent) = self.nodes_by_key.get_mut(&node.parent) {
            parent.children.push(node.key);
        } else if node.parent == self.root.key {
            self.root.children.push(node.key);
        } else {
            self.buffer.insert(node.key, node);
            return;
        }
        // We save the node key to its height
        match self.nodes_by_height.get_mut(&node.height) {
            Some(has_node) => has_node.push(node.key),
            None => {
                self.nodes_by_height.insert(node.height, vec![node.key]);
            }
        };
        // This is the newest node hence we take its height as the new system height
        self.height = node.height;
        // We save the node itself with its key as the key
        self.nodes_by_key.insert(node.key, node);
        // We double check for nodes that should have already been removed
        self.nodes_by_key.remove(&self.root.parent);
        if let Some(old_root) = self
            .nodes_by_height
            .remove(&(self.root.height.saturating_sub(1)))
        {
            for old in old_root {
                self.nodes_by_key.remove(&old);
            }
        }

        let mut reinsert = Vec::new();
        let mut buffer_clear = Vec::new();
        // We check the nodes in the buffer wether they have expired,
        // then we check if their parents have been pushed into the system.
        for (key, buffer_node) in &self.buffer {
            if buffer_node.height < self.allowed_oldest() {
                buffer_clear.push(*key);
                continue;
            }
            if let Some(parent) = self.nodes_by_key.get_mut(&buffer_node.parent) {
                parent.children.push(*key);
                reinsert.push(*key);
            }
        }
        // If we found the parent of a node in the buffer, we save it
        for r in reinsert {
            if let Some(reinsertable) = self.buffer.remove(&r) {
                match self.nodes_by_height.get_mut(&reinsertable.height) {
                    Some(has_node) => has_node.push(r),
                    None => {
                        self.nodes_by_height.insert(reinsertable.height, vec![r]);
                    }
                };
                self.nodes_by_key.insert(r, reinsertable);
            }
        }
        // If the node has expired we remove if from the buffer-
        for bc in buffer_clear {
            self.buffer.remove(&bc);
        }
    }
/// Main logic of the reorganizational functionality. Determines the validity of the
/// inserted node by checking its height and its parent then
/// saves it into a branch if a viable parent is present and the height is acceptable,
/// or into the buffer if parent is not present but has a good height.
/// Otherwise the node is discarded.
/// The height of the node is considered good if its greater than that of the current root.
/// Panics
/// A panic will occur if a node has a child listed that we do not have
/// stored by its key.
Main logic of the reorganizational functionality. Determines the validity of the
inserted node by checking its height and its parent then
saves it into a branch if a viable parent is present and the height is acceptable,
or into the buffer if parent is not present but has a good height.
Otherwise the node is discarded.
The height of the node is considered good if its greater than that of the current root.
Panics
A panic will occur if a node has a child listed that we do not have
stored by its key.
Main logic of the reorganizational functionality . Determines the validity of the inserted node by checking its height and its parent then saves it into a branch if a viable parent is present and the height is acceptable or into the buffer if parent is not present but has a good height . Otherwise the node is discarded . The height of the node is considered good if its greater than that of the current root . Panics A panic will occur if a node has a child listed that we do not have stored by its key .
pub fn insert(&mut self, node: ReorgNode<K, M>, most_valuable: Option<bool>) {
        // if new node older than we search, we don't care about it
        if node.height <= self.allowed_oldest() {
            return;
        }
        // if new nodes parent isn't stored already and it's height isn't greater than
        // what we know the newest to be, we don't care about it
        if !self.nodes_by_key.contains_key(&node.parent) && node.height <= self.height {
            return;
        }
        // when the root nodes depth reaches the threshold we predetermined
        if self.root.height == self.allowed_oldest() {
            match self.root.children.len() {
                0 => {}
                1 => {
                    // In case the root has only one child, the child becomes the new node.
                    // If this fails that means the children of the root were already removed.
                    self.root = self.nodes_by_key.remove(&self.root.children[0]).unwrap();
                }
                _ => {
                    // In case the root has multiple children we determine the longest branch.
                    let remove = self.root.children.clone();
                    // We replace the current root with its child that heirs the longest lineage.
                    // If this fails that means that the branch has already been removed.
                    self.root =
                        self.nodes_by_key
                            .remove(&self.find_longest_branch(Some(
                                most_valuable.unwrap_or(self.value_based),
                            )))
                            .unwrap();
                    for dead_branch in remove {
                        // we delete every branch stemming from the root other than the longest one
                        if dead_branch != self.root.key {
                            self.delete_children(&dead_branch);
                        }
                    }
                }
            }
        }
        // Retrieving the inserted nodes parent to append said node to the
        // parents list of children. If neither ifs trigger than parent is not part
        // of the system, and we put the node into the buffer.
        if let Some(parent) = self.nodes_by_key.get_mut(&node.parent) {
            parent.children.push(node.key);
        } else if node.parent == self.root.key {
            self.root.children.push(node.key);
        } else {
            self.buffer.insert(node.key, node);
            return;
        }
        // We save the node key to its height
        match self.nodes_by_height.get_mut(&node.height) {
            Some(has_node) => has_node.push(node.key),
            None => {
                self.nodes_by_height.insert(node.height, vec![node.key]);
            }
        };
        // This is the newest node hence we take its height as the new system height
        self.height = node.height;
        // We save the node itself with its key as the key
        self.nodes_by_key.insert(node.key, node);
        // We double check for nodes that should have already been removed
        self.nodes_by_key.remove(&self.root.parent);
        if let Some(old_root) = self
            .nodes_by_height
            .remove(&(self.root.height.saturating_sub(1)))
        {
            for old in old_root {
                self.nodes_by_key.remove(&old);
            }
        }
        let mut reinsert = Vec::new();
        let mut buffer_clear = Vec::new();
        // We check the nodes in the buffer wether they have expired,
        // then we check if their parents have been pushed into the system.
        for (key, buffer_node) in &self.buffer {
            if buffer_node.height < self.allowed_oldest() {
                buffer_clear.push(*key);
                continue;
            }
            if let Some(parent) = self.nodes_by_key.get_mut(&buffer_node.parent) {
                parent.children.push(*key);
                reinsert.push(*key);
            }
        }
        // If we found the parent of a node in the buffer, we save it
        for r in reinsert {
            if let Some(reinsertable) = self.buffer.remove(&r) {
                match self.nodes_by_height.get_mut(&reinsertable.height) {
                    Some(has_node) => has_node.push(r),
                    None => {
                        self.nodes_by_height.insert(reinsertable.height, vec![r]);
                    }
                };
                self.nodes_by_key.insert(r, reinsertable);
            }
        }
        // If the node has expired we remove if from the buffer-
        for bc in buffer_clear {
            self.buffer.remove(&bc);
        }
    }
pub fn insert ( & mut self , node : ReorgNode < K , M > , most_valuable : Option < bool > ) { // if new node older than we search, we don't care about it if node . height <= self . allowed_oldest ( ) { return ; } // if new nodes parent isn't stored already and it's height isn't greater than // what we know the newest to be, we don't care about it if ! self . nodes_by_key . contains_key ( & node . parent ) && node . height <= self . height { return ; } // when the root nodes depth reaches the threshold we predetermined if self . root . height == self . allowed_oldest ( ) { match self . root . children . len ( ) { 0 => { } 1 => { // In case the root has only one child, the child becomes the new node. // If this fails that means the children of the root were already removed. self . root = self . nodes_by_key . remove ( & self . root . children [ 0 ] ) . unwrap ( ) ; } _ => { // In case the root has multiple children we determine the longest branch. let remove = self . root . children . clone ( ) ; // We replace the current root with its child that heirs the longest lineage. // If this fails that means that the branch has already been removed. self . root = self . nodes_by_key . remove ( & self . find_longest_branch ( Some ( most_valuable . unwrap_or ( self . value_based ) , ) ) ) . unwrap ( ) ; for dead_branch in remove { // we delete every branch stemming from the root other than the longest one if dead_branch != self . root . key { self . delete_children ( & dead_branch ) ; } } } } } // Retrieving the inserted nodes parent to append said node to the // parents list of children. If neither ifs trigger than parent is not part // of the system, and we put the node into the buffer. if let Some ( parent ) = self . nodes_by_key . get_mut ( & node . parent ) { parent . children . push ( node . key ) ; } else if node . parent == self . root . key { self . root . children . push ( node . key ) ; } else { self . buffer . insert ( node . key , node ) ; return ; } // We save the node key to its height match self . nodes_by_height . get_mut ( & node . height ) { Some ( has_node ) => has_node . push ( node . key ) , None => { self . nodes_by_height . insert ( node . height , vec ! [ node key ] ) ; } } ; // This is the newest node hence we take its height as the new system height self . height = node . height ; // We save the node itself with its key as the key self . nodes_by_key . insert ( node . key , node ) ; // We double check for nodes that should have already been removed self . nodes_by_key . remove ( & self . root . parent ) ; if let Some ( old_root ) = self . nodes_by_height . remove ( & ( self . root . height . saturating_sub ( 1 ) ) ) { for old in old_root { self . nodes_by_key . remove ( & old ) ; } } let mut reinsert = Vec :: new ( ) ; let mut buffer_clear = Vec :: new ( ) ; // We check the nodes in the buffer wether they have expired, // then we check if their parents have been pushed into the system. for ( key , buffer_node ) in & self . buffer { if buffer_node . height < self . allowed_oldest ( ) { buffer_clear . push ( * key ) ; continue ; } if let Some ( parent ) = self . nodes_by_key . get_mut ( & buffer_node . parent ) { parent . children . push ( * key ) ; reinsert . push ( * key ) ; } } // If we found the parent of a node in the buffer, we save it for r in reinsert { if let Some ( reinsertable ) = self . buffer . remove ( & r ) { match self . nodes_by_height . get_mut ( & reinsertable . height ) { Some ( has_node ) => has_node . push ( r ) , None => { self . nodes_by_height . insert ( reinsertable . height , vec ! [ r ] ) ; } } ; self . nodes_by_key . insert ( r , reinsertable ) ; } } // If the node has expired we remove if from the buffer- for bc in buffer_clear { self . buffer . remove ( & bc ) ; } }
Main logic of the reorganizational functionality.
Main logic of the reorganizational functionality .
&mut self
node
ReorgNode<K, M>
most_valuable
Option<bool>
&mut self
node
ReorgNode<K, M>
most_valuable
Option<bool>