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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 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 271 272 273 274 275 276 277 278 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 327 328 329 330 331 332 333 334 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 369 370 371 372 373 374 375 376 377 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 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 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 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
//! This crate provides the [`SegVec`][crate::SegVec] data structure.
//!
//! It is similar to [`Vec`][std::vec::Vec], but allocates memory in chunks, referred to as
//! "segments". This involves a few trade-offs:
//!
//! #### Pros:
//!
//! - Element addresses are stable across [`push`][crate::SegVec::push] operations even if the
//! `SegVec` must grow.
//! - Resizing only allocates the additional space needed, and doesn't
//! require copying.
//!
//! #### Cons:
//!
//! - Operations are slower (some, like [`insert`][crate::SegVec::insert],
//! [`remove`][crate::SegVec::remove], and [`drain`][crate::SegVec::drain], are much slower)
//! than for a `Vec` due to the need for multiple pointer dereferences and conversion between
//! linear indexes and `(segment, offset)` pairs
//! - Direct slicing is unavailable (i.e. no `&[T]` or `&mut [T]`), though `slice` and
//! `slice_mut` are available
//!
//! ## Use Cases
//!
//! 1. You have a long-lived `Vec` whose size fluctuates between very large and very small throughout the life of the program.
//! 2. You have a large append-only `Vec` and would benefit from stable references to the elements
//!
//! ## Features
//!
//! - `small-vec` - Uses [`SmallVec`](https://github.com/servo/rust-smallvec) instead of `Vec` to store the list of segments, allowing the first few segment headers to live on the stack. Can speed up access for small `SegVec` values.
//! - `thin-segments` - Uses [`ThinVec`](https://github.com/Gankra/thin-vec) instead of `Vec` to store the data for each segment, meaning that each segment header takes up the space of a single `usize`, rathern than 3 when using `Vec`.
#![allow(clippy::comparison_chain)]
#[cfg(test)]
mod tests;
mod mem_config;
pub use mem_config::*;
mod slice;
pub use slice::*;
pub mod detail {
#[cfg(feature = "thin-segments")]
pub type Segment<T> = thin_vec::ThinVec<T>;
#[cfg(not(feature = "thin-segments"))]
pub type Segment<T> = Vec<T>;
#[cfg(feature = "small-vec")]
pub type Segments<T> = smallvec::SmallVec<[Segment<T>; 3]>;
#[cfg(not(feature = "small-vec"))]
pub type Segments<T> = Vec<Segment<T>>;
}
use std::cmp;
use std::default::Default;
use std::fmt::Debug;
use std::hash::Hash;
use std::iter::{Flatten, FromIterator, FusedIterator};
use std::mem;
use std::ops::{Bound, Index, IndexMut, RangeBounds};
/// A data structure similar to [`Vec`][std::vec::Vec], but that does not copy on re-size and can
/// release memory when it is truncated.
///
/// Capacity is allocated in "segments". A empty `SegVec` of capacity 0 does not allocate.
/// Allocating new segments is controlled by a [`MemConfig`] policy. Segvec comes with three
/// predefined implementations. These implementations take an non-zero parameter which defines
/// the minimum number of elements in a segment, all segments are multiples of this 'FACTOR'.
/// This `FACTOR` should ideally be a power of two as this optimizes to much more efficient code.
///
/// 1. [`Linear<FACTOR>`]
/// All segments have the same size. This is the fastest when `FACTOR` is big enough. Consequently
/// there is some memory overhead when only very few elements are stored. When a `SegVec` grows it
/// will have the least memory overhead. When not given then `FACTOR` defaults to 1024.
/// 2. [`Proportional<FACTOR>`]
/// Segments grow proportionally to their segment number `[FACTOR, 2*FACTOR, 3*FACTOR, ..]`.
/// Unfortunately the math is somewhat expensive which makes this slow.
/// 3. [`Exponential<FACTOR>`]
/// Segments grow exponentially to their segment number, each subsequent segment is as large as
/// the size of all preceeding segments `[FACTOR, FACTOR, 2*FACTOR, 4*FACTOR, 8*FACTOR, ..]`.
/// `Exponential` is slightly wasteful with memory (up to 50% might be unused in the worst case).
/// When not given then `FACTOR` defaults to 16.
///
/// The default `MemConfig` is `Exponential<1>` which should work for most cases, especially when
/// very few elements are frequently expected.
///
/// Altogether you get these three defaults:
/// * `SegVec<T>`
/// Use it when very few elements (less than 10) are frequently expected.
/// * `SegVec<T, Exponential>`
/// Good compromise when the expected number of elements can vary widely.
/// * `SegVec<T, Linear>`
/// The fastest. But wastes memory when only few elements are expected (<500).
pub struct SegVec<T, C: MemConfig = Exponential<1>> {
len: usize,
segments: detail::Segments<T>,
config: C,
}
impl<T, C: MemConfig> SegVec<T, C> {
/// Create a new [`SegVec`][crate::SegVec] with a length and capacity of 0.
///
/// ```
/// # use segvec::SegVec;
/// let mut v: SegVec<i32> = SegVec::new();
/// assert_eq!(v.capacity(), 0);
/// v.reserve(1);
/// assert_eq!(v.capacity(), 1);
/// ```
pub fn new() -> Self {
C::debug_assert_config();
SegVec {
len: 0,
segments: detail::Segments::new(),
config: C::new(),
}
}
/// Create a new [`SegVec`][crate::SegVec] with a length of 0 and a capacity large enough to
/// hold the given number of elements.
///
/// ```
/// # use segvec::SegVec;
/// let v: SegVec<i32> = SegVec::with_capacity(5);
/// assert!(v.capacity() >= 5);
/// ```
///
/// # Panics
/// - If the required capacity overflows `usize`
pub fn with_capacity(capacity_hint: usize) -> Self {
let mut v = SegVec::new();
v.reserve(capacity_hint);
v
}
/// The number of elements in the [`SegVec`][crate::SegVec]
///
/// ```
/// # use segvec::SegVec;
/// let mut v: SegVec<i32> = SegVec::new();
/// v.push(1);
/// v.push(2);
/// assert_eq!(v.len(), 2);
/// ```
#[inline]
pub fn len(&self) -> usize {
self.len
}
/// Returns `true` if the [`SegVec`][crate::SegVec] contains no elements.
///
/// ```
/// # use segvec::SegVec;
/// let mut v: SegVec<i32> = SegVec::with_capacity(10);
/// assert!(v.is_empty());
/// v.push(1);
/// assert!(!v.is_empty());
/// ```
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// The capacity of the [`SegVec`][crate::SegVec]
///
/// ```
/// # use segvec::SegVec;
/// let mut v: SegVec<i32> = SegVec::with_capacity(3);
/// assert_eq!(v.capacity(), 4);
/// ```
#[inline]
pub fn capacity(&self) -> usize {
self.config.capacity(self.segments.len())
}
/// Reserve enough capacity to insert the given number of elements into the
/// [`SegVec`][crate::SegVec] without allocating. If the capacity is already sufficient,
/// nothing happens.
///
/// ```
/// # use segvec::SegVec;
/// let mut v: SegVec<i32> = SegVec::new();
/// assert_eq!(v.capacity(), 0);
/// v.reserve(3);
/// assert_eq!(v.capacity(), 4);
/// ```
///
/// # Panics
/// - If the required capacity overflows `usize`
#[inline]
pub fn reserve(&mut self, additional: usize) {
let min_cap = match self.len().checked_add(additional) {
Some(c) => c,
None => capacity_overflow(),
};
if min_cap > self.capacity() {
self.reserve_cold(min_cap);
}
}
// do the real reserving in a cold path
#[cold]
fn reserve_cold(&mut self, min_cap: usize) {
let (segment, _) = self.config.segment_and_offset(min_cap - 1);
for i in self.segments.len()..=segment {
let seg_size = self.config.segment_size(i);
self.segments.push(detail::Segment::with_capacity(seg_size));
}
self.config.update_capacity(self.segments.len());
}
/// Returns a reference to the data at the given index in the [`SegVec`][crate::SegVec], if it
/// exists.
///
/// ```
/// # use segvec::SegVec;
/// let mut v: SegVec<i32> = SegVec::new();
/// assert_eq!(v.get(0), None);
/// v.push(1);
/// assert_eq!(*v.get(0).unwrap(), 1);
/// ```
pub fn get(&self, index: usize) -> Option<&T> {
if index < self.len {
let (seg, offset) = self.config.segment_and_offset(index);
// Safety: we just checked `index < self.len`, thus this element must exist.
unsafe { Some(self.segments.get_unchecked(seg).get_unchecked(offset)) }
} else {
None
}
}
/// Returns a mutable reference to the data at the given index in the [`SegVec`][crate::SegVec],
/// if it exists.
///
/// ```
/// # use segvec::SegVec;
/// let mut v: SegVec<i32> = SegVec::new();
/// assert_eq!(v.get_mut(0), None);
/// v.push(1);
/// assert_eq!(*v.get_mut(0).unwrap(), 1);
/// ```
pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
if index < self.len {
let (seg, offset) = self.config.segment_and_offset(index);
// Safety: we just checked `index < self.len`, thus this element must exist.
unsafe {
Some(
self.segments
.get_unchecked_mut(seg)
.get_unchecked_mut(offset),
)
}
} else {
None
}
}
/// Pushes a new value onto the end of the [`SegVec`][crate::SegVec], resizing if necessary.
///
/// ```
/// # use segvec::SegVec;
/// let mut v: SegVec<i32> = SegVec::new();
/// v.push(1);
/// assert_eq!(v[0], 1);
/// ```
///
/// # Panics
/// - If the required capacity overflows `usize`
pub fn push(&mut self, val: T) {
// reserve will panic on overflow
self.reserve(1);
let (seg, _) = self.config.segment_and_offset(self.len);
// Safety: we just reserved space for this element.
unsafe {
self.segments.get_unchecked_mut(seg).push(val);
}
self.len += 1;
}
/// Removes the last value from the [`SegVec`][crate::SegVec] and returns it, or returns `None`
/// if it is empty.
///
/// ```
/// # use segvec::SegVec;
/// let mut v: SegVec<i32> = SegVec::new();
/// v.push(1);
/// assert_eq!(v.pop().unwrap(), 1);
/// ```
pub fn pop(&mut self) -> Option<T> {
match self.len {
0 => None,
size => {
let (seg, offset) = self.config.segment_and_offset(size);
self.len -= 1;
match offset {
// Safety: we checked above that `self.len > 0` thus we can pop the last
// element.
0 => unsafe { self.segments.get_unchecked_mut(seg - 1).pop() },
_ => unsafe { self.segments.get_unchecked_mut(seg).pop() },
}
}
}
}
/// Truncates the [`SegVec`][crate::SegVec] to the given length.
/// If the given length is larger than the current length, this is a no-op.
/// Otherwise, the capacity is reduced and any excess elements are dropped.
///
/// ```
/// # use segvec::SegVec;
/// let mut v: SegVec<i32> = SegVec::new();
/// v.push(1);
/// v.push(2);
/// v.push(3);
/// assert_eq!(v.len(), 3);
/// assert!(v.capacity() >= 3);
/// v.truncate(1);
/// assert_eq!(v.len(), 1);
/// ```
pub fn truncate(&mut self, len: usize) {
if len < self.capacity() {
let (seg, offset) = self.config.segment_and_offset(len);
if offset == 0 {
self.segments.drain(seg..);
} else {
if len < self.len {
// Safety: we just checked `len < self.len`
unsafe { self.segments.get_unchecked_mut(seg).drain(offset..) };
}
self.segments.drain(seg + 1..);
}
self.len = len;
self.config.update_capacity(self.segments.len());
}
}
/// Resizes the [`SegVec`][crate::SegVec] so that the length is equal to `new_len`.
///
/// If `new_len` is greater than `len`, the `SegVec` is extended by the difference, with
/// each additional slot filled with the result of calling the closure `f`. The return
/// values from `f` will end up in the `SegVec` in the order they have been generated. If
/// `new_len` is less than `len`, the `SegVec` is simply truncated. If `new_len` is equal
/// to `len`, this is a no-op.
///
/// ```
/// # use segvec::SegVec;
/// let mut v: SegVec<i32> = SegVec::new();
/// let mut counter = 0i32;
/// v.resize_with(4, || { counter += 1; counter });
/// assert_eq!(counter, 4);
/// assert_eq!(v.len(), 4);
/// assert_eq!(v.pop().unwrap(), 4);
/// ```
pub fn resize_with<F>(&mut self, new_len: usize, f: F)
where
F: FnMut() -> T,
{
let cur_len = self.len();
if new_len > cur_len {
let to_add = new_len - cur_len;
self.extend(std::iter::repeat_with(f).take(to_add));
} else if new_len < cur_len {
self.truncate(new_len);
}
}
/// Resizes the [`SegVec`][crate::SegVec] so that the length is equal to `new_len`.
///
/// If `new_len` is greater than `len`, the `SegVec` is extended by the difference, with each additional slot filled with the result of calling the `clone` on `val`.
/// The cloned values will end up in the `SegVec` in the order they have been generated.
/// If `new_len` is less than `len`, the `SegVec` is simply truncated.
/// If `new_len` is equal to `len`, this is a no-op.
///
/// ```
/// # use segvec::SegVec;
/// let mut v: SegVec<i32> = SegVec::new();
/// v.resize(4, 100);
/// assert_eq!(v.len(), 4);
/// assert_eq!(v.pop().unwrap(), 100);
/// ```
pub fn resize(&mut self, new_len: usize, val: T)
where
T: Clone,
{
let cur_len = self.len();
if new_len > cur_len {
let to_add = new_len - cur_len;
self.extend(std::iter::repeat(val).take(to_add));
} else if new_len < cur_len {
self.truncate(new_len);
}
}
/// Returns an iterator over immutable references to the elements in the
/// [`SegVec`][crate::SegVec].
///
/// ```
/// # use segvec::SegVec;
/// let mut v: SegVec<i32> = SegVec::new();
/// v.push(1);
/// v.push(2);
/// v.push(3);
/// let mut i = v.iter();
/// assert_eq!(*i.next().unwrap(), 1);
/// assert_eq!(*i.next().unwrap(), 2);
/// assert_eq!(*i.next().unwrap(), 3);
/// assert_eq!(i.next(), None);
/// ```
pub fn iter(&self) -> Iter<T> {
Iter {
size: self.len,
iter: self.segments.iter().flatten(),
}
}
/// Insert the given value at the given index in the [`SegVec`][crate::SegVec].
/// This operation requires `O(N)` time due to the fact that the data is segmented -
/// the new element is pushed onto the end and then shifted backwards into position.
///
/// ```
/// # use segvec::SegVec;
/// let mut v: SegVec<i32> = SegVec::new();
/// v.push(1);
/// v.push(2);
/// v.insert(0, 100);
/// assert_eq!(v[0], 100);
/// ```
///
/// # Panics
/// - If the given index is greater than `self.len()`
/// - If the required capacity overflows `usize`
pub fn insert(&mut self, index: usize, val: T) {
if index > self.len {
index_oob("SegVec::insert", index, self.len);
}
if mem::size_of::<T>() == 0 {
self.push(val);
return;
}
self.reserve(1);
let (mut seg_idx, mut seg_offset) = self.config.segment_and_offset(index);
let mut displaced = val;
loop {
let maybe_displaced = unsafe {
// Safety: called index_oob above when out of range
let segment = self.segments.get_unchecked_mut(seg_idx);
let seg_len = segment.len();
let seg_cap = segment.capacity();
if seg_len == 0 {
debug_assert!(
seg_offset == 0,
"expected offset == 0 when inserting into an empty segment"
);
segment.push(displaced);
None
} else if seg_len < seg_cap {
debug_assert!(
seg_offset <= seg_len,
"expected offset <= len when inserting into a partially full segment"
);
let src_ptr = segment.as_mut_ptr().add(seg_offset);
let dst_ptr = src_ptr.add(1);
std::ptr::copy(src_ptr, dst_ptr, seg_len - seg_offset);
std::ptr::write(src_ptr, displaced);
segment.set_len(seg_len + 1);
None
} else {
debug_assert!(
seg_offset < seg_len,
"expected offset < len when inserting into a full segment"
);
// Safety: just asserted the validty.
let new_displaced = std::ptr::read(segment.get_unchecked_mut(seg_len - 1));
let src_ptr = segment.as_mut_ptr().add(seg_offset);
let dst_ptr = src_ptr.add(1);
std::ptr::copy(src_ptr, dst_ptr, seg_len - seg_offset - 1);
std::ptr::write(src_ptr, displaced);
Some(new_displaced)
}
};
match maybe_displaced {
Some(new_displaced) => {
displaced = new_displaced;
seg_idx += 1;
seg_offset = 0;
}
None => break,
}
}
self.len += 1
}
/// Removes the value at the given index in the [`SegVec`][crate::SegVec] and returns it.
/// This operation requires `O(N)` time due to the fact that the data is segmented -
/// the element is shifted to the end and then popped.
///
/// ```
/// # use segvec::SegVec;
/// let mut v: SegVec<i32> = SegVec::new();
/// v.push(1);
/// v.push(2);
/// assert_eq!(v.remove(1), 2);
/// assert_eq!(v.len(), 1);
/// ```
///
/// # Panics
/// - If the given index is greater than or equal to `self.len()`
pub fn remove(&mut self, index: usize) -> T {
if index >= self.len {
index_oob("SegVec::remove", index, self.len);
}
if mem::size_of::<T>() == 0 {
return self.pop().unwrap();
}
let (mut seg_idx, seg_offset) = self.config.segment_and_offset(index);
// SAFETY:
// At this point, it is known that index points to a valid, non-zero-sized T in
// the structure, and so it is safe to read a value of type T from this location
let segment = unsafe { self.segments.get_unchecked(seg_idx) };
let removed = unsafe { std::ptr::read(segment.get_unchecked(seg_offset)) };
let mut orig_len = segment.len();
let mut orig_cap = segment.capacity();
// SAFETY:
// 1. index is known to be strictly less than self.len
// 2. from #1, seg_offset is known to be strictly less than orig_len
// 3. from #2, seg_offset + 1 is known to be less than or equal to orig_len,
// and orig_len - 1 is known to be greater than or equal to seg_offset
// 4. from #1-3, the pseudo-operation copy(segment[seg_offset+1..orig_len], segment[seg_offset..orig_len-1])
// is known to be valid
// 5. all elements from 0 to orig_len are initialized, by definition of orig_len
// 6. from #2 and #5, we know it is safe to set the length of the segment to orig_len-1
unsafe {
// copy segment[seg_offset+1..orig_len] to segment[seg_offset..orig_len-1], then reduce the length of the segment by 1:
// before copy: [_, X, a, b, c] (X is the value read into `removed`)
// after copy: [_, a, b, c, c]
// after resize: [_, a, b, c] (the second c is not dropped, per the implementation of `set_len`)
let segment_mut = self.segments.get_unchecked_mut(seg_idx);
let dst_ptr = segment_mut.get_unchecked_mut(seg_offset) as *mut T;
let src_ptr = dst_ptr.add(1);
std::ptr::copy(src_ptr, dst_ptr, orig_len - seg_offset - 1);
segment_mut.set_len(orig_len - 1);
}
// if the initial segment was full, it may be necessary to shift elements back from subsequent segments to keep continuity
while orig_len == orig_cap {
// advance to the next segment index (which may be out of bounds)
seg_idx += 1;
if seg_idx >= self.segments.len() {
break;
}
// SAFETY: the segment at seg_idx is now known to be in-bounds
let segment = unsafe { self.segments.get_unchecked(seg_idx) };
orig_len = segment.len();
orig_cap = segment.capacity();
if orig_len > 0 {
// SAFETY:
// orig_len is known to be non-zero now, so reading from the 0th index in the segment at seg_idx is safe.
let displaced = unsafe {
std::ptr::read(
self.segments
.get_unchecked_mut(seg_idx)
.get_unchecked_mut(0),
)
};
unsafe {
// seg_idx-1 is known to exist, and to have exactly one empty slot to push into
self.segments.get_unchecked_mut(seg_idx - 1).push(displaced);
// SAFETY:
// 1. orig_len is known to be non-zero now, so the head of the segment is known to be a valid pointer to a T
// 2. from #1, 1 is known to be less than or equal to orig_len,
// and orig_len - 1 is known to be greater than or equal to 0
// 3. from #1+2, the pseudo-operation copy(segment[1..orig_len], segment[0..orig_len-1]) is known to be valid
// 4. all elements from 0 to orig_len are initialized, by definition of orig_len
// 6. from #1 and #4, we know it is safe to set the length of the segment to orig_len-1
let segment_mut = self.segments.get_unchecked_mut(seg_idx);
let dst_ptr = segment_mut.as_mut_ptr();
let src_ptr = dst_ptr.add(1);
std::ptr::copy(src_ptr, dst_ptr, orig_len - 1);
segment_mut.set_len(orig_len - 1);
}
}
}
// total length has decreased by 1, reflect this
self.len -= 1;
removed
}
/// Returns an iterator that removes and returns values from within the given range of the
/// [`SegVec`][crate::SegVec]. See [`Drain`][crate::Drain] for more information.
///
/// ```
/// # use segvec::SegVec;
/// let mut v: SegVec<i32> = SegVec::new();
/// v.push(1);
/// v.push(2);
/// v.drain(..).for_each(|v| println!("{}", v));
/// assert_eq!(v.len(), 0);
/// ```
///
/// # Panics
/// - If the end index is greater than `self.len()`
/// - If the start index is greater than the end index.
pub fn drain<R>(&mut self, range: R) -> Drain<T, C>
where
R: RangeBounds<usize>,
{
let (start, end) = bounds(self.len, "SegVec::drain", range);
Drain {
inner: self,
drained: 0,
index: start,
total: end - start,
}
}
/// Returns a [`Slice`][crate::Slice] over the given range in the [`SegVec`][crate::SegVec].
///
/// ```
/// # use segvec::SegVec;
/// let mut v: SegVec<i32> = SegVec::new();
/// v.push(1);
/// v.push(2);
/// let s = v.slice(1..);
/// assert_eq!(s[0], 2);
/// ```
///
/// # Panics
/// - If the end index is greater than `self.len()`
/// - If the start index is greater than the end index.
pub fn slice<R>(&self, range: R) -> Slice<'_, T>
where
R: RangeBounds<usize>,
{
let (start, end) = bounds(self.len, "SegVec::slice", range);
Slice::new(self, start, end - start)
}
/// Returns a [`SliceMut`][crate::SliceMut] over the given range in the
/// [`SegVec`][crate::SegVec].
///
/// ```
/// # use segvec::SegVec;
/// let mut v: SegVec<i32> = SegVec::new();
/// v.push(1);
/// v.push(2);
/// let mut s = v.slice_mut(..1);
/// s[0] = 100;
/// assert_eq!(v[0], 100);
/// ```
///
/// # Panics
/// - If the end index is greater than `self.len()`
/// - If the start index is greater than the end index.
pub fn slice_mut<R>(&mut self, range: R) -> SliceMut<'_, T>
where
R: RangeBounds<usize>,
{
let (start, end) = bounds(self.len, "SegVec::slice_mut", range);
SliceMut::new(self, start, end - start)
}
/// Reverses the elements in the [`SegVec`][crate::SegVec].
///
/// ```
/// # use segvec::SegVec;
/// let mut v: SegVec<i32> = SegVec::new();
/// v.push(1);
/// v.push(2);
/// v.push(3);
/// v.push(4);
/// v.push(5);
/// v.push(6);
/// v.reverse();
/// assert_eq!(v.into_iter().collect::<Vec<_>>(), vec![6, 5, 4, 3, 2, 1]);
/// ```
pub fn reverse(&mut self) {
if self.len() < 2 {
return;
}
let mut left = 0;
let mut right = self.len() - 1;
while left < right {
self.swap(left, right);
left += 1;
right -= 1;
}
}
/// Sort the [`SegVec`][crate::SegVec] in ascending order (unstable)
pub fn sort_unstable(&mut self)
where
T: Ord,
{
self.sort_unstable_by(Ord::cmp)
}
// TODO: The indexing here can use .get_unchecked() as well, but I did not yet touched
// this code (cehteh)
fn _sort_partition<F>(&mut self, lo: usize, hi: usize, compare: &mut F) -> usize
where
F: FnMut(&T, &T) -> cmp::Ordering,
{
let pivot = lo;
let mut left = lo;
let mut right = hi + 1;
while left < right {
loop {
left += 1;
if left >= right || compare(&self[pivot], &self[left]).is_lt() {
break;
}
}
loop {
right -= 1;
if right <= left || compare(&self[right], &self[pivot]).is_lt() {
break;
}
}
if right > left {
self.swap(right, left);
}
}
let final_pivot_location = if compare(&self[right], &self[pivot]).is_lt() {
right
} else {
right - 1
};
self.swap(final_pivot_location, pivot);
final_pivot_location
}
fn _sort_quicksort<F>(&mut self, lo: usize, hi: usize, compare: &mut F)
where
F: FnMut(&T, &T) -> cmp::Ordering,
{
if hi > lo {
match hi - lo {
1 => {
if compare(&self[hi], &self[lo]).is_lt() {
self.swap(lo, hi);
}
}
_ => {
let mid = self._sort_partition(lo, hi, compare);
if mid > lo {
self._sort_quicksort(lo, mid - 1, compare);
}
if mid < hi {
self._sort_quicksort(mid + 1, hi, compare);
}
}
}
}
}
/// Sort the [`SegVec`][crate::SegVec] in ascending order (unstable) using the given comparison function
pub fn sort_unstable_by<F>(&mut self, mut compare: F)
where
F: FnMut(&T, &T) -> cmp::Ordering,
{
match self.len() {
..=1 => {}
len => self._sort_quicksort(0, len - 1, &mut compare),
}
}
fn swap(&mut self, a: usize, b: usize) {
if a != b && std::mem::size_of::<T>() > 0 {
let a_ptr = &mut self[a] as *mut T;
let b_ptr = &mut self[b] as *mut T;
// SAFETY:
// 1. a != b, so a_ptr and b_ptr cannot alias one another
// 2. If either a or b are invalid as indexes into the structure, a panic
// will occur before we get here. Thus, a_ptr and b_ptr are both derived
// from valid references to some element T in the structure.
unsafe { std::ptr::swap(a_ptr, b_ptr) };
}
}
}
impl<T, C: MemConfig> Default for SegVec<T, C> {
fn default() -> Self {
Self::new()
}
}
impl<T: Clone, C: MemConfig> Clone for SegVec<T, C> {
fn clone(&self) -> Self {
SegVec {
len: self.len,
segments: self.segments.clone(),
config: C::new(),
}
}
}
impl<T, C: MemConfig> Index<usize> for SegVec<T, C> {
type Output = T;
fn index(&self, index: usize) -> &Self::Output {
match self.get(index) {
Some(t) => t,
None => index_oob("SegVec::index", index, self.len),
}
}
}
impl<T, C: MemConfig> IndexMut<usize> for SegVec<T, C> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
let size = self.len;
match self.get_mut(index) {
Some(t) => t,
None => index_oob("SegVec::index_mut", index, size),
}
}
}
impl<T: Debug, C: MemConfig> Debug for SegVec<T, C> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_list().entries(self.iter()).finish()
}
}
impl<T: Hash, C: MemConfig> Hash for SegVec<T, C> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.iter().for_each(|i| i.hash(state));
}
}
impl<T, C: MemConfig> PartialEq for SegVec<T, C>
where
T: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
if self.len() != other.len() {
return false;
}
(0..self.len()).all(|i| self[i] == other[i])
}
}
impl<T, C: MemConfig> Eq for SegVec<T, C> where Self: PartialEq {}
impl<T, C: MemConfig> Extend<T> for SegVec<T, C> {
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
let iter = iter.into_iter();
let (min_size, max_size) = iter.size_hint();
let additional = std::cmp::max(max_size.unwrap_or(0), min_size);
self.reserve(additional);
for i in iter {
self.push(i);
}
}
}
impl<'a, T: Copy + 'a, C: MemConfig> Extend<&'a T> for SegVec<T, C> {
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
<Self as Extend<T>>::extend(self, iter.into_iter().copied())
}
}
impl<T, C: MemConfig> FromIterator<T> for SegVec<T, C> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let mut v = SegVec::new();
v.extend(iter);
v
}
}
impl<'a, T: Clone + 'a, C: MemConfig> FromIterator<&'a T> for SegVec<T, C> {
fn from_iter<I: IntoIterator<Item = &'a T>>(iter: I) -> Self {
let mut v = SegVec::new();
v.extend(iter.into_iter().cloned());
v
}
}
impl<T, C: MemConfig> IntoIterator for SegVec<T, C> {
type IntoIter = IntoIter<T>;
type Item = T;
fn into_iter(self) -> Self::IntoIter {
IntoIter {
size: self.len,
iter: self.segments.into_iter().flatten(),
}
}
}
/// Iterator over immutable references to items in a [`SegVec`][crate::SegVec].
pub struct Iter<'a, T> {
size: usize,
iter: std::iter::Flatten<std::slice::Iter<'a, detail::Segment<T>>>,
}
impl<'a, T: 'a> Iterator for Iter<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
match self.iter.next() {
Some(i) => {
self.size -= 1;
Some(i)
}
None => None,
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.size, Some(self.size))
}
}
impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
fn next_back(&mut self) -> Option<Self::Item> {
match self.iter.next_back() {
Some(i) => {
self.size -= 1;
Some(i)
}
None => None,
}
}
}
impl<'a, T> FusedIterator for Iter<'a, T> {}
impl<'a, T> ExactSizeIterator for Iter<'a, T> {}
/// Consuming iterator over items in a [`SegVec`][crate::SegVec].
pub struct IntoIter<T> {
size: usize,
iter: std::iter::Flatten<<detail::Segments<T> as std::iter::IntoIterator>::IntoIter>,
}
impl<T> Iterator for IntoIter<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
match self.iter.next() {
Some(i) => {
self.size -= 1;
Some(i)
}
None => None,
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.size, Some(self.size))
}
}
impl<T> DoubleEndedIterator for IntoIter<T> {
fn next_back(&mut self) -> Option<Self::Item> {
match self.iter.next_back() {
Some(i) => {
self.size -= 1;
Some(i)
}
None => None,
}
}
}
impl<T> FusedIterator for IntoIter<T> {}
impl<T> ExactSizeIterator for IntoIter<T> {}
/// Removes and returns elements from a range in a [`SegVec`][crate::SegVec].
/// Any un-consumed elements are removed and dropped when a `Drain` is dropped.
/// If a `Drain` is forgotten (via [`std::mem::forget`]), it is unspecified how many elements are
/// removed. The current implementation calls `SegVec::remove` on a single element on each call to
/// `next`.
pub struct Drain<'a, T, C: MemConfig> {
inner: &'a mut SegVec<T, C>,
index: usize,
total: usize,
drained: usize,
}
impl<'a, T, C: MemConfig> Iterator for Drain<'a, T, C> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
if self.drained < self.total {
let next = self.inner.remove(self.index);
self.drained += 1;
Some(next)
} else {
None
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let left = self.total - self.drained;
(left, Some(left))
}
}
impl<'a, T, C: MemConfig> DoubleEndedIterator for Drain<'a, T, C> {
fn next_back(&mut self) -> Option<Self::Item> {
let left = self.total - self.drained;
if left > 0 {
let next = self.inner.remove(self.index + (left - 1));
self.drained += 1;
Some(next)
} else {
None
}
}
}
impl<'a, T, C: MemConfig> FusedIterator for Drain<'a, T, C> {}
impl<'a, T, C: MemConfig> ExactSizeIterator for Drain<'a, T, C> {}
impl<'a, T, C: MemConfig> Drop for Drain<'a, T, C> {
fn drop(&mut self) {
self.for_each(drop);
}
}
#[cold]
fn capacity_overflow() -> ! {
panic!("SegVec: capacity overflow")
}
#[cold]
fn index_oob(caller: &str, idx: usize, len: usize) -> ! {
panic!(
"{}: index out of bounds: index is {}, len is {}",
caller, idx, len
)
}
fn bounds<R>(len: usize, caller: &str, range: R) -> (usize, usize)
where
R: RangeBounds<usize>,
{
let start = range.start_bound();
let start = match start {
Bound::Included(&start) => start,
Bound::Excluded(start) => start.checked_add(1).expect("start bound fits into usize"),
Bound::Unbounded => 0,
};
let end = range.end_bound();
let end = match end {
Bound::Included(end) => end.checked_add(1).expect("end bound fits into usize"),
Bound::Excluded(&end) => end,
Bound::Unbounded => len,
};
if start > end {
panic!("{}: lower bound {} > upper bound {}", caller, start, end);
}
if end > len {
index_oob(caller, end, len);
}
(start, end)
}