rluvaton commented on code in PR #8653:
URL: https://github.com/apache/arrow-rs/pull/8653#discussion_r2443496247
##########
arrow-buffer/src/buffer/mutable.rs:
##########
@@ -222,6 +222,82 @@ impl MutableBuffer {
}
}
+ /// Creates a new [`MutableBuffer`] by repeating the contents of
`slice_to_repeat`
+ /// `repeat_count` times.
+ pub fn new_repeated<T: ArrowNativeType>(repeat_count: usize,
slice_to_repeat: &[T]) -> Self {
+ if slice_to_repeat.is_empty() || repeat_count == 0 {
+ return Self::new(0);
+ }
+
+ // If we keep extending from ourself we will reach it pretty fast
+ let value_len = slice_to_repeat.len();
+ let final_len = repeat_count * value_len;
+ let mut mutable = Self::with_capacity(final_len);
+
+ mutable.push_slice_repeated(repeat_count, slice_to_repeat);
+
+ mutable
+ }
+
+ /// Adding to this mutable buffer `slice_to_repeat` repeated
`repeat_count` times.
+ pub fn push_slice_repeated<T: ArrowNativeType>(
+ &mut self,
+ repeat_count: usize,
+ slice_to_repeat: &[T],
+ ) {
+ if repeat_count == 0 || slice_to_repeat.is_empty() {
+ return;
+ }
+
+ // Ensure capacity
+ let additional = repeat_count * mem::size_of_val(slice_to_repeat);
+ self.reserve(additional);
+
+ // No need to special case small repeat counts
+ if repeat_count <= 3 {
+ for _ in 0..repeat_count {
+ self.extend_from_slice(slice_to_repeat);
+ }
+
+ return;
+ }
+
+ // If we keep extending from ourself we will reach it pretty fast
Review Comment:
TODO - update the comment to mention logarithmic and add more comments to
explain the algorithm
--
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]