mycelium_experimental/map.rs
1use std::fmt;
2use std::mem::ManuallyDrop;
3use std::ptr;
4
5use crate::base::{self};
6use epoch;
7
8/// A map based on a lock-free skip list.
9pub struct SkipMap {
10 inner: base::SkipList,
11}
12
13impl SkipMap {
14 /// Returns a new, empty map.
15 pub fn new() -> SkipMap {
16 SkipMap {
17 inner: base::SkipList::new(epoch::default_collector().clone()),
18 }
19 }
20
21 /// Returns `true` if the map is empty.
22 pub fn is_empty(&self) -> bool {
23 self.inner.is_empty()
24 }
25
26 /// Returns the number of entries in the map.
27 ///
28 /// If the map is being concurrently modified, consider the returned number just an
29 /// approximation without any guarantees.
30 pub fn len(&self) -> usize {
31 self.inner.len()
32 }
33}
34
35/*
36impl SkipMap
37{
38 /// Returns the entry with the smallest key.
39 pub fn front(&self) -> Option<Entry> {
40 let guard = &epoch::pin();
41 try_pin_loop(move || self.inner.front(guard)).map(Entry::new)
42 }
43
44 /// Returns the entry with the largest key.
45 pub fn back(&self) -> Option<Entry> {
46 let guard = &epoch::pin();
47 try_pin_loop(move || self.inner.back(guard)).map(Entry::new)
48 }
49
50 /// Returns `true` if the map contains a value for the specified key.
51 pub fn contains_key<Q>(&self, key: [u8; 16]) -> bool
52 where
53 Q: Ord + ?Sized,
54 {
55 let guard = &epoch::pin();
56 self.inner.contains_key(key, guard)
57 }
58
59 /// Returns an entry with the specified `key`.
60 pub fn get<Q>(&self, key: [u8;16]) -> Option<Entry>
61 where
62 Q: Ord + ?Sized,
63 {
64 let guard = &epoch::pin();
65 try_pin_loop(|| self.inner.get(key, guard)).map(Entry::new)
66 }
67
68 /// Returns an `Entry` pointing to the lowest element whose key is above
69 /// the given bound. If no such element is found then `None` is
70 /// returned.
71 pub fn lower_bound<'a>(&'a self, bound: Bound<[u8;16]>) -> Option<Entry>
72 {
73 let guard = &epoch::pin();
74 try_pin_loop(|| self.inner.lower_bound(bound, guard)).map(Entry::new)
75 }
76
77 /// Returns an `Entry` pointing to the highest element whose key is below
78 /// the given bound. If no such element is found then `None` is
79 /// returned.
80 pub fn upper_bound<'a>(&'a self, bound: Bound<[u8;16]>) -> Option<Entry>
81 {
82 let guard = &epoch::pin();
83 try_pin_loop(|| self.inner.upper_bound(bound, guard)).map(Entry::new)
84 }
85
86 /// Finds an entry with the specified key, or inserts a new `key`-`value` pair if none exist.
87 pub fn get_or_insert(&self, key: [u8;16], value: Vec<u8>) -> Entry {
88 let guard = &epoch::pin();
89 Entry::new(self.inner.get_or_insert(key, value, guard))
90 }
91
92 /// Returns an iterator over all entries in the map.
93 pub fn iter(&self) -> Iter {
94 Iter {
95 inner: self.inner.ref_iter(),
96 }
97 }
98
99 /*
100 /// Returns an iterator over a subset of entries in the skip list.
101 pub fn range(
102 &self,
103 range: ([u8;16], [u8;16]),
104 ) -> Range
105 {
106 Range {
107 inner: self.inner.ref_range(range),
108 }
109 }
110 */
111}
112*/
113
114/*
115impl SkipMap
116{
117 /// Inserts a `key`-`value` pair into the map and returns the new entry.
118 ///
119 /// If there is an existing entry with this key, it will be removed before inserting the new
120 /// one.
121 pub fn insert(&self, key: [u8;16], value: Vec<u8>) -> Entry {
122 let guard = &epoch::pin();
123 Entry::new(self.inner.insert(key, value, guard))
124 }
125
126 /// Removes an entry with the specified `key` from the map and returns it.
127 pub fn remove(&self, key: [u8;16]) -> Option<Entry>
128 {
129 let guard = &epoch::pin();
130 self.inner.remove(key, guard).map(Entry::new)
131 }
132
133 /// Removes an entry from the front of the map.
134 pub fn pop_front(&self) -> Option<Entry> {
135 let guard = &epoch::pin();
136 self.inner.pop_front(guard).map(Entry::new)
137 }
138
139 /// Removes an entry from the back of the map.
140 pub fn pop_back(&self) -> Option<Entry> {
141 let guard = &epoch::pin();
142 self.inner.pop_back(guard).map(Entry::new)
143 }
144
145 /// Iterates over the map and removes every entry.
146 pub fn clear(&self) {
147 let guard = &mut epoch::pin();
148 self.inner.clear(guard);
149 }
150}
151
152impl Default for SkipMap {
153 fn default() -> SkipMap {
154 SkipMap::new()
155 }
156}
157
158impl fmt::Debug for SkipMap
159{
160 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
161 f.pad("SkipMap { .. }")
162 }
163}
164*/
165
166impl IntoIterator for SkipMap {
167 type Item = ([u8;16], Vec<u8>);
168 type IntoIter = IntoIter;
169
170 fn into_iter(self) -> IntoIter {
171 IntoIter {
172 inner: self.inner.into_iter(),
173 }
174 }
175}
176
177/*
178impl<'a> IntoIterator for &'a SkipMap
179{
180 type Item = Entry<'a>;
181 type IntoIter = Iter<'a>;
182
183 fn into_iter(self) -> Iter<'a> {
184 self.iter()
185 }
186}
187*/
188
189/*
190impl FromIterator<([u8;16], Vec<u8>)> for SkipMap
191{
192 fn from_iter<I>(iter: I) -> SkipMap
193 where
194 I: IntoIterator<Item = ([u8;16], Vec<u8>)>,
195 {
196 let s = SkipMap::new();
197 for (k, v) in iter {
198 s.get_or_insert(k, v);
199 }
200 s
201 }
202}
203*/
204
205/// A reference-counted entry in a map.
206pub struct Entry<'a> {
207 inner: ManuallyDrop<base::RefEntry<'a>>,
208}
209
210impl<'a> Entry<'a> {
211 fn new(inner: base::RefEntry) -> Entry {
212 Entry { inner: ManuallyDrop::new(inner) }
213 }
214
215 /// Returns a reference to the key.
216 pub fn key(&self) -> [u8;16] {
217 self.inner.key()
218 }
219
220 /// Returns a reference to the value.
221 pub fn value(&self) -> &Vec<u8> {
222 self.inner.value()
223 }
224
225 /// Returns `true` if the entry is removed from the map.
226 pub fn is_removed(&self) -> bool {
227 self.inner.is_removed()
228 }
229}
230
231impl<'a> Drop for Entry<'a>
232{
233 fn drop(&mut self) {
234 unsafe {
235 ManuallyDrop::into_inner(ptr::read(&mut self.inner))
236 .release_with_pin(|| epoch::pin());
237 }
238 }
239}
240
241impl<'a> Entry<'a>
242{
243 /// Moves to the next entry in the map.
244 pub fn move_next(&mut self) -> bool {
245 let guard = &epoch::pin();
246 self.inner.move_next(guard)
247 }
248
249 /// Moves to the previous entry in the map.
250 pub fn move_prev(&mut self) -> bool {
251 let guard = &epoch::pin();
252 self.inner.move_prev(guard)
253 }
254
255 /// Returns the next entry in the map.
256 pub fn next(&self) -> Option<Entry> {
257 let guard = &epoch::pin();
258 self.inner.next(guard).map(Entry::new)
259 }
260
261 /// Returns the previous entry in the map.
262 pub fn prev(&self) -> Option<Entry> {
263 let guard = &epoch::pin();
264 self.inner.prev(guard).map(Entry::new)
265 }
266}
267
268impl<'a> Entry<'a>
269{
270 /// Removes the entry from the map.
271 ///
272 /// Returns `true` if this call removed the entry and `false` if it was already removed.
273 pub fn remove(&self) -> bool {
274 let guard = &epoch::pin();
275 self.inner.remove(guard)
276 }
277}
278
279impl<'a> Clone for Entry<'a> {
280 fn clone(&self) -> Entry<'a> {
281 Entry {
282 inner: self.inner.clone(),
283 }
284 }
285}
286
287impl<'a> fmt::Debug for Entry<'a>
288{
289 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
290 f.debug_tuple("Entry")
291 .field(&self.key())
292 .field(self.value())
293 .finish()
294 }
295}
296
297/// An owning iterator over the entries of a `SkipMap`.
298pub struct IntoIter {
299 inner: base::IntoIter,
300}
301
302impl Iterator for IntoIter {
303 type Item = ([u8;16], Vec<u8>);
304
305 fn next(&mut self) -> Option<([u8;16], Vec<u8>)> {
306 self.inner.next()
307 }
308}
309
310impl fmt::Debug for IntoIter {
311 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
312 f.pad("IntoIter { .. }")
313 }
314}
315
316/// An iterator over the entries of a `SkipMap`.
317#[allow(dead_code)]
318pub struct Iter<'a> {
319 inner: base::RefIter<'a>,
320}
321/*
322impl<'a> Iterator for Iter<'a>
323{
324 type Item = Entry<'a>;
325
326 fn next(&mut self) -> Option<Entry<'a>> {
327 let guard = &epoch::pin();
328 self.inner.next(guard).map(Entry::new)
329 }
330}
331
332impl<'a> DoubleEndedIterator for Iter<'a>
333{
334 fn next_back(&mut self) -> Option<Entry<'a>> {
335 let guard = &epoch::pin();
336 self.inner.next_back(guard).map(Entry::new)
337 }
338}
339*/
340
341impl<'a> fmt::Debug for Iter<'a> {
342 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
343 f.pad("Iter { .. }")
344 }
345}
346
347/*
348/// An iterator over the entries of a `SkipMap`.
349pub struct Range<'a>
350{
351 pub(crate) inner: base::RefRange<'a>,
352}
353*/
354/*
355impl<'a> Iterator for Range<'a>
356{
357 type Item = Entry<'a>;
358
359 fn next(&mut self) -> Option<Entry<'a>> {
360 let guard = &epoch::pin();
361 self.inner.next(guard).map(Entry::new)
362 }
363}
364
365impl<'a> DoubleEndedIterator for Range<'a>
366{
367 fn next_back(&mut self) -> Option<Entry<'a>> {
368 let guard = &epoch::pin();
369 self.inner.next_back(guard).map(Entry::new)
370 }
371}
372
373impl<'a> fmt::Debug for Range<'a>
374{
375 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
376 f.debug_struct("Range")
377 .field("range", &self.inner.range)
378 .field("head", &self.inner.head)
379 .field("tail", &self.inner.tail)
380 .finish()
381 }
382}
383*/