Skip to content
Snippets Groups Projects
Commit a5778c5e authored by frederik stehli's avatar frederik stehli
Browse files

[min_max] fixed bwd search and added tests

parent bb930a7d
No related branches found
No related tags found
No related merge requests found
...@@ -309,8 +309,8 @@ impl MinMax { ...@@ -309,8 +309,8 @@ impl MinMax {
while bottom_up && current_node > 0 { while bottom_up && current_node > 0 {
if current_node % 2 == 0 { if current_node % 2 == 0 {
if self.heap[current_node as usize - 1].max_excess >= look_for if self.heap[current_node as usize - 1].max_excess >= -1*look_for
&& self.heap[current_node as usize - 1].min_excess <= look_for && self.heap[current_node as usize - 1].min_excess <= -1*look_for
{ {
bottom_up = false; bottom_up = false;
top_down = true; top_down = true;
...@@ -325,34 +325,51 @@ impl MinMax { ...@@ -325,34 +325,51 @@ impl MinMax {
} }
while top_down { while top_down {
if self.heap[current_node as usize * 2 + 2].max_excess >= look_for if current_node >= self.heap.len() as u64 / 2 {
&& self.heap[current_node as usize * 2 + 2].min_excess <= look_for top_down = false;
block_search = true;
} else {
if self.heap[current_node as usize * 2 + 2].max_excess - self.heap[current_node as usize * 2 + 2].min_excess >= look_for.abs()
{ {
current_node = current_node * 2 + 2; current_node = current_node * 2 + 2;
} else if self.heap[current_node as usize * 2 + 1].max_excess >= look_for } else if self.heap[current_node as usize * 2 + 1].max_excess - self.heap[current_node as usize * 2 + 1].min_excess >= look_for.abs()
&& self.heap[current_node as usize * 2 + 1].min_excess <= look_for
{ {
current_node = current_node * 2 + 1; current_node = current_node * 2 + 1;
} else { } else {
//todo konnte nicht gefunden werden!! //todo konnte nicht gefunden werden!!
} }
}
}
if current_node < self.heap.len() as u64 / 2 { /*while top_down {
if current_node <= self.heap.len() as u64 / 2 {
top_down = false; top_down = false;
block_search = true; block_search = true;
} else {
if self.heap[current_node as usize * 2 + 2].max_excess >= -1 * look_for
&& self.heap[current_node as usize * 2 + 2].min_excess <= -1 * look_for
{
current_node = current_node * 2 + 2;
} else if self.heap[current_node as usize * 2 + 1].max_excess >= -1 * look_for
&& self.heap[current_node as usize * 2 + 1].min_excess <= -1 * look_for
{
current_node = current_node * 2 + 1;
} else {
//todo konnte nicht gefunden werden!!
} }
} }
}*/
if block_search { if block_search {
block_no = current_node - (self.heap.len() / 2) as u64; block_no = current_node - (self.heap.len() / 2) as u64;
begin_of_block = block_no * self.block_size; begin_of_block = block_no * self.block_size;
end_of_block = begin_of_block + self.block_size - 1; end_of_block = begin_of_block + self.block_size - 1;
position = end_of_block; position = end_of_block;
while !found && position >= end_of_block { while !found && position >= begin_of_block {
if self.bits[position + 1] { if self.bits[position + 1] {
look_for -= 1;
} else {
look_for += 1; look_for += 1;
} else {
look_for -= 1;
} }
if look_for == 0 { if look_for == 0 {
found = true; found = true;
...@@ -366,7 +383,7 @@ impl MinMax { ...@@ -366,7 +383,7 @@ impl MinMax {
Ok(position) Ok(position)
} else { } else {
//todo konnte nicht gefunden werden!! //todo konnte nicht gefunden werden!!
Ok(1) Ok(10000000)
} }
} }
...@@ -536,11 +553,14 @@ mod tests { ...@@ -536,11 +553,14 @@ mod tests {
} }
#[test] #[test]
#[ignore]
fn test_bwd_search() { fn test_bwd_search() {
let bits = let bits =
bit_vec![true, true, true, false, true, false, false, true, true, false, false, false]; bit_vec![true, true, true, false, true, false, false, true, true, false, false, false];
let min_max = MinMax::new(bits, 4); let min_max = MinMax::new(bits, 4);
assert_eq!(min_max.bwd_search(7, 1).unwrap(), 4); assert_eq!(min_max.bwd_search(7, 1).unwrap(), 4);
assert_eq!(min_max.bwd_search(5, -1).unwrap(), 0);
assert_eq!(min_max.bwd_search(10, 0).unwrap(), 6);
} }
#[test] #[test]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment