vecdb/traits/from_coarser.rs
1use std::ops::RangeInclusive;
2
3/// Maps coarser-grained indices to ranges of finer-grained indices.
4///
5/// Useful for hierarchical index systems where one index type represents
6/// a range of another (e.g., mapping hour indices to timestamp ranges).
7pub trait FromCoarserIndex<T>
8where
9 T: Ord + From<usize>,
10{
11 /// Returns the minimum fine-grained index represented by the coarse index.
12 fn min_from(coarser: T) -> usize;
13
14 /// Returns the maximum fine-grained index represented by the coarse index.
15 /// Note: May exceed actual data length - use `max_from` for bounded results.
16 fn max_from_(coarser: T) -> usize;
17
18 /// Returns the maximum fine-grained index, bounded by the data length.
19 fn max_from(coarser: T, len: usize) -> usize {
20 Self::max_from_(coarser).min(len - 1)
21 }
22
23 /// Returns the inclusive range of fine-grained indices for the coarse index.
24 fn inclusive_range_from(coarser: T, len: usize) -> RangeInclusive<usize>
25 where
26 T: Clone,
27 {
28 Self::min_from(coarser.clone())..=Self::max_from(coarser, len)
29 }
30}