vortex_array/array/
view.rs1use std::fmt::Debug;
5use std::fmt::Formatter;
6use std::ops::Deref;
7
8use vortex_error::VortexResult;
9
10use crate::ArrayRef;
11use crate::array::Array;
12use crate::array::ArrayId;
13use crate::array::VTable;
14use crate::dtype::DType;
15use crate::stats::StatsSetRef;
16use crate::validity::Validity;
17
18pub struct ArrayView<'a, V: VTable> {
24 array: &'a ArrayRef,
25 data: &'a V::TypedArrayData,
26}
27
28impl<V: VTable> Copy for ArrayView<'_, V> {}
29
30impl<V: VTable> Clone for ArrayView<'_, V> {
31 fn clone(&self) -> Self {
32 *self
33 }
34}
35
36impl<'a, V: VTable> ArrayView<'a, V> {
37 pub(crate) unsafe fn new_unchecked(array: &'a ArrayRef, data: &'a V::TypedArrayData) -> Self {
40 debug_assert!(array.is::<V>());
41 Self { array, data }
42 }
43
44 pub fn array(&self) -> &'a ArrayRef {
46 self.array
47 }
48
49 pub fn data(&self) -> &'a V::TypedArrayData {
51 self.data
52 }
53
54 pub fn slots(&self) -> &'a [Option<ArrayRef>] {
56 self.array.slots()
57 }
58
59 pub fn dtype(&self) -> &DType {
61 self.array.dtype()
62 }
63
64 pub fn len(&self) -> usize {
66 self.array.len()
67 }
68
69 pub fn is_empty(&self) -> bool {
71 self.array.len() == 0
72 }
73
74 pub fn encoding_id(&self) -> ArrayId {
76 self.array.encoding_id()
77 }
78
79 pub fn statistics(&self) -> StatsSetRef<'_> {
81 self.array.statistics()
82 }
83
84 pub fn validity(&self) -> VortexResult<Validity> {
86 self.array.validity()
87 }
88
89 pub fn into_owned(self) -> Array<V> {
91 unsafe { Array::<V>::from_array_ref_unchecked(self.array.clone()) }
93 }
94}
95
96impl<V: VTable> AsRef<ArrayRef> for ArrayView<'_, V> {
97 fn as_ref(&self) -> &ArrayRef {
98 self.array
99 }
100}
101
102impl<V: VTable> Deref for ArrayView<'_, V> {
103 type Target = V::TypedArrayData;
104
105 fn deref(&self) -> &V::TypedArrayData {
106 self.data
107 }
108}
109
110impl<V: VTable> Debug for ArrayView<'_, V> {
111 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
112 f.debug_struct("ArrayView")
113 .field("encoding", &self.array.encoding_id())
114 .field("dtype", self.array.dtype())
115 .field("len", &self.array.len())
116 .finish()
117 }
118}