Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactor so mutable vectors support ordinary read functions: get/coun…

…t/nth/function-call, with thread safety check
  • Loading branch information...
commit 6e2ff8788fbff36d0ce019c7bfc6adff4fc58cf9 1 parent deff641
@richhickey richhickey authored
View
13 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
View
5 src/jvm/clojure/lang/Associative.java
@@ -9,14 +9,11 @@
* 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);
}
View
19 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);
+}
View
4 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);
}
View
4 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);
View
16 src/jvm/clojure/lang/PersistentVector.java
@@ -369,7 +369,7 @@ else if(subidx == 0)
}
}
-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 @@ final private int tailoff(){
}
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 @@ public Object valAt(Object key){
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){
View
8 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 @@ else if(key instanceof Number && (coll instanceof String || coll.getClass().isAr
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))
Please sign in to comment.
Something went wrong with that request. Please try again.