From ad85b0e92aac9bb64fb0887ad1410d4e07dfc2ef Mon Sep 17 00:00:00 2001 From: Rich Hickey Date: Mon, 3 Aug 2009 14:09:11 -0400 Subject: renamed mutable -> transient, immutable! -> persistent! --- src/clj/clojure/core.clj | 36 +++++++++++++------------ src/jvm/clojure/lang/IEditableCollection.java | 2 +- src/jvm/clojure/lang/IMutableAssociative.java | 18 ------------- src/jvm/clojure/lang/IMutableCollection.java | 20 -------------- src/jvm/clojure/lang/IMutableVector.java | 20 -------------- src/jvm/clojure/lang/ITransientAssociative.java | 18 +++++++++++++ src/jvm/clojure/lang/ITransientCollection.java | 20 ++++++++++++++ src/jvm/clojure/lang/ITransientVector.java | 20 ++++++++++++++ src/jvm/clojure/lang/PersistentVector.java | 32 +++++++++++----------- 9 files changed, 94 insertions(+), 92 deletions(-) delete mode 100644 src/jvm/clojure/lang/IMutableAssociative.java delete mode 100644 src/jvm/clojure/lang/IMutableCollection.java delete mode 100644 src/jvm/clojure/lang/IMutableVector.java create mode 100644 src/jvm/clojure/lang/ITransientAssociative.java create mode 100644 src/jvm/clojure/lang/ITransientCollection.java create mode 100644 src/jvm/clojure/lang/ITransientVector.java diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj index ef5f2482..a275236d 100644 --- a/src/clj/clojure/core.clj +++ b/src/clj/clojure/core.clj @@ -4319,37 +4319,39 @@ [promise val] (promise val)) ;;;;;;;;;;;;;;;;;;;;; editable collections ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -(defn mutable - "Returns a new, mutable version of the collection, in constant time." +(defn transient + "Returns a new, transient version of the collection, in constant time." [#^clojure.lang.IEditableCollection coll] - (.mutable coll)) + (.asTransient coll)) -(defn immutable! - "Returns a new, immutable version of the mutable collection, in constant time." - [#^clojure.lang.IMutableCollection coll] - (.immutable coll)) +(defn persistent! + "Returns a new, persistent version of the transient collection, in + constant time. The transient collection cannot be used after this + call, any such use will throw an exception." + [#^clojure.lang.ITransientCollection coll] + (.persistent coll)) (defn conj! - "Adds x to the mutable collection, and return coll. The 'addition' + "Adds x to the transient collection, and return coll. The 'addition' may happen at different 'places' depending on the concrete type." - [#^clojure.lang.IMutableCollection coll x] + [#^clojure.lang.ITransientCollection coll x] (.conj coll x)) (defn assoc! - "When applied to a mutable map, adds mapping of key(s) to - val(s). When applied to a mutable vector, sets the val at index. + "When applied to a transient map, adds mapping of key(s) to + val(s). When applied to a transient vector, sets the val at index. Note - index must be <= (count vector). Returns coll." - ([#^clojure.lang.IMutableAssociative coll key val] (.assoc coll key val)) - ([#^clojure.lang.IMutableAssociative coll key val & kvs] + ([#^clojure.lang.ITransientAssociative coll key val] (.assoc coll key val)) + ([#^clojure.lang.ITransientAssociative coll key val & kvs] (let [ret (.assoc coll key val)] (if kvs (recur ret (first kvs) (second kvs) (nnext kvs)) ret)))) (defn pop! - "Removes the last item from a mutable vector. If + "Removes the last item from a transient vector. If the collection is empty, throws an exception. Returns coll" - [#^clojure.lang.IMutableVector coll] + [#^clojure.lang.ITransientVector coll] (.pop coll)) ;redef into with batch support @@ -4358,10 +4360,10 @@ from-coll conjoined." [to from] (if (instance? clojure.lang.IEditableCollection to) - (#(loop [ret (mutable to) items (seq from)] + (#(loop [ret (transient to) items (seq from)] (if items (recur (conj! ret (first items)) (next items)) - (immutable! ret)))) + (persistent! ret)))) (#(loop [ret to items (seq from)] (if items (recur (conj ret (first items)) (next items)) diff --git a/src/jvm/clojure/lang/IEditableCollection.java b/src/jvm/clojure/lang/IEditableCollection.java index be447958..63ccb53a 100644 --- a/src/jvm/clojure/lang/IEditableCollection.java +++ b/src/jvm/clojure/lang/IEditableCollection.java @@ -13,5 +13,5 @@ package clojure.lang; public interface IEditableCollection{ -IMutableCollection mutable(); +ITransientCollection asTransient(); } diff --git a/src/jvm/clojure/lang/IMutableAssociative.java b/src/jvm/clojure/lang/IMutableAssociative.java deleted file mode 100644 index 1eef92c2..00000000 --- a/src/jvm/clojure/lang/IMutableAssociative.java +++ /dev/null @@ -1,18 +0,0 @@ -/** - * 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 Jul 17, 2009 */ - -package clojure.lang; - -public interface IMutableAssociative extends IMutableCollection, ILookup{ - -IMutableAssociative assoc(Object key, Object val); -} diff --git a/src/jvm/clojure/lang/IMutableCollection.java b/src/jvm/clojure/lang/IMutableCollection.java deleted file mode 100644 index a41f1701..00000000 --- a/src/jvm/clojure/lang/IMutableCollection.java +++ /dev/null @@ -1,20 +0,0 @@ -/** - * 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 Jul 17, 2009 */ - -package clojure.lang; - -public interface IMutableCollection{ - -IMutableCollection conj(Object val); - -IPersistentCollection immutable(); -} diff --git a/src/jvm/clojure/lang/IMutableVector.java b/src/jvm/clojure/lang/IMutableVector.java deleted file mode 100644 index 3deab315..00000000 --- a/src/jvm/clojure/lang/IMutableVector.java +++ /dev/null @@ -1,20 +0,0 @@ -/** - * 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 Jul 17, 2009 */ - -package clojure.lang; - -public interface IMutableVector extends IMutableAssociative, Indexed{ - -IMutableVector assocN(int i, Object val); - -IMutableVector pop(); -} diff --git a/src/jvm/clojure/lang/ITransientAssociative.java b/src/jvm/clojure/lang/ITransientAssociative.java new file mode 100644 index 00000000..a4d2655a --- /dev/null +++ b/src/jvm/clojure/lang/ITransientAssociative.java @@ -0,0 +1,18 @@ +/** + * 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 Jul 17, 2009 */ + +package clojure.lang; + +public interface ITransientAssociative extends ITransientCollection, ILookup{ + +ITransientAssociative assoc(Object key, Object val); +} diff --git a/src/jvm/clojure/lang/ITransientCollection.java b/src/jvm/clojure/lang/ITransientCollection.java new file mode 100644 index 00000000..0079d33b --- /dev/null +++ b/src/jvm/clojure/lang/ITransientCollection.java @@ -0,0 +1,20 @@ +/** + * 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 Jul 17, 2009 */ + +package clojure.lang; + +public interface ITransientCollection{ + +ITransientCollection conj(Object val); + +IPersistentCollection persistent(); +} diff --git a/src/jvm/clojure/lang/ITransientVector.java b/src/jvm/clojure/lang/ITransientVector.java new file mode 100644 index 00000000..6311754c --- /dev/null +++ b/src/jvm/clojure/lang/ITransientVector.java @@ -0,0 +1,20 @@ +/** + * 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 Jul 17, 2009 */ + +package clojure.lang; + +public interface ITransientVector extends ITransientAssociative, Indexed{ + +ITransientVector assocN(int i, Object val); + +ITransientVector pop(); +} diff --git a/src/jvm/clojure/lang/PersistentVector.java b/src/jvm/clojure/lang/PersistentVector.java index 752bccf8..2d55e2e8 100644 --- a/src/jvm/clojure/lang/PersistentVector.java +++ b/src/jvm/clojure/lang/PersistentVector.java @@ -43,24 +43,24 @@ final Object[] tail; public final static PersistentVector EMPTY = new PersistentVector(0, 5, EMPTY_NODE, new Object[]{}); static public PersistentVector create(ISeq items){ - MutableVector ret = EMPTY.mutable(); + TransientVector ret = EMPTY.asTransient(); for(; items != null; items = items.next()) ret = ret.conj(items.first()); - return ret.immutable(); + return ret.persistent(); } static public PersistentVector create(List items){ - MutableVector ret = EMPTY.mutable(); + TransientVector ret = EMPTY.asTransient(); for(Object item : items) ret = ret.conj(item); - return ret.immutable(); + return ret.persistent(); } static public PersistentVector create(Object... items){ - MutableVector ret = EMPTY.mutable(); + TransientVector ret = EMPTY.asTransient(); for(Object item : items) ret = ret.conj(item); - return ret.immutable(); + return ret.persistent(); } PersistentVector(int cnt, int shift, Node root, Object[] tail){ @@ -80,8 +80,8 @@ PersistentVector(IPersistentMap meta, int cnt, int shift, Node root, Object[] ta this.tail = tail; } -public MutableVector mutable(){ - return new MutableVector(this); +public TransientVector asTransient(){ + return new TransientVector(this); } final int tailoff(){ @@ -369,20 +369,20 @@ private Node popTail(int level, Node node){ } } -static final class MutableVector extends AFn implements IMutableVector, Counted{ +static final class TransientVector extends AFn implements ITransientVector, Counted{ int cnt; int shift; Node root; Object[] tail; - MutableVector(int cnt, int shift, Node root, Object[] tail){ + TransientVector(int cnt, int shift, Node root, Object[] tail){ this.cnt = cnt; this.shift = shift; this.root = root; this.tail = tail; } - MutableVector(PersistentVector v){ + TransientVector(PersistentVector v){ this(v.cnt, v.shift, editableRoot(v.root), editableTail(v.tail)); } @@ -413,7 +413,7 @@ static final class MutableVector extends AFn implements IMutableVector, Counted{ return new Node(new AtomicReference(Thread.currentThread()), node.array.clone()); } - public PersistentVector immutable(){ + public PersistentVector persistent(){ ensureEditable(); // Thread owner = root.edit.get(); // if(owner != null && owner != Thread.currentThread()) @@ -432,7 +432,7 @@ static final class MutableVector extends AFn implements IMutableVector, Counted{ return ret; } - public MutableVector conj(Object val){ + public TransientVector conj(Object val){ ensureEditable(); int i = cnt; //room in tail? @@ -536,7 +536,7 @@ static final class MutableVector extends AFn implements IMutableVector, Counted{ return node[i & 0x01f]; } - public MutableVector assocN(int i, Object val){ + public TransientVector assocN(int i, Object val){ ensureEditable(); if(i >= 0 && i < cnt) { @@ -554,7 +554,7 @@ static final class MutableVector extends AFn implements IMutableVector, Counted{ throw new IndexOutOfBoundsException(); } - public MutableVector assoc(Object key, Object val){ + public TransientVector assoc(Object key, Object val){ //note - relies on ensureEditable in assocN if(Util.isInteger(key)) { @@ -579,7 +579,7 @@ static final class MutableVector extends AFn implements IMutableVector, Counted{ return ret; } - public MutableVector pop(){ + public TransientVector pop(){ ensureEditable(); if(cnt == 0) throw new IllegalStateException("Can't pop empty vector"); -- cgit v1.2.3-70-g09d2