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
use crate::array::Array;
use crate::error::{ArrowError, Result};
#[derive(Debug, Clone, PartialEq)]
pub struct Chunk<A: AsRef<dyn Array>> {
arrays: Vec<A>,
}
impl<A: AsRef<dyn Array>> Chunk<A> {
pub fn new(arrays: Vec<A>) -> Self {
Self::try_new(arrays).unwrap()
}
pub fn try_new(arrays: Vec<A>) -> Result<Self> {
if !arrays.is_empty() {
let len = arrays.first().unwrap().as_ref().len();
if arrays
.iter()
.map(|array| array.as_ref())
.any(|array| array.len() != len)
{
return Err(ArrowError::InvalidArgumentError(
"Chunk require all its arrays to have an equal number of rows".to_string(),
));
}
}
Ok(Self { arrays })
}
pub fn arrays(&self) -> &[A] {
&self.arrays
}
pub fn columns(&self) -> &[A] {
&self.arrays
}
pub fn len(&self) -> usize {
self.arrays
.first()
.map(|x| x.as_ref().len())
.unwrap_or_default()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn into_arrays(self) -> Vec<A> {
self.arrays
}
}
impl<A: AsRef<dyn Array>> From<Chunk<A>> for Vec<A> {
fn from(c: Chunk<A>) -> Self {
c.into_arrays()
}
}
impl<A: AsRef<dyn Array>> std::ops::Deref for Chunk<A> {
type Target = [A];
#[inline]
fn deref(&self) -> &[A] {
self.arrays()
}
}