Skip to content

Commit fdec8ba

Browse files
committed
fix: calculation method both for bucket bits in std/nod_std
1 parent e1ebd08 commit fdec8ba

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

src/slotbkvec.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,14 @@ impl<'a, T, const N: usize> SlotBkVec<'a, T, N> {
4545
// N <= 4: last tow bits mask: 0b11 , buckets: (0,1,2,3)
4646
// N <= 8: last three bits mask: 0b111 , buckets: (0,1,2,3,4,5,6,7)
4747
// N <= 16: last four bits mask: 0b1111 , buckets: (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
48-
let buckets = N;
49-
(buckets as f64).log2().ceil() as usize
48+
// this calculation method only works in std: `(N as f64).log2().ceil() as usize`
49+
match N {
50+
1..=2 => 1,
51+
3..=4 => 2,
52+
5..=8 => 3,
53+
9..=16 => 4,
54+
_ => unreachable!(),
55+
}
5056
}
5157

5258
/// Returns the bucket mask from bucket bit numbers.
@@ -86,7 +92,7 @@ impl<'a, T, const N: usize> SlotBkVec<'a, T, N> {
8692
(index << bucket_bits) | (buket & bucket_mask)
8793
}
8894

89-
/// Iterates every element in all of bucket slices in this buckets, as mutable.
95+
/// Iterates every element of all slices of the buckets, as mutable.
9096
#[auto_enums::auto_enum(DoubleEndedIterator)]
9197
fn iter_mut_impl<'s>(&'s mut self) -> impl DoubleEndedIterator<Item = &'s mut T> {
9298
// This implementation does not affect performance, because N is determined
@@ -194,17 +200,17 @@ impl<'a, T, const N: usize> SlotBkVec<'a, T, N> {
194200
self.buckets[buket].remove(index)
195201
}
196202

197-
/// Returns the number of valid elements in all of bucket slices in this buckets.
203+
/// Returns the number of valid elements of all slices of the buckets.
198204
pub fn len(&self) -> usize {
199205
self.iter().count()
200206
}
201207

202-
/// Returns true if all of bucket slices in this buckets contains no valid elements.
208+
/// Returns true if all slices of the buckets contains no valid elements.
203209
pub fn is_empty(&self) -> bool {
204210
self.len() == 0
205211
}
206212

207-
/// Iterates every element in the bucket slice of this buckets, as immutable.
213+
/// Iterates every element of the bucket slice, as immutable.
208214
///
209215
/// # Panics
210216
/// This function panics if the bucket number is overflow (>N).
@@ -218,7 +224,7 @@ impl<'a, T, const N: usize> SlotBkVec<'a, T, N> {
218224
.flatten()
219225
}
220226

221-
/// Iterates every element in the bucket slice of this buckets, as mutable.
227+
/// Iterates every element of the bucket slice, as mutable.
222228
///
223229
/// # Panics
224230
/// This function panics if the bucket number is overflow (>N).
@@ -235,12 +241,12 @@ impl<'a, T, const N: usize> SlotBkVec<'a, T, N> {
235241
.flatten()
236242
}
237243

238-
/// Iterates every element in all of bucket slices in this buckets, as immutable.
244+
/// Iterates every element of all slices of the buckets, as immutable.
239245
pub fn iter<'s>(&'s self) -> impl DoubleEndedIterator<Item = &'s T> {
240246
self.buckets.iter().flat_map(|slice| slice.iter())
241247
}
242248

243-
/// Iterates every element in all of bucket slices in this buckets, as mutable.
249+
/// Iterates every element of all slices of the buckets, as mutable.
244250
pub fn iter_mut<'s>(&'s mut self) -> impl DoubleEndedIterator<Item = &'s mut T> {
245251
// NB: In the future we may use a more concise Rust API like this.
246252
// Although it is possible to use the Captures trick here,

src/slotdeque.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,15 @@ impl<'a, T> SlotDeque<'a, T> {
128128
self.slices.bucket_iter_mut(Self::BUCKET_BACK)
129129
}
130130

131-
/// Iterates every element in this deque, as immutable.
131+
/// Iterates every element of this deque, as immutable.
132132
pub fn iter<'s>(&'s self) -> impl DoubleEndedIterator<Item = &'s T> {
133133
// Compile ok because that borrow immutable on self.
134134
// self.front_iter().chain(self.back_iter())
135135
let [front, back] = self.slices.each_ref();
136136
front.rev().chain(back)
137137
}
138138

139-
/// Iterates every element in this deque, as mutable.
139+
/// Iterates every element of this deque, as mutable.
140140
pub fn iter_mut<'s>(&'s mut self) -> impl DoubleEndedIterator<Item = &'s mut T> {
141141
// Compile error because that borrow twice mutable on self.
142142
// self.front_iter_mut().chain(self.back_iter_mut())

src/slotvec.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use super::ManagedSlice as Slice;
99

1010
/// Provides a slotvec based on external memory.
1111
///
12-
/// Provides a mapping between slots and elements.
12+
/// Provides mappings between slots and elements.
1313
///
1414
/// A slotvec provides a vector(`Vec`)-like interface where each entry is
1515
/// associated with a stable index. Lookup with the index will detect if
@@ -82,7 +82,7 @@ impl<'a, T> SlotVec<'a, T> {
8282
}
8383
}
8484

85-
/// Pushes an element to the back of the bucket slice in the vec.
85+
/// Pushes an element to the back in the vec.
8686
///
8787
/// Returns None if the slice is fixed-size (not a `Vec`) and is full.
8888
pub fn push(&mut self, elem: T) -> Option<usize> {
@@ -127,12 +127,12 @@ impl<'a, T> SlotVec<'a, T> {
127127
self.len() == 0
128128
}
129129

130-
/// Iterates every element in the bucket slice of this vec, as immutable.
130+
/// Iterates every element of this vec, as immutable.
131131
pub fn iter<'s>(&'s self) -> impl DoubleEndedIterator<Item = &'s T> {
132132
self.slice.iter().filter_map(|slot| slot.as_ref())
133133
}
134134

135-
/// Iterates every element in the bucket slice of this vec, as mutable.
135+
/// Iterates every element of this vec, as mutable.
136136
pub fn iter_mut<'s>(&'s mut self) -> impl DoubleEndedIterator<Item = &'s mut T> {
137137
self.slice.iter_mut().filter_map(|slot| slot.as_mut())
138138
}

0 commit comments

Comments
 (0)