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

Commit

Permalink
Removed un-needed methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed May 28, 2022
1 parent f441847 commit 47d95c6
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 76 deletions.
2 changes: 1 addition & 1 deletion src/array/binary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl<O: Offset> BinaryArray<O> {
pub fn new_null(data_type: DataType, length: usize) -> Self {
Self::new(
data_type,
Buffer::new_zeroed(length + 1),
vec![O::default(); 1 + length].into(),
Buffer::new(),
Some(Bitmap::new_zeroed(length)),
)
Expand Down
2 changes: 1 addition & 1 deletion src/array/fixed_size_binary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl FixedSizeBinaryArray {
let size = Self::maybe_get_size(&data_type).unwrap();
Self::new(
data_type,
Buffer::new_zeroed(length * size),
vec![0u8; length * size].into(),
Some(Bitmap::new_zeroed(length)),
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/array/list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl<O: Offset> ListArray<O> {
let child = Self::get_child_type(&data_type).clone();
Self::new(
data_type,
Buffer::new_zeroed(length + 1),
vec![O::default(); 1 + length].into(),
new_empty_array(child).into(),
Some(Bitmap::new_zeroed(length)),
)
Expand Down
2 changes: 1 addition & 1 deletion src/array/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl MapArray {
let field = new_empty_array(Self::get_field(&data_type).data_type().clone()).into();
Self::new(
data_type,
Buffer::new_zeroed(length + 1),
vec![0i32; 1 + length].into(),
field,
Some(Bitmap::new_zeroed(length)),
)
Expand Down
2 changes: 1 addition & 1 deletion src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub trait Array: Send + Sync {

/// The number of null slots on this [`Array`].
/// # Implementation
/// This is `O(1)`.
/// This is `O(1)` since the number of null elements is pre-computed.
#[inline]
fn null_count(&self) -> usize {
if self.data_type() == &DataType::Null {
Expand Down
2 changes: 1 addition & 1 deletion src/array/primitive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ impl<T: NativeType> PrimitiveArray<T> {
pub fn new_null(data_type: DataType, length: usize) -> Self {
Self::new(
data_type,
Buffer::new_zeroed(length),
vec![T::default(); length].into(),
Some(Bitmap::new_zeroed(length)),
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/array/union/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl UnionArray {
};

// all from the same field
let types = Buffer::new_zeroed(length);
let types = vec![0i8; length].into();

Self::new(data_type, types, fields, offsets)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/array/utf8/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ impl<O: Offset> Utf8Array<O> {
pub fn new_null(data_type: DataType, length: usize) -> Self {
Self::new(
data_type,
Buffer::new_zeroed(length + 1),
vec![O::default(); 1 + length].into(),
Buffer::new(),
Some(Bitmap::new_zeroed(length)),
)
Expand Down
95 changes: 27 additions & 68 deletions src/buffer/immutable.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
use either::Either;
use std::{iter::FromIterator, sync::Arc, usize};

use crate::{trusted_len::TrustedLen, types::NativeType};
use crate::types::NativeType;

use super::bytes::Bytes;

/// [`Buffer`] is a contiguous memory region that can
/// be shared across thread boundaries.
/// [`Buffer`] is a contiguous memory region that can be shared across thread boundaries.
///
/// The easiest way to think about `Buffer<T>` is being equivalent to
/// an immutable `Vec<T>`, with the following differences:
/// a `Arc<Vec<T>>`, with the following differences:
/// * `T` must be [`NativeType`]
/// * clone is `O(1)`
/// * memory is sharable across thread boundaries (it is under an `Arc`)
/// * it supports external allocated memory (FFI)
/// * slicing the buffer is `O(1)`
/// * it supports external allocated memory (via FFI)
///
/// The easiest way to create one is to use its implementation of `From` a `Vec`.
///
/// # Examples
/// ```
/// use arrow2::buffer::Buffer;
///
/// let buffer: Buffer<u32> = vec![1, 2, 3].into();
/// assert_eq!(buffer.as_ref(), [1, 2, 3].as_ref());
///
/// // it supports copy-on-write semantics (i.e. back to a `Vec`)
/// let vec: Vec<u32> = buffer.into_mut().right().unwrap();
/// assert_eq!(vec, vec![1, 2, 3]);
///
/// // cloning and slicing is `O(1)` (data is shared)
/// let buffer: Buffer<u32> = vec![1, 2, 3].into();
/// let slice = buffer.clone().slice(1, 1);
/// assert_eq!(slice.as_ref(), [2].as_ref());
/// // no longer possible to get a vec since `slice` and `buffer` share data
/// let same: Buffer<u32> = buffer.into_mut().left().unwrap();
/// ```
#[derive(Clone, PartialEq)]
pub struct Buffer<T: NativeType> {
/// the internal byte buffer.
Expand Down Expand Up @@ -46,20 +66,6 @@ impl<T: NativeType> Buffer<T> {
Self::default()
}

/// Creates a new [`Buffer`] filled with zeros.
#[inline]
pub fn new_zeroed(length: usize) -> Self {
vec![T::default(); length].into()
}

/// Takes ownership of [`Vec`].
/// # Implementation
/// This function is `O(1)`
#[inline]
pub fn from_slice<R: AsRef<[T]>>(data: R) -> Self {
data.as_ref().to_vec().into()
}

/// Auxiliary method to create a new Buffer
pub(crate) fn from_bytes(bytes: Bytes<T>) -> Self {
let length = bytes.len();
Expand Down Expand Up @@ -153,53 +159,6 @@ impl<T: NativeType> Buffer<T> {
}
}

impl<T: NativeType> Buffer<T> {
/// Creates a [`Buffer`] from an [`Iterator`] with a trusted length.
/// Prefer this to `collect` whenever possible, as it often enables auto-vectorization.
/// # Example
/// ```
/// # use arrow2::buffer::Buffer;
/// let v = vec![1u32];
/// let iter = v.iter().map(|x| x * 2);
/// let buffer = unsafe { Buffer::from_trusted_len_iter(iter) };
/// assert_eq!(buffer.len(), 1)
/// ```
#[inline]
pub fn from_trusted_len_iter<I: TrustedLen<Item = T>>(iterator: I) -> Self {
iterator.collect::<Vec<_>>().into()
}

/// Creates a [`Buffer`] from an fallible [`Iterator`] with a trusted length.
#[inline]
pub fn try_from_trusted_len_iter<E, I: TrustedLen<Item = std::result::Result<T, E>>>(
iterator: I,
) -> std::result::Result<Self, E> {
Ok(iterator.collect::<std::result::Result<Vec<_>, E>>()?.into())
}

/// Creates a [`Buffer`] from an [`Iterator`] with a trusted (upper) length.
/// # Safety
/// This method assumes that the iterator's size is correct and is undefined behavior
/// to use it on an iterator that reports an incorrect length.
#[inline]
pub unsafe fn from_trusted_len_iter_unchecked<I: Iterator<Item = T>>(iterator: I) -> Self {
iterator.collect::<Vec<_>>().into()
}

/// # Safety
/// This method assumes that the iterator's size is correct and is undefined behavior
/// to use it on an iterator that reports an incorrect length.
#[inline]
pub unsafe fn try_from_trusted_len_iter_unchecked<
E,
I: Iterator<Item = std::result::Result<T, E>>,
>(
iterator: I,
) -> std::result::Result<Self, E> {
Ok(iterator.collect::<std::result::Result<Vec<_>, E>>()?.into())
}
}

impl<T: NativeType> From<Vec<T>> for Buffer<T> {
#[inline]
fn from(p: Vec<T>) -> Self {
Expand Down

0 comments on commit 47d95c6

Please sign in to comment.