slvm/heap/
vm_hashmap.rs

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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
use crate::{GVm, Value};
use bridge_types::BridgedType;
use std::collections::hash_map::Keys;
use std::collections::HashMap;
use std::hash::{BuildHasher, Hash, Hasher};

/**
 * Provides a wrapper to allow us to build a hashmap with Value keys that hashes String and StringConst
 * to the same hash.
 * Note, this is public only to allow use of insert_id and remove_id which we need to work around
 * borrowing issues.
*/
#[derive(Copy, Clone, Debug)]
pub struct ValHash {
    val: Value,
    hash: u64,
}

impl ValHash {
    /** Make a ValHash from a Value. */
    pub fn from_value<ENV>(vm: &GVm<ENV>, val: Value) -> Self {
        ValHash {
            val,
            hash: val.get_hash(vm),
        }
    }
}

impl PartialEq for ValHash {
    fn eq(&self, other: &Self) -> bool {
        self.hash == other.hash
    }
}

impl Eq for ValHash {}

impl Hash for ValHash {
    fn hash<H: Hasher>(&self, state: &mut H) {
        state.write_u64(self.hash);
    }
}

#[derive(Copy, Clone, Debug, Default)]
struct IdHasher {
    hash: u64,
}

impl BuildHasher for IdHasher {
    type Hasher = IdHasher;

    fn build_hasher(&self) -> Self::Hasher {
        *self
    }
}

impl Hasher for IdHasher {
    fn finish(&self) -> u64 {
        self.hash
    }

    fn write(&mut self, _bytes: &[u8]) {
        panic!("Invalid use of IdHasher!");
    }

    fn write_u64(&mut self, val: u64) {
        self.hash = val;
    }
}

/**
 * Wrapper class for a HashMap<Value, Value>.  We need this because we want String and StringConst
 * to hash to the same value (what a script user will expect) and that requires a VM so can not just
 * implement Hash on Value (will not have access to a VM).
 */
#[derive(Clone, Debug)]
pub struct VMHashMap {
    map: HashMap<ValHash, Value, IdHasher>,
}

impl VMHashMap {
    /** Create a new empty HashMap. */
    pub fn new() -> Self {
        VMHashMap {
            map: HashMap::default(),
        }
    }

    /** Create a new empty HashMap with an initial capacity. */
    pub fn with_capacity(cap: usize) -> Self {
        VMHashMap {
            map: HashMap::with_capacity_and_hasher(cap, IdHasher::default()),
        }
    }

    /** Get the value at key, requires the current VM for hashing. */
    pub fn get<ENV>(&self, vm: &GVm<ENV>, key: Value) -> Option<Value> {
        let id = ValHash::from_value(vm, key);
        self.map.get(&id).copied()
    }

    /** Insert the value at key, requires the current VM for hashing.
     * Returns the old value at key if it exists (None otherwise).
     */
    pub fn insert<ENV>(&mut self, vm: &GVm<ENV>, key: Value, val: Value) -> Option<Value> {
        let id = ValHash::from_value(vm, key);
        self.map.insert(id, val)
    }

    /** Insert val at the key id provided.  This allows calling code to pre-generate the ValHash.
     * This is a borrow checker workaround.
     */
    pub fn insert_id(&mut self, id: ValHash, val: Value) -> Option<Value> {
        self.map.insert(id, val)
    }

    /** Number of items in the HashMap. */
    pub fn len(&self) -> usize {
        self.map.len()
    }

    /** Is this HashMap empty? */
    pub fn is_empty(&self) -> bool {
        self.map.is_empty()
    }

    /** Does this HashMap contain key? */
    pub fn contains_key<ENV>(&self, vm: &GVm<ENV>, key: Value) -> bool {
        let id = ValHash::from_value(vm, key);
        self.map.contains_key(&id)
    }

    /** Clear (remove all key/values) from the HashMap. */
    pub fn clear(&mut self) {
        self.map.clear();
    }

    /** Remove key from the HashMap.  Return the old value if it existed (None otherwise). */
    pub fn remove<ENV>(&mut self, vm: &GVm<ENV>, key: Value) -> Option<Value> {
        let id = ValHash::from_value(vm, key);
        self.map.remove(&id)
    }

    /** Remove the key from HashMap (like remove) except caller pre-generates the ValHash.  Used to
     * work around the borrow checker. */
    pub fn remove_id(&mut self, id: ValHash) -> Option<Value> {
        self.map.remove(&id)
    }

    /** Returns an iterator over all the keys in the HashMap. */
    pub fn keys(&self) -> VMMapKeys {
        VMMapKeys {
            keys: self.map.keys(),
        }
    }

