This repository has been archived by the owner on Jan 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bimap.clj
73 lines (67 loc) · 1.85 KB
/
bimap.clj
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
(ns detritus.bimap
"A bi-directional map type."
{:author ["Christophe Grand <christophe@cgrand.net>"]})
(defprotocol ReversibleMap
(rmap [m]))
(defn- rdissoc [d r v]
(if-let [[_ k] (find r v)] (dissoc d k) d))
(deftype Bimap [^clojure.lang.IPersistentMap direct reverse]
Object
(hashCode [x]
(.hashCode direct))
(equals [x y]
(.equals direct y))
clojure.lang.IPersistentMap
(without [this k]
(Bimap.
(dissoc direct k)
(rdissoc reverse direct k)))
(assocEx [this k v]
(if (or (contains? direct k) (contains? reverse v))
(throw (Exception. "Key or value already present"))
(assoc this k v)))
java.lang.Iterable
(iterator [this]
(.iterator direct))
clojure.lang.Associative
(assoc [this k v]
(Bimap.
(assoc (rdissoc direct reverse v) k v)
(assoc (rdissoc reverse direct k) v k)))
(containsKey [this k]
(contains? direct k))
(entryAt [this k]
(find direct k))
clojure.lang.IPersistentCollection
(cons [this x]
(if-let [[k v] (and (vector? x) x)]
(assoc this k v)
(reduce (fn [m [k v]] (assoc m k v)) this x)))
(empty [this]
(.empty direct))
(equiv [this x]
(.equiv direct x))
clojure.lang.Seqable
(seq [this]
(seq direct))
clojure.lang.ILookup
(valAt [this k else]
(direct k else))
(valAt [this k]
(direct k))
clojure.lang.Counted
(count [this]
(count direct))
ReversibleMap
(rmap [this]
(Bimap. reverse direct)))
(defn bimap
"Constructs a bi-directional mapping between the given keys and values.
Given a mapping or single argument constituting a sequence of k-v
pairs, returns the resulting bimap.
Given varargs, consecutive args are treated as k-v pairs and the
resulting bimap is returned."
([kvs]
(reduce (partial apply assoc) (Bimap. {} {}) kvs))
([k v & kvs]
(bimap (cons [k v] (partition 2 kvs)))))