1use std::collections::HashMap;
2use std::collections::BTreeMap;
3use std::hash::Hash;
4use std::sync::RwLock;
5use std::sync::RwLockReadGuard;
6use std::sync::RwLockWriteGuard;
7
8
9pub struct SimpleConcurrentHashMap<K, V>(RwLock<HashMap<K, V>>);
13
14impl<K, V> SimpleConcurrentHashMap<K, V>
15where
16 K: Eq + Hash,
17{
18 pub fn new() -> Self {
19 SimpleConcurrentHashMap(RwLock::new(HashMap::new()))
20 }
21
22 pub fn insert(&self, key: K, value: V) -> Option<V> {
23 let mut lock = self.write();
24 lock.insert(key, value)
25 }
26
27 pub fn read(&self) -> RwLockReadGuard<HashMap<K, V>> {
28 self.0.read().unwrap()
29 }
30
31 pub fn write(&self) -> RwLockWriteGuard<HashMap<K, V>> {
32 self.0.write().unwrap()
33 }
34
35 pub fn contains_key(&self, key: &K) -> bool {
36 self.read().contains_key(key)
37 }
38}
39
40#[derive(Debug)]
41pub struct SimpleConcurrentBTreeMap<K,V>(RwLock<BTreeMap<K,V>>);
42
43impl <K,V>Default for SimpleConcurrentBTreeMap<K,V> where K: Ord{
44
45 fn default() -> Self {
46 SimpleConcurrentBTreeMap(RwLock::new(BTreeMap::new()))
47 }
48
49}
50
51impl <K,V> SimpleConcurrentBTreeMap<K,V>
52 where K: Ord
53{
54 pub fn new() -> Self {
55 SimpleConcurrentBTreeMap(RwLock::new(BTreeMap::new()))
56 }
57
58 pub fn read(&self) -> RwLockReadGuard<BTreeMap<K, V>> {
59 self.0.read().unwrap()
60 }
61
62 pub fn write(&self) -> RwLockWriteGuard<BTreeMap<K, V>> {
63 self.0.write().unwrap()
64 }
65
66 pub fn insert(&self, key: K, value: V) -> Option<V> {
67 let mut lock = self.write();
68 lock.insert(key, value)
69 }
70
71 pub fn contains_key(&self, key: &K) -> bool {
72 self.read().contains_key(key)
73 }
74
75
76}