Skip to content
Browse files

interim checkin, added Tuple, ArraySeq

  • Loading branch information...
1 parent 3d54fc2 commit 89fc0caa43811c7dfcb47a29aa59e78d295e6a1c @richhickey richhickey committed Jun 19, 2006
Showing with 118 additions and 1 deletion.
  1. +1 −1 clojure.iml
  2. +43 −0 src/jvm/clojure/lang/ArraySeq.java
  3. +74 −0 src/jvm/clojure/lang/Tuple.java
View
2 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" />
View
43 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;
+}
+}
View
74 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);
+}
+}

0 comments on commit 89fc0ca

Please sign in to comment.
Something went wrong with that request. Please try again.