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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
use super::hash::{Hash, HashedKey, Hasher};
use super::node::{
    insert_rec, lookup_one, remove_eq_rec, remove_rec, replace_rec, replace_with_rec, size_rec,
    update_rec, Entry, LookupRet, Node, NodeIter,
};
pub use super::operation::{InsertError, RemoveError, ReplaceError, UpdateError};
use std::borrow::Borrow;
use std::error::Error;
use std::fmt::Debug;
use std::iter::FromIterator;
use std::marker::PhantomData;
use std::mem::swap;
use std::slice;

#[derive(Debug, Clone)]
#[must_use = "`Hamt`s are not modified in place, instead modified copies are returned`"]
pub struct Hamt<H: Hasher + Default, K: PartialEq + Eq + Hash, V> {
    root: Node<K, V>,
    hasher: PhantomData<H>,
}

pub struct HamtIter<'a, K, V> {
    stack: Vec<NodeIter<'a, K, V>>,
    content: Option<slice::Iter<'a, (K, V)>>,
}

impl<H: Hasher + Default, K: Eq + Hash, V> Default for Hamt<H, K, V> {
    fn default() -> Self {
        Self::new()
    }
}

impl<H: Hasher + Default, K: Eq + Hash, V> Hamt<H, K, V> {
    pub fn new() -> Self {
        Hamt {
            root: Node::new(),
            hasher: PhantomData,
        }
    }

    pub fn is_empty(&self) -> bool {
        self.root.is_empty()
    }

    pub fn size(&self) -> usize {
        size_rec(&self.root)
    }
}

impl<H: Hasher + Default, K: Clone + Eq + Hash, V: Clone> Hamt<H, K, V> {
    pub fn insert(&self, k: K, v: V) -> Result<Self, InsertError> {
        let h = HashedKey::compute(self.hasher, &k);
        let newroot = insert_rec(&self.root, h, 0, k, v)?;
        Ok(Hamt {
            root: newroot,
            hasher: PhantomData,
        })
    }
}

impl<H: Hasher + Default, K: Eq + Hash + Clone, V: PartialEq + Clone> Hamt<H, K, V> {
    pub fn remove_match<Q>(&self, k: &Q, v: &V) -> Result<Self, RemoveError>
    where
        K: Borrow<Q>,
        Q: Hash + Eq,
    {
        let h = HashedKey::compute(self.hasher, &k);
        let newroot = remove_eq_rec(&self.root, h, 0, k, v)?;
        match newroot {
            None => Ok(Self::new()),
            Some(r) => Ok(Hamt {
                root: r,
                hasher: PhantomData,
            }),
        }
    }
}

impl<H: Hasher + Default, K: Clone + Eq + Hash, V: Clone> Hamt<H, K, V> {
    pub fn remove<Q>(&self, k: &Q) -> Result<Self, RemoveError>
    where
        K: Borrow<Q>,
        Q: Hash + Eq,
    {
        let h = HashedKey::compute(self.hasher, k);
        let newroot = remove_rec(&self.root, h, 0, k)?;
        match newroot {
            None => Ok(Self::new()),
            Some(r) => Ok(Hamt {
                root: r,
                hasher: PhantomData,
            }),
        }
    }
}

impl<H: Hasher + Default, K: Eq + Hash + Clone, V: Clone> Hamt<H, K, V> {
    /// Replace the element at the key by the v and return the new tree
    /// and the old value.
    pub fn replace(&self, k: &K, v: V) -> Result<(Self, V), ReplaceError> {
        let h = HashedKey::compute(self.hasher, &k);
        let (newroot, oldv) = replace_rec(&self.root, h, 0, k, v)?;
        Ok((
            Hamt {
                root: newroot,
                hasher: PhantomData,
            },
            oldv,
        ))
    }

    /// Replace the element at the key by the v and return the new tree
    /// and the old value.
    pub fn replace_with<F>(&self, k: &K, f: F) -> Result<Self, ReplaceError>
    where
        F: FnOnce(&V) -> V,
    {
        let h = HashedKey::compute(self.hasher, &k);
        let newroot = replace_with_rec(&self.root, h, 0, k, f)?;
        Ok(Hamt {
            root: newroot,
            hasher: PhantomData,
        })
    }
}

impl<H: Hasher + Default, K: Eq + Hash + Clone, V: Clone> Hamt<H, K, V> {
    /// Update the element at the key K.
    ///
    /// If the closure F in parameter returns None, then the key is deleted.
    ///
    /// If the key is not present then UpdateError::KeyNotFound is returned
    pub fn update<F, U>(&self, k: &K, f: F) -> Result<Self, UpdateError<U>>
    where
        F: FnOnce(&V) -> Result<Option<V>, U>,
        U: Error + Debug + 'static,
    {
        let h = HashedKey::compute(self.hasher, &k);
        let newroot = update_rec(&self.root, h, 0, k, f)?;
        match newroot {
            None => Ok(Self::new()),
            Some(r) => Ok(Hamt {
                root: r,
                hasher: PhantomData,
            }),
        }
    }

