tustvold commented on code in PR #5440:
URL: https://github.com/apache/arrow-rs/pull/5440#discussion_r1510619611


##########
arrow-buffer/src/builder/offset.rs:
##########
@@ -0,0 +1,238 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::ops::{Add, Sub};
+
+use crate::{ArrowNativeType, OffsetBuffer, ScalarBuffer};
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct OffsetsBuilder<O: ArrowNativeType> {
+    offsets: Vec<O>,
+}
+
+/// builder for [`OffsetBuffer`]
+impl<O: ArrowNativeType + Add<Output = O> + Sub<Output = O>> OffsetsBuilder<O> 
{
+    /// create a new builder containing only 1 zero offset
+    pub fn new(capacity: usize) -> Self {
+        let mut offsets = Vec::with_capacity(capacity + 1);
+        offsets.push(O::usize_as(0));
+        Self::new_unchecked(offsets)
+    }
+
+    /// create a new builder containing capacity number of zero offsets
+    pub fn new_zeroed(capacity: usize) -> Self {
+        let offsets = vec![O::usize_as(0); capacity + 1];
+        Self::new_unchecked(offsets)
+    }
+
+    /// create from offsets
+    /// caller guarantees that offsets are monotonically increasing values
+    #[inline]
+    pub fn new_unchecked(offsets: Vec<O>) -> Self {
+        Self { offsets }
+    }
+
+    /// push a length into the builder.
+    #[inline]
+    pub fn push_length(&mut self, length: O) {
+        let last_offset = self.offsets.last().unwrap();
+        let next_offset = *last_offset + length;
+        self.offsets.push(next_offset);
+    }
+
+    /// try to safely push a length of usize type into builder
+    #[inline]
+    pub fn try_push_usize_length(&mut self, length: usize) -> Result<(), 
String> {
+        self.push_length(O::from_usize(length).ok_or(format!(
+            "Cannot safely convert usize length {length} to offset"
+        ))?);
+        Ok(())
+    }
+
+    /// extend the builder with an Iterator of lengths
+    pub fn extend_from_lengths(&mut self, lengths: impl IntoIterator<Item = 
O>) {
+        let lengths_iter = lengths.into_iter();
+        let size_hint = match lengths_iter.size_hint().1 {
+            Some(h_bound) => h_bound,
+            None => lengths_iter.size_hint().0,
+        };
+        self.reserve(size_hint);
+        lengths_iter.for_each(|length| self.push_length(length));
+    }
+
+    /// extend with an Iterator of usize lengths
+    pub fn try_extend_from_usize_lengths(
+        &mut self,
+        lengths: impl IntoIterator<Item = usize>,
+    ) -> Result<(), String> {
+        self.extend_from_lengths(
+            lengths
+                .into_iter()
+                .map(|u_len| {
+                    O::from_usize(u_len).ok_or(format!(
+                        "Cannot safely convert usize length {u_len} to offset"
+                    ))
+                })
+                .collect::<Result<Vec<O>, String>>()?,
+        );
+        Ok(())
+    }
+
+    /// extend from another OffsetsBuilder
+    /// it get a lengths iterator from another builder and extend the builder 
with the iter
+    pub fn extend_from_builder(&mut self, offsets_builder: OffsetsBuilder<O>) {
+        let lengths = offsets_builder.lengths();
+        self.extend_from_lengths(lengths);
+    }
+
+    /// takes the builder itself and returns an [`OffsetBuffer`]
+    pub fn finish(self) -> OffsetBuffer<O> {
+        OffsetBuffer::new(ScalarBuffer::from(self.offsets))
+    }
+
+    pub fn capacity(&self) -> usize {
+        self.offsets.capacity() - 1
+    }
+
+    #[allow(clippy::len_without_is_empty)]
+    pub fn len(&self) -> usize {
+        self.offsets.len()
+    }
+
+    /// Last offset
+    pub fn last(&self) -> O {
+        *self.offsets.last().unwrap()
+    }
+
+    pub fn reserve(&mut self, additional: usize) {
+        self.offsets.reserve(additional);
+    }
+
+    pub fn reserve_exact(&mut self, additional: usize) {
+        self.offsets.reserve_exact(additional);
+    }
+
+    pub fn shrink_to_fit(&mut self) {
+        self.offsets.shrink_to_fit();
+    }
+
+    /// get an iterator of lengths from builder's underlying offsets
+    pub fn lengths(&self) -> impl IntoIterator<Item = O> {

Review Comment:
   What is the use-case for this?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to