segmented-vec 0.2.3

A vector with stable element addresses using segmented allocation and O(1) index-to-segment mapping
Documentation
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
//! Sorting algorithms for SegmentedVec.
//!
//! This module contains implementations of sorting algorithms adapted from the
//! Rust standard library to work with non-contiguous memory.

use std::mem::MaybeUninit;
use std::ptr;

/// Threshold for switching to insertion sort.
const INSERTION_SORT_THRESHOLD: usize = 20;

/// Sorts `v[start..end]` using insertion sort.
///
/// Adapted from the Rust standard library's `insertion_sort_shift_left`.
#[inline]
pub fn insertion_sort<T, F>(
    v: &mut impl IndexedAccess<T>,
    start: usize,
    end: usize,
    is_less: &mut F,
) where
    F: FnMut(&T, &T) -> bool,
{
    for i in (start + 1)..end {
        // Insert v[i] into the sorted sequence v[start..i].
        let mut j = i;
        while j > start && is_less(v.get_ref(j), v.get_ref(j - 1)) {
            v.swap(j, j - 1);
            j -= 1;
        }
    }
}

/// Sorts using heapsort. Guarantees O(n log n) worst-case.
///
/// Adapted from the Rust standard library's heapsort.
#[inline(never)]
pub fn heapsort<T, F>(v: &mut impl IndexedAccess<T>, start: usize, end: usize, is_less: &mut F)
where
    F: FnMut(&T, &T) -> bool,
{
    let len = end - start;
    if len < 2 {
        return;
    }

    // Build the heap in-place.
    for i in (0..len / 2).rev() {
        sift_down(v, start, i, len, is_less);
    }

    // Pop elements from the heap one by one.
    for i in (1..len).rev() {
        v.swap(start, start + i);
        sift_down(v, start, 0, i, is_less);
    }
}

/// Sift down element at `node` in heap rooted at `start` with size `heap_size`.
#[inline]
fn sift_down<T, F>(
    v: &mut impl IndexedAccess<T>,
    start: usize,
    mut node: usize,
    heap_size: usize,
    is_less: &mut F,
) where
    F: FnMut(&T, &T) -> bool,
{
    loop {
        let mut child = 2 * node + 1;
        if child >= heap_size {
            break;
        }

        // Choose the greater child.
        if child + 1 < heap_size && is_less(v.get_ref(start + child), v.get_ref(start + child + 1))
        {
            child += 1;
        }

        // Stop if the invariant holds.
        if !is_less(v.get_ref(start + node), v.get_ref(start + child)) {
            break;
        }

        v.swap(start + node, start + child);
        node = child;
    }
}

/// Sorts using quicksort with heapsort fallback.
///
/// Adapted from the Rust standard library's quicksort implementation.
pub fn quicksort<T, F>(v: &mut impl IndexedAccess<T>, start: usize, end: usize, is_less: &mut F)
where
    F: FnMut(&T, &T) -> bool,
{
    let len = end - start;
    if len < 2 {
        return;
    }

    // Use insertion sort for small arrays.
    if len <= INSERTION_SORT_THRESHOLD {
        insertion_sort(v, start, end, is_less);
        return;
    }

    // Limit recursion depth to 2 * log2(len) to guarantee O(n log n) worst-case.
    let limit = 2 * (usize::BITS - len.leading_zeros());
    quicksort_recursive(v, start, end, is_less, limit);
}

/// Recursive quicksort with recursion limit.
fn quicksort_recursive<T, F>(
    v: &mut impl IndexedAccess<T>,
    start: usize,
    end: usize,
    is_less: &mut F,
    mut limit: u32,
) where
    F: FnMut(&T, &T) -> bool,
{
    let mut start = start;
    let mut end = end;

    loop {
        let len = end - start;

        if len <= INSERTION_SORT_THRESHOLD {
            insertion_sort(v, start, end, is_less);
            return;
        }

        // If we've hit the recursion limit, fall back to heapsort.
        if limit == 0 {
            heapsort(v, start, end, is_less);
            return;
        }
        limit -= 1;

        // Choose pivot using median-of-three.
        let mid = start + len / 2;
        let pivot_idx = choose_pivot(v, start, mid, end - 1, is_less);

        // Move pivot to the start.
        v.swap(start, pivot_idx);

        // Partition around the pivot.
        let pivot_final = partition(v, start, end, is_less);

        // Recurse on the smaller partition first to limit stack depth.
        let left_len = pivot_final - start;
        let right_len = end - pivot_final - 1;

        if left_len < right_len {
            quicksort_recursive(v, start, pivot_final, is_less, limit);
            start = pivot_final + 1;
        } else {
            quicksort_recursive(v, pivot_final + 1, end, is_less, limit);
            end = pivot_final;
        }
    }
}

/// Choose pivot using median-of-three.
#[inline]
fn choose_pivot<T, F>(
    v: &impl IndexedAccess<T>,
    a: usize,
    b: usize,
    c: usize,
    is_less: &mut F,
) -> usize
where
    F: FnMut(&T, &T) -> bool,
{
    // Return the index of the median of the three elements.
    if is_less(v.get_ref(a), v.get_ref(b)) {
        if is_less(v.get_ref(b), v.get_ref(c)) {
            b
        } else if is_less(v.get_ref(a), v.get_ref(c)) {
            c
        } else {
            a
        }
    } else if is_less(v.get_ref(a), v.get_ref(c)) {
        a
    } else if is_less(v.get_ref(b), v.get_ref(c)) {
        c
    } else {
        b
    }
}

/// Hoare partition scheme.
///
/// Partitions `v[start..end]` around the pivot at `v[start]`.
/// Returns the final position of the pivot.
fn partition<T, F>(
    v: &mut impl IndexedAccess<T>,
    start: usize,
    end: usize,
    is_less: &mut F,
) -> usize
where
    F: FnMut(&T, &T) -> bool,
{
    let mut left = start + 1;
    let mut right = end - 1;

    loop {
        // Move left pointer right while elements are less than pivot.
        while left <= right && is_less(v.get_ref(left), v.get_ref(start)) {
            left += 1;
        }

        // Move right pointer left while elements are greater than or equal to pivot.
        while left <= right && !is_less(v.get_ref(right), v.get_ref(start)) {
            right -= 1;
        }

        if left > right {
            break;
        }

        v.swap(left, right);
        left += 1;
        right -= 1;
    }

    // Move pivot to its final position.
    v.swap(start, right);
    right
}

/// Stable merge sort implementation.
///
/// Adapted from the Rust standard library's merge sort.
pub fn merge_sort<T, F>(v: &mut impl IndexedAccess<T>, start: usize, end: usize, is_less: &mut F)
where
    F: FnMut(&T, &T) -> bool,
{
    let len = end - start;
    if len < 2 {
        return;
    }

    // Use insertion sort for small arrays.
    if len <= INSERTION_SORT_THRESHOLD {
        insertion_sort(v, start, end, is_less);
        return;
    }

    // Allocate scratch space for merging.
    let scratch_len = len / 2 + 1;
    let mut scratch: Vec<MaybeUninit<T>> = Vec::with_capacity(scratch_len);
    // SAFETY: We're only using this as uninitialized storage.
    unsafe {
        scratch.set_len(scratch_len);
    }

    merge_sort_with_scratch(v, start, end, &mut scratch, is_less);
}

/// Merge sort with provided scratch space.
fn merge_sort_with_scratch<T, F>(
    v: &mut impl IndexedAccess<T>,
    start: usize,
    end: usize,
    scratch: &mut [MaybeUninit<T>],
    is_less: &mut F,
) where
    F: FnMut(&T, &T) -> bool,
{
    let len = end - start;
    if len < 2 {
        return;
    }

    if len <= INSERTION_SORT_THRESHOLD {
        insertion_sort(v, start, end, is_less);
        return;
    }

    let mid = start + len / 2;

    // Recursively sort both halves.
    merge_sort_with_scratch(v, start, mid, scratch, is_less);
    merge_sort_with_scratch(v, mid, end, scratch, is_less);

    // If already sorted, we're done.
    if !is_less(v.get_ref(mid), v.get_ref(mid - 1)) {
        return;
    }

    // Merge the two sorted halves.
    merge(v, start, mid, end, scratch, is_less);
}

