Skip to content

Commit ff6bd79

Browse files
mariusaefacebook-github-bot
authored andcommitted
ndslice: consistent definition of "len()" (#759)
Summary: Pull Request resolved: #759 "len()" should refer to the size of the data structure. In the case of an extent, the natural definition is the number of dimensions. For example, the natural Index implementation for Extent is to give the size of the indexed dimension. This would then require that len() returns the number of dimensions for consistency. We change 'len' to be defined thus, and provide "num_ranks" to answer how many ranks are contained in the extent. Reviewed By: shayne-fletcher Differential Revision: D79569471 fbshipit-source-id: ea9e2d7bcf8de0e8d558988d6a53c1c99bdc6b48
1 parent e50d865 commit ff6bd79

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

ndslice/src/view.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,14 @@ impl Extent {
101101
self.labels().iter().position(|l| l == label)
102102
}
103103

104-
/// Returns the number of dimensions in this extent.
105-
pub fn num_dim(&self) -> usize {
106-
self.labels().len()
107-
}
108-
109104
/// Creates a `Point` in this extent with the given coordinates.
110105
///
111106
/// Returns an error if the coordinate dimensionality does not
112107
/// match.
113108
pub fn point(&self, coords: Vec<usize>) -> Result<Point, PointError> {
114-
if coords.len() != self.num_dim() {
109+
if coords.len() != self.len() {
115110
return Err(PointError::DimMismatch {
116-
expected: self.num_dim(),
111+
expected: self.len(),
117112
actual: coords.len(),
118113
});
119114
}
@@ -128,15 +123,15 @@ impl Extent {
128123

129124
/// Returns the point corresponding to the provided rank in this extent.
130125
pub fn point_of_rank(&self, mut rank: usize) -> Result<Point, PointError> {
131-
if rank >= self.len() {
126+
if rank >= self.num_ranks() {
132127
return Err(PointError::OutOfRange {
133128
size: self.len(),
134129
rank,
135130
});
136131
}
137132

138133
let mut stride: usize = self.sizes().iter().product();
139-
let mut coords = vec![0; self.num_dim()];
134+
let mut coords = vec![0; self.len()];
140135
for (i, size) in self.sizes().iter().enumerate() {
141136
stride /= size;
142137
coords[i] = rank / stride;
@@ -149,14 +144,19 @@ impl Extent {
149144
})
150145
}
151146

152-
/// The total size of the extent.
147+
/// The number of dimensions in the extent.
153148
pub fn len(&self) -> usize {
154-
self.sizes().iter().product()
149+
self.sizes().len()
155150
}
156151

157-
/// Whether the extent is empty.
152+
/// Whether the extent has zero dimensionbs.
158153
pub fn is_empty(&self) -> bool {
159-
self.sizes().iter().all(|&s| s == 0)
154+
self.sizes().is_empty()
155+
}
156+
157+
/// The number of ranks in the extent.
158+
pub fn num_ranks(&self) -> usize {
159+
self.sizes().iter().product()
160160
}
161161

162162
/// Convert this extent into its labels and sizes.
@@ -620,7 +620,7 @@ mod test {
620620
let _p1 = extent.point(vec![1, 2, 3]).unwrap();
621621
let _p2 = vec![1, 2, 3].in_(&extent).unwrap();
622622

623-
assert_eq!(extent.len(), 4 * 5 * 6);
623+
assert_eq!(extent.num_ranks(), 4 * 5 * 6);
624624

625625
let p3 = extent.point_of_rank(0).unwrap();
626626
assert_eq!(p3.coords(), &[0, 0, 0]);

0 commit comments

Comments
 (0)