-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy patharray.rs
More file actions
52 lines (45 loc) · 1.79 KB
/
array.rs
File metadata and controls
52 lines (45 loc) · 1.79 KB
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use vortex_error::VortexExpect as _;
use vortex_error::VortexResult;
use crate::ArrayRef;
use crate::array::{Array, ArrayParts, EmptyArrayData, TypedArrayRef};
use crate::arrays::Reversed;
/// Slot index for the inner (to-be-reversed) child array.
pub(super) const CHILD_SLOT: usize = 0;
pub(super) const NUM_SLOTS: usize = 1;
pub(super) const SLOT_NAMES: [&str; NUM_SLOTS] = ["child"];
/// Extension trait for accessing [`ReversedArray`](crate::arrays::ReversedArray) properties.
pub trait ReversedArrayExt: TypedArrayRef<Reversed> {
/// Returns the inner array whose elements will be yielded in reverse order.
fn child(&self) -> &ArrayRef {
self.as_ref().slots()[CHILD_SLOT]
.as_ref()
.vortex_expect("validated ReversedArray child slot")
}
}
impl<T: TypedArrayRef<Reversed>> ReversedArrayExt for T {}
impl Array<Reversed> {
/// Wraps `child` in a [`ReversedArray`](crate::arrays::ReversedArray).
pub fn try_new(child: ArrayRef) -> VortexResult<Self> {
let dtype = child.dtype().clone();
let len = child.len();
Array::try_from_parts(
ArrayParts::new(Reversed, dtype, len, EmptyArrayData).with_slots(vec![Some(child)]),
)
}
/// Wraps `child` in a [`ReversedArray`](crate::arrays::ReversedArray) without validation.
///
/// # Safety
///
/// Caller must ensure `child` is a valid array.
pub unsafe fn new_unchecked(child: ArrayRef) -> Self {
let dtype = child.dtype().clone();
let len = child.len();
unsafe {
Array::from_parts_unchecked(
ArrayParts::new(Reversed, dtype, len, EmptyArrayData).with_slots(vec![Some(child)]),
)
}
}
}