summaryrefslogtreecommitdiff
path: root/src/jvm
diff options
context:
space:
mode:
Diffstat (limited to 'src/jvm')
-rw-r--r--src/jvm/clojure/lang/ArraySeq.java2
-rw-r--r--src/jvm/clojure/lang/IArray.java19
-rw-r--r--src/jvm/clojure/lang/IndexedSeq.java16
-rw-r--r--src/jvm/clojure/lang/PersistentArray.java68
-rw-r--r--src/jvm/clojure/lang/PersistentArrayIdentityMap.java2
-rw-r--r--src/jvm/clojure/lang/PersistentArrayMap.java2
-rw-r--r--src/jvm/clojure/lang/RT.java11
-rw-r--r--src/jvm/clojure/lang/Tuple.java43
8 files changed, 141 insertions, 22 deletions
diff --git a/src/jvm/clojure/lang/ArraySeq.java b/src/jvm/clojure/lang/ArraySeq.java
index 01e48529..e01459a4 100644
--- a/src/jvm/clojure/lang/ArraySeq.java
+++ b/src/jvm/clojure/lang/ArraySeq.java
@@ -12,7 +12,7 @@
package clojure.lang;
-public class ArraySeq implements ISeq{
+public class ArraySeq implements IndexedSeq{
final Object[] array;
final int i;
diff --git a/src/jvm/clojure/lang/IArray.java b/src/jvm/clojure/lang/IArray.java
new file mode 100644
index 00000000..f8546c15
--- /dev/null
+++ b/src/jvm/clojure/lang/IArray.java
@@ -0,0 +1,19 @@
+package clojure.lang;
+
+/**
+ * Copyright (c) Rich Hickey. All rights reserved.
+ * The use and distribution terms for this software are covered by the
+ * Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
+ * which can be found in the file CPL.TXT 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.
+ */
+
+public interface IArray extends ISequential {
+int length();
+
+Object get(int i);
+
+IArray set(int i,Object val) throws Exception;
+}
diff --git a/src/jvm/clojure/lang/IndexedSeq.java b/src/jvm/clojure/lang/IndexedSeq.java
new file mode 100644
index 00000000..a4c37641
--- /dev/null
+++ b/src/jvm/clojure/lang/IndexedSeq.java
@@ -0,0 +1,16 @@
+/**
+ * Copyright (c) Rich Hickey. All rights reserved.
+ * The use and distribution terms for this software are covered by the
+ * Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
+ * which can be found in the file CPL.TXT 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.
+ */
+
+package clojure.lang;
+
+public interface IndexedSeq extends ISeq{
+
+public int index();
+}
diff --git a/src/jvm/clojure/lang/PersistentArray.java b/src/jvm/clojure/lang/PersistentArray.java
index 45a91740..4f8c16bf 100644
--- a/src/jvm/clojure/lang/PersistentArray.java
+++ b/src/jvm/clojure/lang/PersistentArray.java
@@ -46,7 +46,7 @@ import java.util.Random;
* I added hybrid most-recent-sequential-range + shared-bitset idea, multi-thread-safety
*/
-public class PersistentArray implements Iterable, ISequential{
+public class PersistentArray implements Iterable, IArray {
public Iterator iterator(){
return new ValIter(this);
@@ -109,7 +109,7 @@ static class EntryLink extends Entry{
}
}
-static class Seq implements ISeq{
+static class Seq implements IndexedSeq{
final PersistentArray p;
final int i;
@@ -127,6 +127,10 @@ static class Seq implements ISeq{
return new Seq(p, i + 1);
return null;
}
+
+ public int index() {
+ return i;
+ }
}
static class ValIter implements Iterator{
@@ -157,7 +161,7 @@ final int baseline;
final BitSet history;
public PersistentArray(int size){
- this(size, null);
+ this(size, (Object)null);
}
public PersistentArray(int size, Object defaultVal){
@@ -178,7 +182,29 @@ PersistentArray(Master master,int rev,int baseline, BitSet history){
this.history = history;
}
+public PersistentArray(int size, ISeq seq) throws Exception {
+ this(size);
+ int load = 0;
+ for(int i=0;seq != null && i < size;i++, seq=seq.rest())
+ {
+ master.array[i] = new Entry(0,seq.first());
+ ++load;
+ }
+
+ master.load = load;
+}
+
+public PersistentArray(IArray init) {
+ this(init.length());
+ int load = 0;
+ for(int i=0;i < init.length();i++)
+ {
+ master.array[i] = new Entry(0,init.get(i));
+ ++load;
+ }
+ master.load = load;
+}
final public int length(){
return master.array.length;
@@ -254,6 +280,42 @@ final public PersistentArray set(int i,Object val) {
}
}
+
+public boolean equals(Object key){
+ if(this == key) return true;
+ if(key == null || !(key instanceof IArray)) return false;
+
+ final IArray a = (IArray) key;
+
+ if(a.length() != length())
+ return false;
+
+ for(int i = 0; i < length(); i++)
+ {
+ if(!equalKey(get(i),a.get(i)))
+ return false;
+ }
+
+ return true;
+}
+
+public int hashCode(){
+ int ret = 0;
+ for(int i = 0; i < length(); i++)
+ {
+ Object o = get(i);
+ if(o != null)
+ ret ^= o.hashCode();
+ }
+ return ret;
+}
+
+private boolean equalKey(Object k1,Object k2){
+ if(k1 == null)
+ return k2 == null;
+ return k1.equals(k2);
+}
+
final void doSet(int i, Object val){
// Entry oldEntry, newEntry;
// do
diff --git a/src/jvm/clojure/lang/PersistentArrayIdentityMap.java b/src/jvm/clojure/lang/PersistentArrayIdentityMap.java
index 3bc8c56f..57a1bf59 100644
--- a/src/jvm/clojure/lang/PersistentArrayIdentityMap.java
+++ b/src/jvm/clojure/lang/PersistentArrayIdentityMap.java
@@ -25,7 +25,7 @@ IPersistentMap empty() {
return EMPTY;
}
-public PersistentArrayIdentityMap(Object[] init) {
+public PersistentArrayIdentityMap(Object... init) {
super(init);
}
diff --git a/src/jvm/clojure/lang/PersistentArrayMap.java b/src/jvm/clojure/lang/PersistentArrayMap.java
index 920b0bf1..b5c0e39a 100644
--- a/src/jvm/clojure/lang/PersistentArrayMap.java
+++ b/src/jvm/clojure/lang/PersistentArrayMap.java
@@ -37,7 +37,7 @@ protected PersistentArrayMap(){
* This ctor captures/aliases the passed array, so do not modify later
* @param init {key1,val1,key2,val2,...}
*/
-public PersistentArrayMap(Object[] init){
+public PersistentArrayMap(Object... init){
this.array = init;
}
diff --git a/src/jvm/clojure/lang/RT.java b/src/jvm/clojure/lang/RT.java
index 645da926..9347d3a6 100644
--- a/src/jvm/clojure/lang/RT.java
+++ b/src/jvm/clojure/lang/RT.java
@@ -64,6 +64,17 @@ public class RT{
// ?Boolean.TRUE:null;
// }
+static public ISeq seq(Object coll) throws Exception {
+ if(coll == null || coll instanceof ISeq)
+ return (ISeq) coll;
+ else if(coll instanceof ISequential)
+ return ((ISequential) coll).seq();
+ else if(coll instanceof Object[])
+ return ArraySeq.create((Object[]) coll);
+ else
+ throw new IllegalArgumentException("Don't know how to create ISeq from arg");
+}
+
static public Iter iter(Object coll)
{
if(coll == null || coll instanceof Iter)
diff --git a/src/jvm/clojure/lang/Tuple.java b/src/jvm/clojure/lang/Tuple.java
index 2f7c8e97..b105479f 100644
--- a/src/jvm/clojure/lang/Tuple.java
+++ b/src/jvm/clojure/lang/Tuple.java
@@ -12,15 +12,12 @@
package clojure.lang;
-public class Tuple implements ISequential{
+public class Tuple implements IArray{
final Object[] array;
final public static Tuple EMPTY = new Tuple();
-static public Tuple create(Object... init){
- return new Tuple(init);
-}
/**
* This ctor captures/aliases the passed array, so do not modify later !
* @param init {key1,val1,key2,val2,...}
@@ -29,26 +26,40 @@ public Tuple(Object... init){
this.array = init;
}
+public int length() {
+ return array.length;
+}
+
public Object get(int i){
- return array[i];
+ return array[i];
+}
+
+/**
+ *
+ * @param i
+ * @param val
+ * @return a PersistentArray
+ */
+public IArray set(int i, Object val) {
+ return (new PersistentArray(this)).set(i, val);
}
public boolean equals(Object key){
- if(this == key) return true;
- if(key == null || getClass() != key.getClass()) return false;
+ if(this == key) return true;
+ if(key == null || !(key instanceof IArray)) return false;
- final Tuple tuple = (Tuple) key;
+ final IArray a = (IArray) key;
- if(tuple.array.length != array.length)
- return false;
+ if(a.length() != array.length)
+ return false;
- for(int i = 0; i < array.length; i++)
- {
- if(!equalKey(array[i],tuple.array[i]))
- return false;
- }
+ for(int i = 0; i < array.length; i++)
+ {
+ if(!equalKey(array[i],a.get(i)))
+ return false;
+ }
- return true;
+ return true;
}
public int hashCode(){