1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
/*! * Ways to transform and access matrices * * At the moment slicing is not very usable and can only be used to downsize * matrices with [retain](../struct.Matrix.html#method.retain) and * [retain_mut](../struct.Matrix.html#method.retain_mut). In the future it will be available * for iterating through matrices and indexing into matrices to get values. */ use std::ops::Range; use crate::matrices::{Row, Column}; /** * A slice defines across one dimension what values are accepted, * it can act like a filter. Slices can also be constructed via * boolean logic operations in the same way as in predicate logic expressions. */ #[non_exhaustive] pub enum Slice { /** A slice that accepts all indexes */ All(), /** A slice that accepts no indexes */ None(), /** A slice that accepts only the provided index */ Single(usize), /** A slice that accepts only indexes within the range */ Range(Range<usize>), /** * A slice which rejects all indexes accepted by the argument, and accepts all indexes * rejected by the argument. */ Not(Box<Slice>), /** * A slice which accepts only indexes accepted by both arguments, and rejects all others. */ And(Box<Slice>, Box<Slice>), /** * A slice which accepts indexes accepted by either arguments, and rejects only * indexes accepted by neither. This is an inclusive or. * * You could construct an exclusive or by using combinations of AND, OR and NOT as * (a AND (NOT b)) OR ((NOT a) AND b) = a XOR b. */ Or(Box<Slice>, Box<Slice>), } /** * A kind of slice that can be taken on a matrix, over its rows and columns. */ pub struct Slice2D { rows: Slice, columns: Slice, } impl Slice { /** * Checks if this slice accepts some index. */ pub fn accepts(&self, index: usize) -> bool { match self { Slice::All() => true, Slice::None() => false, Slice::Single(i) => i == &index, Slice::Range(range) => range.contains(&index), Slice::Not(slice) => !slice.accepts(index), Slice::And(slice1, slice2) => slice1.accepts(index) && slice2.accepts(index), Slice::Or(slice1, slice2) => slice1.accepts(index) || slice2.accepts(index), } } /** * Returns the negation of this slice */ pub fn not(self) -> Slice { Slice::Not(Box::new(self)) } /** * Returns the and of this slice and the other one */ pub fn and(self, other: Slice) -> Slice { Slice::And(Box::new(self), Box::new(other)) } /** * Returns the or of this slice and the other one */ pub fn or(self, other: Slice) -> Slice { Slice::Or(Box::new(self), Box::new(other)) } } /** * A builder object to create a slice. This exists to make forgetting to specify rows * *and* columns a compilation error rather than a runtime one. */ pub struct EmptySlice2DBuilder {} /** * A builder object to create a slice. This exists to make forgetting to specify rows * *and* columns a compilation error rather than a runtime one. */ pub struct RowSlice2DBuilder { rows: Slice } /** * A builder object to create a slice. This exists to make forgetting to specify rows * *and* columns a compilation error rather than a runtime one. */ pub struct ColumnSlice2DBuilder { columns: Slice } /** * Constructs a builder object to create a 2d slice * * The full syntax to create a `Slice2D` is like so: * * ``` * use easy_ml::matrices::slices; * use easy_ml::matrices::slices::Slice; * slices::new() * .rows(Slice::All()) * .columns(Slice::Single(1)); * ``` * * Rows and Column slices can be specified in either order but both must be given. */ pub fn new() -> EmptySlice2DBuilder { Slice2D::new() } impl Slice2D { /** * Constructs a builder object to create a 2d slice * * The full syntax to create a `Slice2D` is like so: * * ``` * use easy_ml::matrices::slices::{Slice2D, Slice}; * Slice2D::new() * .rows(Slice::All()) * .columns(Slice::Single(1)); * ``` * * Rows and Column slices can be specified in either order but both must be given. */ pub fn new() -> EmptySlice2DBuilder { EmptySlice2DBuilder { } } /** * Checks if this 2 dimensional slice accepts some index. The row and column * slices it is composed from must accept the row and column respectively. */ pub fn accepts(&self, row: Row, column: Column) -> bool { self.rows.accepts(row) && self.columns.accepts(column) } } impl EmptySlice2DBuilder { /** * Constructs a new builder object with the rows defined first. */ pub fn rows(self, rows: Slice) -> RowSlice2DBuilder { RowSlice2DBuilder { rows, } } /** * Constructs a new builder object with the columns defined first. */ pub fn columns(self, columns: Slice) -> ColumnSlice2DBuilder { ColumnSlice2DBuilder { columns, } } } impl RowSlice2DBuilder { /** * Constructs a 2d slice with rows and columns defined. */ pub fn columns(self, columns: Slice) -> Slice2D { Slice2D { rows: self.rows, columns, } } } impl ColumnSlice2DBuilder { /** * Constructs a 2d slice with rows and columns defined. */ pub fn rows(self, rows: Slice) -> Slice2D { Slice2D { rows, columns: self.columns, } } }