-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathview.rs
More file actions
118 lines (97 loc) · 3.15 KB
/
Copy pathview.rs
File metadata and controls
118 lines (97 loc) · 3.15 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
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use std::fmt::Debug;
use std::fmt::Formatter;
use std::ops::Deref;
use vortex_error::VortexResult;
use crate::ArrayRef;
use crate::array::Array;
use crate::array::ArrayId;
use crate::array::VTable;
use crate::dtype::DType;
use crate::stats::StatsSetRef;
use crate::validity::Validity;
/// A lightweight, `Copy`-able typed view into an [`ArrayRef`].
///
/// Vtable methods receive `ArrayView<V>` so they can inspect common array metadata and
/// encoding-specific data without cloning or downcasting an [`ArrayRef`]. The view is only valid for
/// the lifetime of the borrowed array.
pub struct ArrayView<'a, V: VTable> {
array: &'a ArrayRef,
data: &'a V::TypedArrayData,
}
impl<V: VTable> Copy for ArrayView<'_, V> {}
impl<V: VTable> Clone for ArrayView<'_, V> {
fn clone(&self) -> Self {
*self
}
}
impl<'a, V: VTable> ArrayView<'a, V> {
/// # Safety
/// Caller must ensure `data` is the `V::TypedArrayData` stored inside `array`.
pub(crate) unsafe fn new_unchecked(array: &'a ArrayRef, data: &'a V::TypedArrayData) -> Self {
debug_assert!(array.is::<V>());
Self { array, data }
}
/// Returns the erased array handle that owns this view.
pub fn array(&self) -> &'a ArrayRef {
self.array
}
/// Returns the encoding-specific data stored by this array.
pub fn data(&self) -> &'a V::TypedArrayData {
self.data
}
/// Returns this array's child slots.
pub fn slots(&self) -> &'a [Option<ArrayRef>] {
self.array.slots()
}
/// Returns the logical dtype.
pub fn dtype(&self) -> &DType {
self.array.dtype()
}
/// Returns the number of rows.
pub fn len(&self) -> usize {
self.array.len()
}
/// Returns `true` when the array has no rows.
pub fn is_empty(&self) -> bool {
self.array.len() == 0
}
/// Returns the encoding ID.
pub fn encoding_id(&self) -> ArrayId {
self.array.encoding_id()
}
/// Returns the array's statistics set.
pub fn statistics(&self) -> StatsSetRef<'_> {
self.array.statistics()
}
/// Returns the array's validity representation.
pub fn validity(&self) -> VortexResult<Validity> {
self.array.validity()
}
/// Clone the underlying [`ArrayRef`] and return it as an owned typed handle.
pub fn into_owned(self) -> Array<V> {
// SAFETY: we are ourselves type checked as 'V'
unsafe { Array::<V>::from_array_ref_unchecked(self.array.clone()) }
}
}
impl<V: VTable> AsRef<ArrayRef> for ArrayView<'_, V> {
fn as_ref(&self) -> &ArrayRef {
self.array
}
}
impl<V: VTable> Deref for ArrayView<'_, V> {
type Target = V::TypedArrayData;
fn deref(&self) -> &V::TypedArrayData {
self.data
}
}
impl<V: VTable> Debug for ArrayView<'_, V> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ArrayView")
.field("encoding", &self.array.encoding_id())
.field("dtype", self.array.dtype())
.field("len", &self.array.len())
.finish()
}
}