diff options
author | Aria Shrimpton <me@aria.rip> | 2024-01-25 14:03:08 +0000 |
---|---|---|
committer | Aria Shrimpton <me@aria.rip> | 2024-01-25 14:03:08 +0000 |
commit | 3ad6406b1aa22f242c6695aa533e4325c682e76d (patch) | |
tree | c023155eba4e7f6fb9a116a1f0cdc4409f655394 /src/crates/library | |
parent | 69ceb8dc27efc1d280ffbfafc51a5b2c9fb63a8f (diff) |
add btreemap
Diffstat (limited to 'src/crates/library')
-rw-r--r-- | src/crates/library/src/btreemap.rs | 92 | ||||
-rw-r--r-- | src/crates/library/src/lib.rs | 1 |
2 files changed, 93 insertions, 0 deletions
diff --git a/src/crates/library/src/btreemap.rs b/src/crates/library/src/btreemap.rs new file mode 100644 index 0000000..d3f5144 --- /dev/null +++ b/src/crates/library/src/btreemap.rs @@ -0,0 +1,92 @@ +/*LIBSPEC-NAME* +rust-hashmap-spec std::collections::BTreeMap +*ENDLIBSPEC-NAME*/ + +use crate::traits::Mapping; +pub use std::collections::BTreeMap; +use std::hash::Hash; + +/*IMPL* +Mapping +*ENDIMPL*/ +impl<K: Ord + Hash, V> Mapping<K, V> for BTreeMap<K, V> { + /*LIBSPEC* + /*OPNAME* + len len pre-len post-len + *ENDOPNAME*/ + (define (pre-len xs) (is-map? xs)) + (define (len xs) (cons xs (length xs))) + (define (post-len xs r) (equal? r (len xs))) + *ENDLIBSPEC*/ + fn len(&self) -> usize { + BTreeMap::len(self) + } + + /*LIBSPEC* + /*OPNAME* + contains contains pre-contains post-contains + *ENDOPNAME*/ + (define (pre-contains xs) (is-map? xs)) + (define (contains xs k) (assoc k xs)) + (define (post-contains xs k r) (equal? r (contains xs k))) + *ENDLIBSPEC*/ + fn contains(&mut self, x: &K) -> bool { + BTreeMap::contains_key(self, x) + } + + /*LIBSPEC* + /*OPNAME* + insert insert pre-insert post-insert + *ENDOPNAME*/ + (define (pre-insert xs) (is-map? xs)) + (define (insert xs k v) + (let ([idx (index-where xs (lambda (p) (equal? k (car p))))]) + (cond [idx (list-set xs idx (cons k v))] + [else (list* (cons k v) xs)]))) + (define (post-insert xs k v r) (equal? r (insert xs k v))) + *ENDLIBSPEC*/ + fn insert(&mut self, key: K, val: V) -> Option<V> { + BTreeMap::insert(self, key, val) + } + + /*LIBSPEC* + /*OPNAME* + get get pre-get post-get + *ENDOPNAME*/ + (define (pre-get xs) (is-map? xs)) + (define (get xs k) (cdr (assoc k xs))) + (define (post-get xs k r) (equal? r (get xs k))) + *ENDLIBSPEC*/ + fn get(&mut self, key: &K) -> Option<&V> { + BTreeMap::get(self, key) + } + + /*LIBSPEC* + /*OPNAME* + remove remove pre-remove post-remove + *ENDOPNAME*/ + (define (pre-remove xs) (is-map? xs)) + (define (remove xs k) (cdr (assoc k xs))) + (define (post-remove xs k r) (equal? r (remove xs k))) + *ENDLIBSPEC*/ + fn remove(&mut self, key: &K) -> Option<V> { + BTreeMap::remove(self, key) + } + + /*LIBSPEC* + /*OPNAME* + clear clear pre-clear post-clear + *ENDOPNAME*/ + (define (pre-clear xs) (is-map? xs)) + (define (clear xs) null) + (define (post-clear xs r) (equal? r (clear xs))) + *ENDLIBSPEC*/ + fn clear(&mut self) { + BTreeMap::clear(self) + } +} + +#[cfg(test)] +mod tests { + // TODO +} diff --git a/src/crates/library/src/lib.rs b/src/crates/library/src/lib.rs index db5aac7..219ccb3 100644 --- a/src/crates/library/src/lib.rs +++ b/src/crates/library/src/lib.rs @@ -12,6 +12,7 @@ mod eager_unique_vector; mod lazy_sorted_vector; mod lazy_unique_vector; +mod btreemap; mod hashmap; mod hashset; mod list; |