Skip to content

Commit

Permalink
repeat [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Mar 7, 2025
1 parent 2d32fbc commit 8eb93dc
Show file tree
Hide file tree
Showing 38 changed files with 76 additions and 83 deletions.
4 changes: 2 additions & 2 deletions crates/polars-arrow/src/array/binview/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ impl<T: ViewType + ?Sized> MutableBinaryViewArray<T> {
self.init_validity(false);
}
self.views
.extend(std::iter::repeat(View::default()).take(additional));
.extend(std::iter::repeat_n(View::default(), additional));
if let Some(validity) = &mut self.validity {
validity.extend_constant(additional, false);
}
Expand All @@ -365,7 +365,7 @@ impl<T: ViewType + ?Sized> MutableBinaryViewArray<T> {
})
.unwrap_or_default();
self.views
.extend(std::iter::repeat(view_value).take(additional));
.extend(std::iter::repeat_n(view_value, additional));
}

impl_mutable_array_mut_validity!();
Expand Down
7 changes: 4 additions & 3 deletions crates/polars-arrow/src/array/boolean/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,10 @@ impl MutableBooleanArray {
if value.is_none() && self.validity.is_none() {
// When the validity is None, all elements so far are valid. When one of the elements is set of null,
// the validity must be initialized.
self.validity = Some(MutableBitmap::from_trusted_len_iter(
std::iter::repeat(true).take(self.len()),
));
self.validity = Some(MutableBitmap::from_trusted_len_iter(std::iter::repeat_n(
true,
self.len(),
)));
}
if let Some(x) = self.validity.as_mut() {
x.set(index, value.is_some())
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-arrow/src/array/fixed_size_list/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<A: ffi::ArrowArrayRef> FromFfi<A> for FixedSizeListArray {
let child = unsafe { array.child(0) }?;
let values = ffi::try_from(child)?;

let length = if values.len() == 0 {
let length = if values.is_empty() {
0
} else {
polars_ensure!(width > 0, InvalidOperation: "Zero-width array with values");
Expand Down
4 changes: 2 additions & 2 deletions crates/polars-arrow/src/array/fixed_size_list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl FixedSizeListArray {
values.len() / size,
length,
);
polars_ensure!(size != 0 || values.len() == 0, ComputeError:
polars_ensure!(size != 0 || values.is_empty(), ComputeError:
"zero width FixedSizeListArray has values (length = {}).",
values.len(),
);
Expand All @@ -84,7 +84,7 @@ impl FixedSizeListArray {

#[inline]
fn has_invariants(&self) -> bool {
let has_valid_length = (self.size == 0 && self.values().len() == 0)
let has_valid_length = (self.size == 0 && self.values().is_empty())
|| (self.size > 0
&& self.values().len() % self.size() == 0
&& self.values().len() / self.size() == self.length);
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-arrow/src/array/static_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub trait StaticArray:
fn full_null(length: usize, dtype: ArrowDataType) -> Self;

fn full(length: usize, value: Self::ValueT<'_>, dtype: ArrowDataType) -> Self {
Self::arr_from_iter_with_dtype(dtype, std::iter::repeat(value).take(length))
Self::arr_from_iter_with_dtype(dtype, std::iter::repeat_n(value, length))
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/polars-arrow/src/bitmap/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ impl MutableBitmap {
let required = (self.length + additional).saturating_add(7) / 8;
// add remaining as full bytes
self.buffer
.extend(std::iter::repeat(0b11111111u8).take(required - existing));
.extend(std::iter::repeat_n(0b11111111u8, required - existing));
self.length += additional;
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/polars-arrow/src/legacy/kernels/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ where
if truthy {
av.extend_from_slice(&values[lower..upper])
} else {
av.extend_trusted_len(std::iter::repeat(value).take(upper - lower))
av.extend_trusted_len(std::iter::repeat_n(value, upper - lower))
}
});

Expand All @@ -49,7 +49,7 @@ pub fn set_with_mask<T: NativeType>(
let mut buf = Vec::with_capacity(array.len());
BinaryMaskedSliceIterator::new(mask).for_each(|(lower, upper, truthy)| {
if truthy {
buf.extend_trusted_len(std::iter::repeat(value).take(upper - lower))
buf.extend_trusted_len(std::iter::repeat_n(value, upper - lower))
} else {
buf.extend_from_slice(&values[lower..upper])
}
Expand Down
5 changes: 4 additions & 1 deletion crates/polars-arrow/src/legacy/kernels/sorted_join/left.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ pub fn join<T: PartialOrd + Copy + Debug>(

let first_right = right[right_idx as usize];
let mut left_idx = left.partition_point(|v| v < &first_right) as IdxSize;
out_rhs.extend(std::iter::repeat(NullableIdxSize::null()).take(left_idx as usize));
out_rhs.extend(std::iter::repeat_n(
NullableIdxSize::null(),
left_idx as usize,
));
out_lhs.extend(left_offset..(left_idx + left_offset));

for &val_l in &left[left_idx as usize..] {
Expand Down
6 changes: 4 additions & 2 deletions crates/polars-arrow/src/trusted_len.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ unsafe impl<A: Clone> TrustedLen for std::iter::Repeat<A> {}
unsafe impl<A, F: FnMut() -> A> TrustedLen for std::iter::RepeatWith<F> {}
unsafe impl<A: TrustedLen> TrustedLen for std::iter::Take<A> {}

unsafe impl<A: Clone> TrustedLen for std::iter::RepeatN<A> {}

unsafe impl<T> TrustedLen for &mut dyn TrustedLen<Item = T> {}
unsafe impl<T> TrustedLen for Box<dyn TrustedLen<Item = T> + '_> {}

Expand Down Expand Up @@ -100,11 +102,11 @@ where
}
}

impl<J: Clone> TrustMyLength<std::iter::Take<std::iter::Repeat<J>>, J> {
impl<J: Clone> TrustMyLength<std::iter::RepeatN<J>, J> {
/// Create a new `TrustMyLength` iterator that repeats `value` `len` times.
pub fn new_repeat_n(value: J, len: usize) -> Self {
// SAFETY: This is always safe since repeat(..).take(n) always repeats exactly `n` times`.
unsafe { Self::new(std::iter::repeat(value).take(len), len) }
unsafe { Self::new(std::iter::repeat_n(value, len), len) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub(crate) fn create_extension<I: Iterator<Item = Option<T>> + TrustedLen, T: Si
// when we transmute from &[u8] to T, T must be aligned correctly,
// so we pad with bytes until the alignment matches
let n_padding = (buf.as_ptr() as usize) % t_alignment;
buf.extend(std::iter::repeat(0).take(n_padding));
buf.extend(std::iter::repeat_n(0, n_padding));

// transmute T as bytes and copy in buffer
for opt_t in iter.into_iter() {
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-core/src/chunked_array/ops/full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl ChunkFullNull for BinaryChunked {
impl<'a> ChunkFull<&'a [u8]> for BinaryOffsetChunked {
fn full(name: PlSmallStr, value: &'a [u8], length: usize) -> Self {
let mut mutable = MutableBinaryArray::with_capacities(length, length * value.len());
mutable.extend_values(std::iter::repeat(value).take(length));
mutable.extend_values(std::iter::repeat_n(value, length));
let arr: BinaryArray<i64> = mutable.into();
let mut out = ChunkedArray::with_chunk(name, arr);
out.set_sorted_flag(IsSorted::Ascending);
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-core/src/chunked_array/ops/rolling_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ mod inner_mod {
let validity_slice = validity.as_mut_slice();

let mut values = Vec::with_capacity(ca.len());
values.extend(std::iter::repeat(T::Native::default()).take(window_size - 1));
values.extend(std::iter::repeat_n(T::Native::default(), window_size - 1));

for offset in 0..self.len() + 1 - window_size {
debug_assert!(offset + window_size <= arr.len());
Expand Down
8 changes: 4 additions & 4 deletions crates/polars-core/src/chunked_array/ops/sort/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ where
let mut vals = Vec::with_capacity(ca.len());

if !options.nulls_last {
let iter = std::iter::repeat(T::Native::default()).take(null_count);
let iter = std::iter::repeat_n(T::Native::default(), null_count);
vals.extend(iter);
}

Expand All @@ -225,7 +225,7 @@ where
sort_impl_unstable(mut_slice, options);

if options.nulls_last {
vals.extend(std::iter::repeat(T::Native::default()).take(ca.null_count()));
vals.extend(std::iter::repeat_n(T::Native::default(), ca.null_count()));
}

let arr = PrimitiveArray::new(
Expand Down Expand Up @@ -534,7 +534,7 @@ impl ChunkSort<BinaryOffsetType> for BinaryOffsetChunked {
length_so_far = values.len() as i64;
offsets.push(length_so_far);
}
offsets.extend(std::iter::repeat(length_so_far).take(null_count));
offsets.extend(std::iter::repeat_n(length_so_far, null_count));

// SAFETY: offsets are correctly created.
let arr = unsafe {
Expand All @@ -547,7 +547,7 @@ impl ChunkSort<BinaryOffsetType> for BinaryOffsetChunked {
ChunkedArray::with_chunk(self.name().clone(), arr)
},
(_, false) => {
offsets.extend(std::iter::repeat(length_so_far).take(null_count));
offsets.extend(std::iter::repeat_n(length_so_far, null_count));

for val in v {
values.extend_from_slice(val);
Expand Down
6 changes: 1 addition & 5 deletions crates/polars-error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,7 @@ impl From<regex::Error> for PolarsError {
#[cfg(feature = "object_store")]
impl From<object_store::Error> for PolarsError {
fn from(err: object_store::Error) -> Self {
std::io::Error::new(
std::io::ErrorKind::Other,
format!("object-store error: {err:?}"),
)
.into()
std::io::Error::other(format!("object-store error: {err:?}")).into()
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/polars-json/src/json/write/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn null_serializer(
take: usize,
) -> Box<dyn StreamingIterator<Item = [u8]> + Send + Sync> {
let f = |_x: (), buf: &mut Vec<u8>| buf.extend_from_slice(b"null");
materialize_serializer(f, std::iter::repeat(()).take(len), offset, take)
materialize_serializer(f, std::iter::repeat_n((), len), offset, take)
}

fn primitive_serializer<'a, T: NativeType + itoa::Integer>(
Expand Down
8 changes: 4 additions & 4 deletions crates/polars-ops/src/chunked_array/repeat_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn check_lengths(length_srs: usize, length_by: usize) -> PolarsResult<()> {

fn new_by(by: &IdxCa, len: usize) -> IdxCa {
if let Some(x) = by.get(0) {
let values = std::iter::repeat(x).take(len).collect::<Vec<IdxSize>>();
let values = std::iter::repeat_n(x, len).collect::<Vec<IdxSize>>();
IdxCa::new(PlSmallStr::EMPTY, values)
} else {
IdxCa::full_null(PlSmallStr::EMPTY, len)
Expand All @@ -32,7 +32,7 @@ where
(left_len, right_len) if left_len == right_len => {
Ok(arity::binary(ca, by, |arr, by| {
let iter = arr.into_iter().zip(by).map(|(opt_v, opt_by)| {
opt_by.map(|by| std::iter::repeat(opt_v.copied()).take(*by as usize))
opt_by.map(|by| std::iter::repeat_n(opt_v.copied(), *by as usize))
});

// SAFETY: length of iter is trusted.
Expand Down Expand Up @@ -64,7 +64,7 @@ fn repeat_by_bool(ca: &BooleanChunked, by: &IdxCa) -> PolarsResult<ListChunked>
(left_len, right_len) if left_len == right_len => {
Ok(arity::binary(ca, by, |arr, by| {
let iter = arr.into_iter().zip(by).map(|(opt_v, opt_by)| {
opt_by.map(|by| std::iter::repeat(opt_v).take(*by as usize))
opt_by.map(|by| std::iter::repeat_n(opt_v, *by as usize))
});

// SAFETY: length of iter is trusted.
Expand All @@ -91,7 +91,7 @@ fn repeat_by_binary(ca: &BinaryChunked, by: &IdxCa) -> PolarsResult<ListChunked>
(left_len, right_len) if left_len == right_len => {
Ok(arity::binary(ca, by, |arr, by| {
let iter = arr.into_iter().zip(by).map(|(opt_v, opt_by)| {
opt_by.map(|by| std::iter::repeat(opt_v).take(*by as usize))
opt_by.map(|by| std::iter::repeat_n(opt_v, *by as usize))
});

// SAFETY: length of iter is trusted.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ where
match value {
// left and right matches
Some(indexes_b) => {
result_idx_left.extend(std::iter::repeat(idx_a).take(indexes_b.len()));
result_idx_left.extend(std::iter::repeat_n(idx_a, indexes_b.len()));
result_idx_right.extend_from_slice(bytemuck::cast_slice(indexes_b));
},
// only left values, right = null
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-ops/src/frame/join/merge_sorted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ where
}
// b is depleted fill with a indicator
let remaining = cap - out.len();
out.extend(std::iter::repeat(A_INDICATOR).take(remaining));
out.extend(std::iter::repeat_n(A_INDICATOR, remaining));
return out;
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-ops/src/series/ops/is_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ fn is_in_null(s: &Series, other: &Series, nulls_equal: bool) -> PolarsResult<Boo
// If other has null values, then all are true, else all are false.
BooleanChunked::from_iter_values(
ca_in.name().clone(),
std::iter::repeat(other.has_nulls()).take(ca_in.len()),
std::iter::repeat_n(other.has_nulls(), ca_in.len()),
)
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ mod tests {
// 0b00001011
// ]
let mut expected = vec![128u8, 2, 1, 6, 2, 7, 3, 0b01101101, 0b00001011];
expected.extend(std::iter::repeat(0).take(256 * 3 / 8 - 2)); // 128 values, 3 bits, 2 already used
expected.extend(std::iter::repeat_n(0, 256 * 3 / 8 - 2)); // 128 values, 3 bits, 2 already used

let mut buffer = vec![];
encode(data.into_iter(), &mut buffer, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl<K: ExtraPayload> GenericJoinProbe<K> {
let indexes_right = &indexes_right.0;
self.join_tuples_a.extend_from_slice(indexes_right);
self.join_tuples_b
.extend(std::iter::repeat(df_idx_left).take(indexes_right.len()));
.extend(std::iter::repeat_n(df_idx_left, indexes_right.len()));
},
None => {
self.join_tuples_b.push(df_idx_left);
Expand Down Expand Up @@ -238,7 +238,7 @@ impl<K: ExtraPayload> GenericJoinProbe<K> {
let indexes_left = &indexes_left.0;
self.join_tuples_a.extend_from_slice(indexes_left);
self.join_tuples_b
.extend(std::iter::repeat(df_idx_right).take(indexes_left.len()));
.extend(std::iter::repeat_n(df_idx_right, indexes_left.len()));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/polars-plan/src/plans/conversion/dsl_to_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,8 @@ pub fn to_alp_impl(lp: DslPlan, ctxt: &mut DslConversionContext) -> PolarsResult
)
.map_err(|e| e.context(failed_here!(sort)))?;

nulls_last.extend(std::iter::repeat(n).take(exprs.len()));
descending.extend(std::iter::repeat(d).take(exprs.len()));
nulls_last.extend(std::iter::repeat_n(n, exprs.len()));
descending.extend(std::iter::repeat_n(d, exprs.len()));
expanded_cols.extend(exprs);
}
sort_options.nulls_last = nulls_last;
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-python/src/interop/numpy/to_numpy_series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ fn series_to_numpy_with_copy(py: Python, s: &Series, writable: bool) -> PyObject
},
Null => {
let n = s.len();
let values = std::iter::repeat(f32::NAN).take(n);
let values = std::iter::repeat_n(f32::NAN, n);
PyArray1::from_iter(py, values).into_py_any(py).unwrap()
},
Unknown(_) | BinaryOffset => unreachable!(),
Expand Down
8 changes: 4 additions & 4 deletions crates/polars-python/src/map/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ where
I: Iterator<Item = Option<T>> + 'a,
{
let mut avs = Vec::with_capacity(len);
avs.extend(std::iter::repeat(AnyValue::Null).take(init_null_count));
avs.extend(std::iter::repeat_n(AnyValue::Null, init_null_count));
avs.push(first_value);

for opt_val in iter {
Expand Down Expand Up @@ -1184,7 +1184,7 @@ impl<'a> ApplyLambda<'a> for ListChunked {
) -> PyResult<Series> {
let pypolars = polars(py).bind(py);
let mut avs = Vec::with_capacity(self.len());
avs.extend(std::iter::repeat(AnyValue::Null).take(init_null_count));
avs.extend(std::iter::repeat_n(AnyValue::Null, init_null_count));
avs.push(first_value);

let call_with_value = |val: Series| {
Expand Down Expand Up @@ -1499,7 +1499,7 @@ impl<'a> ApplyLambda<'a> for ArrayChunked {
) -> PyResult<Series> {
let pypolars = polars(py).bind(py);
let mut avs = Vec::with_capacity(self.len());
avs.extend(std::iter::repeat(AnyValue::Null).take(init_null_count));
avs.extend(std::iter::repeat_n(AnyValue::Null, init_null_count));
avs.push(first_value);

let call_with_value = |val: Series| {
Expand Down Expand Up @@ -1951,7 +1951,7 @@ impl<'a> ApplyLambda<'a> for StructChunked {
first_value: AnyValue<'a>,
) -> PyResult<Series> {
let mut avs = Vec::with_capacity(self.len());
avs.extend(std::iter::repeat(AnyValue::Null).take(init_null_count));
avs.extend(std::iter::repeat_n(AnyValue::Null, init_null_count));
avs.push(first_value);

for val in iter_struct(self).skip(init_null_count + 1) {
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-python/src/series/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl PySeries {
DataType::Null => {
let null: Option<u8> = None;
let n = series.len();
let iter = std::iter::repeat(null).take(n);
let iter = std::iter::repeat_n(null, n);
use std::iter::{Repeat, Take};
struct NullIter {
iter: Take<Repeat<Option<u8>>>,
Expand Down
10 changes: 2 additions & 8 deletions crates/polars-utils/src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,20 +238,14 @@ impl io::Seek for MemReader {
io::SeekFrom::Start(position) => usize::min(position as usize, self.total_len()),
io::SeekFrom::End(offset) => {
let Some(position) = self.total_len().checked_add_signed(offset as isize) else {
return Err(io::Error::new(
io::ErrorKind::Other,
"Seek before to before buffer",
));
return Err(io::Error::other("Seek before to before buffer"));
};

position
},
io::SeekFrom::Current(offset) => {
let Some(position) = self.position.checked_add_signed(offset as isize) else {
return Err(io::Error::new(
io::ErrorKind::Other,
"Seek before to before buffer",
));
return Err(io::Error::other("Seek before to before buffer"));
};

position
Expand Down
Loading

0 comments on commit 8eb93dc

Please sign in to comment.