postgres_array/array.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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
use std::fmt;
use std::ops::{Index, IndexMut};
use std::slice;
use std::vec;
use crate::Dimension;
/// A multi-dimensional array.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Array<T> {
dims: Vec<Dimension>,
data: Vec<T>,
}
impl<T: fmt::Display> fmt::Display for Array<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.dims.iter().any(|dim| dim.lower_bound != 1) {
for dim in &self.dims {
write!(
fmt,
"[{}:{}]",
dim.lower_bound,
dim.lower_bound + dim.len - 1
)?;
}
write!(fmt, "=")?;
}
fmt_helper(0, &self.dims, &mut self.data.iter(), fmt)
}
}
fn fmt_helper<'a, T, I>(
depth: usize,
dims: &[Dimension],
data: &mut I,
fmt: &mut fmt::Formatter<'_>,
) -> fmt::Result
where
I: Iterator<Item = &'a T>,
T: 'a + fmt::Display,
{
if dims.len() == 0 {
return write!(fmt, "{{}}");
}
if depth == dims.len() {
return write!(fmt, "{}", data.next().unwrap());
}
write!(fmt, "{{")?;
for i in 0..dims[depth].len {
if i != 0 {
write!(fmt, ",")?;
}
fmt_helper(depth + 1, dims, data, fmt)?;
}
write!(fmt, "}}")
}
impl<T> Array<T> {
/// Creates a new `Array` from its underlying components.
///
/// The data array should be provided in the higher-dimensional equivalent
/// of row-major order.
///
/// # Panics
///
/// Panics if the number of elements provided does not match the number of
/// elements specified by the dimensions.
pub fn from_parts(data: Vec<T>, dimensions: Vec<Dimension>) -> Array<T> {
assert!(
(data.is_empty() && dimensions.is_empty())
|| data.len() as i32 == dimensions.iter().fold(1, |acc, i| acc * i.len),
"size mismatch"
);
Array {
dims: dimensions,
data,
}
}
/// Creates a new one-dimensional array.
pub fn from_vec(data: Vec<T>, lower_bound: i32) -> Array<T> {
Array {
dims: vec![Dimension {
len: data.len() as i32,
lower_bound,
}],
data,
}
}
/// Wraps this array in a new dimension of size 1.
///
/// For example, the one dimensional array `[1, 2]` would turn into the
/// two-dimensional array `[[1, 2]]`.
pub fn wrap(&mut self, lower_bound: i32) {
self.dims.insert(
0,
Dimension {
len: 1,
lower_bound,
},
);
}
/// Consumes another array, appending it to the top level dimension of this
/// array.
///
/// The dimensions of the other array must be the same as the dimensions
/// of this array with the first dimension removed. This includes lower
/// bounds as well as lengths.
///
/// For example, if `[3, 4]` is pushed onto `[[1, 2]]`, the result is
/// `[[1, 2], [3, 4]]`.
///
/// # Panics
///
/// Panics if the dimensions of the two arrays do not match.
pub fn push(&mut self, other: Array<T>) {
assert!(
self.dims.len() - 1 == other.dims.len(),
"cannot append differently shaped arrays"
);
for (dim1, dim2) in self.dims.iter().skip(1).zip(other.dims.iter()) {
assert!(dim1 == dim2, "cannot append differently shaped arrays");
}
self.dims[0].len += 1;
self.data.extend(other.data);
}
/// Returns the dimensions of this array.
pub fn dimensions(&self) -> &[Dimension] {
&self.dims
}
fn shift_idx(&self, indices: &[i32]) -> i32 {
assert_eq!(self.dims.len(), indices.len());
self.dims
.iter()
.zip(indices.iter().cloned())
.rev()
.fold((0, 1), |(acc, stride), (dim, idx)| {
let shifted = dim.shift(idx);
(acc + shifted * stride, dim.len * stride)
})
.0
}
/// Returns an iterator over references to the elements of the array in the
/// higher-dimensional equivalent of row-major order.
pub fn iter(&self) -> Iter<'_, T> {
Iter {
inner: self.data.iter(),
}
}
/// Returns an iterator over mutable references to the elements of the
/// array in the higher-dimensional equivalent of row-major order.
pub fn iter_mut(&mut self) -> IterMut<'_, T> {
IterMut {
inner: self.data.iter_mut(),
}
}
/// Returns the underlying data vector for this Array in the
/// higher-dimensional equivalent of row-major order.
pub fn into_inner(self) -> Vec<T> {
self.data
}
}
/// A trait implemented by types that can index into an `Array`.
pub trait ArrayIndex {
/// Calculates the index into the `Array`'s underlying storage specified
/// by the value of `self`.
///
/// # Panics
///
/// Panics if the value of `self` does not correspond to an in-bounds
/// element of the `Array`.
fn index<T>(&self, array: &Array<T>) -> i32;
}
impl<'a> ArrayIndex for &'a [i32] {
fn index<T>(&self, array: &Array<T>) -> i32 {
array.shift_idx(*self)
}
}
impl ArrayIndex for i32 {
fn index<T>(&self, array: &Array<T>) -> i32 {
let slice: &[i32] = &[*self];
ArrayIndex::index(&slice, array)
}
}
macro_rules! tuple_impl {
($($name:ident : $t:ty),+) => {
impl ArrayIndex for ($($t,)+) {
fn index<T>(&self, array: &Array<T>) -> i32 {
let ($($name,)+) = *self;
let slice: &[i32] = &[$($name),+];
ArrayIndex::index(&slice, array)
}
}
}
}
tuple_impl!(a: i32);
tuple_impl!(a: i32, b: i32);
tuple_impl!(a: i32, b: i32, c: i32);
tuple_impl!(a: i32, b: i32, c: i32, d: i32);
tuple_impl!(a: i32, b: i32, c: i32, d: i32, e: i32);
tuple_impl!(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32);
tuple_impl!(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32, g: i32);
tuple_impl!(
a: i32,
b: i32,
c: i32,
d: i32,
e: i32,
f: i32,
g: i32,
h: i32
);
tuple_impl!(
a: i32,
b: i32,
c: i32,
d: i32,
e: i32,
f: i32,
g: i32,
h: i32,
i: i32
);
/// Indexes into the `Array`, retrieving a reference to the contained
/// value.
///
/// Since `Array`s can be multi-dimensional, the `Index` trait is
/// implemented for a variety of index types. In the most generic case, a
/// `&[i32]` can be used. In addition, a bare `i32` as well as tuples
/// of up to 10 `i32` values may be used for convenience.
///
/// # Panics
///
/// Panics if the index does not correspond to an in-bounds element of the
/// `Array`.
///
/// # Examples
///
/// ```rust
/// # use postgres_array::Array;
/// let mut array = Array::from_vec(vec![0i32, 1, 2, 3], 0);
/// assert_eq!(2, array[2]);
///
/// array.wrap(0);
/// array.push(Array::from_vec(vec![4, 5, 6, 7], 0));
///
/// assert_eq!(6, array[(1, 2)]);
/// ```
impl<T, I: ArrayIndex> Index<I> for Array<T> {
type Output = T;
fn index(&self, idx: I) -> &T {
let idx = idx.index(self);
&self.data[idx as usize]
}
}
impl<T, I: ArrayIndex> IndexMut<I> for Array<T> {
fn index_mut(&mut self, idx: I) -> &mut T {
let idx = idx.index(self);
&mut self.data[idx as usize]
}
}
impl<'a, T: 'a> IntoIterator for &'a Array<T> {
type Item = &'a T;
type IntoIter = Iter<'a, T>;
fn into_iter(self) -> Iter<'a, T> {
self.iter()
}
}
impl<'a, T: 'a> IntoIterator for &'a mut Array<T> {
type Item = &'a mut T;
type IntoIter = IterMut<'a, T>;
fn into_iter(self) -> IterMut<'a, T> {
self.iter_mut()
}
}
impl<T> IntoIterator for Array<T> {
type Item = T;
type IntoIter = IntoIter<T>;
fn into_iter(self) -> IntoIter<T> {
IntoIter {
inner: self.data.into_iter(),
}
}
}
/// An iterator over references to values of an `Array` in the
/// higher-dimensional equivalent of row-major order.
pub struct Iter<'a, T> {
inner: slice::Iter<'a, T>,
}
impl<'a, T: 'a> Iterator for Iter<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<&'a T> {
self.inner.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
}
impl<'a, T: 'a> DoubleEndedIterator for Iter<'a, T> {
fn next_back(&mut self) -> Option<&'a T> {
self.inner.next_back()
}
}
impl<'a, T: 'a> ExactSizeIterator for Iter<'a, T> {
fn len(&self) -> usize {
self.inner.len()
}
}
/// An iterator over mutable references to values of an `Array` in the
/// higher-dimensional equivalent of row-major order.
pub struct IterMut<'a, T> {
inner: slice::IterMut<'a, T>,
}
impl<'a, T: 'a> Iterator for IterMut<'a, T> {
type Item = &'a mut T;
fn next(&mut self) -> Option<&'a mut T> {
self.inner.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
}
impl<'a, T: 'a> DoubleEndedIterator for IterMut<'a, T> {
fn next_back(&mut self) -> Option<&'a mut T> {
self.inner.next_back()
}
}
impl<'a, T: 'a> ExactSizeIterator for IterMut<'a, T> {
fn len(&self) -> usize {
self.inner.len()
}
}
/// An iterator over values of an `Array` in the higher-dimensional
/// equivalent of row-major order.
pub struct IntoIter<T> {
inner: vec::IntoIter<T>,
}
impl<T> Iterator for IntoIter<T> {
type Item = T;
fn next(&mut self) -> Option<T> {
self.inner.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
}
impl<T> DoubleEndedIterator for IntoIter<T> {
fn next_back(&mut self) -> Option<T> {
self.inner.next_back()
}
}
impl<T> ExactSizeIterator for IntoIter<T> {
fn len(&self) -> usize {
self.inner.len()
}
}