    /** Return an iterator over all the (key, value) pairs in the HashMap. */
    pub fn iter(&self) -> VMHashMapIter {
        VMHashMapIter {
            iter: self.map.iter(),
        }
    }
}

/// A [`VMHashMap`] that contains a [`BridgedType`] can be represented as a rust value.
impl BridgedType for VMHashMap {}

impl Default for VMHashMap {
    fn default() -> Self {
        Self::new()
    }
}

/** Iterator over the key vals in a HashMap. */
pub struct VMHashMapIter<'a> {
    iter: std::collections::hash_map::Iter<'a, ValHash, Value>,
}

impl Iterator for VMHashMapIter<'_> {
    type Item = (Value, Value);

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next().map(|(k, v)| (k.val, *v))
    }
}

/** Iterator over the keys in a HashMap. */
pub struct VMMapKeys<'a> {
    keys: Keys<'a, ValHash, Value>,
}

impl Iterator for VMMapKeys<'_> {
    type Item = Value;

    fn next(&mut self) -> Option<Self::Item> {
        self.keys.next().map(|v| v.val)
    }
}

#[cfg(test)]
mod tests {
    use crate::vm_hashmap::VMHashMap;
    use crate::{Value, Vm};

    #[test]
    fn test_map_str() {
        let mut vm = Vm::new();
        let mut m = VMHashMap::default();
        let cs = Value::StringConst(vm.intern("Test String"));
        let ds = vm.alloc_string("Test String".to_string());
        let i: Value = 1.into();
        m.insert(&mut vm, cs, i);
        assert_eq!(m.get(&vm, cs).unwrap(), i);
        assert_eq!(m.get(&vm, ds).unwrap(), i);
        let i: Value = 10.into();
        m.insert(&mut vm, ds, i);
        assert_eq!(m.get(&vm, cs).unwrap(), i);
        assert_eq!(m.get(&vm, ds).unwrap(), i);
        let old = m.remove(&mut vm, cs).unwrap();
        assert_eq!(old, i);
        assert!(m.get(&vm, cs).is_none());
        assert!(m.get(&vm, ds).is_none());
    }

    #[test]
    fn test_map_sym_key_sanity() {
        let mut vm = Vm::new();
        let mut m = VMHashMap::default();
        let sym = Value::Symbol(vm.intern("Test String"));
        let key = Value::Keyword(vm.intern("Test String"));
        let i: Value = 1.into();
        m.insert(&mut vm, sym, i);
        assert_eq!(m.get(&vm, sym).unwrap(), i);
        assert!(m.get(&vm, key).is_none());
        let i2: Value = 10.into();
        m.insert(&mut vm, key, i2);
        assert_eq!(m.get(&vm, sym).unwrap(), i);
        assert_eq!(m.get(&vm, key).unwrap(), i2);
        let old = m.remove(&mut vm, sym).unwrap();
        assert_eq!(old, i);
        assert!(m.get(&vm, sym).is_none());
        assert_eq!(m.get(&vm, key).unwrap(), i2);
    }

    #[test]
    fn test_map_key_iter_sanity() {
        let mut vm = Vm::new();
        let mut m = VMHashMap::default();
        let key1 = Value::Keyword(vm.intern("one"));
        let key2 = Value::Keyword(vm.intern("two"));
        let key3 = Value::Keyword(vm.intern("three"));
        assert_eq!(m.keys().count(), 0);
        let i1: Value = 1.into();
        m.insert(&mut vm, key1, i1);
        let i2: Value = 1.into();
        m.insert(&mut vm, key2, i2);
        let i3: Value = 1.into();
        m.insert(&mut vm, key3, i3);
        assert_eq!(m.keys().count(), 3);
        m.remove(&mut vm, key1);
        assert_eq!(m.keys().count(), 2);
    }

    #[test]
    fn test_map_iter_sanity() {
        let mut vm = Vm::new();
        let mut m = VMHashMap::default();
        let key1 = Value::Keyword(vm.intern("one"));
        let key2 = Value::Keyword(vm.intern("two"));
        let key3 = Value::Keyword(vm.intern("three"));
        assert_eq!(m.iter().count(), 0);
        let i1: Value = 1.into();
        m.insert(&mut vm, key1, i1);
        let i2: Value = 1.into();
        m.insert(&mut vm, key2, i2);
        let i3: Value = 1.into();
        m.insert(&mut vm, key3, i3);
        assert_eq!(m.iter().count(), 3);
        m.remove(&mut vm, key1);
        assert_eq!(m.iter().count(), 2);
    }
}