| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | #![stable(feature = "rust1", since = "1.0.0")]
|
| 83 | |
| 84 | |
| 85 | #![cfg_attr(test, allow(unused_imports, dead_code))]
|
| 86 |
|
| 87 | use core::borrow::{Borrow, BorrowMut};
|
| 88 | #[cfg(not(no_global_oom_handling))]
|
| 89 | use core::cmp::Ordering::{self, Less};
|
| 90 | #[cfg(not(no_global_oom_handling))]
|
| 91 | use core::mem;
|
| 92 | #[cfg(not(no_global_oom_handling))]
|
| 93 | use core::mem::size_of;
|
| 94 | #[cfg(not(no_global_oom_handling))]
|
| 95 | use core::ptr;
|
| 96 |
|
| 97 | use crate::alloc::Allocator;
|
| 98 | #[cfg(not(no_global_oom_handling))]
|
| 99 | use crate::alloc::Global;
|
| 100 | #[cfg(not(no_global_oom_handling))]
|
| 101 | use crate::borrow::ToOwned;
|
| 102 | use crate::boxed::Box;
|
| 103 | use crate::vec::Vec;
|
| 104 |
|
| 105 | #[unstable(feature = "slice_range", issue = "76393")]
|
| 106 | pub use core::slice::range;
|
| 107 | #[unstable(feature = "array_chunks", issue = "74985")]
|
| 108 | pub use core::slice::ArrayChunks;
|
| 109 | #[unstable(feature = "array_chunks", issue = "74985")]
|
| 110 | pub use core::slice::ArrayChunksMut;
|
| 111 | #[unstable(feature = "array_windows", issue = "75027")]
|
| 112 | pub use core::slice::ArrayWindows;
|
| 113 | #[stable(feature = "inherent_ascii_escape", since = "1.60.0")]
|
| 114 | pub use core::slice::EscapeAscii;
|
| 115 | #[stable(feature = "slice_get_slice", since = "1.28.0")]
|
| 116 | pub use core::slice::SliceIndex;
|
| 117 | #[stable(feature = "from_ref", since = "1.28.0")]
|
| 118 | pub use core::slice::{from_mut, from_ref};
|
| 119 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 120 | pub use core::slice::{from_raw_parts, from_raw_parts_mut};
|
| 121 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 122 | pub use core::slice::{Chunks, Windows};
|
| 123 | #[stable(feature = "chunks_exact", since = "1.31.0")]
|
| 124 | pub use core::slice::{ChunksExact, ChunksExactMut};
|
| 125 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 126 | pub use core::slice::{ChunksMut, Split, SplitMut};
|
| 127 | #[unstable(feature = "slice_group_by", issue = "80552")]
|
| 128 | pub use core::slice::{GroupBy, GroupByMut};
|
| 129 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 130 | pub use core::slice::{Iter, IterMut};
|
| 131 | #[stable(feature = "rchunks", since = "1.31.0")]
|
| 132 | pub use core::slice::{RChunks, RChunksExact, RChunksExactMut, RChunksMut};
|
| 133 | #[stable(feature = "slice_rsplit", since = "1.27.0")]
|
| 134 | pub use core::slice::{RSplit, RSplitMut};
|
| 135 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 136 | pub use core::slice::{RSplitN, RSplitNMut, SplitN, SplitNMut};
|
| 137 | #[stable(feature = "split_inclusive", since = "1.51.0")]
|
| 138 | pub use core::slice::{SplitInclusive, SplitInclusiveMut};
|
| 139 |
|
| 140 | |
| 141 | |
| 142 | |
| 143 |
|
| 144 | |
| 145 | |
| 146 | #[cfg(test)]
|
| 147 | pub use hack::into_vec;
|
| 148 |
|
| 149 | |
| 150 | |
| 151 | #[cfg(test)]
|
| 152 | pub use hack::to_vec;
|
| 153 |
|
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | pub(crate) mod hack {
|
| 159 | use core::alloc::Allocator;
|
| 160 |
|
| 161 | use crate::boxed::Box;
|
| 162 | use crate::vec::Vec;
|
| 163 |
|
| 164 | |
| 165 | |
| 166 | |
| 167 | pub fn into_vec<T, A: Allocator>(b: Box<[T], A>) -> Vec<T, A> {
|
| 168 | unsafe {
|
| 169 | let len = b.len();
|
| 170 | let (b, alloc) = Box::into_raw_with_allocator(b);
|
| 171 | Vec::from_raw_parts_in(b as *mut T, len, len, alloc)
|
| 172 | }
|
| 173 | }
|
| 174 |
|
| 175 | #[cfg(not(no_global_oom_handling))]
|
| 176 | #[inline]
|
| 177 | pub fn to_vec<T: ConvertVec, A: Allocator>(s: &[T], alloc: A) -> Vec<T, A> {
|
| 178 | T::to_vec(s, alloc)
|
| 179 | }
|
| 180 |
|
| 181 | #[cfg(not(no_global_oom_handling))]
|
| 182 | pub trait ConvertVec {
|
| 183 | fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A>
|
| 184 | where
|
| 185 | Self: Sized;
|
| 186 | }
|
| 187 |
|
| 188 | #[cfg(not(no_global_oom_handling))]
|
| 189 | impl<T: Clone> ConvertVec for T {
|
| 190 | #[inline]
|
| 191 | default fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A> {
|
| 192 | struct DropGuard<'a, T, A: Allocator> {
|
| 193 | vec: &'a mut Vec<T, A>,
|
| 194 | num_init: usize,
|
| 195 | }
|
| 196 | impl<'a, T, A: Allocator> Drop for DropGuard<'a, T, A> {
|
| 197 | #[inline]
|
| 198 | fn drop(&mut self) {
|
| 199 | |
| 200 | |
| 201 | unsafe {
|
| 202 | self.vec.set_len(self.num_init);
|
| 203 | }
|
| 204 | }
|
| 205 | }
|
| 206 | let mut vec = Vec::with_capacity_in(s.len(), alloc);
|
| 207 | let mut guard = DropGuard { vec: &mut vec, num_init: 0 };
|
| 208 | let slots = guard.vec.spare_capacity_mut();
|
| 209 | |
| 210 | |
| 211 | for (i, b) in s.iter().enumerate().take(slots.len()) {
|
| 212 | guard.num_init = i;
|
| 213 | slots[i].write(b.clone());
|
| 214 | }
|
| 215 | core::mem::forget(guard);
|
| 216 | |
| 217 | |
| 218 | unsafe {
|
| 219 | vec.set_len(s.len());
|
| 220 | }
|
| 221 | vec
|
| 222 | }
|
| 223 | }
|
| 224 |
|
| 225 | #[cfg(not(no_global_oom_handling))]
|
| 226 | impl<T: Copy> ConvertVec for T {
|
| 227 | #[inline]
|
| 228 | fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A> {
|
| 229 | let mut v = Vec::with_capacity_in(s.len(), alloc);
|
| 230 | |
| 231 | |
| 232 | |
| 233 | unsafe {
|
| 234 | s.as_ptr().copy_to_nonoverlapping(v.as_mut_ptr(), s.len());
|
| 235 | v.set_len(s.len());
|
| 236 | }
|
| 237 | v
|
| 238 | }
|
| 239 | }
|
| 240 | }
|
| 241 |
|
| 242 | #[cfg(not(test))]
|
| 243 | impl<T> [T] {
|
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | #[cfg(not(no_global_oom_handling))]
|
| 271 | #[rustc_allow_incoherent_impl]
|
| 272 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 273 | #[inline]
|
| 274 | pub fn sort(&mut self)
|
| 275 | where
|
| 276 | T: Ord,
|
| 277 | {
|
| 278 | merge_sort(self, |a, b| a.lt(b));
|
| 279 | }
|
| 280 |
|
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | #[cfg(not(no_global_oom_handling))]
|
| 327 | #[rustc_allow_incoherent_impl]
|
| 328 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 329 | #[inline]
|
| 330 | pub fn sort_by<F>(&mut self, mut compare: F)
|
| 331 | where
|
| 332 | F: FnMut(&T, &T) -> Ordering,
|
| 333 | {
|
| 334 | merge_sort(self, |a, b| compare(a, b) == Less);
|
| 335 | }
|
| 336 |
|
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | #[cfg(not(no_global_oom_handling))]
|
| 369 | #[rustc_allow_incoherent_impl]
|
| 370 | #[stable(feature = "slice_sort_by_key", since = "1.7.0")]
|
| 371 | #[inline]
|
| 372 | pub fn sort_by_key<K, F>(&mut self, mut f: F)
|
| 373 | where
|
| 374 | F: FnMut(&T) -> K,
|
| 375 | K: Ord,
|
| 376 | {
|
| 377 | merge_sort(self, |a, b| f(a).lt(&f(b)));
|
| 378 | }
|
| 379 |
|
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | #[cfg(not(no_global_oom_handling))]
|
| 416 | #[rustc_allow_incoherent_impl]
|
| 417 | #[stable(feature = "slice_sort_by_cached_key", since = "1.34.0")]
|
| 418 | #[inline]
|
| 419 | pub fn sort_by_cached_key<K, F>(&mut self, f: F)
|
| 420 | where
|
| 421 | F: FnMut(&T) -> K,
|
| 422 | K: Ord,
|
| 423 | {
|
| 424 | |
| 425 | macro_rules! sort_by_key {
|
| 426 | ($t:ty, $slice:ident, $f:ident) => {{
|
| 427 | let mut indices: Vec<_> =
|
| 428 | $slice.iter().map($f).enumerate().map(|(i, k)| (k, i as $t)).collect();
|
| 429 | |
| 430 | |
| 431 | |
| 432 | indices.sort_unstable();
|
| 433 | for i in 0..$slice.len() {
|
| 434 | let mut index = indices[i].1;
|
| 435 | while (index as usize) < i {
|
| 436 | index = indices[index as usize].1;
|
| 437 | }
|
| 438 | indices[i].1 = index;
|
| 439 | $slice.swap(i, index as usize);
|
| 440 | }
|
| 441 | }};
|
| 442 | }
|
| 443 |
|
| 444 | let sz_u8 = mem::size_of::<(K, u8)>();
|
| 445 | let sz_u16 = mem::size_of::<(K, u16)>();
|
| 446 | let sz_u32 = mem::size_of::<(K, u32)>();
|
| 447 | let sz_usize = mem::size_of::<(K, usize)>();
|
| 448 |
|
| 449 | let len = self.len();
|
| 450 | if len < 2 {
|
| 451 | return;
|
| 452 | }
|
| 453 | if sz_u8 < sz_u16 && len <= (u8::MAX as usize) {
|
| 454 | return sort_by_key!(u8, self, f);
|
| 455 | }
|
| 456 | if sz_u16 < sz_u32 && len <= (u16::MAX as usize) {
|
| 457 | return sort_by_key!(u16, self, f);
|
| 458 | }
|
| 459 | if sz_u32 < sz_usize && len <= (u32::MAX as usize) {
|
| 460 | return sort_by_key!(u32, self, f);
|
| 461 | }
|
| 462 | sort_by_key!(usize, self, f)
|
| 463 | }
|
| 464 |
|
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | #[cfg(not(no_global_oom_handling))]
|
| 475 | #[rustc_allow_incoherent_impl]
|
| 476 | #[rustc_conversion_suggestion]
|
| 477 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 478 | #[inline]
|
| 479 | pub fn to_vec(&self) -> Vec<T>
|
| 480 | where
|
| 481 | T: Clone,
|
| 482 | {
|
| 483 | self.to_vec_in(Global)
|
| 484 | }
|
| 485 |
|
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | #[cfg(not(no_global_oom_handling))]
|
| 500 | #[rustc_allow_incoherent_impl]
|
| 501 | #[inline]
|
| 502 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 503 | pub fn to_vec_in<A: Allocator>(&self, alloc: A) -> Vec<T, A>
|
| 504 | where
|
| 505 | T: Clone,
|
| 506 | {
|
| 507 | |
| 508 | hack::to_vec(self, alloc)
|
| 509 | }
|
| 510 |
|
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | #[rustc_allow_incoherent_impl]
|
| 526 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 527 | #[inline]
|
| 528 | pub fn into_vec<A: Allocator>(self: Box<Self, A>) -> Vec<T, A> {
|
| 529 | |
| 530 | hack::into_vec(self)
|
| 531 | }
|
| 532 |
|
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | #[rustc_allow_incoherent_impl]
|
| 554 | #[cfg(not(no_global_oom_handling))]
|
| 555 | #[stable(feature = "repeat_generic_slice", since = "1.40.0")]
|
| 556 | pub fn repeat(&self, n: usize) -> Vec<T>
|
| 557 | where
|
| 558 | T: Copy,
|
| 559 | {
|
| 560 | if n == 0 {
|
| 561 | return Vec::new();
|
| 562 | }
|
| 563 |
|
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 |
|
| 569 | |
| 570 | let capacity = self.len().checked_mul(n).expect("capacity overflow");
|
| 571 | let mut buf = Vec::with_capacity(capacity);
|
| 572 |
|
| 573 | |
| 574 | buf.extend(self);
|
| 575 | {
|
| 576 | let mut m = n >> 1;
|
| 577 | |
| 578 | while m > 0 {
|
| 579 | |
| 580 | unsafe {
|
| 581 | ptr::copy_nonoverlapping(
|
| 582 | buf.as_ptr(),
|
| 583 | (buf.as_mut_ptr() as *mut T).add(buf.len()),
|
| 584 | buf.len(),
|
| 585 | );
|
| 586 | |
| 587 | let buf_len = buf.len();
|
| 588 | buf.set_len(buf_len * 2);
|
| 589 | }
|
| 590 |
|
| 591 | m >>= 1;
|
| 592 | }
|
| 593 | }
|
| 594 |
|
| 595 | |
| 596 | |
| 597 | let rem_len = capacity - buf.len(); |
| 598 | if rem_len > 0 {
|
| 599 | |
| 600 | unsafe {
|
| 601 | |
| 602 | ptr::copy_nonoverlapping(
|
| 603 | buf.as_ptr(),
|
| 604 | (buf.as_mut_ptr() as *mut T).add(buf.len()),
|
| 605 | rem_len,
|
| 606 | );
|
| 607 | |
| 608 | buf.set_len(capacity);
|
| 609 | }
|
| 610 | }
|
| 611 | buf
|
| 612 | }
|
| 613 |
|
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | #[rustc_allow_incoherent_impl]
|
| 623 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 624 | pub fn concat<Item: ?Sized>(&self) -> <Self as Concat<Item>>::Output
|
| 625 | where
|
| 626 | Self: Concat<Item>,
|
| 627 | {
|
| 628 | Concat::concat(self)
|
| 629 | }
|
| 630 |
|
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | #[rustc_allow_incoherent_impl]
|
| 642 | #[stable(feature = "rename_connect_to_join", since = "1.3.0")]
|
| 643 | pub fn join<Separator>(&self, sep: Separator) -> <Self as Join<Separator>>::Output
|
| 644 | where
|
| 645 | Self: Join<Separator>,
|
| 646 | {
|
| 647 | Join::join(self, sep)
|
| 648 | }
|
| 649 |
|
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | #[rustc_allow_incoherent_impl]
|
| 661 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 662 | #[deprecated(since = "1.3.0", note = "renamed to join")]
|
| 663 | pub fn connect<Separator>(&self, sep: Separator) -> <Self as Join<Separator>>::Output
|
| 664 | where
|
| 665 | Self: Join<Separator>,
|
| 666 | {
|
| 667 | Join::join(self, sep)
|
| 668 | }
|
| 669 | }
|
| 670 |
|
| 671 | #[cfg(not(test))]
|
| 672 | impl [u8] {
|
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | #[cfg(not(no_global_oom_handling))]
|
| 683 | #[rustc_allow_incoherent_impl]
|
| 684 | #[must_use = "this returns the uppercase bytes as a new Vec, \
|
| 685 | without modifying the original"]
|
| 686 | #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
| 687 | #[inline]
|
| 688 | pub fn to_ascii_uppercase(&self) -> Vec<u8> {
|
| 689 | let mut me = self.to_vec();
|
| 690 | me.make_ascii_uppercase();
|
| 691 | me
|
| 692 | }
|
| 693 |
|
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | #[cfg(not(no_global_oom_handling))]
|
| 704 | #[rustc_allow_incoherent_impl]
|
| 705 | #[must_use = "this returns the lowercase bytes as a new Vec, \
|
| 706 | without modifying the original"]
|
| 707 | #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
| 708 | #[inline]
|
| 709 | pub fn to_ascii_lowercase(&self) -> Vec<u8> {
|
| 710 | let mut me = self.to_vec();
|
| 711 | me.make_ascii_lowercase();
|
| 712 | me
|
| 713 | }
|
| 714 | }
|
| 715 |
|
| 716 | |
| 717 | |
| 718 | |
| 719 |
|
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | #[unstable(feature = "slice_concat_trait", issue = "27747")]
|
| 750 | pub trait Concat<Item: ?Sized> {
|
| 751 | #[unstable(feature = "slice_concat_trait", issue = "27747")]
|
| 752 | |
| 753 | type Output;
|
| 754 |
|
| 755 | |
| 756 | #[unstable(feature = "slice_concat_trait", issue = "27747")]
|
| 757 | fn concat(slice: &Self) -> Self::Output;
|
| 758 | }
|
| 759 |
|
| 760 | |
| 761 | #[unstable(feature = "slice_concat_trait", issue = "27747")]
|
| 762 | pub trait Join<Separator> {
|
| 763 | #[unstable(feature = "slice_concat_trait", issue = "27747")]
|
| 764 | |
| 765 | type Output;
|
| 766 |
|
| 767 | |
| 768 | #[unstable(feature = "slice_concat_trait", issue = "27747")]
|
| 769 | fn join(slice: &Self, sep: Separator) -> Self::Output;
|
| 770 | }
|
| 771 |
|
| 772 | #[cfg(not(no_global_oom_handling))]
|
| 773 | #[unstable(feature = "slice_concat_ext", issue = "27747")]
|
| 774 | impl<T: Clone, V: Borrow<[T]>> Concat<T> for [V] {
|
| 775 | type Output = Vec<T>;
|
| 776 |
|
| 777 | fn concat(slice: &Self) -> Vec<T> {
|
| 778 | let size = slice.iter().map(|slice| slice.borrow().len()).sum();
|
| 779 | let mut result = Vec::with_capacity(size);
|
| 780 | for v in slice {
|
| 781 | result.extend_from_slice(v.borrow())
|
| 782 | }
|
| 783 | result
|
| 784 | }
|
| 785 | }
|
| 786 |
|
| 787 | #[cfg(not(no_global_oom_handling))]
|
| 788 | #[unstable(feature = "slice_concat_ext", issue = "27747")]
|
| 789 | impl<T: Clone, V: Borrow<[T]>> Join<&T> for [V] {
|
| 790 | type Output = Vec<T>;
|
| 791 |
|
| 792 | fn join(slice: &Self, sep: &T) -> Vec<T> {
|
| 793 | let mut iter = slice.iter();
|
| 794 | let first = match iter.next() {
|
| 795 | Some(first) => first,
|
| 796 | None => return vec![],
|
| 797 | };
|
| 798 | let size = slice.iter().map(|v| v.borrow().len()).sum::<usize>() + slice.len() - 1;
|
| 799 | let mut result = Vec::with_capacity(size);
|
| 800 | result.extend_from_slice(first.borrow());
|
| 801 |
|
| 802 | for v in iter {
|
| 803 | result.push(sep.clone());
|
| 804 | result.extend_from_slice(v.borrow())
|
| 805 | }
|
| 806 | result
|
| 807 | }
|
| 808 | }
|
| 809 |
|
| 810 | #[cfg(not(no_global_oom_handling))]
|
| 811 | #[unstable(feature = "slice_concat_ext", issue = "27747")]
|
| 812 | impl<T: Clone, V: Borrow<[T]>> Join<&[T]> for [V] {
|
| 813 | type Output = Vec<T>;
|
| 814 |
|
| 815 | fn join(slice: &Self, sep: &[T]) -> Vec<T> {
|
| 816 | let mut iter = slice.iter();
|
| 817 | let first = match iter.next() {
|
| 818 | Some(first) => first,
|
| 819 | None => return vec![],
|
| 820 | };
|
| 821 | let size =
|
| 822 | slice.iter().map(|v| v.borrow().len()).sum::<usize>() + sep.len() * (slice.len() - 1);
|
| 823 | let mut result = Vec::with_capacity(size);
|
| 824 | result.extend_from_slice(first.borrow());
|
| 825 |
|
| 826 | for v in iter {
|
| 827 | result.extend_from_slice(sep);
|
| 828 | result.extend_from_slice(v.borrow())
|
| 829 | }
|
| 830 | result
|
| 831 | }
|
| 832 | }
|
| 833 |
|
| 834 | |
| 835 | |
| 836 | |
| 837 |
|
| 838 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 839 | impl<T> Borrow<[T]> for Vec<T> {
|
| 840 | fn borrow(&self) -> &[T] {
|
| 841 | &self[..]
|
| 842 | }
|
| 843 | }
|
| 844 |
|
| 845 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 846 | impl<T> BorrowMut<[T]> for Vec<T> {
|
| 847 | fn borrow_mut(&mut self) -> &mut [T] {
|
| 848 | &mut self[..]
|
| 849 | }
|
| 850 | }
|
| 851 |
|
| 852 | #[cfg(not(no_global_oom_handling))]
|
| 853 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 854 | impl<T: Clone> ToOwned for [T] {
|
| 855 | type Owned = Vec<T>;
|
| 856 | #[cfg(not(test))]
|
| 857 | fn to_owned(&self) -> Vec<T> {
|
| 858 | self.to_vec()
|
| 859 | }
|
| 860 |
|
| 861 | #[cfg(test)]
|
| 862 | fn to_owned(&self) -> Vec<T> {
|
| 863 | hack::to_vec(self, Global)
|
| 864 | }
|
| 865 |
|
| 866 | fn clone_into(&self, target: &mut Vec<T>) {
|
| 867 | |
| 868 | target.truncate(self.len());
|
| 869 |
|
| 870 | |
| 871 | |
| 872 | let (init, tail) = self.split_at(target.len());
|
| 873 |
|
| 874 | |
| 875 | target.clone_from_slice(init);
|
| 876 | target.extend_from_slice(tail);
|
| 877 | }
|
| 878 | }
|
| 879 |
|
| 880 | |
| 881 | |
| 882 | |
| 883 |
|
| 884 | |
| 885 | |
| 886 | |
| 887 | #[cfg(not(no_global_oom_handling))]
|
| 888 | fn insert_head<T, F>(v: &mut [T], is_less: &mut F)
|
| 889 | where
|
| 890 | F: FnMut(&T, &T) -> bool,
|
| 891 | {
|
| 892 | if v.len() >= 2 && is_less(&v[1], &v[0]) {
|
| 893 | unsafe {
|
| 894 | |
| 895 | |
| 896 | |
| 897 | |
| 898 | |
| 899 | |
| 900 | |
| 901 | |
| 902 | |
| 903 | |
| 904 | |
| 905 | |
| 906 | |
| 907 | |
| 908 | |
| 909 | |
| 910 | |
| 911 | let tmp = mem::ManuallyDrop::new(ptr::read(&v[0]));
|
| 912 |
|
| 913 | |
| 914 | |
| 915 | |
| 916 | |
| 917 | |
| 918 | |
| 919 | |
| 920 | |
| 921 | |
| 922 | |
| 923 | let mut hole = InsertionHole { src: &*tmp, dest: &mut v[1] };
|
| 924 | ptr::copy_nonoverlapping(&v[1], &mut v[0], 1);
|
| 925 |
|
| 926 | for i in 2..v.len() {
|
| 927 | if !is_less(&v[i], &*tmp) {
|
| 928 | break;
|
| 929 | }
|
| 930 | ptr::copy_nonoverlapping(&v[i], &mut v[i - 1], 1);
|
| 931 | hole.dest = &mut v[i];
|
| 932 | }
|
| 933 | |
| 934 | }
|
| 935 | }
|
| 936 |
|
| 937 | |
| 938 | struct InsertionHole<T> {
|
| 939 | src: *const T,
|
| 940 | dest: *mut T,
|
| 941 | }
|
| 942 |
|
| 943 | impl<T> Drop for InsertionHole<T> {
|
| 944 | fn drop(&mut self) {
|
| 945 | unsafe {
|
| 946 | ptr::copy_nonoverlapping(self.src, self.dest, 1);
|
| 947 | }
|
| 948 | }
|
| 949 | }
|
| 950 | }
|
| 951 |
|
| 952 | |
| 953 | |
| 954 | |
| 955 | |
| 956 | |
| 957 | |
| 958 | |
| 959 | #[cfg(not(no_global_oom_handling))]
|
| 960 | unsafe fn merge<T, F>(v: &mut [T], mid: usize, buf: *mut T, is_less: &mut F)
|
| 961 | where
|
| 962 | F: FnMut(&T, &T) -> bool,
|
| 963 | {
|
| 964 | let len = v.len();
|
| 965 | let v = v.as_mut_ptr();
|
| 966 | let (v_mid, v_end) = unsafe { (v.add(mid), v.add(len)) };
|
| 967 |
|
| 968 | |
| 969 | |
| 970 | |
| 971 | |
| 972 | |
| 973 | |
| 974 | |
| 975 | |
| 976 | |
| 977 | |
| 978 | |
| 979 | |
| 980 | |
| 981 | |
| 982 | |
| 983 | |
| 984 | |
| 985 | let mut hole;
|
| 986 |
|
| 987 | if mid <= len - mid {
|
| 988 | |
| 989 | unsafe {
|
| 990 | ptr::copy_nonoverlapping(v, buf, mid);
|
| 991 | hole = MergeHole { start: buf, end: buf.add(mid), dest: v };
|
| 992 | }
|
| 993 |
|
| 994 | |
| 995 | let left = &mut hole.start;
|
| 996 | let mut right = v_mid;
|
| 997 | let out = &mut hole.dest;
|
| 998 |
|
| 999 | while *left < hole.end && right < v_end {
|
| 1000 | |
| 1001 | |
| 1002 | unsafe {
|
| 1003 | let to_copy = if is_less(&*right, &**left) {
|
| 1004 | get_and_increment(&mut right)
|
| 1005 | } else {
|
| 1006 | get_and_increment(left)
|
| 1007 | };
|
| 1008 | ptr::copy_nonoverlapping(to_copy, get_and_increment(out), 1);
|
| 1009 | }
|
| 1010 | }
|
| 1011 | } else {
|
| 1012 | |
| 1013 | unsafe {
|
| 1014 | ptr::copy_nonoverlapping(v_mid, buf, len - mid);
|
| 1015 | hole = MergeHole { start: buf, end: buf.add(len - mid), dest: v_mid };
|
| 1016 | }
|
| 1017 |
|
| 1018 | |
| 1019 | let left = &mut hole.dest;
|
| 1020 | let right = &mut hole.end;
|
| 1021 | let mut out = v_end;
|
| 1022 |
|
| 1023 | while v < *left && buf < *right {
|
| 1024 | |
| 1025 | |
| 1026 | unsafe {
|
| 1027 | let to_copy = if is_less(&*right.offset(-1), &*left.offset(-1)) {
|
| 1028 | decrement_and_get(left)
|
| 1029 | } else {
|
| 1030 | decrement_and_get(right)
|
| 1031 | };
|
| 1032 | ptr::copy_nonoverlapping(to_copy, decrement_and_get(&mut out), 1);
|
| 1033 | }
|
| 1034 | }
|
| 1035 | }
|
| 1036 | |
| 1037 | |
| 1038 |
|
| 1039 | unsafe fn get_and_increment<T>(ptr: &mut *mut T) -> *mut T {
|
| 1040 | let old = *ptr;
|
| 1041 | *ptr = unsafe { ptr.offset(1) };
|
| 1042 | old
|
| 1043 | }
|
| 1044 |
|
| 1045 | unsafe fn decrement_and_get<T>(ptr: &mut *mut T) -> *mut T {
|
| 1046 | *ptr = unsafe { ptr.offset(-1) };
|
| 1047 | *ptr
|
| 1048 | }
|
| 1049 |
|
| 1050 | |
| 1051 | struct MergeHole<T> {
|
| 1052 | start: *mut T,
|
| 1053 | end: *mut T,
|
| 1054 | dest: *mut T,
|
| 1055 | }
|
| 1056 |
|
| 1057 | impl<T> Drop for MergeHole<T> {
|
| 1058 | fn drop(&mut self) {
|
| 1059 | |
| 1060 | unsafe {
|
| 1061 | let len = self.end.sub_ptr(self.start);
|
| 1062 | ptr::copy_nonoverlapping(self.start, self.dest, len);
|
| 1063 | }
|
| 1064 | }
|
| 1065 | }
|
| 1066 | }
|
| 1067 |
|
| 1068 | |
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | |
| 1073 | |
| 1074 | |
| 1075 | |
| 1076 | |
| 1077 | |
| 1078 | |
| 1079 | |
| 1080 | #[cfg(not(no_global_oom_handling))]
|
| 1081 | fn merge_sort<T, F>(v: &mut [T], mut is_less: F)
|
| 1082 | where
|
| 1083 | F: FnMut(&T, &T) -> bool,
|
| 1084 | {
|
| 1085 | |
| 1086 | const MAX_INSERTION: usize = 20;
|
| 1087 | |
| 1088 | const MIN_RUN: usize = 10;
|
| 1089 |
|
| 1090 | |
| 1091 | if size_of::<T>() == 0 {
|
| 1092 | return;
|
| 1093 | }
|
| 1094 |
|
| 1095 | let len = v.len();
|
| 1096 |
|
| 1097 | |
| 1098 | if len <= MAX_INSERTION {
|
| 1099 | if len >= 2 {
|
| 1100 | for i in (0..len - 1).rev() {
|
| 1101 | insert_head(&mut v[i..], &mut is_less);
|
| 1102 | }
|
| 1103 | }
|
| 1104 | return;
|
| 1105 | }
|
| 1106 |
|
| 1107 | |
| 1108 | |
| 1109 | |
| 1110 | |
| 1111 | let mut buf = Vec::with_capacity(len / 2);
|
| 1112 |
|
| 1113 | |
| 1114 | |
| 1115 | |
| 1116 | |
| 1117 | let mut runs = vec![];
|
| 1118 | let mut end = len;
|
| 1119 | while end > 0 {
|
| 1120 | |
| 1121 | let mut start = end - 1;
|
| 1122 | if start > 0 {
|
| 1123 | start -= 1;
|
| 1124 | unsafe {
|
| 1125 | if is_less(v.get_unchecked(start + 1), v.get_unchecked(start)) {
|
| 1126 | while start > 0 && is_less(v.get_unchecked(start), v.get_unchecked(start - 1)) {
|
| 1127 | start -= 1;
|
| 1128 | }
|
| 1129 | v[start..end].reverse();
|
| 1130 | } else {
|
| 1131 | while start > 0 && !is_less(v.get_unchecked(start), v.get_unchecked(start - 1))
|
| 1132 | {
|
| 1133 | start -= 1;
|
| 1134 | }
|
| 1135 | }
|
| 1136 | }
|
| 1137 | }
|
| 1138 |
|
| 1139 | |
| 1140 | |
| 1141 | while start > 0 && end - start < MIN_RUN {
|
| 1142 | start -= 1;
|
| 1143 | insert_head(&mut v[start..end], &mut is_less);
|
| 1144 | }
|
| 1145 |
|
| 1146 | |
| 1147 | runs.push(Run { start, len: end - start });
|
| 1148 | end = start;
|
| 1149 |
|
| 1150 | |
| 1151 | while let Some(r) = collapse(&runs) {
|
| 1152 | let left = runs[r + 1];
|
| 1153 | let right = runs[r];
|
| 1154 | unsafe {
|
| 1155 | merge(
|
| 1156 | &mut v[left.start..right.start + right.len],
|
| 1157 | left.len,
|
| 1158 | buf.as_mut_ptr(),
|
| 1159 | &mut is_less,
|
| 1160 | );
|
| 1161 | }
|
| 1162 | runs[r] = Run { start: left.start, len: left.len + right.len };
|
| 1163 | runs.remove(r + 1);
|
| 1164 | }
|
| 1165 | }
|
| 1166 |
|
| 1167 | |
| 1168 | debug_assert!(runs.len() == 1 && runs[0].start == 0 && runs[0].len == len);
|
| 1169 |
|
| 1170 | |
| 1171 | |
| 1172 | |
| 1173 | |
| 1174 | |
| 1175 | |
| 1176 | |
| 1177 | |
| 1178 | |
| 1179 | |
| 1180 | |
| 1181 | |
| 1182 | |
| 1183 | |
| 1184 | #[inline]
|
| 1185 | fn collapse(runs: &[Run]) -> Option<usize> {
|
| 1186 | let n = runs.len();
|
| 1187 | if n >= 2
|
| 1188 | && (runs[n - 1].start == 0
|
| 1189 | || runs[n - 2].len <= runs[n - 1].len
|
| 1190 | || (n >= 3 && runs[n - 3].len <= runs[n - 2].len + runs[n - 1].len)
|
| 1191 | || (n >= 4 && runs[n - 4].len <= runs[n - 3].len + runs[n - 2].len))
|
| 1192 | {
|
| 1193 | if n >= 3 && runs[n - 3].len < runs[n - 1].len { Some(n - 3) } else { Some(n - 2) }
|
| 1194 | } else {
|
| 1195 | None
|
| 1196 | }
|
| 1197 | }
|
| 1198 |
|
| 1199 | #[derive(Clone, Copy)]
|
| 1200 | struct Run {
|
| 1201 | start: usize,
|
| 1202 | len: usize,
|
| 1203 | }
|
| 1204 | }
|