Skip to content

Commit 6035f12

Browse files
committed
feat: add Coordinate and DynamicStorage
1 parent e6e9b93 commit 6035f12

File tree

4 files changed

+262
-150
lines changed

4 files changed

+262
-150
lines changed

src/coordinate.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use std::fmt;
2+
use std::ops::Index;
3+
use std::ops::IndexMut;
4+
5+
#[derive(Debug, Clone, PartialEq)]
6+
pub struct Coordinate {
7+
indices: Vec<usize>,
8+
}
9+
10+
impl Coordinate {
11+
pub fn new(indices: Vec<usize>) -> Self {
12+
Self { indices }
13+
}
14+
15+
pub fn len(&self) -> usize {
16+
self.indices.len()
17+
}
18+
19+
pub fn iter(&self) -> std::slice::Iter<'_, usize> {
20+
self.indices.iter()
21+
}
22+
23+
pub fn insert(&self, index: usize, axis: usize) -> Self {
24+
let mut new_indices = self.indices.clone();
25+
new_indices.insert(index, axis);
26+
Self { indices: new_indices }
27+
}
28+
}
29+
30+
impl Index<usize> for Coordinate {
31+
type Output = usize;
32+
33+
fn index(&self, index: usize) -> &Self::Output {
34+
&self.indices[index]
35+
}
36+
}
37+
38+
impl IndexMut<usize> for Coordinate {
39+
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
40+
&mut self.indices[index]
41+
}
42+
}
43+
44+
impl fmt::Display for Coordinate {
45+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46+
use itertools::Itertools;
47+
let idxs = self.indices.iter().map(|&x| format!("{}", x)).join(", ");
48+
write!(f, "({})", idxs)
49+
}
50+
}
51+
52+
#[macro_export]
53+
macro_rules! coord {
54+
($($index:expr),*) => {
55+
Coordinate::new(vec![$($index),*])
56+
};
57+
58+
($index:expr; $count:expr) => {
59+
Coordinate::new(vec![$index; $count])
60+
};
61+
}

src/iter.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
1+
use crate::coord;
2+
use crate::coordinate::Coordinate;
3+
use crate::shape::Shape;
4+
15
pub struct IndexIterator {
2-
shape: Vec<usize>,
3-
current: Vec<usize>,
6+
shape: Shape,
7+
current: Coordinate,
48
done: bool,
59
}
610

711
impl IndexIterator {
8-
pub fn new(shape: &[usize]) -> Self {
9-
let current = vec![0; shape.len()];
12+
pub fn new(shape: &Shape) -> Self {
13+
let current = coord![0; shape.order()];
1014
IndexIterator {
11-
shape: shape.to_vec(),
15+
shape: shape.clone(),
1216
current,
1317
done: false,
1418
}
1519
}
1620
}
1721

1822
impl Iterator for IndexIterator {
19-
type Item = Vec<usize>;
23+
type Item = Coordinate;
2024

2125
fn next(&mut self) -> Option<Self::Item> {
22-
if self.done || self.shape.len() == 0 {
26+
if self.done || self.shape.order() == 0 {
2327
return None;
2428
}
2529

2630
let result = self.current.clone();
2731

28-
for i in (0..self.shape.len()).rev() {
32+
for i in (0..self.shape.order()).rev() {
2933
if self.current[i] + 1 < self.shape[i] {
3034
self.current[i] += 1;
3135
break;
@@ -45,36 +49,37 @@ impl Iterator for IndexIterator {
4549
#[cfg(test)]
4650
mod tests {
4751
use super::*;
52+
use crate::shape;
4853

4954
#[test]
5055
fn test_index_iterator() {
51-
let shape = vec![2, 3];
56+
let shape = shape![2, 3].unwrap();
5257
let mut iter = IndexIterator::new(&shape);
5358

54-
assert_eq!(iter.next(), Some(vec![0, 0]));
55-
assert_eq!(iter.next(), Some(vec![0, 1]));
56-
assert_eq!(iter.next(), Some(vec![0, 2]));
57-
assert_eq!(iter.next(), Some(vec![1, 0]));
58-
assert_eq!(iter.next(), Some(vec![1, 1]));
59-
assert_eq!(iter.next(), Some(vec![1, 2]));
59+
assert_eq!(iter.next(), Some(coord![0, 0]));
60+
assert_eq!(iter.next(), Some(coord![0, 1]));
61+
assert_eq!(iter.next(), Some(coord![0, 2]));
62+
assert_eq!(iter.next(), Some(coord![1, 0]));
63+
assert_eq!(iter.next(), Some(coord![1, 1]));
64+
assert_eq!(iter.next(), Some(coord![1, 2]));
6065
assert_eq!(iter.next(), None);
6166
}
6267

6368
#[test]
6469
fn test_index_iterator_single_dimension() {
65-
let shape = vec![4];
70+
let shape = shape![4].unwrap();
6671
let mut iter = IndexIterator::new(&shape);
6772

68-
assert_eq!(iter.next(), Some(vec![0]));
69-
assert_eq!(iter.next(), Some(vec![1]));
70-
assert_eq!(iter.next(), Some(vec![2]));
71-
assert_eq!(iter.next(), Some(vec![3]));
73+
assert_eq!(iter.next(), Some(coord![0]));
74+
assert_eq!(iter.next(), Some(coord![1]));
75+
assert_eq!(iter.next(), Some(coord![2]));
76+
assert_eq!(iter.next(), Some(coord![3]));
7277
assert_eq!(iter.next(), None);
7378
}
7479

7580
#[test]
7681
fn test_index_iterator_empty_tensor() {
77-
let shape = vec![];
82+
let shape = shape![].unwrap();
7883
let mut iter = IndexIterator::new(&shape);
7984

8085
assert_eq!(iter.next(), None);

0 commit comments

Comments
 (0)