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
use core::cmp::Ordering;

use alloc::vec::Vec;

use crate::{
    SortedInsert, SortedInsertBasic, SortedInsertBinary, SortedInsertBinaryBy,
    SortedInsertBinaryByKey, SortedInsertBy, SortedInsertByKey,
};

impl<T> SortedInsertBasic<T> for Vec<T> {
    #[inline]
    fn insert_element(&mut self, index: usize, element: T) {
        self.insert(index, element);
    }
}

impl<T> SortedInsertBy<T> for Vec<T> {
    #[inline]
    fn get_sorted_insert_index_by<F: FnMut(&T) -> bool>(&self, f: F) -> usize {
        match self.iter().rposition(f) {
            Some(i) => i + 1,
            None => 0,
        }
    }
}

impl<T> SortedInsertByKey<T> for Vec<T> {}

impl<T: Ord> SortedInsert<T> for Vec<T> {}

impl<T> SortedInsertBinaryBy<T> for Vec<T> {
    #[inline]
    fn get_sorted_insert_index_binary_by<F: FnMut(&T) -> Ordering>(&mut self, f: F) -> usize {
        match self.binary_search_by(f) {
            Ok(i) => i + 1,
            Err(i) => i,
        }
    }
}

impl<T> SortedInsertBinaryByKey<T> for Vec<T> {}

impl<T: Ord> SortedInsertBinary<T> for Vec<T> {}