summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRich Hickey <richhickey@gmail.com>2009-08-02 11:45:13 -0400
committerRich Hickey <richhickey@gmail.com>2009-08-02 11:45:13 -0400
commit6e2ff8788fbff36d0ce019c7bfc6adff4fc58cf9 (patch)
treee1cccb16430ad37e9abfdc6c273729fd1a228d2e
parentdeff64115db501b5adf28432e657d3d9fb823ff9 (diff)
refactor so mutable vectors support ordinary read functions: get/count/nth/function-call, with thread safety checkchunks
-rw-r--r--src/clj/clojure/core.clj13
-rw-r--r--src/jvm/clojure/lang/Associative.java5
-rw-r--r--src/jvm/clojure/lang/ILookup.java19
-rw-r--r--src/jvm/clojure/lang/IMutableAssociative.java4
-rw-r--r--src/jvm/clojure/lang/IMutableVector.java4
-rw-r--r--src/jvm/clojure/lang/PersistentVector.java16
-rw-r--r--src/jvm/clojure/lang/RT.java8
7 files changed, 41 insertions, 28 deletions
diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj
index 293adab5..a2907aa8 100644
--- a/src/clj/clojure/core.clj
+++ b/src/clj/clojure/core.clj
@@ -4350,18 +4350,7 @@
"Removes the last item from a mutable vector. If
the collection is empty, throws an exception. Returns coll"
[#^clojure.lang.IMutableVector coll]
- (.pop coll))
-
-(defn nth!
- "Returns the value at the index. get! returns nil if index out of
- bounds, nth! throws an exception"
- [#^clojure.lang.IMutableVector coll index]
- (.nth coll index))
-
-(defn get!
- "Returns the value mapped to key, nil if key not present."
- [#^clojure.lang.IMutableAssociative coll key]
- (.valAt coll key))
+ (.pop coll))
;redef into with batch support
(defn into
diff --git a/src/jvm/clojure/lang/Associative.java b/src/jvm/clojure/lang/Associative.java
index 891def5f..a2399946 100644
--- a/src/jvm/clojure/lang/Associative.java
+++ b/src/jvm/clojure/lang/Associative.java
@@ -9,14 +9,11 @@ package clojure.lang;
* the terms of this license.
* You must not remove this notice, or any other, from this software.
*/
-public interface Associative extends IPersistentCollection{
+public interface Associative extends IPersistentCollection, ILookup{
boolean containsKey(Object key);
IMapEntry entryAt(Object key);
Associative assoc(Object key, Object val);
-Object valAt(Object key);
-
-Object valAt(Object key, Object notFound);
}
diff --git a/src/jvm/clojure/lang/ILookup.java b/src/jvm/clojure/lang/ILookup.java
new file mode 100644
index 00000000..b124955d
--- /dev/null
+++ b/src/jvm/clojure/lang/ILookup.java
@@ -0,0 +1,19 @@
+/**
+ * Copyright (c) Rich Hickey. All rights reserved.
+ * The use and distribution terms for this software are covered by the
+ * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+ * which can be found in the file epl-v10.html at the root of this distribution.
+ * By using this software in any fashion, you are agreeing to be bound by
+ * the terms of this license.
+ * You must not remove this notice, or any other, from this software.
+ **/
+
+/* rich Aug 2, 2009 */
+
+package clojure.lang;
+
+public interface ILookup{
+Object valAt(Object key);
+
+Object valAt(Object key, Object notFound);
+}
diff --git a/src/jvm/clojure/lang/IMutableAssociative.java b/src/jvm/clojure/lang/IMutableAssociative.java
index 072a8e19..1eef92c2 100644
--- a/src/jvm/clojure/lang/IMutableAssociative.java
+++ b/src/jvm/clojure/lang/IMutableAssociative.java
@@ -12,9 +12,7 @@
package clojure.lang;
-public interface IMutableAssociative extends IMutableCollection{
-
-Object valAt(Object key);
+public interface IMutableAssociative extends IMutableCollection, ILookup{
IMutableAssociative assoc(Object key, Object val);
}
diff --git a/src/jvm/clojure/lang/IMutableVector.java b/src/jvm/clojure/lang/IMutableVector.java
index 51d19e92..3deab315 100644
--- a/src/jvm/clojure/lang/IMutableVector.java
+++ b/src/jvm/clojure/lang/IMutableVector.java
@@ -12,9 +12,7 @@
package clojure.lang;
-public interface IMutableVector extends IMutableAssociative{
-
-Object nth(int i);
+public interface IMutableVector extends IMutableAssociative, Indexed{
IMutableVector assocN(int i, Object val);
diff --git a/src/jvm/clojure/lang/PersistentVector.java b/src/jvm/clojure/lang/PersistentVector.java
index 937bae69..752bccf8 100644
--- a/src/jvm/clojure/lang/PersistentVector.java
+++ b/src/jvm/clojure/lang/PersistentVector.java
@@ -369,7 +369,7 @@ private Node popTail(int level, Node node){
}
}
-static final class MutableVector implements IMutableVector, Counted{
+static final class MutableVector extends AFn implements IMutableVector, Counted{
int cnt;
int shift;
Node root;
@@ -508,6 +508,11 @@ static final class MutableVector implements IMutableVector, Counted{
}
public Object valAt(Object key){
+ //note - relies on ensureEditable in 2-arg valAt
+ return valAt(key, null);
+ }
+
+ public Object valAt(Object key, Object notFound){
ensureEditable();
if(Util.isInteger(key))
{
@@ -515,7 +520,14 @@ static final class MutableVector implements IMutableVector, Counted{
if(i >= 0 && i < cnt)
return nth(i);
}
- return null;
+ return notFound;
+ }
+
+ public Object invoke(Object arg1) throws Exception{
+ //note - relies on ensureEditable in nth
+ if(Util.isInteger(arg1))
+ return nth(((Number) arg1).intValue());
+ throw new IllegalArgumentException("Key must be integer");
}
public Object nth(int i){
diff --git a/src/jvm/clojure/lang/RT.java b/src/jvm/clojure/lang/RT.java
index 263ec551..18be0bf2 100644
--- a/src/jvm/clojure/lang/RT.java
+++ b/src/jvm/clojure/lang/RT.java
@@ -615,8 +615,8 @@ static public Object pop(Object x){
static public Object get(Object coll, Object key){
if(coll == null)
return null;
- else if(coll instanceof Associative)
- return ((Associative) coll).valAt(key);
+ else if(coll instanceof ILookup)
+ return ((ILookup) coll).valAt(key);
else if(coll instanceof Map) {
Map m = (Map) coll;
return m.get(key);
@@ -638,8 +638,8 @@ static public Object get(Object coll, Object key){
static public Object get(Object coll, Object key, Object notFound){
if(coll == null)
return notFound;
- else if(coll instanceof Associative)
- return ((Associative) coll).valAt(key, notFound);
+ else if(coll instanceof ILookup)
+ return ((ILookup) coll).valAt(key, notFound);
else if(coll instanceof Map) {
Map m = (Map) coll;
if(m.containsKey(key))