From 5572d24dae42bf686048a1add52437b4b3627ca0 Mon Sep 17 00:00:00 2001 From: Rich Hickey Date: Sat, 5 Aug 2006 17:52:25 +0000 Subject: added IPersistentList, ASeq --- src/cli/runtime/ASeq.cs | 47 ++++++++++++++++++++++++ src/cli/runtime/ArraySeq.cs | 6 +-- src/cli/runtime/Cons.cs | 19 ++-------- src/cli/runtime/EnumeratorSeq.cs | 6 +-- src/cli/runtime/FnSeq.cs | 6 +-- src/cli/runtime/IPersistentList.cs | 23 ++++++++++++ src/cli/runtime/ISeq.cs | 2 +- src/cli/runtime/MapEntry.cs | 6 +-- src/cli/runtime/PersistentArray.cs | 6 +-- src/cli/runtime/PersistentArrayMap.cs | 6 +-- src/cli/runtime/PersistentHashtableMap.cs | 6 +-- src/cli/runtime/PersistentListMap.cs | 10 +++++ src/cli/runtime/PersistentTreeMap.cs | 6 +-- src/jvm/clojure/lang/ASeq.java | 34 +++++++++++++++++ src/jvm/clojure/lang/ArraySeq.java | 2 +- src/jvm/clojure/lang/Cons.java | 2 +- src/jvm/clojure/lang/FnSeq.java | 12 ++++-- src/jvm/clojure/lang/IPersistentCollection.java | 2 +- src/jvm/clojure/lang/IPersistentList.java | 20 ++++++++++ src/jvm/clojure/lang/ISeq.java | 6 +-- src/jvm/clojure/lang/IteratorSeq.java | 2 +- src/jvm/clojure/lang/MapEntry.java | 4 +- src/jvm/clojure/lang/PersistentArray.java | 2 +- src/jvm/clojure/lang/PersistentArrayMap.java | 2 +- src/jvm/clojure/lang/PersistentHashtableMap.java | 12 +++--- src/jvm/clojure/lang/PersistentListMap.java | 8 ++++ src/jvm/clojure/lang/PersistentTreeMap.java | 12 +++--- src/jvm/clojure/lang/RT.java | 10 ++--- 28 files changed, 207 insertions(+), 72 deletions(-) create mode 100644 src/cli/runtime/ASeq.cs create mode 100644 src/cli/runtime/IPersistentList.cs create mode 100644 src/jvm/clojure/lang/ASeq.java create mode 100644 src/jvm/clojure/lang/IPersistentList.java (limited to 'src') diff --git a/src/cli/runtime/ASeq.cs b/src/cli/runtime/ASeq.cs new file mode 100644 index 00000000..9636e80b --- /dev/null +++ b/src/cli/runtime/ASeq.cs @@ -0,0 +1,47 @@ +/** + * 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. + **/ + +using System; + +namespace clojure.lang + { + +public abstract class ASeq : ISeq{ + +public Object peek() { + return first(); +} + +public IPersistentList pop() { + return rest(); +} + +public int count() { + return 1 + RT.count(rest()); +} + +public ISeq seq() { + return this; +} + +public IPersistentCollection cons(Object o) { + return new Cons(o, this); +} + +#region ISeq Members + +abstract public object first(); + +abstract public ISeq rest(); + +#endregion + } + +} diff --git a/src/cli/runtime/ArraySeq.cs b/src/cli/runtime/ArraySeq.cs index bdfbc145..c2c56da7 100644 --- a/src/cli/runtime/ArraySeq.cs +++ b/src/cli/runtime/ArraySeq.cs @@ -15,7 +15,7 @@ using System; namespace clojure.lang { -public class ArraySeq : IndexedSeq{ +public class ArraySeq : ASeq, IndexedSeq{ readonly Object[] array; readonly int i; ISeq _rest; @@ -32,11 +32,11 @@ ArraySeq(Object[] array, int i){ this._rest = this; } -public Object first() { +override public Object first() { return array[i]; } -public ISeq rest() { +override public ISeq rest() { if(_rest == this) { if(i+1 < array.Length) diff --git a/src/cli/runtime/Cons.cs b/src/cli/runtime/Cons.cs index f4361e07..823c3fd8 100644 --- a/src/cli/runtime/Cons.cs +++ b/src/cli/runtime/Cons.cs @@ -15,7 +15,7 @@ using System; namespace clojure.lang { -public class Cons : ISeq +public class Cons : ASeq { private readonly Object _first; @@ -30,31 +30,18 @@ public Cons(Object first, ISeq rest) #region ISeq Members -public object first() +override public object first() { return _first; } -public ISeq rest() +override public ISeq rest() { return _rest; } #endregion - -#region IPersistentCollection Members - -public ISeq seq() - { - return this; - } - -public int count() { - return 1 + RT.count(_rest); -} - -#endregion } } diff --git a/src/cli/runtime/EnumeratorSeq.cs b/src/cli/runtime/EnumeratorSeq.cs index af2bfab0..ef844864 100644 --- a/src/cli/runtime/EnumeratorSeq.cs +++ b/src/cli/runtime/EnumeratorSeq.cs @@ -13,7 +13,7 @@ using System.Collections; namespace clojure.lang { -public class EnumeratorSeq : ISeq{ +public class EnumeratorSeq : ASeq{ IEnumerator e; volatile ISeq _rest; @@ -28,11 +28,11 @@ EnumeratorSeq(IEnumerator e){ this._rest = this; } -public Object first() { +override public Object first() { return e.Current; } -public ISeq rest() { +override public ISeq rest() { if(_rest == this) { lock(this){ diff --git a/src/cli/runtime/FnSeq.cs b/src/cli/runtime/FnSeq.cs index e98a3635..27374eea 100644 --- a/src/cli/runtime/FnSeq.cs +++ b/src/cli/runtime/FnSeq.cs @@ -13,7 +13,7 @@ using System; namespace clojure.lang { -public class FnSeq : ISeq{ +public class FnSeq : ASeq{ Object _first; IFn restFn; @@ -25,11 +25,11 @@ public FnSeq(Object first, IFn restFn) { this._rest = this; } -public Object first() { +override public Object first() { return _first; } -public ISeq rest() { +override public ISeq rest() { if(_rest != this) return _rest; lock(this){ diff --git a/src/cli/runtime/IPersistentList.cs b/src/cli/runtime/IPersistentList.cs new file mode 100644 index 00000000..a5093471 --- /dev/null +++ b/src/cli/runtime/IPersistentList.cs @@ -0,0 +1,23 @@ +/** + * 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. + */ + +using System; + +namespace clojure.lang +{ +public interface IPersistentList : IPersistentCollection, Sequential { + + Object peek(); + + IPersistentList pop(); + +} + +} diff --git a/src/cli/runtime/ISeq.cs b/src/cli/runtime/ISeq.cs index fec4242a..4cd7feea 100644 --- a/src/cli/runtime/ISeq.cs +++ b/src/cli/runtime/ISeq.cs @@ -12,7 +12,7 @@ using System; namespace clojure.lang { - public interface ISeq + public interface ISeq : IPersistentList { Object first(); diff --git a/src/cli/runtime/MapEntry.cs b/src/cli/runtime/MapEntry.cs index 606336d3..88358b27 100644 --- a/src/cli/runtime/MapEntry.cs +++ b/src/cli/runtime/MapEntry.cs @@ -110,18 +110,18 @@ override public ISeq seq() { return new Seq(this); } -class Seq : ISeq{ +class Seq : ASeq{ MapEntry e; public Seq(MapEntry e) { this.e = e; } - public Object first() { + override public Object first() { return e; } - public ISeq rest() { + override public ISeq rest() { return null; } } diff --git a/src/cli/runtime/PersistentArray.cs b/src/cli/runtime/PersistentArray.cs index 8177a4b4..3c766661 100644 --- a/src/cli/runtime/PersistentArray.cs +++ b/src/cli/runtime/PersistentArray.cs @@ -135,7 +135,7 @@ internal class EntryLink : Entry } } -internal class Seq : IndexedSeq{ +internal class Seq : ASeq, IndexedSeq{ readonly PersistentArray p; readonly int i; @@ -144,11 +144,11 @@ internal class Seq : IndexedSeq{ this.i = i; } - public Object first() { + override public Object first() { return p.nth(i); } - public ISeq rest() { + override public ISeq rest() { if(i+1 < p.length()) return new Seq(p, i + 1); return null; diff --git a/src/cli/runtime/PersistentArrayMap.cs b/src/cli/runtime/PersistentArrayMap.cs index e758c547..f4e9e6ee 100644 --- a/src/cli/runtime/PersistentArrayMap.cs +++ b/src/cli/runtime/PersistentArrayMap.cs @@ -182,7 +182,7 @@ override public ISeq seq() { return null; } -internal class Seq : ISeq, IMapEntry{ +internal class Seq : ASeq, IMapEntry{ readonly Object[] array; readonly int i; @@ -199,11 +199,11 @@ internal class Seq : ISeq, IMapEntry{ return array[i+1]; } - public Object first() { + override public Object first() { return this; } - public ISeq rest() { + override public ISeq rest() { if(i+2 < array.Length) return new Seq(array, i + 2); return null; diff --git a/src/cli/runtime/PersistentHashtableMap.cs b/src/cli/runtime/PersistentHashtableMap.cs index 52dea0ad..b54166a0 100644 --- a/src/cli/runtime/PersistentHashtableMap.cs +++ b/src/cli/runtime/PersistentHashtableMap.cs @@ -173,7 +173,7 @@ override public ISeq seq() { return Seq.create(array); } -class Seq : ISeq{ +class Seq : ASeq{ PersistentArray buckets; int b; ISeq e; @@ -201,11 +201,11 @@ class Seq : ISeq{ this.e = e; } - public Object first() { + override public Object first() { return e.first(); } - public ISeq rest() { + override public ISeq rest() { return next(buckets,b,e); } } diff --git a/src/cli/runtime/PersistentListMap.cs b/src/cli/runtime/PersistentListMap.cs index f0c55492..272ca299 100644 --- a/src/cli/runtime/PersistentListMap.cs +++ b/src/cli/runtime/PersistentListMap.cs @@ -49,6 +49,16 @@ internal virtual PersistentListMap next(){ return this; } +public Object peek() + { + return first(); + } + +public IPersistentList pop() + { + return rest(); + } + override public int count(){ return 0; } diff --git a/src/cli/runtime/PersistentTreeMap.cs b/src/cli/runtime/PersistentTreeMap.cs index f43555ee..cbf366c9 100644 --- a/src/cli/runtime/PersistentTreeMap.cs +++ b/src/cli/runtime/PersistentTreeMap.cs @@ -633,7 +633,7 @@ class RedBranchVal : RedBranch{ } } -public class Seq : ISeq{ +public class Seq : ASeq{ readonly ISeq stack; readonly bool asc; @@ -655,11 +655,11 @@ public class Seq : ISeq{ return stack; } - public Object first() { + override public Object first() { return stack.first(); } - public ISeq rest() { + override public ISeq rest() { Node t = (Node)stack.first(); ISeq nextstack = push(asc ? t.right() : t.left(), stack.rest(), asc); if(nextstack != null) diff --git a/src/jvm/clojure/lang/ASeq.java b/src/jvm/clojure/lang/ASeq.java new file mode 100644 index 00000000..272b6e5e --- /dev/null +++ b/src/jvm/clojure/lang/ASeq.java @@ -0,0 +1,34 @@ +/** + * 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 abstract class ASeq implements ISeq{ + +public Object peek() { + return first(); +} + +public IPersistentList pop() { + return rest(); +} + +public int count() { + return 1 + RT.count(rest()); +} + +public ISeq seq() { + return this; +} + +public IPersistentCollection cons(Object o) { + return new Cons(o, this); +} +} diff --git a/src/jvm/clojure/lang/ArraySeq.java b/src/jvm/clojure/lang/ArraySeq.java index 24783727..191e5f80 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 IndexedSeq{ +public class ArraySeq extends ASeq implements IndexedSeq{ final Object[] array; final int i; ISeq _rest; diff --git a/src/jvm/clojure/lang/Cons.java b/src/jvm/clojure/lang/Cons.java index a69b4bcf..65cae175 100644 --- a/src/jvm/clojure/lang/Cons.java +++ b/src/jvm/clojure/lang/Cons.java @@ -12,7 +12,7 @@ package clojure.lang; -public class Cons implements ISeq { +public class Cons extends ASeq { private final Object _first; private final ISeq _rest; diff --git a/src/jvm/clojure/lang/FnSeq.java b/src/jvm/clojure/lang/FnSeq.java index 6f59a437..476145c0 100644 --- a/src/jvm/clojure/lang/FnSeq.java +++ b/src/jvm/clojure/lang/FnSeq.java @@ -10,7 +10,7 @@ package clojure.lang; -public class FnSeq implements ISeq{ +public class FnSeq extends ASeq{ Object _first; IFn restFn; @@ -26,13 +26,19 @@ public Object first() { return _first; } -public ISeq rest() throws Exception { +public ISeq rest() { if(_rest != this) return _rest; synchronized(this){ if(_rest == this) { - _rest = (ISeq) restFn.invoke(); + try{ + _rest = (ISeq) restFn.invoke(); + } + catch(Exception ex) + { + throw new Error(ex.toString()); + } restFn = null; } return _rest; diff --git a/src/jvm/clojure/lang/IPersistentCollection.java b/src/jvm/clojure/lang/IPersistentCollection.java index cd4593df..77e32738 100644 --- a/src/jvm/clojure/lang/IPersistentCollection.java +++ b/src/jvm/clojure/lang/IPersistentCollection.java @@ -15,7 +15,7 @@ public interface IPersistentCollection { int count(); -ISeq seq() throws Exception; +ISeq seq(); IPersistentCollection cons(Object o); } diff --git a/src/jvm/clojure/lang/IPersistentList.java b/src/jvm/clojure/lang/IPersistentList.java new file mode 100644 index 00000000..5a54485e --- /dev/null +++ b/src/jvm/clojure/lang/IPersistentList.java @@ -0,0 +1,20 @@ +/** + * 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 IPersistentList extends IPersistentCollection, Sequential { + + Object peek(); + + IPersistentList pop(); + +} diff --git a/src/jvm/clojure/lang/ISeq.java b/src/jvm/clojure/lang/ISeq.java index 30b561eb..ff6e325f 100644 --- a/src/jvm/clojure/lang/ISeq.java +++ b/src/jvm/clojure/lang/ISeq.java @@ -16,9 +16,9 @@ package clojure.lang; * ISeqs are immutable values, i.e. neither first(), nor rest() changes * or invalidates the ISeq */ -public interface ISeq { +public interface ISeq extends IPersistentList{ -Object first() throws Exception; +Object first() ; -ISeq rest() throws Exception; +ISeq rest() ; } diff --git a/src/jvm/clojure/lang/IteratorSeq.java b/src/jvm/clojure/lang/IteratorSeq.java index e7147f03..dc266279 100644 --- a/src/jvm/clojure/lang/IteratorSeq.java +++ b/src/jvm/clojure/lang/IteratorSeq.java @@ -12,7 +12,7 @@ package clojure.lang; import java.util.Iterator; -public class IteratorSeq implements ISeq{ +public class IteratorSeq extends ASeq{ Iterator iter; volatile Object val; volatile ISeq _rest; diff --git a/src/jvm/clojure/lang/MapEntry.java b/src/jvm/clojure/lang/MapEntry.java index 55aaf1df..702b2937 100644 --- a/src/jvm/clojure/lang/MapEntry.java +++ b/src/jvm/clojure/lang/MapEntry.java @@ -94,11 +94,11 @@ static class Iter implements Iterator{ } -public ISeq seq() throws Exception { +public ISeq seq() { return new Seq(this); } -static class Seq implements ISeq{ +static class Seq extends ASeq{ MapEntry e; public Seq(MapEntry e) { diff --git a/src/jvm/clojure/lang/PersistentArray.java b/src/jvm/clojure/lang/PersistentArray.java index e0673e8e..a0112abf 100644 --- a/src/jvm/clojure/lang/PersistentArray.java +++ b/src/jvm/clojure/lang/PersistentArray.java @@ -127,7 +127,7 @@ static class EntryLink extends Entry{ } } -static class Seq implements IndexedSeq{ +static class Seq extends ASeq implements IndexedSeq{ final PersistentArray p; final int i; diff --git a/src/jvm/clojure/lang/PersistentArrayMap.java b/src/jvm/clojure/lang/PersistentArrayMap.java index 50343926..ee539ba4 100644 --- a/src/jvm/clojure/lang/PersistentArrayMap.java +++ b/src/jvm/clojure/lang/PersistentArrayMap.java @@ -179,7 +179,7 @@ public ISeq seq() { return null; } -static class Seq implements ISeq, IMapEntry{ +static class Seq extends ASeq implements IMapEntry{ final Object[] array; final int i; diff --git a/src/jvm/clojure/lang/PersistentHashtableMap.java b/src/jvm/clojure/lang/PersistentHashtableMap.java index 7ddf7e76..a701156b 100644 --- a/src/jvm/clojure/lang/PersistentHashtableMap.java +++ b/src/jvm/clojure/lang/PersistentHashtableMap.java @@ -162,21 +162,21 @@ public Iterator iterator() { return new Iter(array); } -public ISeq seq() throws Exception { +public ISeq seq() { return Seq.create(array); } -static class Seq implements ISeq{ +static class Seq extends ASeq{ PersistentArray buckets; int b; ISeq e; - static public Seq create(PersistentArray buckets) throws Exception { + static public Seq create(PersistentArray buckets) { return next(buckets, -1, null); } - static Seq next(PersistentArray buckets, int b, ISeq e) throws Exception { + static Seq next(PersistentArray buckets, int b, ISeq e) { if(e != null && e.rest() != null) return new Seq(buckets,b,e.rest()); for(b = b+1;b 0) return Seq.create(tree, true); return null; @@ -616,7 +616,7 @@ static class RedBranchVal extends RedBranch{ } -static public class Seq implements ISeq{ +static public class Seq extends ASeq{ final ISeq stack; final boolean asc; @@ -625,11 +625,11 @@ static public class Seq implements ISeq{ this.asc = asc; } - static Seq create(Node t, boolean asc) throws Exception { + static Seq create(Node t, boolean asc) { return new Seq(push(t, null, asc),asc); } - static ISeq push(Node t, ISeq stack, boolean asc) throws Exception { + static ISeq push(Node t, ISeq stack, boolean asc) { while(t != null) { stack = RT.cons(t,stack); @@ -638,11 +638,11 @@ static public class Seq implements ISeq{ return stack; } - public Object first() throws Exception { + public Object first() { return stack.first(); } - public ISeq rest() throws Exception { + public ISeq rest() { Node t = (Node)stack.first(); ISeq nextstack = push(asc ? t.right() : t.left(), stack.rest(), asc); if(nextstack != null) diff --git a/src/jvm/clojure/lang/RT.java b/src/jvm/clojure/lang/RT.java index 08daaf15..4d3a8c75 100644 --- a/src/jvm/clojure/lang/RT.java +++ b/src/jvm/clojure/lang/RT.java @@ -68,7 +68,7 @@ static public boolean equal(Object k1,Object k2){ // ?Boolean.TRUE:null; // } -static public ISeq seq(Object coll) throws Exception { +static public ISeq seq(Object coll) { if(coll == null || coll instanceof ISeq) return (ISeq) coll; else if(coll instanceof IPersistentCollection) @@ -78,7 +78,7 @@ static public ISeq seq(Object coll) throws Exception { else if(coll instanceof Object[]) return ArraySeq.create((Object[]) coll); else - throw new IllegalArgumentException("Don't know how to create ISeq from arg"); + throw new IllegalAccessError("Don't know how to create ISeq from arg"); } static public Iter iter(Object coll) @@ -193,15 +193,15 @@ static public double doubleCast(Object x) /******************************************* list support ********************************/ -static public Cons cons(Object x, Object y) throws Exception { +static public Cons cons(Object x, Object y) { return new Cons(x, seq(y)); } -static public Object first(Object x) throws Exception { +static public Object first(Object x) { return seq(x).first(); } -static public ISeq rest(Object x) throws Exception { +static public ISeq rest(Object x) { return seq(x).rest(); } -- cgit v1.2.3-70-g09d2