|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use std::ops::Deref; |
| 19 | + |
| 20 | +use crate::{ArrowNativeType, OffsetBuffer}; |
| 21 | + |
| 22 | +#[derive(Debug)] |
| 23 | +pub struct OffsetBufferBuilder<O: ArrowNativeType> { |
| 24 | + offsets: Vec<O>, |
| 25 | + last_offset: usize, |
| 26 | +} |
| 27 | + |
| 28 | +/// Builder of [`OffsetBuffer`] |
| 29 | +impl<O: ArrowNativeType> OffsetBufferBuilder<O> { |
| 30 | + /// Create a new builder with space for `capacity + 1` offsets |
| 31 | + pub fn new(capacity: usize) -> Self { |
| 32 | + let mut offsets = Vec::with_capacity(capacity + 1); |
| 33 | + offsets.push(O::usize_as(0)); |
| 34 | + Self { |
| 35 | + offsets, |
| 36 | + last_offset: 0, |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /// Push a slice of `length` bytes |
| 41 | + /// |
| 42 | + /// # Panics |
| 43 | + /// |
| 44 | + /// Panics if adding `length` would overflow `usize` |
| 45 | + #[inline] |
| 46 | + pub fn push_length(&mut self, length: usize) { |
| 47 | + self.last_offset = self.last_offset.checked_add(length).expect("overflow"); |
| 48 | + self.offsets.push(O::usize_as(self.last_offset)) |
| 49 | + } |
| 50 | + |
| 51 | + /// Reserve space for at least `additional` further offsets |
| 52 | + #[inline] |
| 53 | + pub fn reserve(&mut self, additional: usize) { |
| 54 | + self.offsets.reserve(additional); |
| 55 | + } |
| 56 | + |
| 57 | + /// Takes the builder itself and returns an [`OffsetBuffer`] |
| 58 | + /// |
| 59 | + /// # Panics |
| 60 | + /// |
| 61 | + /// Panics if adding `length` would overflow `usize` |
| 62 | + pub fn finish(self) -> OffsetBuffer<O> { |
| 63 | + O::from_usize(self.last_offset).expect("overflow"); |
| 64 | + unsafe { OffsetBuffer::new_unchecked(self.offsets.into()) } |
| 65 | + } |
| 66 | + |
| 67 | + /// Builds the [OffsetBuffer] without resetting the builder. |
| 68 | + /// |
| 69 | + /// # Panics |
| 70 | + /// |
| 71 | + /// Panics if adding `length` would overflow `usize` |
| 72 | + pub fn finish_cloned(&self) -> OffsetBuffer<O> { |
| 73 | + O::from_usize(self.last_offset).expect("overflow"); |
| 74 | + unsafe { OffsetBuffer::new_unchecked(self.offsets.clone().into()) } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +impl<O: ArrowNativeType> Deref for OffsetBufferBuilder<O> { |
| 79 | + type Target = [O]; |
| 80 | + |
| 81 | + fn deref(&self) -> &Self::Target { |
| 82 | + self.offsets.as_ref() |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +#[cfg(test)] |
| 87 | +mod tests { |
| 88 | + use crate::OffsetBufferBuilder; |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn test_basic() { |
| 92 | + let mut builder = OffsetBufferBuilder::<i32>::new(5); |
| 93 | + assert_eq!(builder.len(), 1); |
| 94 | + assert_eq!(&*builder, &[0]); |
| 95 | + let finished = builder.finish_cloned(); |
| 96 | + assert_eq!(finished.len(), 1); |
| 97 | + assert_eq!(&*finished, &[0]); |
| 98 | + |
| 99 | + builder.push_length(2); |
| 100 | + builder.push_length(6); |
| 101 | + builder.push_length(0); |
| 102 | + builder.push_length(13); |
| 103 | + |
| 104 | + let finished = builder.finish(); |
| 105 | + assert_eq!(&*finished, &[0, 2, 8, 8, 21]); |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + #[should_panic(expected = "overflow")] |
| 110 | + fn test_usize_overflow() { |
| 111 | + let mut builder = OffsetBufferBuilder::<i32>::new(5); |
| 112 | + builder.push_length(1); |
| 113 | + builder.push_length(usize::MAX); |
| 114 | + builder.finish(); |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + #[should_panic(expected = "overflow")] |
| 119 | + fn test_i32_overflow() { |
| 120 | + let mut builder = OffsetBufferBuilder::<i32>::new(5); |
| 121 | + builder.push_length(1); |
| 122 | + builder.push_length(i32::MAX as usize); |
| 123 | + builder.finish(); |
| 124 | + } |
| 125 | +} |
0 commit comments