-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
interim checkin, added Tuple, ArraySeq
- Loading branch information
1 parent
3d54fc2
commit 89fc0ca
Showing
3 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |