Triangle Matrix
Upper and lower triangle matrix indexing operations.
Upper and lower triangle matrices indexing operations.
Provides the [TriangleIndex
] trait for triangle matrix indexing operations
on a one dimensional collection. All operations are delegated to the inner
collection using Deref
and DerefMut
. Requires delegating the length of an
axis, n
, using the [TriangleType
] trait.
Example
// Create a wrapper storing the length of an axis and the collection.
;
// Implement `Deref` and `DerefMut`, delegating `Deref::Target` to the vector.
// ...
// Delegate `n` to the `usize` field and specify `Ty` as the triangle type.
// A `4 * 4` upper triangle matrix where the elements are the usize indices.
let n = 4;
let v = Vec from_iter;
let mut m: = VecTri;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
*m.get_element_mut = 10;
*m.get_element_mut = 11;
// Thanks to `Deref`, we can index into the underlying vector.
assert_eq!;
Alternatively we can omit the generic type Ty
on our struct, and qualify the
triangle type;
;
// Implement `Deref` and `DerefMut`, delegating `Deref::Target` to the vector.
// ...
// Delegate `n` to the `usize` field and specify `Ty` as the triangle type.
let n = 4;
let v = Vec from_iter;
let m: VecTri = VecTri;
assert_eq!;
assert_eq!;
Indexing
A type implementing TriangleIndex
(or Deref
and DerefMut
, for that matter)
would typically not be appropriate for a public facing API. Depending on what we
with to represent, we may need to manipulate indices prior to making calls to
TriangleIndex
. Take for example a symmetric matrix using a lower triangle
data structure where we have no use of diagonal elements. This could be
represented as the following;
/// A symmetric matrix with no diagonal elements.
let n = 4;
let v = Vec from_iter;
let m = SymmetricMatrix ;
/// A Symmetric matrix of 5 elements where we don't need the `i == j` diagonal.
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;