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
mod collections;
use core::cmp::Ordering;
use std::sync::{Arc, RwLock};
#[doc(hidden)]
pub trait SortedInsertArcRwLockBasic<T> {
#[doc(hidden)]
fn insert_element(&mut self, index: usize, element: Arc<RwLock<T>>);
}
pub trait SortedInsertArcRwLockBy<T>: SortedInsertArcRwLockBasic<T> {
/// 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.
///
/// ## Safety
///
/// This function will panic if the element is locked.
#[inline]
fn sorted_insert_by<F: FnMut(&Arc<RwLock<T>>, &T) -> bool>(
&mut self,
element: Arc<RwLock<T>>,
mut f: F,
) -> usize {
let element_guard = element.read().unwrap();
let index = self.get_sorted_insert_index_by(|e| f(e, &*element_guard));
drop(element_guard);
self.insert_element(index, element);
index
}
#[doc(hidden)]
fn get_sorted_insert_index_by<F: FnMut(&Arc<RwLock<T>>) -> bool>(&self, f: F) -> usize;
}
pub trait SortedInsertArcRwLockByKey<T>: SortedInsertArcRwLockBy<T> {
/// 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.
///
/// ## Safety
///
/// This function will panic if the element is locked.
#[inline]
fn sorted_insert_asc_by_key<A: Ord, F: FnMut(&T) -> &A>(
&mut self,
element: Arc<RwLock<T>>,
mut f: F,
) -> usize {
self.sorted_insert_by(element.clone(), |e, element_t| {
let e_guard = e.read().unwrap();
f(&*e_guard) <= f(element_t)
})
}
/// 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.
///
/// ## Safety
///
/// This function will panic if the element is locked.
#[inline]
fn sorted_insert_desc_by_key<A: Ord, F: FnMut(&T) -> &A>(
&mut self,
element: Arc<RwLock<T>>,
mut f: F,
) -> usize {
self.sorted_insert_by(element.clone(), |e, element_t| {
let e_guard = e.read().unwrap();
f(&*e_guard) >= f(element_t)
})
}
}
pub trait SortedInsertArcRwLock<T: Ord>: SortedInsertArcRwLockByKey<T> {
/// 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.
///
/// ## Safety
///
/// This function will panic if the element is locked.
#[inline]
fn sorted_insert_asc(&mut self, element: Arc<RwLock<T>>) -> usize {
self.sorted_insert_asc_by_key(element, |element| element)
}
/// 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.
///
/// ## Safety
///
/// This function will panic if the element is locked.
#[inline]
fn sorted_insert_desc(&mut self, element: Arc<RwLock<T>>) -> usize {
self.sorted_insert_desc_by_key(element, |element| element)
}
}
pub trait SortedInsertBinaryArcRwLockBy<T>: SortedInsertArcRwLockBy<T> {
/// 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.
///
/// ## Safety
///
/// This function will panic if the element is locked.
fn sorted_insert_binary_by<F: FnMut(&Arc<RwLock<T>>, &T) -> Ordering>(
&mut self,
element: Arc<RwLock<T>>,
mut f: F,
) -> usize {
let element_guard = element.read().unwrap();
let index = self.get_sorted_insert_index_binary_by(|e| f(e, &*element_guard));
drop(element_guard);
self.insert_element(index, element);
index
}
#[doc(hidden)]
fn get_sorted_insert_index_binary_by<F: FnMut(&Arc<RwLock<T>>) -> Ordering>(
&mut self,
f: F,
) -> usize;
}
pub trait SortedInsertBinaryArcRwLockByKey<T>: SortedInsertBinaryArcRwLockBy<T> {
/// 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.
///
/// ## Safety
///
/// This function will panic if the element is locked.
#[inline]
fn sorted_insert_binary_asc_by_key<A: Ord, F: FnMut(&T) -> &A>(
&mut self,
element: Arc<RwLock<T>>,
mut f: F,
) -> usize {
self.sorted_insert_binary_by(element.clone(), |e, element_t| {
let e_guard = e.read().unwrap();
f(&*e_guard).cmp(f(element_t))
})
}
/// 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.
///
/// ## Safety
///
/// This function will panic if the element is locked.
#[inline]
fn sorted_insert_binary_desc_by_key<A: Ord, F: FnMut(&T) -> &A>(
&mut self,
element: Arc<RwLock<T>>,
mut f: F,
) -> usize {
self.sorted_insert_binary_by(element.clone(), |e, element_t| {
let e_guard = e.read().unwrap();
f(element_t).cmp(f(&*e_guard))
})
}
}
pub trait SortedInsertBinaryArcRwLock<T: Ord>: SortedInsertBinaryArcRwLockByKey<T> {
/// 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.
///
/// ## Safety
///
/// This function will panic if the element is locked.
#[inline]
fn sorted_insert_asc_binary(&mut self, element: Arc<RwLock<T>>) -> usize {
self.sorted_insert_binary_asc_by_key(element, |element| element)
}
/// 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.
///
/// ## Safety
///
/// This function will panic if the element is locked.
#[inline]
fn sorted_insert_desc_binary(&mut self, element: Arc<RwLock<T>>) -> usize {
self.sorted_insert_binary_desc_by_key(element, |element| element)
}
}