MCPcopy Create free account
hub / github.com/apache/arrow-rs / from_lengths

Method from_lengths

arrow-buffer/src/buffer/offset.rs:121–137  ·  view source on GitHub ↗

Create a new [`OffsetBuffer`] from the iterator of slice lengths ``` # use arrow_buffer::OffsetBuffer; let offsets = OffsetBuffer:: ::from_lengths([1, 3, 5]); assert_eq!(offsets.as_ref(), &[0, 1, 4, 9]); ``` If you want to create an [`OffsetBuffer`] where all lengths are the same, consider using the faster [`OffsetBuffer::from_repeated_length`] instead. # Panics Panics on overflow

(lengths: I)

Source from the content-addressed store, hash-verified

119 ///
120 /// Panics on overflow
121 pub fn from_lengths<I>(lengths: I) -> Self
122 where
123 I: IntoIterator<Item = usize>,
124 {
125 let iter = lengths.into_iter();
126 let mut out = Vec::with_capacity(iter.size_hint().0 + 1);
127 out.push(O::usize_as(0));
128
129 let mut acc = 0_usize;
130 for length in iter {
131 acc = acc.checked_add(length).expect("usize overflow");
132 out.push(O::usize_as(acc))
133 }
134 // Check for overflow
135 O::from_usize(acc).expect("offset overflow");
136 Self(out.into())
137 }
138
139 /// Create a new [`OffsetBuffer`] where each slice has the same length
140 /// `length`, repeated `n` times.

Callers

nothing calls this directly

Calls 4

into_iterMethod · 0.45
size_hintMethod · 0.45
pushMethod · 0.45
checked_addMethod · 0.45

Tested by

no test coverage detected