You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
message:"adding an empty row is not allowed".to_owned(),
158
+
});
159
+
}
141
160
ifself.columns != row.len(){
142
161
returnErr(MatrixFormatError{
143
162
message:format!(
@@ -193,6 +212,11 @@ impl<C> Matrix<C> {
193
212
columns:usize,
194
213
values:Vec<C>,
195
214
) -> Result<Self,MatrixFormatError>{
215
+
if rows != 0 && columns == 0{
216
+
returnErr(MatrixFormatError{
217
+
message:"creating a matrix with empty rows is not allowed".to_owned(),
218
+
});
219
+
}
196
220
if rows * columns != values.len(){
197
221
returnErr(MatrixFormatError{message:format!("length of vector does not correspond to announced dimensions ({} instead of {}×{}={})", values.len(), rows, columns, rows*columns)});
198
222
}
@@ -230,10 +254,17 @@ impl<C> Matrix<C> {
230
254
}
231
255
}
232
256
257
+
/// Check if the matrix is empty.
258
+
#[must_use]
259
+
pubfnis_empty(&self) -> bool{
260
+
self.rows == 0
261
+
}
262
+
233
263
/// Create a matrix from something convertible to an iterator on rows,
234
264
/// each row being convertible to an iterator on columns.
235
265
///
236
-
/// An error will be returned if length of rows differ.
266
+
/// An error will be returned if length of rows differ or if empty rows
267
+
/// are added.
237
268
///
238
269
/// ```
239
270
/// use pathfinding::matrix::*;
@@ -374,14 +405,25 @@ impl<C> Matrix<C> {
374
405
}
375
406
376
407
/// Return an iterator on neighbours of a given matrix cell, with or
377
-
/// without considering diagonals.
408
+
/// without considering diagonals. The neighbours list is determined
409
+
/// at the time of calling this method and will not change even if new
410
+
/// rows are added between the method call and the iterator consumption.
411
+
///
412
+
/// This function returns an empty iterator if the reference position does
413
+
/// not correspond to an existing matrix element.
378
414
pubfnneighbours(
379
415
&self,
380
416
(r, c):(usize,usize),
381
417
diagonals:bool,
382
418
) -> implIterator<Item = (usize,usize)>{
383
-
let row_range = RangeInclusive::new(r.saturating_sub(1),(self.rows - 1).min(r + 1));
384
-
let col_range = RangeInclusive::new(c.saturating_sub(1),(self.columns - 1).min(c + 1));
419
+
let(row_range, col_range) = if r < self.rows && c < self.columns{
420
+
(
421
+
r.saturating_sub(1)..(self.rows).min(r + 2),
422
+
c.saturating_sub(1)..(self.columns).min(c + 2),
423
+
)
424
+
}else{
425
+
(0..0,0..0)
426
+
};
385
427
row_range
386
428
.cartesian_product(col_range)
387
429
.filter(move |&(rr, cc)| (rr != r || cc != c) && (diagonals || rr == r || cc == c))
@@ -455,7 +497,9 @@ impl<C> Matrix<C> {
455
497
self.into_iter()
456
498
}
457
499
458
-
/// Return an iterator on the Matrix indices, first row first.
500
+
/// Return an iterator on the Matrix indices, first row first. The values are
501
+
/// computed when this method is called and will not change even if new rows are
0 commit comments