Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

expose shrink_to_fit to mutable arrays #467

Merged
merged 3 commits into from Oct 2, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/array/binary/mutable.rs
Expand Up @@ -135,6 +135,13 @@ impl<O: Offset> MutableBinaryArray<O> {
let a: BinaryArray<O> = self.into();
Arc::new(a)
}
/// Shrinks the capacity of the [`MutableBinaryArray`] to fit its current length.
pub fn shrink_to_fit(&mut self) {
self.values.shrink_to_fit();
ritchie46 marked this conversation as resolved.
Show resolved Hide resolved
if let Some(validity) = &mut self.validity {
validity.shrink_to_fit()
}
}
}

impl<O: Offset> MutableArray for MutableBinaryArray<O> {
Expand Down Expand Up @@ -180,6 +187,10 @@ impl<O: Offset> MutableArray for MutableBinaryArray<O> {
fn push_null(&mut self) {
self.push::<&[u8]>(None)
}

fn shrink_to_fit(&mut self) {
self.shrink_to_fit()
}
}

impl<O: Offset, P: AsRef<[u8]>> FromIterator<Option<P>> for MutableBinaryArray<O> {
Expand Down
12 changes: 12 additions & 0 deletions src/array/boolean/mutable.rs
Expand Up @@ -293,6 +293,14 @@ impl MutableBooleanArray {
// Safety: `I` is `TrustedLen`
unsafe { Self::try_from_trusted_len_iter_unchecked(iterator) }
}

/// Shrinks the capacity of the [`MutableBooleanArray`] to fit its current length.
pub fn shrink_to_fit(&mut self) {
self.values.shrink_to_fit();
if let Some(validity) = &mut self.validity {
validity.shrink_to_fit()
}
}
}

/// Creates a Bitmap and an optional [`MutableBitmap`] from an iterator of `Option<bool>`.
Expand Down Expand Up @@ -461,6 +469,10 @@ impl MutableArray for MutableBooleanArray {
fn push_null(&mut self) {
self.push(None)
}

fn shrink_to_fit(&mut self) {
self.shrink_to_fit()
}
}

impl Extend<Option<bool>> for MutableBooleanArray {
Expand Down
9 changes: 9 additions & 0 deletions src/array/dictionary/mutable.rs
Expand Up @@ -104,6 +104,12 @@ impl<K: DictionaryKey, M: MutableArray> MutableDictionaryArray<K, M> {
let a: DictionaryArray<K> = self.into();
Arc::new(a)
}

/// Shrinks the capacity of the [`MutableDictionaryArray`] to fit its current length.
pub fn shrink_to_fit(&mut self) {
self.values.shrink_to_fit();
self.keys.shrink_to_fit();
}
}

impl<K: DictionaryKey, M: 'static + MutableArray> MutableArray for MutableDictionaryArray<K, M> {
Expand Down Expand Up @@ -144,6 +150,9 @@ impl<K: DictionaryKey, M: 'static + MutableArray> MutableArray for MutableDictio
fn push_null(&mut self) {
self.keys.push(None)
}
fn shrink_to_fit(&mut self) {
self.shrink_to_fit()
}
}

impl<K, M, T: Hash> TryExtend<Option<T>> for MutableDictionaryArray<K, M>
Expand Down
12 changes: 12 additions & 0 deletions src/array/fixed_size_binary/mutable.rs
Expand Up @@ -149,6 +149,14 @@ impl MutableFixedSizeBinaryArray {
pub unsafe fn value_unchecked(&self, i: usize) -> &[u8] {
std::slice::from_raw_parts(self.values.as_ptr().add(i * self.size), self.size)
}

/// Shrinks the capacity of the [`MutablePrimitive`] to fit its current length.
pub fn shrink_to_fit(&mut self) {
self.values.shrink_to_fit();
if let Some(validity) = &mut self.validity {
validity.shrink_to_fit()
}
}
}

/// Accessors
Expand Down Expand Up @@ -204,6 +212,10 @@ impl MutableArray for MutableFixedSizeBinaryArray {
fn push_null(&mut self) {
self.values.extend_constant(self.size, 0);
}

fn shrink_to_fit(&mut self) {
self.shrink_to_fit()
}
}

