use core::fmt::Display;
pub trait IndexType: Copy + Display + Sized + Eq + Ord {
const MAX: Self;
fn get(self) -> usize;
fn new(element_num: usize) -> Result<Self, &'static str>;
#[inline(always)]
fn defined(self) -> bool {
self != Self::MAX
}
}
macro_rules! index_error {
(u64) => {
"Failed to insert a new element into IntervalMap/Set: number of elements is too large for u64."
};
($name:ident) => {
concat!(
"Failed to insert a new element into IntervalMap/Set: number of elements is too large for ",
stringify!($name),
", try using u64.")
};
}
macro_rules! impl_index {
($type:ident) => {
impl IndexType for $type {
const MAX: Self = core::$type::MAX;
#[inline(always)]
fn get(self) -> usize {
self as usize
}
#[inline]
fn new(element_num: usize) -> Result<Self, &'static str> {
let element_num = element_num as $type;
if element_num == core::$type::MAX {
Err(index_error!($type))
} else {
Ok(element_num as $type)
}
}
}
};
}
impl_index!(u8);
impl_index!(u16);
impl_index!(u32);
impl_index!(u64);
pub type DefaultIx = u32;