Skip to content

Instantly share code, notes, and snippets.

@lbradstreet
Created January 23, 2017 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbradstreet/de5f5d7db55f7ae36338b8bba08d7ba2 to your computer and use it in GitHub Desktop.
Save lbradstreet/de5f5d7db55f7ae36338b8bba08d7ba2 to your computer and use it in GitHub Desktop.
(ns ^:no-doc onyx.messaging.int-object-map
(:import [org.agrona.collections Int2ObjectHashMap
Int2ObjectHashMap$KeyIterator Int2ObjectHashMap$EntryIterator]
[clojure.lang ILookup Associative IPersistentMap Counted ISeq]))
(defprotocol IInt2ObjectMap
(clone [this]))
(deftype IntObjectMap [^Int2ObjectHashMap m]
clojure.lang.IPersistentMap
(assoc [this k v]
(let [vp ^IntObjectMap (clone this)]
(.put ^Int2ObjectHashMap (.m vp) (int k) v)
vp))
#_(dissoc [this k]
(let [vp ^IntObjectMap (clone this)]
(.remove ^Int2ObjectHashMap (.m vp) (int k))
vp))
ISeq
(seq [this]
)
Counted
(count [this]
(.size m))
ILookup
(valAt [this k]
(.get m (int k)))
IInt2ObjectMap
(clone [this]
;; Needs to be locked because we're using a deftype, not defrecord
(locking this
(IntObjectMap.
(let [iterator ^Int2ObjectHashMap$EntryIterator (.iterator (.entrySet ^Int2ObjectHashMap (.m this)))
new-hm ^Int2ObjectHashMap (Int2ObjectHashMap.)]
(while (.hasNext iterator)
(let [kv ^Int2ObjectHashMap$EntryIterator (.next iterator)
k ^java.lang.Integer (.getKey kv)
v (.getValue kv)]
(.put new-hm k v)))
new-hm)))))
(defn int2objectmap []
(IntObjectMap. (Int2ObjectHashMap.)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment