sorted_insert/arc_rw_lock/
mod.rs

1mod collections;
2
3use core::cmp::Ordering;
4use std::sync::{Arc, RwLock};
5
6#[doc(hidden)]
7pub trait SortedInsertArcRwLockBasic<T> {
8    #[doc(hidden)]
9    fn insert_element(&mut self, index: usize, element: Arc<RwLock<T>>);
10}
11
12pub trait SortedInsertArcRwLockBy<T>: SortedInsertArcRwLockBasic<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    ///
15    /// ## Safety
16    ///
17    /// This function will panic if the element is locked.
18    #[inline]
19    fn sorted_insert_by<F: FnMut(&Arc<RwLock<T>>, &T) -> bool>(
20        &mut self,
21        element: Arc<RwLock<T>>,
22        mut f: F,
23    ) -> usize {
24        let element_guard = element.read().unwrap();
25
26        let index = self.get_sorted_insert_index_by(|e| f(e, &*element_guard));
27
28        drop(element_guard);
29
30        self.insert_element(index, element);
31
32        index
33    }
34
35    #[doc(hidden)]
36    fn get_sorted_insert_index_by<F: FnMut(&Arc<RwLock<T>>) -> bool>(&self, f: F) -> usize;
37}
38
39pub trait SortedInsertArcRwLockByKey<T>: SortedInsertArcRwLockBy<T> {
40    /// 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.
41    ///
42    /// ## Safety
43    ///
44    /// This function will panic if the element is locked.
45    #[inline]
46    fn sorted_insert_asc_by_key<A: Ord, F: FnMut(&T) -> &A>(
47        &mut self,
48        element: Arc<RwLock<T>>,
49        mut f: F,
50    ) -> usize {
51        self.sorted_insert_by(element.clone(), |e, element_t| {
52            let e_guard = e.read().unwrap();
53
54            f(&*e_guard) <= f(element_t)
55        })
56    }
57
58    /// 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.
59    ///
60    /// ## Safety
61    ///
62    /// This function will panic if the element is locked.
63    #[inline]
64    fn sorted_insert_desc_by_key<A: Ord, F: FnMut(&T) -> &A>(
65        &mut self,
66        element: Arc<RwLock<T>>,
67        mut f: F,
68    ) -> usize {
69        self.sorted_insert_by(element.clone(), |e, element_t| {
70            let e_guard = e.read().unwrap();
71
72            f(&*e_guard) >= f(element_t)
73        })
74    }
75}
76
77pub trait SortedInsertArcRwLock<T: Ord>: SortedInsertArcRwLockByKey<T> {
78    /// 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.
79    ///
80    /// ## Safety
81    ///
82    /// This function will panic if the element is locked.
83    #[inline]
84    fn sorted_insert_asc(&mut self, element: Arc<RwLock<T>>) -> usize {
85        self.sorted_insert_asc_by_key(element, |element| element)
86    }
87
88    /// 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.
89    ///
90    /// ## Safety
91    ///
92    /// This function will panic if the element is locked.
93    #[inline]
94    fn sorted_insert_desc(&mut self, element: Arc<RwLock<T>>) -> usize {
95        self.sorted_insert_desc_by_key(element, |element| element)
96    }
97}
98
99pub trait SortedInsertBinaryArcRwLockBy<T>: SortedInsertArcRwLockBy<T> {
100    /// 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.
101    ///
102    /// ## Safety
103    ///
104    /// This function will panic if the element is locked.
105    fn sorted_insert_binary_by<F: FnMut(&Arc<RwLock<T>>, &T) -> Ordering>(
106        &mut self,
107        element: Arc<RwLock<T>>,
108        mut f: F,
109    ) -> usize {
110        let element_guard = element.read().unwrap();
111
112        let index = self.get_sorted_insert_index_binary_by(|e| f(e, &*element_guard));
113
114        drop(element_guard);
115
116        self.insert_element(index, element);
117
118        index
119    }
120
121    #[doc(hidden)]
122    fn get_sorted_insert_index_binary_by<F: FnMut(&Arc<RwLock<T>>) -> Ordering>(
123        &mut self,
124        f: F,
125    ) -> usize;
126}
127
128pub trait SortedInsertBinaryArcRwLockByKey<T>: SortedInsertBinaryArcRwLockBy<T> {
129    /// 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.
130    ///
131    /// ## Safety
132    ///
133    /// This function will panic if the element is locked.
134    #[inline]
135    fn sorted_insert_binary_asc_by_key<A: Ord, F: FnMut(&T) -> &A>(
136        &mut self,
137        element: Arc<RwLock<T>>,
138        mut f: F,
139    ) -> usize {
140        self.sorted_insert_binary_by(element.clone(), |e, element_t| {
141            let e_guard = e.read().unwrap();
142
143            f(&*e_guard).cmp(f(element_t))
144        })
145    }
146
147    /// 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.
148    ///
149    /// ## Safety
150    ///
151    /// This function will panic if the element is locked.
152    #[inline]
153    fn sorted_insert_binary_desc_by_key<A: Ord, F: FnMut(&T) -> &A>(
154        &mut self,
155        element: Arc<RwLock<T>>,
156        mut f: F,
157    ) -> usize {
158        self.sorted_insert_binary_by(element.clone(), |e, element_t| {
159            let e_guard = e.read().unwrap();
160
161            f(element_t).cmp(f(&*e_guard))
162        })
163    }
164}
165
166pub trait SortedInsertBinaryArcRwLock<T: Ord>: SortedInsertBinaryArcRwLockByKey<T> {
167    /// 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.
168    ///
169    /// ## Safety
170    ///
171    /// This function will panic if the element is locked.
172    #[inline]
173    fn sorted_insert_asc_binary(&mut self, element: Arc<RwLock<T>>) -> usize {
174        self.sorted_insert_binary_asc_by_key(element, |element| element)
175    }
176
177    /// 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.
178    ///
179    /// ## Safety
180    ///
181    /// This function will panic if the element is locked.
182    #[inline]
183    fn sorted_insert_desc_binary(&mut self, element: Arc<RwLock<T>>) -> usize {
184        self.sorted_insert_binary_desc_by_key(element, |element| element)
185    }
186}