diff options
author | Rich Hickey <richhickey@gmail.com> | 2006-06-19 14:46:34 +0000 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2006-06-19 14:46:34 +0000 |
commit | 89fc0caa43811c7dfcb47a29aa59e78d295e6a1c (patch) | |
tree | 940bd7c5aec06958ac36da7cb05a6d9f7fbca520 | |
parent | 3d54fc288b133949a5f49a078641ca07e7ccf3d0 (diff) |
interim checkin, added Tuple, ArraySeq
-rw-r--r-- | clojure.iml | 2 | ||||
-rw-r--r-- | src/jvm/clojure/lang/ArraySeq.java | 43 | ||||
-rw-r--r-- | src/jvm/clojure/lang/Tuple.java | 74 |
3 files changed, 118 insertions, 1 deletions
diff --git a/clojure.iml b/clojure.iml index d238297d..edbba3de 100644 --- a/clojure.iml +++ b/clojure.iml @@ -5,7 +5,7 @@ <output url="file://$MODULE_DIR$/classes" /> <exclude-output /> <content url="file://$MODULE_DIR$"> - <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> + <sourceFolder url="file://$MODULE_DIR$/src/jvm" isTestSource="false" /> </content> <orderEntry type="inheritedJdk" /> <orderEntry type="sourceFolder" forTests="false" /> diff --git a/src/jvm/clojure/lang/ArraySeq.java b/src/jvm/clojure/lang/ArraySeq.java new file mode 100644 index 00000000..01e48529 --- /dev/null +++ b/src/jvm/clojure/lang/ArraySeq.java @@ -0,0 +1,43 @@ +/** + * 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. + **/ + +/* rich Jun 19, 2006 */ + +package clojure.lang; + +public class ArraySeq implements ISeq{ +final Object[] array; +final int i; + +static public ArraySeq create(Object[] array){ + if(array.length == 0) + return null; + return new ArraySeq(array, 0); +} + +ArraySeq(Object[] array, int i){ + this.array = array; + this.i = i; +} + +public Object first() { + return array[i]; +} + +public ISeq rest() { + if(i+1 < array.length) + return new ArraySeq(array, i + 1); + return null; +} + +public int index(){ + return i; +} +} diff --git a/src/jvm/clojure/lang/Tuple.java b/src/jvm/clojure/lang/Tuple.java new file mode 100644 index 00000000..2f7c8e97 --- /dev/null +++ b/src/jvm/clojure/lang/Tuple.java @@ -0,0 +1,74 @@ +/** + * 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. + **/ + +/* rich Jun 19, 2006 */ + +package clojure.lang; + +public class Tuple implements ISequential{ + +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,...} + */ +public Tuple(Object... init){ + this.array = init; +} + +public Object get(int i){ + return array[i]; +} + +public boolean equals(Object key){ + if(this == key) return true; + if(key == null || getClass() != key.getClass()) return false; + + final Tuple tuple = (Tuple) key; + + if(tuple.array.length != array.length) + return false; + + for(int i = 0; i < array.length; i++) + { + if(!equalKey(array[i],tuple.array[i])) + return false; + } + + return true; +} + +public int hashCode(){ + int ret = 0; + for(int i = 0; i < array.length; i++) + { + Object o = array[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); +} + +public ISeq seq() { + return ArraySeq.create(array); +} +} |