Insertion
This commit is contained in:
parent
b22dd00177
commit
0f3ee41673
1 changed files with 13 additions and 0 deletions
|
|
@ -8,6 +8,7 @@ pub struct NodeModifier<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> NodeModifier<'a> {
|
impl<'a> NodeModifier<'a> {
|
||||||
|
//Builders
|
||||||
pub fn from_random(root: &'a mut Node) -> NodeModifier<'a> {
|
pub fn from_random(root: &'a mut Node) -> NodeModifier<'a> {
|
||||||
let mut selected = root;
|
let mut selected = root;
|
||||||
while random_range(0..PICK_STOP_PROBABILITY) == PICK_STOP_PROBABILITY - 1 {
|
while random_range(0..PICK_STOP_PROBABILITY) == PICK_STOP_PROBABILITY - 1 {
|
||||||
|
|
@ -21,4 +22,16 @@ impl<'a> NodeModifier<'a> {
|
||||||
pub fn from_node(node: &'a mut Node) -> NodeModifier<'a> {
|
pub fn from_node(node: &'a mut Node) -> NodeModifier<'a> {
|
||||||
NodeModifier { picked_node: node }
|
NodeModifier { picked_node: node }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn add_node(&mut self, node: Node) {
|
||||||
|
self.picked_node.children.push(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insert_node(&mut self, mut node: Node, between: Option<usize>) {
|
||||||
|
let children_count = self.picked_node.children.len();
|
||||||
|
let operated_index = between.unwrap_or(random_range(0..children_count));
|
||||||
|
let moved = self.picked_node.children.remove(operated_index);
|
||||||
|
node.children.push(moved);
|
||||||
|
self.picked_node.children.insert(operated_index, node);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue