btree_monstousity 0.0.2

a code port of BTreeMap but with comparator functions
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
#![allow(unused_imports)]

#[macro_use]
#[allow(unstable_name_collisions)]
mod definitions {
    #[cfg(not(all(feature = "allocator_api", feature = "new_uninit")))]
    use alloc::alloc::Layout;
    use alloc::boxed::Box;
    use cfg_if::cfg_if;
    use core::{cmp::Ordering, mem::MaybeUninit};

    cfg_if! {
        if #[cfg(feature = "allocator_api")] {
            pub use alloc::alloc::{Allocator, Global};
        } else {
            use core::ptr::NonNull;

            pub trait Allocator {
                #[inline]
                fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, ()> {
                    unsafe {
                        let len = layout.size();
                        let data = if len == 0 {
                            layout.align() as _
                        } else {
                            NonNull::new(alloc::alloc::alloc(layout)).ok_or(())?.as_ptr()
                        };
                        Ok(NonNull::new_unchecked(core::ptr::slice_from_raw_parts_mut(
                            data, len,
                        )))
                    }
                }

                #[inline]
                unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
                    if layout.size() != 0 {
                        unsafe { alloc::alloc::dealloc(ptr.as_ptr(), layout) }
                    }
                }
            }

            #[derive(Copy, Clone, Debug)]
            pub struct Global;

            impl Allocator for Global {}
        }
    }

    cfg_if! {
        if #[cfg(feature = "hasher_prefixfree_extras")] {
            pub use core::hash::Hasher;
        } else {
            pub trait Hasher: core::hash::Hasher {
                #[inline]
                fn write_length_prefix(&mut self, len: usize) {
                    self.write_usize(len);
                }
            }

            impl<H: core::hash::Hasher> Hasher for H {}
        }
    }

    cfg_if! {
        if #[cfg(feature = "core_intrinsics")] {
            pub use core::intrinsics;
        } else {
            pub mod intrinsics {
                pub fn abort() -> ! {
                    cfg_if::cfg_if! {
                        if #[cfg(feature = "std")] {
                            std::process::abort()
                        } else {
                            panic!()
                        }
                    }
                }
            }
        }
    }

    cfg_if! {
        if #[cfg(feature = "allocator_api")] {
            macro_rules! A {
                (Box<$t:ty$(, $a:ty)?>) => { Box<$t$(, $a)?> };
                (Vec<$t:ty$(, $a:ty)?>) => { Vec<$t$(, $a)?> };
            }
        } else {
            macro_rules! A {
                (Box<$t:ty$(, $a:ty)?>) => { Box<$t> };
                (Vec<$t:ty$(, $a:ty)?>) => { Vec<$t> };
            }
        }
    }

    pub trait NewUninit<T, A: Allocator = Global> {
        type SelfWithAlloc<U: ?Sized, B: Allocator>;

        #[cfg(not(feature = "allocator_api"))]
        unsafe fn from_raw_in(ptr: *mut T, alloc: A) -> Self::SelfWithAlloc<T, Global>;

        fn new_uninit_in(alloc: A) -> Self::SelfWithAlloc<MaybeUninit<T>, A>;
        fn new_uninit() -> Self::SelfWithAlloc<MaybeUninit<T>, Global>;
    }

    pub trait AssumeInit<T, A: Allocator = Global>: NewUninit<MaybeUninit<T>, A> {
        unsafe fn assume_init(self) -> Self::SelfWithAlloc<T, A>;
    }

    impl<T, A: Allocator> NewUninit<T, A> for A!(Box<T, A>) {
        type SelfWithAlloc<U: ?Sized, B: Allocator> = A!(Box<U, B>);

        #[cfg(not(feature = "allocator_api"))]
        #[inline]
        unsafe fn from_raw_in(ptr: *mut T, _: A) -> A!(Box<T, Global>) {
            Self::from_raw(ptr)
        }

        #[inline]
        fn new_uninit_in(alloc: A) -> A!(Box<MaybeUninit<T>, A>) {
            cfg_if! {
                if #[cfg(all(feature = "allocator_api", feature = "new_uninit"))] {
                    <A!(Box<_, _>)>::new_uninit_in(alloc)
                } else {
                    unsafe {
                        let layout = Layout::new::<MaybeUninit<T>>();
                        match alloc.allocate(layout) {
                            Ok(ptr) => <A!(Box<_, _>)>::from_raw_in(ptr.cast().as_ptr(), alloc),
                            Err(_) => alloc::alloc::handle_alloc_error(layout),
                        }
                    }
                }
            }
        }

        #[inline]
        fn new_uninit() -> A!(Box<MaybeUninit<T>>) {
            cfg_if! {
                if #[cfg(feature = "new_uninit")] {
                    <A!(Box<_>)>::new_uninit()
                } else {
                    <A!(Box<_>)>::new_uninit_in(Global)
                }
            }
        }
    }

    #[cfg(not(feature = "new_uninit"))]
    cfg_if! {
        if #[cfg(feature = "allocator_api")] {
            impl<T, A: Allocator> AssumeInit<T, A> for A!(Box<MaybeUninit<T>, A>) {
                #[inline]
                unsafe fn assume_init(self) -> A!(Box<T, A>) {
                    let (raw, alloc) = Self::into_raw_with_allocator(self);
                    <A!(Box<_, _>)>::from_raw_in(raw as *mut T, alloc)
                }
            }
        } else {
            impl<T> AssumeInit<T, Global> for A!(Box<MaybeUninit<T>>) {
                #[inline]
                unsafe fn assume_init(self) -> A!(Box<T>) {
                    let raw = Self::into_raw(self);
                    <A!(Box<_>)>::from_raw(raw as *mut T)
                }
            }
        }
    }

    pub trait MaybeUninitSlice<T> {
        unsafe fn slice_assume_init_ref(slice: &[MaybeUninit<T>]) -> &[T];
    }

    #[cfg(not(feature = "maybe_uninit_slice"))]
    impl<T> MaybeUninitSlice<T> for MaybeUninit<T> {
        unsafe fn slice_assume_init_ref(slice: &[MaybeUninit<T>]) -> &[T] {
            &*(slice as *const [MaybeUninit<T>] as *const [T])
        }
    }

    pub trait SliceIndex<T: ?Sized>: core::slice::SliceIndex<T> {
        unsafe fn get_unchecked(self, slice: *const T) -> *const Self::Output;
        unsafe fn get_unchecked_mut(self, slice: *mut T) -> *mut Self::Output;
    }

    impl<T> SliceIndex<[T]> for usize {
        unsafe fn get_unchecked(self, slice: *const [T]) -> *const T {
            slice.as_ptr().add(self)
        }
        unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut T {
            slice.as_mut_ptr().add(self)
        }
    }

    #[allow(clippy::wrong_self_convention)]
    pub trait SlicePtrGet {
        type Item;
        fn as_ptr(self) -> *const Self::Item;
        unsafe fn get_unchecked<I>(self, index: I) -> *const I::Output
        where
            I: SliceIndex<[Self::Item]>;
    }

    #[allow(clippy::wrong_self_convention)]
    pub trait SlicePtrGetMut: SlicePtrGet {
        fn as_mut_ptr(self) -> *mut Self::Item;
        unsafe fn get_unchecked_mut<I>(self, index: I) -> *mut I::Output
        where
            I: SliceIndex<[Self::Item]>;
    }

    #[cfg(not(feature = "slice_ptr_get"))]
    impl<T> SlicePtrGet for *const [T] {
        type Item = T;
        fn as_ptr(self) -> *const T {
            self as *const T
        }
        unsafe fn get_unchecked<I>(self, index: I) -> *const I::Output
        where
            I: SliceIndex<[T]>,
        {
            index.get_unchecked(self)
        }
    }

    #[cfg(not(feature = "slice_ptr_get"))]
    impl<T> SlicePtrGet for *mut [T] {
        type Item = T;
        fn as_ptr(self) -> *const T {
            self as *const T
        }
        unsafe fn get_unchecked<I>(self, index: I) -> *const I::Output
        where
            I: SliceIndex<[T]>,
        {
            index.get_unchecked(self)
        }
    }

    #[cfg(not(feature = "slice_ptr_get"))]
    impl<T> SlicePtrGetMut for *mut [T] {
        fn as_mut_ptr(self) -> *mut T {
            self as *mut T
        }
        unsafe fn get_unchecked_mut<I>(self, index: I) -> *mut I::Output
        where
            I: SliceIndex<[T]>,
        {
            index.get_unchecked_mut(self)
        }
    }

    pub trait ExactSizeIsEmpty: ExactSizeIterator {
        fn is_empty(&self) -> bool {
            self.len() == 0
        }
    }

    #[cfg(not(feature = "exact_size_is_empty"))]
    impl<I: ExactSizeIterator> ExactSizeIsEmpty for I {}

    pub trait SliceIsSorted {
        type Element;

        fn is_sorted(&self) -> bool
        where
            Self::Element: PartialOrd,
        {
            self.is_sorted_by(PartialOrd::partial_cmp)
        }

        fn is_sorted_by<'a, F>(&'a self, compare: F) -> bool
        where
            F: FnMut(&'a Self::Element, &'a Self::Element) -> Option<Ordering>;
    }

    #[allow(clippy::wrong_self_convention)]
    pub trait IterIsSorted: Iterator {
        fn is_sorted(self) -> bool
        where
            Self: Sized,
            Self::Item: PartialOrd,
        {
            IterIsSorted::is_sorted_by(self, PartialOrd::partial_cmp)
        }

        fn is_sorted_by<F>(self, compare: F) -> bool
        where
            Self: Sized,
            F: FnMut(&Self::Item, &Self::Item) -> Option<Ordering>;
    }

    #[cfg(not(feature = "is_sorted"))]
    impl<T> SliceIsSorted for [T] {
        type Element = T;

        fn is_sorted_by<'a, F>(&'a self, mut compare: F) -> bool
        where
            F: FnMut(&'a T, &'a T) -> Option<Ordering>,
        {
            self.iter().is_sorted_by(|a, b| compare(*a, *b))
        }
    }

    #[cfg(not(feature = "is_sorted"))]
    #[allow(clippy::needless_borrow)]
    impl<I: Iterator> IterIsSorted for I {
        fn is_sorted_by<F>(mut self, compare: F) -> bool
        where
            F: FnMut(&I::Item, &I::Item) -> Option<Ordering>,
        {
            #[inline]
            fn check<'a, T>(
                last: &'a mut T,
                mut compare: impl FnMut(&T, &T) -> Option<Ordering> + 'a,
            ) -> impl FnMut(T) -> bool + 'a {
                move |curr| {
                    if let Some(Ordering::Greater) | None = compare(&last, &curr) {
                        return false;
                    }
                    *last = curr;
                    true
                }
            }

            let mut last = match self.next() {
                Some(e) => e,
                None => return true,
            };

            self.all(check(&mut last, compare))
        }
    }

    macro_rules! decorate_if {
        (
            $(#[$attr:meta])*
            if #[cfg($vis_m:meta)] { $(#[$vis_attr:meta])* $vis:vis }
            if #[cfg($const:meta)] { const }
            $($rest:tt)+
        ) => {
            cfg_if::cfg_if! {
                if #[cfg(all($vis_m, $const))] {
                    $(#[$attr])*
                    $(#[$vis_attr])*
                    #[cfg_attr(docsrs, doc(cfg($vis_m)))]
                    $vis const $($rest)+
                } else if #[cfg($vis_m)] {
                    $(#[$attr])*
                    $(#[$vis_attr])*
                    #[cfg_attr(docsrs, doc(cfg($vis_m)))]
                    $vis $($rest)+
                } else if #[cfg($const)] {
                    $(#[$attr])*
                    #[allow(dead_code)]
                    pub(crate) const $($rest)+
                } else {
                    $(#[$attr])*
                    #[allow(dead_code)]
                    pub(crate) $($rest)+
                }
            }
        };

        (
            $(#[$attr:meta])*
            $vis:vis
            if #[cfg($const:meta)] { const }
            $($rest:tt)+
        ) => {
            cfg_if::cfg_if! {
                if #[cfg($const)] {
                    $(#[$attr])*
                    $vis const $($rest)+
                } else {
                    $(#[$attr])*
                    $vis $($rest)+
                }
            }
        };

        (
            $(#[$attr:meta])*
            if #[cfg($vis_m:meta)] { $(#[$vis_attr:meta])* $vis:vis }
            $($rest:tt)+
        ) => {
            cfg_if::cfg_if! {
                if #[cfg($vis_m)] {
                    $(#[$attr])*
                    $(#[$vis_attr])*
                    #[cfg_attr(docsrs, doc(cfg($vis_m)))]
                    $vis $($rest)+
                } else {
                    $(#[$attr])*
                    #[allow(dead_code)]
                    pub(crate) $($rest)+
                }
            }
        };
    }
}

pub(crate) use definitions::{
    intrinsics, Allocator, AssumeInit as _, Global, Hasher as _, MaybeUninitSlice as _,
    NewUninit as _, SlicePtrGet as _, SlicePtrGetMut as _,
};
#[cfg(test)]
pub(crate) use definitions::{ExactSizeIsEmpty as _, SliceIsSorted as _};