    /// Update or insert the element at the key K
    ///
    /// If the element is not present, then V is added, otherwise the closure F is apply
    /// to the found element. If the closure returns None, then the key is deleted
    pub fn insert_or_update<F, E>(&self, k: K, v: V, f: F) -> Result<Self, E>
    where
        F: FnOnce(&V) -> Result<Option<V>, E>,
        V: Clone,
        E: Error + Debug + 'static,
    {
        match self.update(&k, f) {
            Ok(new_self) => Ok(new_self),
            Err(UpdateError::KeyNotFound) =>
            // unwrap is safe: only error than can be raised is an EntryExist which is fundamentally impossible in this error case handling
            {
                Ok(self.insert(k, v).unwrap())
            }
            Err(UpdateError::ValueCallbackError(x)) => Err(x),
        }
    }

    /// Update or insert the element at the key K
    ///
    /// If the element is not present, then V is added, otherwise the closure F is apply
    /// to the found element. If the closure returns None, then the key is deleted.
    ///
    /// This is similar to 'insert_or_update' except the closure shouldn't be failing
    pub fn insert_or_update_simple<F>(&self, k: K, v: V, f: F) -> Self
    where
        F: for<'a> FnOnce(&'a V) -> Option<V>,
        V: Clone,
    {
        use std::convert::Infallible;
        match self.update(&k, |x| Ok::<_, Infallible>(f(x))) {
            Ok(new_self) => new_self,
            Err(UpdateError::ValueCallbackError(_)) => unreachable!(), // callback always wrapped in Ok
            Err(UpdateError::KeyNotFound) => {
                // unwrap is safe: only error than can be raised is an EntryExist which is fundamentally impossible in this error case handling
                self.insert(k, v).unwrap()
            }
        }
    }
}

impl<H: Hasher + Default, K: Hash + Eq, V> Hamt<H, K, V> {
    /// Try to get the element related to key K
    pub fn lookup<Q>(&self, k: &Q) -> Option<&V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq,
    {
        let h = HashedKey::compute(self.hasher, k);
        let mut n = &self.root;
        let mut lvl = 0;
        loop {
            match lookup_one(n, &h, lvl, k) {
                LookupRet::NotFound => return None,
                LookupRet::Found(v) => return Some(v),
                LookupRet::ContinueIn(subnode) => {
                    lvl += 1;
                    n = subnode;
                }
            }
        }
    }

    /// Check if the key is contained into the HAMT
    pub fn contains_key<Q>(&self, k: &Q) -> bool
    where
        K: Borrow<Q>,
        Q: Hash + Eq,
    {
        self.lookup(k).is_some()
    }

    pub fn iter(&self) -> HamtIter<K, V> {
        HamtIter {
            stack: vec![self.root.iter()],
            content: None,
        }
    }
}

impl<'a, K, V> Iterator for HamtIter<'a, K, V> {
    type Item = (&'a K, &'a V);

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let mut x = None;
            swap(&mut self.content, &mut x);
            match x {
                Some(mut iter) => match iter.next() {
                    None => self.content = None,
                    Some(o) => {
                        self.content = Some(iter);
                        return Some((&o.0, &o.1));
                    }
                },
                None => match self.stack.last_mut() {
                    None => return None,
                    Some(last) => match last.next() {
                        None => {
                            self.stack.pop();
                        }
                        Some(next) => match next.as_ref() {
                            Entry::SubNode(ref sub) => self.stack.push(sub.iter()),
                            Entry::Leaf(_, k, v) => return Some((k, v)),
                            Entry::LeafMany(_, ref col) => self.content = Some(col.iter()),
                        },
                    },
                },
            }
        }
    }
}

impl<H: Default + Hasher, K: Eq + Hash + Clone, V: Clone> FromIterator<(K, V)> for Hamt<H, K, V> {
    fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
        let mut h = Hamt::new();
        for (k, v) in iter {
            match h.insert(k, v) {
                Err(_) => {}
                Ok(newh) => h = newh,
            }
        }
        h
    }
}

impl<H: Default + Hasher, K: Eq + Hash, V: PartialEq> PartialEq for Hamt<H, K, V> {
    fn eq(&self, other: &Self) -> bool {
        // optimised the obvious cases first
        if self.is_empty() && other.is_empty() {
            return true;
        }
        if self.is_empty() != other.is_empty() {
            return false;
        }
        // then compare key and values
        // TODO : optimise by comparing nodes directly
        for (k, v) in self.iter() {
            if let Some(v2) = other.lookup(k) {
                if v != v2 {
                    return false;
                }
            } else {
                return false;
            }
        }
        true
    }
}

impl<H: Default + Hasher, K: Eq + Hash, V: Eq> Eq for Hamt<H, K, V> {}

impl<'a, H: Default + Hasher, K: Eq + Hash, V> IntoIterator for &'a Hamt<H, K, V> {
    type Item = (&'a K, &'a V);

    type IntoIter = HamtIter<'a, K, V>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}