Back
aget (clj)
(source)function
(aget array idx)
(aget array idx & idxs)
Returns the value at the index/indices. Works on Java arrays of all
types.
Examples
mikera/core.matrix
(ns clojure.core.matrix.macros-clj
"Namespace for core.matrix macros. Keeping them separate allows us to do conditional
macros that can handle the differences between Clojure and Clojurescript."
(:require [clojure.core.matrix.macros :refer [TODO c-for]])
(:import [java.util Arrays]))
(defmacro abutnth [i xs]
`(let [n# (alength ~xs)
length# (int (dec n#))
new-xs# (Arrays/copyOf ~xs length#)]
(c-for [j# (int ~i) (< j# (dec n#)) (inc j#)]
(aset new-xs# (int j#) (aget ~xs (int (inc j#)))))
new-xs#))
(defmacro areverse [xs]
`(let [n# (alength ~xs)
new-xs# (Arrays/copyOf ~xs (int n#))]
(c-for [i# (int 0) (< i# (quot n# 2)) (inc i#)]
(let [j# (- (- n# 1) i#)
t# (aget new-xs# j#)]
(aset new-xs# j# (aget new-xs# i#))
(aset new-xs# i# t#)))
new-xs#))
hraberg/deuce
(ns deuce.emacs.chartab
(:use [deuce.emacs-lisp :only (defun defvar)])
(:require [clojure.core :as c]
[deuce.emacs.alloc :as alloc]
[deuce.emacs.data :as data]
[deuce.emacs.fns :as fns])
(:import [deuce.emacs.data CharTable]
[java.util Arrays])
(:refer-clojure :exclude []))
(defun char-table-extra-slot (char-table n)
"Return the value of CHAR-TABLE's extra-slot number N."
(aget ^objects (.extras ^CharTable char-table) n))
typedclojure/typedclojure
(ns ^:no-doc typed.ann.clojure
"Type annotations for the base Clojure distribution."
#?(:cljs (:require-macros [typed.ann-macros.clojure :as macros]))
(:require [clojure.core :as cc]
[typed.clojure :as t]
#?(:clj [typed.ann-macros.clojure :as macros])
#?(:clj typed.ann.clojure.jvm) ;; jvm annotations
#?(:clj clojure.core.typed))
#?(:clj
(:import (clojure.lang PersistentHashSet PersistentList
APersistentMap #_IPersistentCollection
#_ITransientSet
IRef)
(java.util Comparator Collection))))
cc/alength [(ReadOnlyArray t/Any) :-> t/AnyInteger]
cc/aclone (t/All [x] [(ReadOnlyArray x) :-> (Array x)])
cc/aget (t/All [x]
(t/IFn [(ReadOnlyArray x)
t/AnyInteger :-> x]
[(ReadOnlyArray (ReadOnlyArray x))
t/AnyInteger t/AnyInteger :-> x]
[(ReadOnlyArray (ReadOnlyArray (ReadOnlyArray x)))
t/AnyInteger t/AnyInteger t/AnyInteger :-> x]
[(ReadOnlyArray (ReadOnlyArray (ReadOnlyArray (ReadOnlyArray x))))
t/AnyInteger t/AnyInteger t/AnyInteger t/AnyInteger :-> x]
; don't support unsound cases
[(ReadOnlyArray (ReadOnlyArray (ReadOnlyArray (ReadOnlyArray (ReadOnlyArray x)))))
t/AnyInteger t/AnyInteger t/AnyInteger t/AnyInteger t/AnyInteger :-> x]))
clojure/core.rrb-vector
(ns clojure.core.rrb-vector.transients
(:require [clojure.core.rrb-vector.parameters :as p]
[clojure.core.rrb-vector.nodes :refer [ranges last-range
overflow?]])
(:import (clojure.core.rrb_vector.nodes NodeManager)
(clojure.core ArrayManager)
(java.util.concurrent.atomic AtomicReference)))
(def ^ITransientHelper transient-helper
(reify ITransientHelper
(editableRoot [this nm am root]
(let [new-arr (clojure.core/aclone ^objects (.array nm root))]
(if (== 33 (alength ^objects new-arr))
(aset new-arr 32 (aclone (ints (aget ^objects new-arr 32)))))
(.node nm (AtomicReference. (Thread/currentThread)) new-arr)))
(ensureEditable [this nm am root-edit current-node shift]
(if (identical? root-edit (.edit nm current-node))
current-node
(if (zero? shift)
(let [new-arr (.aclone am (.array nm current-node))]
(.node nm root-edit new-arr))
(let [new-arr (aclone ^objects (.array nm current-node))]
(if (== 33 (alength ^objects new-arr))
(aset new-arr 32 (aclone (ints (aget ^objects new-arr 32)))))
(.node nm root-edit new-arr)))))
;; Note 2: In the worst case, when the tree is nearly full,
;; calling overflow? here takes run time O(tree_depth^2) here.
;; That could be made O(tree_depth). One way would be to call
;; pushTail in hopes that it succeeds, but return some distinctive
;; value indicating a failure on the full condition, and create
;; the node via a .newPath call at most recent recursive pushTail
;; call that has an empty slot available.
(pushTail [this nm am shift cnt root-edit current-node tail-node]
(let [ret (.ensureEditable this nm am root-edit current-node shift)]
(if (.regular nm ret)
(do (loop [n ret shift shift]
(let [arr (.array nm n)
subidx (bit-and (bit-shift-right (dec cnt) shift) 0x1f)]
(if (== shift 5)
(aset ^objects arr subidx tail-node)
(let [child (aget ^objects arr subidx)]
(if (nil? child)
(aset ^objects arr subidx
(.newPath this nm am
(.array nm tail-node)
root-edit
(unchecked-subtract-int shift 5)
tail-node))
(let [editable-child
(.ensureEditable this nm am
root-edit
child
(unchecked-subtract-int
shift 5))]
(aset ^objects arr subidx editable-child)
(recur editable-child (- shift (int 5)))))))))
ret)
(let [arr (.array nm ret)
rngs (ranges nm ret)
li (unchecked-dec-int (aget rngs 32))
cret (if (== shift 5)
nil
(let [child (.ensureEditable this nm am
root-edit
(aget ^objects arr li)
(unchecked-subtract-int
shift 5))
ccnt (unchecked-add-int
(int (if (pos? li)
(unchecked-subtract-int
(aget rngs li)
(aget rngs (unchecked-dec-int li)))
(aget rngs 0)))
;; add 32 elems to account for the
;; new full tail we plan to add to
;; the subtree.
(int 32))]
;; See Note 2
(if-not (overflow? nm child
(unchecked-subtract-int shift 5)
ccnt)
(.pushTail this nm am
(unchecked-subtract-int shift 5)
ccnt
root-edit
child
tail-node))))]
(if cret
(do (aset ^objects arr li cret)
(aset rngs li (unchecked-add-int (aget rngs li) 32))
ret)
(do (when (>= li 31)
;; See Note 1
(let [msg (str "Assigning index " (inc li) " of vector"
" object array to become a node, when that"
" index should only be used for storing"
" range arrays.")
data {:shift shift, :cnd cnt,
:current-node current-node,
:tail-node tail-node, :rngs rngs, :li li,
:cret cret}]
(throw (ex-info msg data))))
(aset ^objects arr (inc li)
(.newPath this nm am
(.array nm tail-node)
root-edit
(unchecked-subtract-int shift 5)
tail-node))
(aset rngs (unchecked-inc-int li)
(unchecked-add-int (aget rngs li) 32))
(aset rngs 32 (unchecked-inc-int (aget rngs 32)))
ret))))))
(popTail [this nm am shift cnt root-edit current-node]
(let [ret (.ensureEditable this nm am root-edit current-node shift)]
(if (.regular nm ret)
(let [subidx (bit-and
(bit-shift-right (unchecked-subtract-int cnt (int 2))
(int shift))
(int 0x1f))]
(cond
(> shift 5)
(let [child (.popTail this nm am
(unchecked-subtract-int shift 5)
cnt ;; TBD: Should this be smaller than cnt?
root-edit
(aget ^objects (.array nm ret) subidx))]
(if (and (nil? child) (zero? subidx))
nil
(let [arr (.array nm ret)]
(aset ^objects arr subidx child)
ret)))
:else
(let [arr (.array nm ret)]
(aset ^objects arr subidx nil)
ret)))
(let [rngs (ranges nm ret)
subidx (unchecked-dec-int (aget rngs 32))]
(cond
(> shift 5)
(let [child (aget ^objects (.array nm ret) subidx)
child-cnt (if (zero? subidx)
(aget rngs 0)
(unchecked-subtract-int
(aget rngs subidx)
(aget rngs (unchecked-dec-int subidx))))
new-child (.popTail this nm am
(unchecked-subtract-int shift 5)
child-cnt
root-edit
child)]
(cond
(and (nil? new-child) (zero? subidx))
nil
(.regular nm child)
(let [arr (.array nm ret)]
(aset rngs subidx
(unchecked-subtract-int (aget rngs subidx) 32))
(aset ^objects arr subidx new-child)
(if (nil? new-child)
(aset rngs 32 (unchecked-dec-int (aget rngs 32))))
ret)
:else
(let [rng (last-range nm child)
diff (unchecked-subtract-int
rng
(if new-child (last-range nm new-child) 0))
arr (.array nm ret)]
(aset rngs subidx
(unchecked-subtract-int (aget rngs subidx) diff))
(aset ^objects arr subidx new-child)
(if (nil? new-child)
(aset rngs 32 (unchecked-dec-int (aget rngs 32))))
ret)))
:else
(let [arr (.array nm ret)
child (aget ^objects arr subidx)]
(aset ^objects arr subidx nil)
(aset rngs subidx 0)
(aset rngs 32 (unchecked-dec-int (aget rngs 32)))
ret))))))
(doAssoc [this nm am shift root-edit current-node i val]
(let [ret (.ensureEditable this nm am root-edit current-node shift)]
(if (.regular nm ret)
(loop [shift shift
node ret]
(if (zero? shift)
(let [arr (.array nm node)]
(.aset am arr (bit-and i 0x1f) val))
(let [arr (.array nm node)
subidx (bit-and (bit-shift-right i shift) 0x1f)
next-shift (int (unchecked-subtract-int shift 5))
child (.ensureEditable this nm am
root-edit
(aget ^objects arr subidx)
next-shift)]
(aset ^objects arr subidx child)
(recur next-shift child))))
(let [arr (.array nm ret)
rngs (ranges nm ret)
subidx (bit-and (bit-shift-right i shift) 0x1f)
subidx (loop [subidx subidx]
(if (< i (aget rngs subidx))
subidx
(recur (unchecked-inc-int subidx))))
i (if (zero? subidx)
i
(unchecked-subtract-int
i (aget rngs (unchecked-dec-int subidx))))]
(aset ^objects arr subidx
(.doAssoc this nm am
(unchecked-subtract-int shift 5)
root-edit
(aget ^objects arr subidx)
i
val))))
ret))
datastax/fallout
(ns clojure.core.rrb-vector.transients
(:require [clojure.core.rrb-vector.nodes :refer [ranges last-range]])
(:import (clojure.core.rrb_vector.nodes NodeManager)
(clojure.core ArrayManager)
(java.util.concurrent.atomic AtomicReference)))
(ensureEditable [this nm am root-edit current-node shift]
(if (identical? root-edit (.edit nm current-node))
current-node
(if (zero? shift)
(let [new-arr (.aclone am (.array nm current-node))]
(.node nm root-edit new-arr))
(let [new-arr (aclone ^objects (.array nm current-node))]
(if (== 33 (alength ^objects new-arr))
(aset new-arr 32 (aclone (ints (aget ^objects new-arr 32)))))
(.node nm root-edit new-arr)))))
(pushTail [this nm am shift cnt root-edit current-node tail-node]
(let [ret (.ensureEditable this nm am root-edit current-node shift)]
(if (.regular nm ret)
(do (loop [n ret shift shift]
(let [arr (.array nm n)
subidx (bit-and (bit-shift-right (dec cnt) shift) 0x1f)]
(if (== shift 5)
(aset ^objects arr subidx tail-node)
(let [child (aget ^objects arr subidx)]
(if (nil? child)
(aset ^objects arr subidx
(.newPath this nm am
(.array nm tail-node)
root-edit
(unchecked-subtract-int shift 5)
tail-node))
(let [editable-child
(.ensureEditable this nm am
root-edit
child
(unchecked-subtract-int
shift 5))]
(aset ^objects arr subidx editable-child)
(recur editable-child (- shift 5))))))))
ret)
(let [arr (.array nm ret)
rngs (ranges nm ret)
li (unchecked-dec-int (aget rngs 32))
cret (if (== shift 5)
nil
(let [child (.ensureEditable this nm am
root-edit
(aget ^objects arr li)
(unchecked-subtract-int
shift 5))
ccnt (if (pos? li)
(unchecked-subtract-int
(aget rngs li)
(aget rngs (unchecked-dec-int li)))
(aget rngs 0))]
(if-not (== ccnt (bit-shift-left 1 shift))
(.pushTail this nm am
(unchecked-subtract-int shift 5)
(unchecked-inc-int ccnt)
root-edit
child
tail-node))))]
(if cret
(do (aset ^objects arr li cret)
(aset rngs li (unchecked-add-int (aget rngs li) 32))
ret)
(do (aset ^objects arr (inc li)
(.newPath this nm am
(.array nm tail-node)
root-edit
(unchecked-subtract-int shift 5)
tail-node))
(aset rngs (unchecked-inc-int li)
(unchecked-add-int (aget rngs li) 32))
(aset rngs 32 (unchecked-inc-int (aget rngs 32)))
ret))))))
(popTail [this nm am shift cnt root-edit current-node]
(let [ret (.ensureEditable this nm am root-edit current-node shift)]
(if (.regular nm ret)
(let [subidx (bit-and
(bit-shift-right (unchecked-dec-int cnt) shift)
0x1f)]
(cond
(> shift 5)
(let [child (.popTail this nm am
(unchecked-subtract-int shift 5)
cnt
root-edit
(aget ^objects (.array nm ret) subidx))]
(if (and (nil? child) (zero? subidx))
nil
(let [arr (.array nm ret)]
(aset ^objects arr subidx child)
ret)))
:else
(let [arr (.array nm ret)]
(aset ^objects arr subidx nil)
ret)))
(let [rngs (ranges nm ret)
subidx (bit-and
(bit-shift-right (unchecked-dec-int cnt) shift)
0x1f)
subidx (loop [subidx subidx]
(if (or (zero? (aget rngs (unchecked-inc-int subidx)))
(== subidx 31))
subidx
(recur (unchecked-inc-int subidx))))]
(cond
(> shift 5)
(let [child (aget ^objects (.array nm ret) subidx)
child-cnt (if (zero? subidx)
(aget rngs 0)
(unchecked-subtract-int
(aget rngs subidx)
(aget rngs (unchecked-dec-int subidx))))
new-child (.popTail this nm am
(unchecked-subtract-int subidx 5)
child-cnt
root-edit
child)]
(cond
(and (nil? new-child) (zero? subidx))
nil
(.regular nm child)
(let [arr (.array nm ret)]
(aset rngs subidx
(unchecked-subtract-int (aget rngs subidx) 32))
(aset ^objects arr subidx new-child)
(if (nil? new-child)
(aset rngs 32 (unchecked-dec-int (aget rngs 32))))
ret)
:else
(let [rng (last-range nm child)
diff (unchecked-subtract-int
rng
(if new-child (last-range nm new-child) 0))
arr (.array nm ret)]
(aset rngs subidx
(unchecked-subtract-int (aget rngs subidx) diff))
(aset ^objects arr subidx new-child)
(if (nil? new-child)
(aset rngs 32 (unchecked-dec-int (aget rngs 32))))
ret)))
:else
(let [arr (.array nm ret)
child (aget ^objects arr subidx)]
(aset ^objects arr subidx nil)
(aset rngs subidx 0)
(aset rngs 32 (unchecked-dec-int (aget rngs 32)))
ret))))))
(doAssoc [this nm am shift root-edit current-node i val]
(let [ret (.ensureEditable this nm am root-edit current-node shift)]
(if (.regular nm ret)
(loop [shift shift
node ret]
(if (zero? shift)
(let [arr (.array nm node)]
(.aset am arr (bit-and i 0x1f) val))
(let [arr (.array nm node)
subidx (bit-and (bit-shift-right i shift) 0x1f)
child (.ensureEditable this nm am
root-edit
(aget ^objects arr subidx)
shift)]
(aset ^objects arr subidx child)
(recur (unchecked-subtract-int shift 5) child))))
(let [arr (.array nm ret)
rngs (ranges nm ret)
subidx (bit-and (bit-shift-right i shift) 0x1f)
subidx (loop [subidx subidx]
(if (< i (aget rngs subidx))
subidx
(recur (unchecked-inc-int subidx))))
i (if (zero? subidx)
i
(unchecked-subtract-int
i (aget rngs (unchecked-dec-int subidx))))]
(aset ^objects arr subidx
(.doAssoc this nm am
(unchecked-subtract-int shift 5)
root-edit
(aget ^objects arr subidx)
i
val))))
ret))