From 325d3827cb8e8f2e5b3a09ed46c9afa713000df1 Mon Sep 17 00:00:00 2001 From: Ronno Das Date: Wed, 11 Sep 2024 21:13:22 +0200 Subject: [PATCH] IndexMut -> BorrowMut --- src/combinations.rs | 51 +++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/src/combinations.rs b/src/combinations.rs index 0b2806f68..c8ec14142 100644 --- a/src/combinations.rs +++ b/src/combinations.rs @@ -1,5 +1,5 @@ use core::array; -use core::ops::IndexMut; +use core::borrow::BorrowMut; use std::fmt; use std::iter::FusedIterator; @@ -39,41 +39,33 @@ pub struct CombinationsGeneric { first: bool, } -pub trait PoolIndex: IndexMut { +pub trait PoolIndex: BorrowMut<[usize]> { type Item; - fn len(&self) -> usize; fn extract_item>(&self, pool: &LazyBuffer) -> Self::Item where T: Clone; - fn as_slice(&self) -> &[usize]; + + #[inline] + fn len(&self) -> usize { + self.borrow().len() + } } impl PoolIndex for Vec { type Item = Vec; - fn len(&self) -> usize { - self.len() - } fn extract_item>(&self, pool: &LazyBuffer) -> Vec where T: Clone, { pool.get_at(self) } - - fn as_slice(&self) -> &[usize] { - self - } } impl PoolIndex for [usize; K] { type Item = [T; K]; - fn len(&self) -> usize { - K - } - fn extract_item>(&self, pool: &LazyBuffer) -> [T; K] where T: Clone, @@ -81,8 +73,8 @@ impl PoolIndex for [usize; K] { pool.get_array(*self) } - fn as_slice(&self) -> &[usize] { - self + fn len(&self) -> usize { + K } } @@ -142,7 +134,7 @@ impl> CombinationsGeneric { } = self; { let n = pool.count(); - (n, remaining_for(n, first, indices.as_slice()).unwrap()) + (n, remaining_for(n, first, indices.borrow()).unwrap()) } } @@ -164,19 +156,21 @@ impl> CombinationsGeneric { /// /// Returns true if we've run out of combinations, false otherwise. fn increment_indices(&mut self) -> bool { - if self.indices.len() == 0 { + // Borrow once instead of noise each time it's indexed + let indices = self.indices.borrow_mut(); + + if indices.is_empty() { return true; // Done } - // Scan from the end, looking for an index to increment - let mut i: usize = self.indices.len() - 1; + let mut i: usize = indices.len() - 1; // Check if we need to consume more from the iterator - if self.indices[i] == self.pool.len() - 1 { + if indices[i] == self.pool.len() - 1 { self.pool.get_next(); // may change pool size } - while self.indices[i] == i + self.pool.len() - self.indices.len() { + while indices[i] == i + self.pool.len() - indices.len() { if i > 0 { i -= 1; } else { @@ -186,11 +180,10 @@ impl> CombinationsGeneric { } // Increment index, and reset the ones to its right - self.indices[i] += 1; - for j in i + 1..self.indices.len() { - self.indices[j] = self.indices[j - 1] + 1; + indices[i] += 1; + for j in i + 1..indices.len() { + indices[j] = indices[j - 1] + 1; } - // If we've made it this far, we haven't run out of combos false } @@ -245,8 +238,8 @@ where fn size_hint(&self) -> (usize, Option) { let (mut low, mut upp) = self.pool.size_hint(); - low = remaining_for(low, self.first, self.indices.as_slice()).unwrap_or(usize::MAX); - upp = upp.and_then(|upp| remaining_for(upp, self.first, self.indices.as_slice())); + low = remaining_for(low, self.first, self.indices.borrow()).unwrap_or(usize::MAX); + upp = upp.and_then(|upp| remaining_for(upp, self.first, self.indices.borrow())); (low, upp) }