From a8ba1dbdec6976596e005b7ffe2d06355280a10f Mon Sep 17 00:00:00 2001 From: Rich Hickey Date: Fri, 4 Aug 2006 23:43:39 +0000 Subject: added AnArray --- src/cli/runtime/AnArray.cs | 45 ++++++++++++++++++++++++++++++++++++++ src/cli/runtime/PersistentArray.cs | 20 +++++++---------- src/cli/runtime/Tuple.cs | 12 +++++----- 3 files changed, 59 insertions(+), 18 deletions(-) create mode 100644 src/cli/runtime/AnArray.cs (limited to 'src/cli') diff --git a/src/cli/runtime/AnArray.cs b/src/cli/runtime/AnArray.cs new file mode 100644 index 00000000..6bdb9d29 --- /dev/null +++ b/src/cli/runtime/AnArray.cs @@ -0,0 +1,45 @@ +/** + * 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; +using System.Collections; + +namespace clojure.lang +{ +public abstract class AnArray : Obj, IArray { + + public override Obj withMeta(IPersistentMap meta) + { + Obj ret = (Obj)MemberwiseClone(); + ret._meta = meta; + return ret; + } + + + #region IArray Members + + abstract public int length(); + + abstract public object nth(int i); + + abstract public IArray assocN(int i, object val); + + #endregion + + #region IPersistentCollection Members + + abstract public int count(); + + abstract public ISeq seq(); + + #endregion + } + +} diff --git a/src/cli/runtime/PersistentArray.cs b/src/cli/runtime/PersistentArray.cs index 969833a5..3235eb44 100644 --- a/src/cli/runtime/PersistentArray.cs +++ b/src/cli/runtime/PersistentArray.cs @@ -44,7 +44,8 @@ namespace clojure.lang * Java implementation is lock-free */ -public class PersistentArray : Obj, IEnumerable, IArray{ + public class PersistentArray : AnArray, IEnumerable + { #region IEnumerable Members @@ -55,19 +56,14 @@ public class PersistentArray : Obj, IEnumerable, IArray{ #endregion - public ISeq seq() + override public ISeq seq() { if (length() > 0) return new Seq(this, 0); return null; } - public override Obj withMeta(IPersistentMap meta) - { - Obj ret = (Obj)MemberwiseClone(); - ret._meta = meta; - return ret; - } + internal class Master{ internal readonly Entry[] array; @@ -257,15 +253,15 @@ public PersistentArray(IArray init) :this(init.length()) { data.master.load = load; } -virtual public int count(){ +override public int count(){ return data.master.array.Length; } -virtual public int length(){ +override public int length(){ return data.master.array.Length; } -virtual public Object nth(int i){ +override public Object nth(int i){ Entry e = getEntry(i); if(e != null) return e.val; @@ -328,7 +324,7 @@ for (Entry e = (Entry)data.master.array[i]; e != null; e = e.rest()) return null; } -virtual public IArray assocN(int i,Object val) { +override public IArray assocN(int i,Object val) { //if (data.master.load >= data.master.maxLoad) // { // isolate(); diff --git a/src/cli/runtime/Tuple.cs b/src/cli/runtime/Tuple.cs index 07560fbf..fa6dfda1 100644 --- a/src/cli/runtime/Tuple.cs +++ b/src/cli/runtime/Tuple.cs @@ -15,7 +15,7 @@ using System; namespace clojure.lang { -public class Tuple : IArray{ +public class Tuple : AnArray{ readonly Object[] array; @@ -29,20 +29,20 @@ public Tuple(params Object[] init){ this.array = init; } -public int count() { +override public int count() { return array.Length; } -public int length() { +override public int length() { return array.Length; } -public Object nth(int i){ +override public Object nth(int i){ return array[i]; } -public IArray assocN(int i, Object val) { +override public IArray assocN(int i, Object val) { Object[] newArray = (Object[])array.Clone(); newArray[i] = val; return new Tuple(newArray); @@ -83,7 +83,7 @@ private bool equalKey(Object k1,Object k2){ return k1.Equals(k2); } -public ISeq seq() { +override public ISeq seq() { return ArraySeq.create(array); } } -- cgit v1.2.3-18-g5258