tensor4all_tcicore/cached_function/index_int.rs
1//! Index integer trait for multi-index elements.
2//!
3//! Use smaller types (e.g., `u8`) for quantics TCI where `local_dim = 2`.
4
5/// Trait for index element types.
6///
7/// All indices within a single `CachedFunction` must use the same integer type.
8/// Implemented for `u8`, `u16`, `u32`, and `usize`. Use `u8` for quantics TCI
9/// where `local_dim = 2`.
10///
11/// # Examples
12///
13/// ```
14/// use tensor4all_tcicore::{IndexInt, CachedFunction};
15///
16/// // Using u8 indices for quantics (local_dim = 2)
17/// let cf = CachedFunction::new(
18/// |idx: &[u8]| idx.iter().map(|&x| x as usize).sum::<usize>(),
19/// &[2, 2, 2],
20/// ).unwrap();
21/// assert_eq!(cf.eval(&[1u8, 0, 1]), 2);
22/// ```
23pub trait IndexInt: Copy + Send + Sync + 'static {
24 /// Convert this index element to `usize`.
25 fn to_usize(self) -> usize;
26}
27
28impl IndexInt for u8 {
29 fn to_usize(self) -> usize {
30 self as usize
31 }
32}
33
34impl IndexInt for u16 {
35 fn to_usize(self) -> usize {
36 self as usize
37 }
38}
39
40impl IndexInt for u32 {
41 fn to_usize(self) -> usize {
42 self as usize
43 }
44}
45
46impl IndexInt for usize {
47 fn to_usize(self) -> usize {
48 self
49 }
50}