1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#[cfg(not(feature = "bigidx"))]
use arrow::array::UInt32Array;
#[cfg(feature = "bigidx")]
use arrow::array::UInt64Array;
use num::{NumCast, Signed, Zero};
pub trait IndexToUsize {
fn negative_to_usize(self, len: usize) -> Option<usize>;
}
impl<I> IndexToUsize for I
where
I: PartialOrd + PartialEq + NumCast + Signed + Zero,
{
#[inline]
fn negative_to_usize(self, len: usize) -> Option<usize> {
if self >= Zero::zero() {
if (self.to_usize().unwrap()) < len {
Some(self.to_usize().unwrap())
} else {
None
}
} else {
let subtract = self.abs().to_usize().unwrap();
if subtract > len {
None
} else {
Some(len - subtract)
}
}
}
}
#[cfg(not(feature = "bigidx"))]
pub type IdxSize = u32;
#[cfg(feature = "bigidx")]
pub type IdxSize = u64;
#[cfg(not(feature = "bigidx"))]
pub type IdxArr = UInt32Array;
#[cfg(feature = "bigidx")]
pub type IdxArr = UInt64Array;
pub fn indexes_to_usizes(idx: &[IdxSize]) -> impl Iterator<Item = usize> + '_ {
idx.iter().map(|idx| *idx as usize)
}