tustvold commented on code in PR #5440: URL: https://github.com/apache/arrow-rs/pull/5440#discussion_r1525518246
########## arrow-buffer/src/builder/offset.rs: ########## @@ -0,0 +1,218 @@ +// 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}; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct OffsetBufferBuilder<O: ArrowNativeType> { + offsets: Vec<O>, +} + +/// Builder of [`OffsetBuffer`] +impl<O: ArrowNativeType + Add<Output = O> + Sub<Output = O>> OffsetBufferBuilder<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)); + unsafe { 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]; + unsafe { Self::new_unchecked(offsets) } + } + + /// Create from offsets. + /// # Safety + /// Caller guarantees that offsets are monotonically increasing values. + #[inline] + pub unsafe fn new_unchecked(offsets: Vec<O>) -> Self { + Self { offsets } + } + + /// Try to safely push a length of usize type into builder + #[inline] + pub fn try_push_length(&mut self, length: usize) -> Result<(), String> { + self.push_length(O::from_usize(length).ok_or("offset overflow".to_string())?); + Ok(()) + } + + /// Push a length of usize type into builder + 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); + } + + /// Extend from another OffsetsBuilder. + /// It gets a lengths iterator from another builder and extend the builder with the iter. + pub fn extend_with_builder(&mut self, another: OffsetBufferBuilder<O>) { Review Comment: I would remove this method, I'm not sure of the use-case for it ########## arrow-buffer/src/builder/offset.rs: ########## @@ -0,0 +1,218 @@ +// 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}; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct OffsetBufferBuilder<O: ArrowNativeType> { + offsets: Vec<O>, +} + +/// Builder of [`OffsetBuffer`] +impl<O: ArrowNativeType + Add<Output = O> + Sub<Output = O>> OffsetBufferBuilder<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)); + unsafe { 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]; + unsafe { Self::new_unchecked(offsets) } + } + + /// Create from offsets. + /// # Safety + /// Caller guarantees that offsets are monotonically increasing values. + #[inline] + pub unsafe fn new_unchecked(offsets: Vec<O>) -> Self { + Self { offsets } + } + + /// Try to safely push a length of usize type into builder + #[inline] + pub fn try_push_length(&mut self, length: usize) -> Result<(), String> { + self.push_length(O::from_usize(length).ok_or("offset overflow".to_string())?); + Ok(()) + } + + /// Push a length of usize type into builder + 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); + } + + /// Extend from another OffsetsBuilder. + /// It gets a lengths iterator from another builder and extend the builder with the iter. + pub fn extend_with_builder(&mut self, another: OffsetBufferBuilder<O>) { + self.reserve(another.len() - 1); + let mut last_offset = another.offsets[0]; Review Comment: Btw `windows(2)` is a nicer pattern for this ########## arrow-buffer/src/builder/offset.rs: ########## @@ -0,0 +1,218 @@ +// 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}; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct OffsetBufferBuilder<O: ArrowNativeType> { + offsets: Vec<O>, +} + +/// Builder of [`OffsetBuffer`] +impl<O: ArrowNativeType + Add<Output = O> + Sub<Output = O>> OffsetBufferBuilder<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)); + unsafe { 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]; + unsafe { Self::new_unchecked(offsets) } + } + + /// Create from offsets. + /// # Safety + /// Caller guarantees that offsets are monotonically increasing values. + #[inline] + pub unsafe fn new_unchecked(offsets: Vec<O>) -> Self { + Self { offsets } + } + + /// Try to safely push a length of usize type into builder + #[inline] + pub fn try_push_length(&mut self, length: usize) -> Result<(), String> { + self.push_length(O::from_usize(length).ok_or("offset overflow".to_string())?); + Ok(()) + } + + /// Push a length of usize type into builder + pub fn push_length(&mut self, length: O) { Review Comment: ```suggestion pub fn push_length(&mut self, length: usize) { ``` -- 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]