/// Merges two sorted runs: `v[start..mid]` and `v[mid..end]`.
#[allow(clippy::needless_range_loop)]
fn merge<T, F>(
    v: &mut impl IndexedAccess<T>,
    start: usize,
    mid: usize,
    end: usize,
    scratch: &mut [MaybeUninit<T>],
    is_less: &mut F,
) where
    F: FnMut(&T, &T) -> bool,
{
    let left_len = mid - start;
    let right_len = end - mid;

    // Copy the shorter half to scratch.
    if left_len <= right_len {
        // Copy left half to scratch.
        for i in 0..left_len {
            // SAFETY: We're moving the value to scratch.
            unsafe {
                let val = ptr::read(v.get_ptr(start + i));
                scratch[i].write(val);
            }
        }

        // Merge back.
        let mut s = 0; // scratch index
        let mut r = mid; // right index
        let mut w = start; // write index

        while s < left_len && r < end {
            // SAFETY: scratch[s] is initialized, v.get_ptr(r) is valid.
            let take_left = unsafe { !is_less(v.get_ref(r), scratch[s].assume_init_ref()) };

            if take_left {
                // SAFETY: Moving from scratch.
                unsafe {
                    ptr::write(v.get_ptr_mut(w), scratch[s].assume_init_read());
                }
                s += 1;
            } else {
                // SAFETY: Moving within v.
                unsafe {
                    let val = ptr::read(v.get_ptr(r));
                    ptr::write(v.get_ptr_mut(w), val);
                }
                r += 1;
            }
            w += 1;
        }

        // Copy remaining elements from scratch.
        while s < left_len {
            unsafe {
                ptr::write(v.get_ptr_mut(w), scratch[s].assume_init_read());
            }
            s += 1;
            w += 1;
        }
        // Remaining elements from right are already in place.
    } else {
        // Copy right half to scratch.
        for i in 0..right_len {
            unsafe {
                let val = ptr::read(v.get_ptr(mid + i));
                scratch[i].write(val);
            }
        }

        // Merge back from the end.
        let mut s = right_len; // scratch index (exclusive, counting down)
        let mut l = mid; // left index (exclusive, counting down)
        let mut w = end; // write index (exclusive, counting down)

        while s > 0 && l > start {
            // SAFETY: scratch[s-1] is initialized, v.get_ptr(l-1) is valid.
            let take_right = unsafe { is_less(scratch[s - 1].assume_init_ref(), v.get_ref(l - 1)) };

            w -= 1;
            if take_right {
                // SAFETY: Moving from scratch.
                s -= 1;
                unsafe {
                    ptr::write(v.get_ptr_mut(w), scratch[s].assume_init_read());
                }
            } else {
                // SAFETY: Moving within v.
                l -= 1;
                unsafe {
                    let val = ptr::read(v.get_ptr(l));
                    ptr::write(v.get_ptr_mut(w), val);
                }
            }
        }

        // Copy remaining elements from scratch.
        while s > 0 {
            s -= 1;
            w -= 1;
            unsafe {
                ptr::write(v.get_ptr_mut(w), scratch[s].assume_init_read());
            }
        }
        // Remaining elements from left are already in place.
    }
}

/// Trait for indexed access to a collection.
///
/// This abstraction allows the sorting algorithms to work with
/// non-contiguous memory layouts like SegmentedVec.
pub trait IndexedAccess<T> {
    /// Get a reference to the element at index.
    fn get_ref(&self, index: usize) -> &T;

    /// Get a raw pointer to the element at index.
    fn get_ptr(&self, index: usize) -> *const T;

    /// Get a mutable raw pointer to the element at index.
    fn get_ptr_mut(&mut self, index: usize) -> *mut T;

    /// Swap elements at two indices.
    fn swap(&mut self, a: usize, b: usize);
}