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 ++-- 13 files changed, 108 insertions(+), 41 deletions(-) create mode 100644 src/cli/runtime/ASeq.cs create mode 100644 src/cli/runtime/IPersistentList.cs (limited to 'src/cli/runtime') 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) -- cgit v1.2.3-70-g09d2