Skip to content

Commit

Permalink
keep the stack sorted and merged by key
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaGuarracino committed Jan 2, 2025
1 parent 22ae608 commit 6c5a3b8
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/impg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ pub struct SerializableInterval {
metadata: QueryMetadata,
}

// Align ranges to cache line boundary for better memory access
#[repr(align(64))]
#[derive(Debug, Default, Clone)]
pub struct SortedRanges {
pub ranges: Vec<(i32, i32)>
Expand Down Expand Up @@ -142,22 +140,22 @@ impl SortedRanges {
} else {
(new_range.1, new_range.0)
};

// Return regions that don't overlap with existing ranges
let mut non_overlapping = Vec::new();
let mut current = start;

// Find the first range that could overlap
let mut i = match self.ranges.binary_search_by_key(&start, |&(s, _)| s) {
Ok(pos) => pos,
Err(pos) => pos,
};

// Check previous range for overlap
if i > 0 && self.ranges[i - 1].1 > start {
i -= 1;
}

// Process all potentially overlapping ranges
while i < self.ranges.len() && current < end {
let (range_start, range_end) = self.ranges[i];
Expand All @@ -170,11 +168,11 @@ impl SortedRanges {
current = max(current, range_end);
i += 1;
}

if current < end {
non_overlapping.push((current, end));
}

// Now insert the range while maintaining sorted order and merging overlaps
match self.ranges.binary_search_by_key(&start, |&(s, _)| s) {
Ok(pos) | Err(pos) => {
Expand All @@ -186,12 +184,12 @@ impl SortedRanges {
self.ranges[pos].0 = min(start, self.ranges[pos].0);
self.ranges[pos].1 = max(end, self.ranges[pos].1);
self.merge_forward_from(pos);
} else {
} else {
self.ranges.insert(pos, (start, end));
}
}
}

non_overlapping
}

Expand Down Expand Up @@ -432,7 +430,21 @@ impl Impg {
}
}
});
}

// Merge contiguous/overlapping ranges with same sequence_id
stack.sort_by_key(|(id, start, _)| (*id, *start));
let mut write = 0;
for read in 1..stack.len() {
if stack[write].0 == stack[read].0 && // Same sequence_id
stack[write].2 >= stack[read].1 { // Overlapping or contiguous
stack[write].2 = stack[write].2.max(stack[read].2);
} else {
write += 1;
stack.swap(write, read);
}
}
stack.truncate(write + 1);
}
}

results
Expand Down

0 comments on commit 6c5a3b8

Please sign in to comment.