Skip to content

Commit

Permalink
interim checkin, added Tuple, ArraySeq
Browse files Browse the repository at this point in the history
  • Loading branch information
richhickey committed Jun 19, 2006
1 parent 3d54fc2 commit 89fc0ca
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clojure.iml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
43 changes: 43 additions & 0 deletions src/jvm/clojure/lang/ArraySeq.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
74 changes: 74 additions & 0 deletions src/jvm/clojure/lang/Tuple.java
Original file line number Diff line number Diff line change
@@ -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.