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

Commit

Permalink
expose shrink_to_fit to mutable arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Sep 29, 2021
1 parent 94fd267 commit f8196bc
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/array/binary/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ 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();
}
}

impl<O: Offset> MutableArray for MutableBinaryArray<O> {
Expand Down Expand Up @@ -180,6 +184,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
4 changes: 4 additions & 0 deletions src/array/boolean/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@ impl MutableArray for MutableBooleanArray {
fn push_null(&mut self) {
self.push(None)
}

fn shrink_to_fit(&mut self) {
// no-op still have to implement this for bitmaps
}
}

impl Extend<Option<bool>> for MutableBooleanArray {
Expand Down
8 changes: 8 additions & 0 deletions src/array/dictionary/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ 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();
}
}

impl<K: DictionaryKey, M: 'static + MutableArray> MutableArray for MutableDictionaryArray<K, M> {
Expand Down Expand Up @@ -144,6 +149,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
9 changes: 9 additions & 0 deletions src/array/fixed_size_binary/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ 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();
}
}

/// Accessors
Expand Down Expand Up @@ -204,6 +209,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
8 changes: 8 additions & 0 deletions src/array/fixed_size_list/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ impl<M: MutableArray> MutableFixedSizeListArray<M> {
.chain(std::iter::once(false)),
))
}
/// Shrinks the capacity of the [`MutableFixedSizeList`] to fit its current length.
pub fn shrink_to_fit(&mut self) {
self.values.shrink_to_fit()
}
}

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

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

impl<T: NativeType> MutableFixedSizeListArray<MutablePrimitiveArray<T>> {
Expand Down
9 changes: 9 additions & 0 deletions src/array/list/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ 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();
}
}

impl<O: Offset, M: MutableArray + Default> Default for MutableListArray<O, M> {
Expand Down Expand Up @@ -210,4 +216,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
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,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
9 changes: 9 additions & 0 deletions src/array/primitive/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ 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();
}
}

/// Accessors
Expand Down Expand Up @@ -357,6 +362,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
10 changes: 10 additions & 0 deletions src/array/utf8/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ 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();
}
}

impl<O: Offset> MutableUtf8Array<O> {
Expand Down Expand Up @@ -224,6 +230,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

0 comments on commit f8196bc

Please sign in to comment.