sorted_insert/arc_mutex/
mod.rs

1mod collections;
2
3use core::cmp::Ordering;
4use std::sync::{Arc, Mutex};
5
6#[doc(hidden)]
7pub trait SortedInsertArcMutexBasic<T> {
8    #[doc(hidden)]
9    fn insert_element(&mut self, index: usize, element: Arc<Mutex<T>>);
10}
11
12pub trait SortedInsertArcMutexBy<T>: SortedInsertArcMutexBasic<T> {
13    /// Insert elements to this sorted collection by a specific comparator and return the inserted index. Use linear search to find the index where a matching element could be inserted.
14    #[inline]
15    fn sorted_insert_by<F: FnMut(&Arc<Mutex<T>>, &T) -> bool>(
16        &mut self,
17        element: Arc<Mutex<T>>,
18        mut f: F,
19    ) -> usize {
20        let element_guard = element.lock().unwrap();
21
22        let index = self.get_sorted_insert_index_by(|e| f(e, &*element_guard));
23
24        drop(element_guard);
25
26        self.insert_element(index, element);
27
28        index
29    }
30
31    #[doc(hidden)]
32    fn get_sorted_insert_index_by<F: FnMut(&Arc<Mutex<T>>) -> bool>(&self, f: F) -> usize;
33}
34
35pub trait SortedInsertArcMutexByKey<T>: SortedInsertArcMutexBy<T> {
36    /// Insert elements to this sorted collection in ascending order by a specific key and return the inserted index. Use linear search to find the index where a matching element could be inserted.
37    #[inline]
38    fn sorted_insert_asc_by_key<A: Ord, F: FnMut(&T) -> &A>(
39        &mut self,
40        element: Arc<Mutex<T>>,
41        mut f: F,
42    ) -> usize {
43        self.sorted_insert_by(element, |e, element| {
44            let e_guard = e.lock().unwrap();
45
46            f(&*e_guard) <= f(element)
47        })
48    }
49
50    /// Insert elements to this sorted collection in descending order by a specific key and return the inserted index. Use linear search to find the index where a matching element could be inserted.
51    #[inline]
52    fn sorted_insert_desc_by_key<A: Ord, F: FnMut(&T) -> &A>(
53        &mut self,
54        element: Arc<Mutex<T>>,
55        mut f: F,
56    ) -> usize {
57        self.sorted_insert_by(element, |e, element| {
58            let e_guard = e.lock().unwrap();
59
60            f(&*e_guard) >= f(element)
61        })
62    }
63}
64
65pub trait SortedInsertArcMutex<T: Ord>: SortedInsertArcMutexByKey<T> {
66    /// Insert elements to this sorted collection in ascending order and return the inserted index. Use linear search to find the index where a matching element could be inserted.
67    #[inline]
68    fn sorted_insert_asc(&mut self, element: Arc<Mutex<T>>) -> usize {
69        self.sorted_insert_asc_by_key(element, |element| element)
70    }
71
72    /// Insert elements to this sorted collection in descending order and return the inserted index. Use linear search to find the index where a matching element could be inserted.
73    fn sorted_insert_desc(&mut self, element: Arc<Mutex<T>>) -> usize {
74        self.sorted_insert_desc_by_key(element, |element| element)
75    }
76}
77
78pub trait SortedInsertBinaryArcMutexBy<T>: SortedInsertArcMutexBy<T> {
79    /// Insert elements to this sorted collection by a specific comparator and return the inserted index. Use binary search to find the index where a matching element could be inserted.
80    fn sorted_insert_binary_by<F: FnMut(&Arc<Mutex<T>>, &T) -> Ordering>(
81        &mut self,
82        element: Arc<Mutex<T>>,
83        mut f: F,
84    ) -> usize {
85        let element_guard = element.lock().unwrap();
86
87        let index = self.get_sorted_insert_index_binary_by(|e| f(e, &*element_guard));
88
89        drop(element_guard);
90
91        self.insert_element(index, element);
92
93        index
94    }
95
96    #[doc(hidden)]
97    fn get_sorted_insert_index_binary_by<F: FnMut(&Arc<Mutex<T>>) -> Ordering>(
98        &mut self,
99        f: F,
100    ) -> usize;
101}
102
103pub trait SortedInsertBinaryArcMutexByKey<T>: SortedInsertBinaryArcMutexBy<T> {
104    /// Insert elements to this sorted collection in ascending order by a specific key and return the inserted index. Use binary search to find the index where a matching element could be inserted.
105    #[inline]
106    fn sorted_insert_binary_asc_by_key<A: Ord, F: FnMut(&T) -> &A>(
107        &mut self,
108        element: Arc<Mutex<T>>,
109        mut f: F,
110    ) -> usize {
111        self.sorted_insert_binary_by(element, |e, element| {
112            let e_guard = e.lock().unwrap();
113
114            f(&*e_guard).cmp(f(element))
115        })
116    }
117
118    /// Insert elements to this sorted collection in descending order by a specific key and return the inserted index. Use binary search to find the index where a matching element could be inserted.
119    #[inline]
120    fn sorted_insert_binary_desc_by_key<A: Ord, F: FnMut(&T) -> &A>(
121        &mut self,
122        element: Arc<Mutex<T>>,
123        mut f: F,
124    ) -> usize {
125        self.sorted_insert_binary_by(element, |e, element| {
126            let e_guard = e.lock().unwrap();
127
128            f(element).cmp(f(&*e_guard))
129        })
130    }
131}
132
133pub trait SortedInsertBinaryArcMutex<T: Ord>: SortedInsertBinaryArcMutexByKey<T> {
134    /// Insert elements to this sorted collection in ascending order and return the inserted index. Use binary search to find the index where a matching element could be inserted.
135    #[inline]
136    fn sorted_insert_asc_binary(&mut self, element: Arc<Mutex<T>>) -> usize {
137        self.sorted_insert_binary_asc_by_key(element, |element| element)
138    }
139
140    /// Insert elements to this sorted collection in descending order and return the inserted index. Use binary search to find the index where a matching element could be inserted.
141    #[inline]
142    fn sorted_insert_desc_binary(&mut self, element: Arc<Mutex<T>>) -> usize {
143        self.sorted_insert_binary_desc_by_key(element, |element| element)
144    }
145}