This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 223
/
mod.rs
189 lines (162 loc) · 5.17 KB
/
mod.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
use crate::{bitmap::Bitmap, buffer::Buffer, datatypes::DataType, error::Result};
use super::{display_fmt, display_helper, ffi::ToFfi, Array};
mod iterator;
mod mutable;
pub use mutable::*;
/// The Arrow's equivalent to an immutable `Vec<Option<[u8; size]>>`.
/// Cloning and slicing this struct is `O(1)`.
#[derive(Debug, Clone)]
pub struct FixedSizeBinaryArray {
size: i32, // this is redundant with `data_type`, but useful to not have to deconstruct the data_type.
data_type: DataType,
values: Buffer<u8>,
validity: Option<Bitmap>,
offset: usize,
}
impl FixedSizeBinaryArray {
/// Returns a new empty [`FixedSizeBinaryArray`].
pub fn new_empty(data_type: DataType) -> Self {
Self::from_data(data_type, Buffer::new(), None)
}
/// Returns a new null [`FixedSizeBinaryArray`].
pub fn new_null(data_type: DataType, length: usize) -> Self {
Self::from_data(
data_type,
Buffer::new_zeroed(length),
Some(Bitmap::new_zeroed(length)),
)
}
/// Returns a new [`FixedSizeBinaryArray`].
pub fn from_data(data_type: DataType, values: Buffer<u8>, validity: Option<Bitmap>) -> Self {
let size = *Self::get_size(&data_type);
assert_eq!(values.len() % (size as usize), 0);
Self {
size,
data_type,
values,
validity,
offset: 0,
}
}
/// Returns a slice of this [`FixedSizeBinaryArray`].
/// # Implementation
/// This operation is `O(1)` as it amounts to increase 3 ref counts.
pub fn slice(&self, offset: usize, length: usize) -> Self {
let validity = self.validity.clone().map(|x| x.slice(offset, length));
let values = self
.values
.clone()
.slice(offset * self.size as usize, length * self.size as usize);
Self {
data_type: self.data_type.clone(),
size: self.size,
values,
validity,
offset: self.offset + offset,
}
}
/// Returns the values allocated on this [`FixedSizeBinaryArray`].
pub fn values(&self) -> &Buffer<u8> {
&self.values
}
/// Returns value at position `i`.
/// # Panic
/// Panics iff `i >= self.len()`.
#[inline]
pub fn value(&self, i: usize) -> &[u8] {
&self.values()[i * self.size as usize..(i + 1) * self.size as usize]
}
/// Returns the element at index `i` as &str
/// # Safety
/// Assumes that the `i < self.len`.
#[inline]
pub unsafe fn value_unchecked(&self, i: usize) -> &[u8] {
std::slice::from_raw_parts(
self.values.as_ptr().add(i * self.size as usize),
self.size as usize,
)
}
/// Returns the size
pub fn size(&self) -> usize {
self.size as usize
}
}
impl FixedSizeBinaryArray {
pub(crate) fn get_size(data_type: &DataType) -> &i32 {
match data_type {
DataType::FixedSizeBinary(size) => size,
DataType::Extension(_, child, _) => Self::get_size(child),
_ => panic!("Wrong DataType"),
}
}
}
impl Array for FixedSizeBinaryArray {
#[inline]
fn as_any(&self) -> &dyn std::any::Any {
self
}
#[inline]
fn len(&self) -> usize {
self.values.len() / self.size as usize
}
#[inline]
fn data_type(&self) -> &DataType {
&self.data_type
}
fn validity(&self) -> &Option<Bitmap> {
&self.validity
}
fn slice(&self, offset: usize, length: usize) -> Box<dyn Array> {
Box::new(self.slice(offset, length))
}
}
impl std::fmt::Display for FixedSizeBinaryArray {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let a = |x: &[u8]| display_helper(x.iter().map(|x| Some(format!("{:b}", x)))).join(" ");
let iter = self.iter().map(|x| x.map(a));
display_fmt(iter, "FixedSizeBinaryArray", f, false)
}
}
unsafe impl ToFfi for FixedSizeBinaryArray {
fn buffers(&self) -> Vec<Option<std::ptr::NonNull<u8>>> {
vec![
self.validity.as_ref().map(|x| x.as_ptr()),
std::ptr::NonNull::new(self.values.as_ptr() as *mut u8),
]
}
fn offset(&self) -> usize {
self.offset
}
}
impl FixedSizeBinaryArray {
/// Creates a [`FixedSizeBinaryArray`] from an fallible iterator of optional `[u8]`.
pub fn try_from_iter<P: AsRef<[u8]>, I: IntoIterator<Item = Option<P>>>(
iter: I,
size: usize,
) -> Result<Self> {
MutableFixedSizeBinaryArray::try_from_iter(iter, size).map(|x| x.into())
}
/// Creates a [`FixedSizeBinaryArray`] from an iterator of optional `[u8]`.
pub fn from_iter<P: AsRef<[u8]>, I: IntoIterator<Item = Option<P>>>(
iter: I,
size: usize,
) -> Self {
MutableFixedSizeBinaryArray::try_from_iter(iter, size)
.unwrap()
.into()
}
}
pub trait FixedSizeBinaryValues {
fn values(&self) -> &[u8];
fn size(&self) -> usize;
}
impl FixedSizeBinaryValues for FixedSizeBinaryArray {
#[inline]
fn values(&self) -> &[u8] {
&self.values
}
#[inline]
fn size(&self) -> usize {
self.size as usize
}
}