Struct polars_core::chunked_array::object::ObjectArray
source · pub struct ObjectArray<T>where
T: PolarsObject,{ /* private fields */ }Available on crate feature
object only.Implementations§
source§impl<T> ObjectArray<T>where
T: PolarsObject,
impl<T> ObjectArray<T>where
T: PolarsObject,
sourcepub fn values(&self) -> &Arc<Vec<T>>
pub fn values(&self) -> &Arc<Vec<T>>
Get a reference to the underlying data
Examples found in repository?
More examples
src/chunked_array/ops/take/mod.rs (line 507)
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
unsafe fn take_unchecked<I, INulls>(&self, indices: TakeIdx<I, INulls>) -> Self
where
Self: std::marker::Sized,
I: TakeIterator,
INulls: TakeIteratorNulls,
{
// current implementation is suboptimal, every iterator is allocated to UInt32Array
match indices {
TakeIdx::Array(array) => {
if array.null_count() == array.len() {
return Self::full_null(self.name(), array.len());
}
match self.chunks.len() {
1 => {
let values = self.downcast_chunks().get(0).unwrap().values();
let mut ca: Self = array
.into_iter()
.map(|opt_idx| {
opt_idx.map(|idx| values.get_unchecked(*idx as usize).clone())
})
.collect();
ca.rename(self.name());
ca
}
_ => {
return if !array.has_validity() {
let iter = array.values().iter().map(|i| *i as usize);
let taker = self.take_rand();
let mut ca: ObjectChunked<T> =
iter.map(|idx| taker.get_unchecked(idx).cloned()).collect();
ca.rename(self.name());
ca
} else {
let iter = array
.into_iter()
.map(|opt_idx| opt_idx.map(|idx| *idx as usize));
let taker = self.take_rand();
let mut ca: ObjectChunked<T> = iter
.map(|opt_idx| {
opt_idx.and_then(|idx| taker.get_unchecked(idx).cloned())
})
.collect();
ca.rename(self.name());
ca
}
}
}
}
TakeIdx::Iter(iter) => {
if self.is_empty() {
return Self::full_null(self.name(), iter.size_hint().0);
}
let taker = self.take_rand();
let mut ca: ObjectChunked<T> =
iter.map(|idx| taker.get_unchecked(idx).cloned()).collect();
ca.rename(self.name());
ca
}
TakeIdx::IterNulls(iter) => {
if self.is_empty() {
return Self::full_null(self.name(), iter.size_hint().0);
}
let taker = self.take_rand();
let mut ca: ObjectChunked<T> = iter
.map(|opt_idx| opt_idx.and_then(|idx| taker.get(idx).cloned()))
.collect();
ca.rename(self.name());
ca
}
}
}sourcepub fn value(&self, index: usize) -> &T
pub fn value(&self, index: usize) -> &T
Get a value at a certain index location
Examples found in repository?
More examples
src/chunked_array/ops/chunkops.rs (line 164)
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
pub(crate) fn rechunk_object(&self) -> Self {
if self.chunks.len() == 1 {
self.clone()
} else {
let mut builder = ObjectChunkedBuilder::new(self.name(), self.len());
let chunks = self.downcast_iter();
// todo! use iterators once implemented
// no_null path
if !self.has_validity() {
for arr in chunks {
for idx in 0..arr.len() {
builder.append_value(arr.value(idx).clone())
}
}
} else {
for arr in chunks {
for idx in 0..arr.len() {
if arr.is_valid(idx) {
builder.append_value(arr.value(idx).clone())
} else {
builder.append_null()
}
}
}
}
builder.finish()
}
}src/chunked_array/ops/filter.rs (line 178)
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
fn filter(&self, filter: &BooleanChunked) -> PolarsResult<ChunkedArray<ObjectType<T>>>
where
Self: Sized,
{
// broadcast
if filter.len() == 1 {
return match filter.get(0) {
Some(true) => Ok(self.clone()),
_ => Ok(ObjectChunked::from_chunks(self.name(), vec![])),
};
}
if self.is_empty() {
return Err(PolarsError::NoData(
"cannot filter empty object array".into(),
));
}
let chunks = self.downcast_iter().collect::<Vec<_>>();
let mut builder = ObjectChunkedBuilder::<T>::new(self.name(), self.len());
for (idx, mask) in filter.into_iter().enumerate() {
if mask.unwrap_or(false) {
let (chunk_idx, idx) = self.index_to_chunked_index(idx);
unsafe {
let arr = chunks.get_unchecked(chunk_idx);
match arr.is_null(idx) {
true => builder.append_null(),
false => {
let v = arr.value(idx);
builder.append_value(v.clone())
}
}
}
}
}
Ok(builder.finish())
}sourcepub unsafe fn value_unchecked(&self, index: usize) -> &T
pub unsafe fn value_unchecked(&self, index: usize) -> &T
Get a value at a certain index location
Safety
This does not any bound checks. The caller needs to ensure the index is within the size of the array.
Examples found in repository?
More examples
src/chunked_array/object/iterator.rs (line 45)
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
fn next(&mut self) -> Option<Self::Item> {
if self.current == self.current_end {
None
// Safety:
// Se comment below
} else if unsafe { self.array.is_null_unchecked(self.current) } {
self.current += 1;
Some(None)
} else {
let old = self.current;
self.current += 1;
// Safety:
// we just checked bounds in `self.current_end == self.current`
// this is safe on the premise that this struct is initialized with
// current = array.len()
// and that current_end is ever only decremented
unsafe { Some(Some(self.array.value_unchecked(old))) }
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(
self.array.len() - self.current,
Some(self.array.len() - self.current),
)
}
}
impl<'a, T: PolarsObject> std::iter::DoubleEndedIterator for ObjectIter<'a, T> {
fn next_back(&mut self) -> Option<Self::Item> {
if self.current_end == self.current {
None
} else {
self.current_end -= 1;
Some(if self.array.is_null(self.current_end) {
None
} else {
// Safety:
// we just checked bounds in `self.current_end == self.current`
// this is safe on the premise that this struct is initialized with
// current = array.len()
// and that current_end is ever only decremented
unsafe { Some(self.array.value_unchecked(self.current_end)) }
})
}
}
}
/// all arrays have known size.
impl<'a, T: PolarsObject> std::iter::ExactSizeIterator for ObjectIter<'a, T> {}
impl<'a, T: PolarsObject> IntoIterator for &'a ObjectArray<T> {
type Item = Option<&'a T>;
type IntoIter = ObjectIter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
ObjectIter::<'a, T>::new(self)
}
}
pub struct OwnedObjectIter<T: PolarsObject> {
array: ObjectArray<T>,
current: usize,
current_end: usize,
}
impl<T: PolarsObject> OwnedObjectIter<T> {
/// create a new iterator
pub fn new(array: ObjectArray<T>) -> Self {
let current_end = array.len();
OwnedObjectIter::<T> {
array,
current: 0,
current_end,
}
}
}
unsafe impl<T: PolarsObject> TrustedLen for OwnedObjectIter<T> {}
impl<T: PolarsObject> ObjectArray<T> {
pub(crate) fn into_iter_cloned(self) -> OwnedObjectIter<T> {
OwnedObjectIter::<T>::new(self)
}
}
impl<T: PolarsObject> std::iter::Iterator for OwnedObjectIter<T> {
type Item = Option<T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.current == self.current_end {
None
// Safety:
// Se comment below
} else if unsafe { self.array.is_null_unchecked(self.current) } {
self.current += 1;
Some(None)
} else {
let old = self.current;
self.current += 1;
// Safety:
// we just checked bounds in `self.current_end == self.current`
// this is safe on the premise that this struct is initialized with
// current = array.len()
// and that current_end is ever only decremented
unsafe { Some(Some(self.array.value_unchecked(old).clone())) }
}
}sourcepub unsafe fn is_valid_unchecked(&self, i: usize) -> bool
pub unsafe fn is_valid_unchecked(&self, i: usize) -> bool
Examples found in repository?
src/chunked_array/object/mod.rs (line 96)
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
pub unsafe fn is_null_unchecked(&self, i: usize) -> bool {
!self.is_valid_unchecked(i)
}
#[inline]
pub(crate) unsafe fn get_unchecked(&self, item: usize) -> Option<&T> {
if self.is_null_unchecked(item) {
None
} else {
Some(self.value_unchecked(item))
}
}
}
impl<T> Array for ObjectArray<T>
where
T: PolarsObject,
{
fn as_any(&self) -> &dyn Any {
self
}
fn data_type(&self) -> &ArrowDataType {
unimplemented!()
}
fn slice(&self, offset: usize, length: usize) -> Box<dyn Array> {
assert!(
offset + length <= self.len(),
"the offset of the new Buffer cannot exceed the existing length"
);
unsafe { self.slice_unchecked(offset, length) }
}
unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Box<dyn Array> {
let mut new = self.clone();
let len = std::cmp::min(new.len - offset, length);
new.len = len;
new.offset = offset;
new.null_bitmap = new.null_bitmap.map(|x| x.slice_unchecked(offset, len));
Box::new(new)
}
fn len(&self) -> usize {
self.len
}
fn validity(&self) -> Option<&Bitmap> {
self.null_bitmap.as_ref()
}
fn with_validity(&self, validity: Option<Bitmap>) -> Box<dyn Array> {
let mut arr = self.clone();
arr.null_bitmap = validity;
Box::new(arr)
}
fn to_boxed(&self) -> Box<dyn Array> {
Box::new(self.clone())
}
fn as_any_mut(&mut self) -> &mut dyn Any {
unimplemented!()
}
fn null_count(&self) -> usize {
match &self.null_bitmap {
None => 0,
Some(validity) => validity.unset_bits(),
}
}
}
impl<T> ObjectChunked<T>
where
T: PolarsObject,
{
/// Get a hold to an object that can be formatted or downcasted via the Any trait.
///
/// # Safety
///
/// No bounds checks
pub unsafe fn get_object_unchecked(&self, index: usize) -> Option<&dyn PolarsObjectSafe> {
let chunks = self.downcast_chunks();
let (chunk_idx, idx) = self.index_to_chunked_index(index);
let arr = chunks.get_unchecked(chunk_idx);
if arr.is_valid_unchecked(idx) {
Some(arr.value(idx))
} else {
None
}
}More examples
sourcepub unsafe fn is_null_unchecked(&self, i: usize) -> bool
pub unsafe fn is_null_unchecked(&self, i: usize) -> bool
Examples found in repository?
More examples
src/chunked_array/object/iterator.rs (line 34)
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
fn next(&mut self) -> Option<Self::Item> {
if self.current == self.current_end {
None
// Safety:
// Se comment below
} else if unsafe { self.array.is_null_unchecked(self.current) } {
self.current += 1;
Some(None)
} else {
let old = self.current;
self.current += 1;
// Safety:
// we just checked bounds in `self.current_end == self.current`
// this is safe on the premise that this struct is initialized with
// current = array.len()
// and that current_end is ever only decremented
unsafe { Some(Some(self.array.value_unchecked(old))) }
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(
self.array.len() - self.current,
Some(self.array.len() - self.current),
)
}
}
impl<'a, T: PolarsObject> std::iter::DoubleEndedIterator for ObjectIter<'a, T> {
fn next_back(&mut self) -> Option<Self::Item> {
if self.current_end == self.current {
None
} else {
self.current_end -= 1;
Some(if self.array.is_null(self.current_end) {
None
} else {
// Safety:
// we just checked bounds in `self.current_end == self.current`
// this is safe on the premise that this struct is initialized with
// current = array.len()
// and that current_end is ever only decremented
unsafe { Some(self.array.value_unchecked(self.current_end)) }
})
}
}
}
/// all arrays have known size.
impl<'a, T: PolarsObject> std::iter::ExactSizeIterator for ObjectIter<'a, T> {}
impl<'a, T: PolarsObject> IntoIterator for &'a ObjectArray<T> {
type Item = Option<&'a T>;
type IntoIter = ObjectIter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
ObjectIter::<'a, T>::new(self)
}
}
pub struct OwnedObjectIter<T: PolarsObject> {
array: ObjectArray<T>,
current: usize,
current_end: usize,
}
impl<T: PolarsObject> OwnedObjectIter<T> {
/// create a new iterator
pub fn new(array: ObjectArray<T>) -> Self {
let current_end = array.len();
OwnedObjectIter::<T> {
array,
current: 0,
current_end,
}
}
}
unsafe impl<T: PolarsObject> TrustedLen for OwnedObjectIter<T> {}
impl<T: PolarsObject> ObjectArray<T> {
pub(crate) fn into_iter_cloned(self) -> OwnedObjectIter<T> {
OwnedObjectIter::<T>::new(self)
}
}
impl<T: PolarsObject> std::iter::Iterator for OwnedObjectIter<T> {
type Item = Option<T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.current == self.current_end {
None
// Safety:
// Se comment below
} else if unsafe { self.array.is_null_unchecked(self.current) } {
self.current += 1;
Some(None)
} else {
let old = self.current;
self.current += 1;
// Safety:
// we just checked bounds in `self.current_end == self.current`
// this is safe on the premise that this struct is initialized with
// current = array.len()
// and that current_end is ever only decremented
unsafe { Some(Some(self.array.value_unchecked(old).clone())) }
}
}Trait Implementations§
source§impl<T> Array for ObjectArray<T>where
T: PolarsObject,
impl<T> Array for ObjectArray<T>where
T: PolarsObject,
source§fn as_any(&self) -> &dyn Any
fn as_any(&self) -> &dyn Any
Converts itself to a reference of
Any, which enables downcasting to concrete types.source§fn data_type(&self) -> &ArrowDataType
fn data_type(&self) -> &ArrowDataType
The
DataType of the Array. In combination with Array::as_any, this can be
used to downcast trait objects (dyn Array) to concrete arrays.source§fn len(&self) -> usize
fn len(&self) -> usize
The length of the
Array. Every array has a length corresponding to the number of
elements (slots).source§fn as_any_mut(&mut self) -> &mut dyn Any
fn as_any_mut(&mut self) -> &mut dyn Any
Converts itself to a mutable reference of
Any, which enables mutable downcasting to concrete types.source§impl<T> Clone for ObjectArray<T>where
T: PolarsObject + Clone,
impl<T> Clone for ObjectArray<T>where
T: PolarsObject + Clone,
source§fn clone(&self) -> ObjectArray<T>
fn clone(&self) -> ObjectArray<T>
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read more