/System.Xronos/Language/PersistentList.cs
C# | 258 lines | 182 code | 53 blank | 23 comment | 13 complexity | 3f9d6c7e0f74e89b6ac5ad2c3226f38a MD5 | raw file
- /* ****************************************************************************
- *
- * Copyright (c) 2008 Stefan Rusek and Benjamin Pollack
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- * ***************************************************************************/
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Collections;
-
- namespace System.Xronos.Language
- {
- public class PersistentList : Sequence, IPersistentList, IReduce
- {
- object _first;
- PersistentList _rest;
- int _count;
-
- private PersistentList(IPersistentMap meta, object first, PersistentList rest)
- : base(meta)
- {
- _first = first;
- _rest = rest;
-
- if (rest != null)
- _count = rest.Count;
-
- _count++;
- }
-
- public static IPersistentList Create(IList list)
- {
- IPersistentCollection rt = Empty;
- for (int i = list.Count-1; i >= 0; i--)
- {
- rt = rt.cons(list[i]);
- }
- return (IPersistentList)rt;
- }
-
- public static IPersistentList Create(ISequence seq)
- {
- if (seq == null)
- return Empty;
- return (IPersistentList)Create(seq.rest()).cons(seq.first());
- }
-
- public static readonly IPersistentList Empty = new EmptyList();
-
- public override object first()
- {
- return _first;
- }
-
- public override ISequence rest()
- {
- return _rest;
- }
-
- public override ISequence cons(object first)
- {
- return new PersistentList(null, first, this);
- }
-
- public override XronosObject withMeta(IPersistentMap meta)
- {
- return new PersistentList(meta, _first, _rest);
- }
-
- #region IPersistentStack Members
-
- public object peek()
- {
- return first();
- }
-
- public IPersistentStack pop()
- {
- if (_rest != null)
- return (IPersistentList)((PersistentList)_rest).withMeta(meta());
- return null;
- }
-
- #endregion
-
- #region IPersistentCollection Members
-
- public int count()
- {
- return _count;
- }
-
- public ISequence seq()
- {
- if (_count == 0)
- return null;
- return this;
- }
-
- IPersistentCollection IPersistentCollection.cons(object o)
- {
- return new PersistentList(null, o, this);
- }
-
- public IPersistentCollection empty()
- {
- return Empty;
- }
-
- #endregion
-
- #region IReduce Members
-
- public object reduce(object f)
- {
- var ret = first();
- for (var seq = rest(); seq != null; seq = seq.rest())
- ret = FunctionHelper.Invoke<object>(f, ret, seq.first());
- return ret;
- }
-
- public object reduce(object f, object start)
- {
- var ret = start;
- for (ISequence seq = this; seq != null; seq = seq.rest())
- ret = FunctionHelper.Invoke<object>(f, ret, seq.first());
- return ret;
- }
-
- #endregion
-
- private class EmptyList : IPersistentList, IReduce, ICollection, ISequence
- {
- #region IPersistentStack Members
-
- public object peek()
- {
- return null;
- }
-
- public IPersistentStack pop()
- {
- return null;
- }
-
- #endregion
-
- #region IPersistentCollection Members
-
- public int count()
- {
- return 0;
- }
-
- public ISequence seq()
- {
- return null;
- }
-
- public IPersistentCollection cons(object o)
- {
- return new PersistentList(null, o, null);
- }
-
- public IPersistentCollection empty()
- {
- return this;
- }
-
- #endregion
-
- #region IReduce Members
-
- public object reduce(object f)
- {
- return FunctionHelper.Invoke<object>(f);
- }
-
- public object reduce(object f, object start)
- {
- return start;
- }
-
- #endregion
-
- #region ICollection Members
-
- public void CopyTo(Array array, int index)
- {
- return;
- }
-
- public int Count
- {
- get { return 0; }
- }
-
- public bool IsSynchronized
- {
- get { return false; }
- }
-
- public object SyncRoot
- {
- get { return this; }
- }
-
- #endregion
-
- #region IEnumerable Members
-
- public IEnumerator GetEnumerator()
- {
- return new object[0].GetEnumerator();
- }
-
- #endregion
-
- #region ISequence Members
-
- public object first()
- {
- return null;
- }
-
- public ISequence rest()
- {
- return null;
- }
-
- ISequence ISequence.cons(object first)
- {
- return (ISequence)this.cons(first);
- }
-
- #endregion
- }
- }
- }