1use super::{from_raw_parts, memchr};
4use crate::ascii;
5use crate::cmp::{self, BytewiseEq, Ordering};
6use crate::intrinsics::compare_bytes;
7use crate::num::NonZero;
8use crate::ops::ControlFlow;
9
10#[stable(feature = "rust1", since = "1.0.0")]
11impl<T, U> PartialEq<[U]> for [T]
12where
13 T: PartialEq<U>,
14{
15 fn eq(&self, other: &[U]) -> bool {
16 SlicePartialEq::equal(self, other)
17 }
18
19 fn ne(&self, other: &[U]) -> bool {
20 SlicePartialEq::not_equal(self, other)
21 }
22}
23
24#[stable(feature = "rust1", since = "1.0.0")]
25impl<T: Eq> Eq for [T] {}
26
27#[stable(feature = "rust1", since = "1.0.0")]
29impl<T: Ord> Ord for [T] {
30 fn cmp(&self, other: &[T]) -> Ordering {
31 SliceOrd::compare(self, other)
32 }
33}
34
35#[inline]
36fn as_underlying(x: ControlFlow<bool>) -> u8 {
37 unsafe { crate::mem::transmute(x) }
44}
45
46#[stable(feature = "rust1", since = "1.0.0")]
48impl<T: PartialOrd> PartialOrd for [T] {
49 #[inline]
50 fn partial_cmp(&self, other: &[T]) -> Option<Ordering> {
51 SlicePartialOrd::partial_compare(self, other)
52 }
53 #[inline]
54 fn lt(&self, other: &Self) -> bool {
55 as_underlying(self.__chaining_lt(other)) == 1
64 }
65 #[inline]
66 fn le(&self, other: &Self) -> bool {
67 as_underlying(self.__chaining_le(other)) != 0
68 }
69 #[inline]
70 fn gt(&self, other: &Self) -> bool {
71 as_underlying(self.__chaining_gt(other)) == 1
72 }
73 #[inline]
74 fn ge(&self, other: &Self) -> bool {
75 as_underlying(self.__chaining_ge(other)) != 0
76 }
77 #[inline]
78 fn __chaining_lt(&self, other: &Self) -> ControlFlow<bool> {
79 SliceChain::chaining_lt(self, other)
80 }
81 #[inline]
82 fn __chaining_le(&self, other: &Self) -> ControlFlow<bool> {
83 SliceChain::chaining_le(self, other)
84 }
85 #[inline]
86 fn __chaining_gt(&self, other: &Self) -> ControlFlow<bool> {
87 SliceChain::chaining_gt(self, other)
88 }
89 #[inline]
90 fn __chaining_ge(&self, other: &Self) -> ControlFlow<bool> {
91 SliceChain::chaining_ge(self, other)
92 }
93}
94
95#[doc(hidden)]
96trait SlicePartialEq<B> {
98 fn equal(&self, other: &[B]) -> bool;
99
100 fn not_equal(&self, other: &[B]) -> bool {
101 !self.equal(other)
102 }
103}
104
105impl<A, B> SlicePartialEq<B> for [A]
107where
108 A: PartialEq<B>,
109{
110 default fn equal(&self, other: &[B]) -> bool {
111 if self.len() != other.len() {
112 return false;
113 }
114
115 for idx in 0..self.len() {
119 if self[idx] != other[idx] {
121 return false;
122 }
123 }
124
125 true
126 }
127}
128
129impl<A, B> SlicePartialEq<B> for [A]
132where
133 A: BytewiseEq<B>,
134{
135 fn equal(&self, other: &[B]) -> bool {
136 if self.len() != other.len() {
137 return false;
138 }
139
140 unsafe {
143 let size = size_of_val(self);
144 compare_bytes(self.as_ptr() as *const u8, other.as_ptr() as *const u8, size) == 0
145 }
146 }
147}
148
149#[doc(hidden)]
150trait SlicePartialOrd: Sized {
152 fn partial_compare(left: &[Self], right: &[Self]) -> Option<Ordering>;
153}
154
155#[doc(hidden)]
156trait SliceChain: Sized {
158 fn chaining_lt(left: &[Self], right: &[Self]) -> ControlFlow<bool>;
159 fn chaining_le(left: &[Self], right: &[Self]) -> ControlFlow<bool>;
160 fn chaining_gt(left: &[Self], right: &[Self]) -> ControlFlow<bool>;
161 fn chaining_ge(left: &[Self], right: &[Self]) -> ControlFlow<bool>;
162}
163
164type AlwaysBreak<B> = ControlFlow<B, crate::convert::Infallible>;
165
166impl<A: PartialOrd> SlicePartialOrd for A {
167 default fn partial_compare(left: &[A], right: &[A]) -> Option<Ordering> {
168 let elem_chain = |a, b| match PartialOrd::partial_cmp(a, b) {
169 Some(Ordering::Equal) => ControlFlow::Continue(()),
170 non_eq => ControlFlow::Break(non_eq),
171 };
172 let len_chain = |a: &_, b: &_| ControlFlow::Break(usize::partial_cmp(a, b));
173 let AlwaysBreak::Break(b) = chaining_impl(left, right, elem_chain, len_chain);
174 b
175 }
176}
177
178impl<A: PartialOrd> SliceChain for A {
179 default fn chaining_lt(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
180 chaining_impl(left, right, PartialOrd::__chaining_lt, usize::__chaining_lt)
181 }
182 default fn chaining_le(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
183 chaining_impl(left, right, PartialOrd::__chaining_le, usize::__chaining_le)
184 }
185 default fn chaining_gt(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
186 chaining_impl(left, right, PartialOrd::__chaining_gt, usize::__chaining_gt)
187 }
188 default fn chaining_ge(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
189 chaining_impl(left, right, PartialOrd::__chaining_ge, usize::__chaining_ge)
190 }
191}
192
193#[inline]
194fn chaining_impl<'l, 'r, A: PartialOrd, B, C>(
195 left: &'l [A],
196 right: &'r [A],
197 elem_chain: impl Fn(&'l A, &'r A) -> ControlFlow<B>,
198 len_chain: impl for<'a> FnOnce(&'a usize, &'a usize) -> ControlFlow<B, C>,
199) -> ControlFlow<B, C> {
200 let l = cmp::min(left.len(), right.len());
201
202 let lhs = &left[..l];
205 let rhs = &right[..l];
206
207 for i in 0..l {
208 elem_chain(&lhs[i], &rhs[i])?;
209 }
210
211 len_chain(&left.len(), &right.len())
212}
213
214impl<A: AlwaysApplicableOrd> SlicePartialOrd for A {
228 fn partial_compare(left: &[A], right: &[A]) -> Option<Ordering> {
229 Some(SliceOrd::compare(left, right))
230 }
231}
232
233#[rustc_specialization_trait]
234trait AlwaysApplicableOrd: SliceOrd + Ord {}
235
236macro_rules! always_applicable_ord {
237 ($([$($p:tt)*] $t:ty,)*) => {
238 $(impl<$($p)*> AlwaysApplicableOrd for $t {})*
239 }
240}
241
242always_applicable_ord! {
243 [] u8, [] u16, [] u32, [] u64, [] u128, [] usize,
244 [] i8, [] i16, [] i32, [] i64, [] i128, [] isize,
245 [] bool, [] char,
246 [T: ?Sized] *const T, [T: ?Sized] *mut T,
247 [T: AlwaysApplicableOrd] &T,
248 [T: AlwaysApplicableOrd] &mut T,
249 [T: AlwaysApplicableOrd] Option<T>,
250}
251
252#[doc(hidden)]
253trait SliceOrd: Sized {
255 fn compare(left: &[Self], right: &[Self]) -> Ordering;
256}
257
258impl<A: Ord> SliceOrd for A {
259 default fn compare(left: &[Self], right: &[Self]) -> Ordering {
260 let elem_chain = |a, b| match Ord::cmp(a, b) {
261 Ordering::Equal => ControlFlow::Continue(()),
262 non_eq => ControlFlow::Break(non_eq),
263 };
264 let len_chain = |a: &_, b: &_| ControlFlow::Break(usize::cmp(a, b));
265 let AlwaysBreak::Break(b) = chaining_impl(left, right, elem_chain, len_chain);
266 b
267 }
268}
269
270#[rustc_specialization_trait]
278unsafe trait UnsignedBytewiseOrd: Ord {}
279
280unsafe impl UnsignedBytewiseOrd for bool {}
281unsafe impl UnsignedBytewiseOrd for u8 {}
282unsafe impl UnsignedBytewiseOrd for NonZero<u8> {}
283unsafe impl UnsignedBytewiseOrd for Option<NonZero<u8>> {}
284unsafe impl UnsignedBytewiseOrd for ascii::Char {}
285
286impl<A: Ord + UnsignedBytewiseOrd> SliceOrd for A {
289 #[inline]
290 fn compare(left: &[Self], right: &[Self]) -> Ordering {
291 let diff = left.len() as isize - right.len() as isize;
294 let len = if left.len() < right.len() { left.len() } else { right.len() };
297 let left = left.as_ptr().cast();
298 let right = right.as_ptr().cast();
299 let mut order = unsafe { compare_bytes(left, right, len) as isize };
305 if order == 0 {
306 order = diff;
307 }
308 order.cmp(&0)
309 }
310}
311
312impl<A: PartialOrd + UnsignedBytewiseOrd> SliceChain for A {
314 #[inline]
315 fn chaining_lt(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
316 match SliceOrd::compare(left, right) {
317 Ordering::Equal => ControlFlow::Continue(()),
318 ne => ControlFlow::Break(ne.is_lt()),
319 }
320 }
321 #[inline]
322 fn chaining_le(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
323 match SliceOrd::compare(left, right) {
324 Ordering::Equal => ControlFlow::Continue(()),
325 ne => ControlFlow::Break(ne.is_le()),
326 }
327 }
328 #[inline]
329 fn chaining_gt(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
330 match SliceOrd::compare(left, right) {
331 Ordering::Equal => ControlFlow::Continue(()),
332 ne => ControlFlow::Break(ne.is_gt()),
333 }
334 }
335 #[inline]
336 fn chaining_ge(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
337 match SliceOrd::compare(left, right) {
338 Ordering::Equal => ControlFlow::Continue(()),
339 ne => ControlFlow::Break(ne.is_ge()),
340 }
341 }
342}
343
344pub(super) trait SliceContains: Sized {
345 fn slice_contains(&self, x: &[Self]) -> bool;
346}
347
348impl<T> SliceContains for T
349where
350 T: PartialEq,
351{
352 default fn slice_contains(&self, x: &[Self]) -> bool {
353 x.iter().any(|y| *y == *self)
354 }
355}
356
357impl SliceContains for u8 {
358 #[inline]
359 fn slice_contains(&self, x: &[Self]) -> bool {
360 memchr::memchr(*self, x).is_some()
361 }
362}
363
364impl SliceContains for i8 {
365 #[inline]
366 fn slice_contains(&self, x: &[Self]) -> bool {
367 let byte = *self as u8;
368 let bytes: &[u8] = unsafe { from_raw_parts(x.as_ptr() as *const u8, x.len()) };
373 memchr::memchr(byte, bytes).is_some()
374 }
375}
376
377macro_rules! impl_slice_contains {
378 ($($t:ty),*) => {
379 $(
380 impl SliceContains for $t {
381 #[inline]
382 fn slice_contains(&self, arr: &[$t]) -> bool {
383 const LANE_COUNT: usize = 4 * (128 / (size_of::<$t>() * 8));
386 let mut chunks = arr.chunks_exact(LANE_COUNT);
388 for chunk in &mut chunks {
389 if chunk.iter().fold(false, |acc, x| acc | (*x == *self)) {
390 return true;
391 }
392 }
393 return chunks.remainder().iter().any(|x| *x == *self);
395 }
396 }
397 )*
398 };
399}
400
401impl_slice_contains!(u16, u32, u64, i16, i32, i64, f32, f64, usize, isize, char);