impl FixedSizeBinaryValues for MutableFixedSizeBinaryArray {
Expand Down
11 changes: 11 additions & 0 deletions src/array/fixed_size_list/mutable.rs
Expand Up @@ -74,6 +74,13 @@ impl<M: MutableArray> MutableFixedSizeListArray<M> {
None => self.init_validity(),
}
}
/// Shrinks the capacity of the [`MutableFixedSizeList`] to fit its current length.
pub fn shrink_to_fit(&mut self) {
self.values.shrink_to_fit();
if let Some(validity) = &mut self.validity {
validity.shrink_to_fit()
}
}
}

impl<M: MutableArray + 'static> MutableArray for MutableFixedSizeListArray<M> {
Expand Down Expand Up @@ -124,6 +131,10 @@ impl<M: MutableArray + 'static> MutableArray for MutableFixedSizeListArray<M> {
self.init_validity()
}
}

fn shrink_to_fit(&mut self) {
self.shrink_to_fit()
}
}

impl<M, I, T> TryExtend<Option<I>> for MutableFixedSizeListArray<M>
Expand Down
14 changes: 13 additions & 1 deletion src/array/list/mutable.rs
Expand Up @@ -42,6 +42,15 @@ impl<O: Offset, M: MutableArray + Default> MutableListArray<O, M> {
validity: None,
}
}

/// Shrinks the capacity of the [`MutableList`] to fit its current length.
pub fn shrink_to_fit(&mut self) {
self.values.shrink_to_fit();
self.offsets.shrink_to_fit();
if let Some(validity) = &mut self.validity {
validity.shrink_to_fit()
}
}
}

impl<O: Offset, M: MutableArray + Default> Default for MutableListArray<O, M> {
Expand Down Expand Up @@ -179,7 +188,7 @@ impl<O: Offset, M: MutableArray> MutableListArray<O, M> {
}
}

impl<O: Offset, M: MutableArray + 'static> MutableArray for MutableListArray<O, M> {
impl<O: Offset, M: MutableArray + Default + 'static> MutableArray for MutableListArray<O, M> {
fn len(&self) -> usize {
self.offsets.len() - 1
}
Expand Down Expand Up @@ -222,4 +231,7 @@ impl<O: Offset, M: MutableArray + 'static> MutableArray for MutableListArray<O,
fn push_null(&mut self) {
self.push_null()
}
fn shrink_to_fit(&mut self) {
self.shrink_to_fit();
}
}
3 changes: 3 additions & 0 deletions src/array/mod.rs
Expand Up @@ -155,6 +155,9 @@ pub trait MutableArray: std::fmt::Debug {
.map(|x| x.get(index))
.unwrap_or(true)
}

/// Shrink the array to fit its length.
fn shrink_to_fit(&mut self);
}

macro_rules! general_dyn {
Expand Down
12 changes: 12 additions & 0 deletions src/array/primitive/mutable.rs
Expand Up @@ -237,6 +237,14 @@ impl<T: NativeType> MutablePrimitiveArray<T> {
let a: PrimitiveArray<T> = self.into();
Arc::new(a)
}

/// Shrinks the capacity of the [`MutablePrimitive`] to fit its current length.
pub fn shrink_to_fit(&mut self) {
self.values.shrink_to_fit();
if let Some(validity) = &mut self.validity {
validity.shrink_to_fit()
}
}
}

/// Accessors
Expand Down Expand Up @@ -357,6 +365,10 @@ impl<T: NativeType> MutableArray for MutablePrimitiveArray<T> {
fn push_null(&mut self) {
self.push(None)
}

fn shrink_to_fit(&mut self) {
self.shrink_to_fit()
}
}

impl<T: NativeType + NaturalDataType> MutablePrimitiveArray<T> {
Expand Down
13 changes: 13 additions & 0 deletions src/array/utf8/mutable.rs
Expand Up @@ -164,6 +164,15 @@ impl<O: Offset> MutableUtf8Array<O> {
let a: Utf8Array<O> = self.into();
Arc::new(a)
}

/// Shrinks the capacity of the [`MutableUtf8`] to fit its current length.
pub fn shrink_to_fit(&mut self) {
self.values.shrink_to_fit();
self.offsets.shrink_to_fit();
if let Some(validity) = &mut self.validity {
validity.shrink_to_fit()
}
}
}

impl<O: Offset> MutableUtf8Array<O> {
Expand Down Expand Up @@ -224,6 +233,10 @@ impl<O: Offset> MutableArray for MutableUtf8Array<O> {
fn push_null(&mut self) {
self.push::<&str>(None)
}

fn shrink_to_fit(&mut self) {
self.shrink_to_fit()
}
}

impl<O: Offset, P: AsRef<str>> FromIterator<Option<P>> for MutableUtf8Array<O> {
Expand Down