1use super::{Bucket, Entries, IndexSet, IntoIter, Iter};
2use crate::util::{slice_eq, try_simplify_range};
3
4use alloc::boxed::Box;
5use alloc::vec::Vec;
6use core::cmp::Ordering;
7use core::fmt;
8use core::hash::{Hash, Hasher};
9use core::ops::{self, Bound, Index, RangeBounds};
10
11#[repr(transparent)]
19pub struct Slice<T> {
20 pub(crate) entries: [Bucket<T>],
21}
22
23#[allow(unsafe_code)]
26impl<T> Slice<T> {
27 pub(super) const fn from_slice(entries: &[Bucket<T>]) -> &Self {
28 unsafe { &*(entries as *const [Bucket<T>] as *const Self) }
29 }
30
31 pub(super) fn from_boxed(entries: Box<[Bucket<T>]>) -> Box<Self> {
32 unsafe { Box::from_raw(Box::into_raw(entries) as *mut Self) }
33 }
34
35 fn into_boxed(self: Box<Self>) -> Box<[Bucket<T>]> {
36 unsafe { Box::from_raw(Box::into_raw(self) as *mut [Bucket<T>]) }
37 }
38}
39
40impl<T> Slice<T> {
41 pub(crate) fn into_entries(self: Box<Self>) -> Vec<Bucket<T>> {
42 self.into_boxed().into_vec()
43 }
44
45 pub const fn new<'a>() -> &'a Self {
47 Self::from_slice(&[])
48 }
49
50 pub const fn len(&self) -> usize {
52 self.entries.len()
53 }
54
55 pub const fn is_empty(&self) -> bool {
57 self.entries.is_empty()
58 }
59
60 pub fn get_index(&self, index: usize) -> Option<&T> {
64 self.entries.get(index).map(Bucket::key_ref)
65 }
66
67 pub fn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<&Self> {
71 let range = try_simplify_range(range, self.entries.len())?;
72 self.entries.get(range).map(Self::from_slice)
73 }
74
75 pub fn first(&self) -> Option<&T> {
77 self.entries.first().map(Bucket::key_ref)
78 }
79
80 pub fn last(&self) -> Option<&T> {
82 self.entries.last().map(Bucket::key_ref)
83 }
84
85 pub fn split_at(&self, index: usize) -> (&Self, &Self) {
89 let (first, second) = self.entries.split_at(index);
90 (Self::from_slice(first), Self::from_slice(second))
91 }
92
93 pub fn split_first(&self) -> Option<(&T, &Self)> {
96 if let [first, rest @ ..] = &self.entries {
97 Some((&first.key, Self::from_slice(rest)))
98 } else {
99 None
100 }
101 }
102
103 pub fn split_last(&self) -> Option<(&T, &Self)> {
106 if let [rest @ .., last] = &self.entries {
107 Some((&last.key, Self::from_slice(rest)))
108 } else {
109 None
110 }
111 }
112
113 pub fn iter(&self) -> Iter<'_, T> {
115 Iter::new(&self.entries)
116 }
117
118 pub fn binary_search(&self, x: &T) -> Result<usize, usize>
127 where
128 T: Ord,
129 {
130 self.binary_search_by(|p| p.cmp(x))
131 }
132
133 #[inline]
140 pub fn binary_search_by<'a, F>(&'a self, mut f: F) -> Result<usize, usize>
141 where
142 F: FnMut(&'a T) -> Ordering,
143 {
144 self.entries.binary_search_by(move |a| f(&a.key))
145 }
146
147 #[inline]
154 pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, mut f: F) -> Result<usize, usize>
155 where
156 F: FnMut(&'a T) -> B,
157 B: Ord,
158 {
159 self.binary_search_by(|k| f(k).cmp(b))
160 }
161
162 #[must_use]
169 pub fn partition_point<P>(&self, mut pred: P) -> usize
170 where
171 P: FnMut(&T) -> bool,
172 {
173 self.entries.partition_point(move |a| pred(&a.key))
174 }
175}
176
177impl<'a, T> IntoIterator for &'a Slice<T> {
178 type IntoIter = Iter<'a, T>;
179 type Item = &'a T;
180
181 fn into_iter(self) -> Self::IntoIter {
182 self.iter()
183 }
184}
185
186impl<T> IntoIterator for Box<Slice<T>> {
187 type IntoIter = IntoIter<T>;
188 type Item = T;
189
190 fn into_iter(self) -> Self::IntoIter {
191 IntoIter::new(self.into_entries())
192 }
193}
194
195impl<T> Default for &'_ Slice<T> {
196 fn default() -> Self {
197 Slice::from_slice(&[])
198 }
199}
200
201impl<T> Default for Box<Slice<T>> {
202 fn default() -> Self {
203 Slice::from_boxed(Box::default())
204 }
205}
206
207impl<T: Clone> Clone for Box<Slice<T>> {
208 fn clone(&self) -> Self {
209 Slice::from_boxed(self.entries.to_vec().into_boxed_slice())
210 }
211}
212
213impl<T: Copy> From<&Slice<T>> for Box<Slice<T>> {
214 fn from(slice: &Slice<T>) -> Self {
215 Slice::from_boxed(Box::from(&slice.entries))
216 }
217}
218
219impl<T: fmt::Debug> fmt::Debug for Slice<T> {
220 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
221 f.debug_list().entries(self).finish()
222 }
223}
224
225impl<T, U> PartialEq<Slice<U>> for Slice<T>
226where
227 T: PartialEq<U>,
228{
229 fn eq(&self, other: &Slice<U>) -> bool {
230 slice_eq(&self.entries, &other.entries, |b1, b2| b1.key == b2.key)
231 }
232}
233
234impl<T, U> PartialEq<[U]> for Slice<T>
235where
236 T: PartialEq<U>,
237{
238 fn eq(&self, other: &[U]) -> bool {
239 slice_eq(&self.entries, other, |b, o| b.key == *o)
240 }
241}
242
243impl<T, U> PartialEq<Slice<U>> for [T]
244where
245 T: PartialEq<U>,
246{
247 fn eq(&self, other: &Slice<U>) -> bool {
248 slice_eq(self, &other.entries, |o, b| *o == b.key)
249 }
250}
251
252impl<T, U, const N: usize> PartialEq<[U; N]> for Slice<T>
253where
254 T: PartialEq<U>,
255{
256 fn eq(&self, other: &[U; N]) -> bool {
257 <Self as PartialEq<[U]>>::eq(self, other)
258 }
259}
260
261impl<T, const N: usize, U> PartialEq<Slice<U>> for [T; N]
262where
263 T: PartialEq<U>,
264{
265 fn eq(&self, other: &Slice<U>) -> bool {
266 <[T] as PartialEq<Slice<U>>>::eq(self, other)
267 }
268}
269
270impl<T: Eq> Eq for Slice<T> {}
271
272impl<T: PartialOrd> PartialOrd for Slice<T> {
273 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
274 self.iter().partial_cmp(other)
275 }
276}
277
278impl<T: Ord> Ord for Slice<T> {
279 fn cmp(&self, other: &Self) -> Ordering {
280 self.iter().cmp(other)
281 }
282}
283
284impl<T: Hash> Hash for Slice<T> {
285 fn hash<H: Hasher>(&self, state: &mut H) {
286 self.len().hash(state);
287 for value in self {
288 value.hash(state);
289 }
290 }
291}
292
293impl<T> Index<usize> for Slice<T> {
294 type Output = T;
295
296 fn index(&self, index: usize) -> &Self::Output {
297 &self.entries[index].key
298 }
299}
300
301macro_rules! impl_index {
304 ($($range:ty),*) => {$(
305 impl<T, S> Index<$range> for IndexSet<T, S> {
306 type Output = Slice<T>;
307
308 fn index(&self, range: $range) -> &Self::Output {
309 Slice::from_slice(&self.as_entries()[range])
310 }
311 }
312
313 impl<T> Index<$range> for Slice<T> {
314 type Output = Self;
315
316 fn index(&self, range: $range) -> &Self::Output {
317 Slice::from_slice(&self.entries[range])
318 }
319 }
320 )*}
321}
322impl_index!(
323 ops::Range<usize>,
324 ops::RangeFrom<usize>,
325 ops::RangeFull,
326 ops::RangeInclusive<usize>,
327 ops::RangeTo<usize>,
328 ops::RangeToInclusive<usize>,
329 (Bound<usize>, Bound<usize>)
330);
331
332#[cfg(test)]
333mod tests {
334 use super::*;
335
336 #[test]
337 fn slice_index() {
338 fn check(vec_slice: &[i32], set_slice: &Slice<i32>, sub_slice: &Slice<i32>) {
339 assert_eq!(set_slice as *const _, sub_slice as *const _);
340 itertools::assert_equal(vec_slice, set_slice);
341 }
342
343 let vec: Vec<i32> = (0..10).map(|i| i * i).collect();
344 let set: IndexSet<i32> = vec.iter().cloned().collect();
345 let slice = set.as_slice();
346
347 check(&vec[..], &set[..], &slice[..]);
349
350 for i in 0usize..10 {
351 assert_eq!(vec[i], set[i]);
353 assert_eq!(vec[i], slice[i]);
354
355 check(&vec[i..], &set[i..], &slice[i..]);
357
358 check(&vec[..i], &set[..i], &slice[..i]);
360
361 check(&vec[..=i], &set[..=i], &slice[..=i]);
363
364 let bounds = (Bound::Excluded(i), Bound::Unbounded);
366 check(&vec[i + 1..], &set[bounds], &slice[bounds]);
367
368 for j in i..=10 {
369 check(&vec[i..j], &set[i..j], &slice[i..j]);
371 }
372
373 for j in i..10 {
374 check(&vec[i..=j], &set[i..=j], &slice[i..=j]);
376 }
377 }
378 }
379}