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
use crate::prelude::*;
slotmap::new_key_type!{ pub struct OccultIlkId; }
#[derive(Debug,Serialize,Deserialize)]
#[serde(transparent)]
pub struct OccultIlkOwningId(Id);
#[derive(Debug,Clone,Eq,PartialEq,Ord,PartialOrd,Hash)]
#[derive(Serialize,Deserialize)]
#[serde(transparent)]
pub struct OccultIlkName(pub Arc<GoodItemName>);
#[derive(Debug,Serialize,Deserialize)]
pub struct OccultIlkData {
pub p_occ: Arc<dyn OccultedPieceTrait>,
}
type Id = OccultIlkId;
type OId = OccultIlkOwningId;
type K = OccultIlkName;
type V = OccultIlkData;
type Refcount = u32;
#[derive(Debug,Serialize,Deserialize,Default)]
pub struct OccultIlks {
lookup: HashMap<K, Id>,
table: DenseSlotMap<Id, Data>,
}
#[derive(Debug,Serialize,Deserialize)]
pub struct Data {
k: K,
v: V,
refcount: Refcount,
}
impl OccultIlks {
#[throws(as Option)]
pub fn get<I: Borrow<Id>>(&self, id: I) -> &V {
&self.table.get(*id.borrow())?.v
}
pub fn create(&mut self, k: K, v: V) -> OId {
let OccultIlks { lookup, table } = self;
let id = *lookup
.entry(k)
.or_insert_with_key(|k|{
let data = Data {
v,
k: k.clone(),
refcount: 0,
};
table.insert(data)
});
table[id].refcount += 1;
OccultIlkOwningId(id)
}
pub fn dispose(&mut self, id: OId) {
let id: Id = id.0;
let data = &mut self.table[id];
data.refcount = data.refcount.checked_sub(1).unwrap();
if data.refcount == 0 {
let data = self.table.remove(id).unwrap();
self.lookup.remove(&data.k);
}
}
pub fn is_empty(&self) -> bool {
let OccultIlks { lookup, table } = self;
#[allow(unused_parens)]
(
lookup.is_empty()
&& table.is_empty()
)
}
}
impl Borrow<Id> for OId {
fn borrow(&self) -> &Id { &self.0 }
}
impl IOccults {
pub fn is_empty(&self) -> bool {
let IOccults { ilks } = self;
ilks.is_empty()
}
}