reed_solomon_16/engine/shards.rs
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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
use std::ops::{Bound, Index, IndexMut, RangeBounds};
// ======================================================================
// Shards - CRATE
pub(crate) struct Shards {
shard_count: usize,
shard_bytes: usize,
// Flat array of `shard_count * shard_bytes` bytes.
data: Vec<u8>,
}
impl Shards {
pub(crate) fn as_ref_mut(&mut self) -> ShardsRefMut {
ShardsRefMut::new(self.shard_count, self.shard_bytes, self.data.as_mut())
}
pub(crate) fn new() -> Self {
Self {
shard_count: 0,
shard_bytes: 0,
data: Vec::new(),
}
}
pub(crate) fn resize(&mut self, shard_count: usize, shard_bytes: usize) {
assert!(shard_bytes > 0 && shard_bytes & 63 == 0);
self.shard_count = shard_count;
self.shard_bytes = shard_bytes;
self.data.resize(shard_count * shard_bytes, 0);
}
}
// ======================================================================
// Shards - IMPL Index
impl Index<usize> for Shards {
type Output = [u8];
fn index(&self, index: usize) -> &Self::Output {
&self.data[index * self.shard_bytes..(index + 1) * self.shard_bytes]
}
}
// ======================================================================
// Shards - IMPL IndexMut
impl IndexMut<usize> for Shards {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.data[index * self.shard_bytes..(index + 1) * self.shard_bytes]
}
}
// ======================================================================
// ShardsRefMut - PUBLIC
/// Mutable reference to shard array implemented as flat byte array.
pub struct ShardsRefMut<'a> {
shard_count: usize,
shard_bytes: usize,
// Flat array of `shard_count * shard_bytes` bytes.
data: &'a mut [u8],
}
impl<'a> ShardsRefMut<'a> {
/// Returns mutable references to shards at `pos` and `pos + dist`.
///
/// See source code of [`Naive::fft`] for an example.
///
/// # Panics
///
/// If `dist` is `0`.
///
/// [`Naive::fft`]: crate::engine::Naive#method.fft
pub fn dist2_mut(&mut self, mut pos: usize, mut dist: usize) -> (&mut [u8], &mut [u8]) {
pos *= self.shard_bytes;
dist *= self.shard_bytes;
let (a, b) = self.data[pos..].split_at_mut(dist);
(&mut a[..self.shard_bytes], &mut b[..self.shard_bytes])
}
/// Returns mutable references to shards at
/// `pos`, `pos + dist`, `pos + dist * 2` and `pos + dist * 3`.
///
/// See source code of [`NoSimd::fft`] for an example
/// (specifically the private method `fft_butterfly_two_layers`).
///
/// # Panics
///
/// If `dist` is `0`.
///
/// [`NoSimd::fft`]: crate::engine::NoSimd#method.fft
pub fn dist4_mut(
&mut self,
mut pos: usize,
mut dist: usize,
) -> (&mut [u8], &mut [u8], &mut [u8], &mut [u8]) {
pos *= self.shard_bytes;
dist *= self.shard_bytes;
let (ab, cd) = self.data[pos..].split_at_mut(dist * 2);
let (a, b) = ab.split_at_mut(dist);
let (c, d) = cd.split_at_mut(dist);
(
&mut a[..self.shard_bytes],
&mut b[..self.shard_bytes],
&mut c[..self.shard_bytes],
&mut d[..self.shard_bytes],
)
}
/// Returns `true` if this contains no shards.
pub fn is_empty(&self) -> bool {
self.shard_count == 0
}
/// Returns number of shards.
pub fn len(&self) -> usize {
self.shard_count
}
/// Creates new [`ShardsRefMut`] that references given `data`.
///
/// # Panics
///
/// If `data` is smaller than `shard_count * shard_bytes` bytes.
pub fn new(shard_count: usize, shard_bytes: usize, data: &'a mut [u8]) -> Self {
Self {
shard_count,
shard_bytes,
data: &mut data[..shard_count * shard_bytes],
}
}
/// Splits this [`ShardsRefMut`] into two so that
/// first includes shards `0..mid` and second includes shards `mid..`.
pub fn split_at_mut(&mut self, mid: usize) -> (ShardsRefMut, ShardsRefMut) {
let (a, b) = self.data.split_at_mut(mid * self.shard_bytes);
(
ShardsRefMut::new(mid, self.shard_bytes, a),
ShardsRefMut::new(self.shard_count - mid, self.shard_bytes, b),
)
}
/// Fills the given shard-range with `0u8`:s.
pub fn zero<R: RangeBounds<usize>>(&mut self, range: R) {
let start = match range.start_bound() {
Bound::Included(start) => start * self.shard_bytes,
Bound::Excluded(start) => (start + 1) * self.shard_bytes,
Bound::Unbounded => 0,
};
let end = match range.end_bound() {
Bound::Included(end) => (end + 1) * self.shard_bytes,
Bound::Excluded(end) => end * self.shard_bytes,
Bound::Unbounded => self.shard_count * self.shard_bytes,
};
self.data[start..end].fill(0);
}
}
// ======================================================================
// ShardsRefMut - IMPL Index
impl<'a> Index<usize> for ShardsRefMut<'a> {
type Output = [u8];
fn index(&self, index: usize) -> &Self::Output {
&self.data[index * self.shard_bytes..(index + 1) * self.shard_bytes]
}
}
// ======================================================================
// ShardsRefMut - IMPL IndexMut
impl<'a> IndexMut<usize> for ShardsRefMut<'a> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.data[index * self.shard_bytes..(index + 1) * self.shard_bytes]
}
}
// ======================================================================
// ShardsRefMut - CRATE
impl<'a> ShardsRefMut<'a> {
pub(crate) fn copy_within(&mut self, mut src: usize, mut dest: usize, mut count: usize) {
src *= self.shard_bytes;
dest *= self.shard_bytes;
count *= self.shard_bytes;
self.data.copy_within(src..src + count, dest);
}
// Returns mutable references to flat-arrays of shard-ranges
// `x .. x + count` and `y .. y + count`.
//
// Ranges must not overlap.
pub(crate) fn flat2_mut(
&mut self,
mut x: usize,
mut y: usize,
mut count: usize,
) -> (&mut [u8], &mut [u8]) {
x *= self.shard_bytes;
y *= self.shard_bytes;
count *= self.shard_bytes;
if x < y {
let (head, tail) = self.data.split_at_mut(y);
(&mut head[x..x + count], &mut tail[..count])
} else {
let (head, tail) = self.data.split_at_mut(x);
(&mut tail[..count], &mut head[y..y + count])
}
}
}