Skip to content

Commit 27f51aa

Browse files
author
Gabriel Roels
committed
Almost removed all dependancies on slayer_starts
1 parent 5fd9b52 commit 27f51aa

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

src/tree/datastore.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -377,15 +377,15 @@ impl K2Tree {
377377
/* If no more leaves, then remove all stems immediately
378378
and don't bother with complex stuff below */
379379
self.stems = bitvec![0; stem_len];
380-
self.slayer_starts = vec![0];
380+
self.slayer_starts = vec![0]; //DEAD
381381
return Ok(())
382382
}
383383
self.stems.set(stem_bit_pos, false); //Dead leaf parent bit = 0
384384
let mut curr_layer = self.max_slayers-1;
385385
let mut stem_start = self.stem_start(stem_bit_pos);
386386
while curr_layer > 0
387387
&& all_zeroes(&self.stems, stem_start, stem_start+stem_len) {
388-
for layer_start in &mut self.slayer_starts[curr_layer+1..] {
388+
for layer_start in &mut self.slayer_starts[curr_layer+1..] { //DEAD
389389
// NOTE: this was 1 but it looks like that was an uncaught error, changed to stem_len
390390
// if any errors, look here.
391391
*layer_start -= stem_len; //Adjust lower layer start positions to reflect removal of stem
@@ -416,6 +416,7 @@ impl K2Tree {
416416
- Construct leaf corresponding to range containing (x, y)
417417
- Set bit at (x, y) to 1 */
418418
//Either 0 or == max_slayers?
419+
//ALIVE
419420
let mut layer_starts_len = self.slayer_starts.len(); //cannot replace with max_slayers bc might not be max?
420421
let mut layer = self.layer_from_range(stem_range);
421422
let mut subranges: SubRanges;
@@ -452,7 +453,7 @@ impl K2Tree {
452453
in the child layer. */
453454
if layer == layer_starts_len-1 {
454455
stem_start = self.stems.len();
455-
self.slayer_starts.push(stem_start);
456+
self.slayer_starts.push(stem_start); //ALIVE
456457
layer_starts_len += 1;
457458
}
458459
else {
@@ -481,7 +482,7 @@ impl K2Tree {
481482
/* If there are layers after the one we just insert a stem
482483
into: Increase the layer_starts to account for
483484
the extra stem */
484-
for layer_start in &mut self.slayer_starts[layer+1..] {
485+
for layer_start in &mut self.slayer_starts[layer+1..] { //DEAD
485486
*layer_start += stem_len;
486487
}
487488
}
@@ -611,10 +612,10 @@ impl K2Tree {
611612
if self.leaves.len() > 0 {
612613
/* Only insert the extra layers etc. if the
613614
tree isn't all 0s */
614-
for slayer_start in &mut self.slayer_starts {
615+
for slayer_start in &mut self.slayer_starts { //DEAD
615616
*slayer_start += stem_len;
616617
}
617-
self.slayer_starts.insert(0, 0);
618+
self.slayer_starts.insert(0, 0); //DEAD
618619
/* Insert 10...00 to beginning of stems */
619620
for _ in 0..stem_len-1 { self.stems.insert(0, false); }
620621
self.stems.insert(0, true);
@@ -670,8 +671,8 @@ impl K2Tree {
670671
})
671672
}
672673
self.max_slayers -= 1;
673-
self.slayer_starts.remove(0);
674-
for slayer_start in &mut self.slayer_starts {
674+
self.slayer_starts.remove(0); //DEAD
675+
for slayer_start in &mut self.slayer_starts { //DEAD
675676
*slayer_start -= stem_len;
676677
}
677678
/* Remove top layer stem */
@@ -698,8 +699,8 @@ impl K2Tree {
698699
pub unsafe fn shrink_unchecked(&mut self) {
699700
let stem_len = self.stem_len();
700701
self.max_slayers -= 1;
701-
self.slayer_starts.remove(0);
702-
for slayer_start in &mut self.slayer_starts {
702+
self.slayer_starts.remove(0); //DEAD
703+
for slayer_start in &mut self.slayer_starts { //DEAD
703704
*slayer_start -= stem_len;
704705
}
705706
/* Remove top layer stem */
@@ -924,7 +925,7 @@ impl K2Tree {
924925
else {
925926
let nth_leaf = ones_in_range(
926927
&self.stems,
927-
self.slayer_starts[self.max_slayers-1],
928+
self.layer_start(self.max_slayers-1),
928929
stem_bitpos
929930
);
930931
Ok(nth_leaf * self.leaf_len())
@@ -1052,7 +1053,7 @@ impl K2Tree {
10521053
#[allow(dead_code)]
10531054
fn footprint(&self) -> usize {
10541055
let mut size: usize = std::mem::size_of_val(self);
1055-
size += std::mem::size_of::<usize>() * self.slayer_starts.len();
1056+
size += std::mem::size_of::<usize>() * self.slayer_starts.len(); //DEAD
10561057
size += self.stems.len() / 8;
10571058
size += self.leaves.len() / 8;
10581059
size

src/tree/mod.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ use bitvec::vec::BitVec;
2525

2626
impl K2Tree {
2727
fn layer_len(&self, l: usize) -> usize {
28-
if l == self.slayer_starts.len()-1 {
29-
return self.stems.len() - self.slayer_starts[l]
28+
if l == self.max_slayers-1 {
29+
return self.stems.len() - self.layer_start(l)
3030
}
31-
self.slayer_starts[l+1] - self.slayer_starts[l]
31+
let layer_starts = self.layer_starts();
32+
layer_starts[l+1] - layer_starts[l]
3233
}
3334
fn get_coords(&self, leaf_bit_pos: usize) -> [usize; 2] {
3435
/* Start at the leaf_bit and traverse our way up to the top of the tree,
@@ -69,25 +70,26 @@ impl K2Tree {
6970
}
7071
fn parent(&self, stem_start: usize) -> std::result::Result<[usize; 2], ()> {
7172
/* Returns [stem_start, bit_offset] */
72-
if stem_start < self.slayer_starts[1] {
73-
return Err(())
74-
}
7573
let stem_len = self.stem_len();
74+
if stem_start < stem_len {
75+
return Err(()) //First stem cannot have parent
76+
}
7677
/* Find which layer stem_start is in */
78+
let layer_starts = self.layer_starts();
7779
let stem_layer = {
7880
let mut layer = self.max_slayers-1; //If no match, must be in highest layer
79-
for (i, &layer_start) in self.slayer_starts.iter().enumerate() {
81+
for (i, &layer_start) in layer_starts.iter().enumerate() {
8082
if stem_start < layer_start { layer = i-1; break }
8183
}
8284
layer
8385
};
8486
/* Find the nth stem it is in the layer */
85-
let stem_num = (stem_start - self.slayer_starts[stem_layer]) / stem_len;
87+
let stem_num = (stem_start - layer_starts[stem_layer]) / stem_len;
8688
/* Find the nth 1 in the parent layer */
8789
let parent_bit = one_positions_range(
8890
&self.stems,
89-
self.slayer_starts[stem_layer-1],
90-
self.slayer_starts[stem_layer]
91+
layer_starts[stem_layer-1],
92+
layer_starts[stem_layer]
9193
)[stem_num];
9294
Ok([self.stem_start(parent_bit), parent_bit % stem_len])
9395
}

0 commit comments

Comments
 (0)