Skip to content

Commit

Permalink
Add OffsetBuffer::new (#3910)
Browse files Browse the repository at this point in the history
  • Loading branch information
tustvold authored Mar 23, 2023
1 parent bcfe63e commit e4e6c67
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions arrow-buffer/src/buffer/offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ use std::ops::Deref;
pub struct OffsetBuffer<O: ArrowNativeType>(ScalarBuffer<O>);

impl<O: ArrowNativeType> OffsetBuffer<O> {
/// Create a new [`OffsetBuffer`] from the provided [`ScalarBuffer`]
///
/// # Panics
///
/// Panics if `buffer` is not a non-empty buffer containing
/// monotonically increasing values greater than zero
pub fn new(buffer: ScalarBuffer<O>) -> Self {
assert!(!buffer.is_empty(), "offsets cannot be empty");
assert!(buffer[0] > O::usize_as(0), "offsets must be greater than 0");
assert!(
buffer.windows(2).all(|w| w[0] <= w[1]),
"offsets must be monotonically increasing"
);
Self(buffer)
}

/// Create a new [`OffsetBuffer`] from the provided [`ScalarBuffer`]
///
/// # Safety
Expand Down Expand Up @@ -71,3 +87,26 @@ impl<T: ArrowNativeType> AsRef<[T]> for OffsetBuffer<T> {
self
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[should_panic(expected = "offsets cannot be empty")]
fn empty_offsets() {
OffsetBuffer::new(Vec::<i32>::new().into());
}

#[test]
#[should_panic(expected = "offsets must be greater than 0")]
fn negative_offsets() {
OffsetBuffer::new(vec![-1, 0, 1].into());
}

#[test]
#[should_panic(expected = "offsets must be monotonically increasing")]
fn non_monotonic_offsets() {
OffsetBuffer::new(vec![1, 2, 0].into());
}
}

0 comments on commit e4e6c67

Please sign in to comment.