Skip to content

Commit

Permalink
Updated to Java SVN rev 1377: first cut of chunked seq
Browse files Browse the repository at this point in the history
  • Loading branch information
David Miller committed Jun 26, 2009
1 parent ec26584 commit f35ce2f
Show file tree
Hide file tree
Showing 9 changed files with 372 additions and 21 deletions.
4 changes: 4 additions & 0 deletions Clojure/Clojure/Clojure.csproj
Expand Up @@ -99,11 +99,15 @@
<Compile Include="CljCompiler\Ast\VarExpr.cs" />
<Compile Include="CljCompiler\Ast\VectorExpr.cs" />
<Compile Include="CljCompiler\Ast\StringExpr.cs" />
<Compile Include="Lib\ArrayChunk.cs" />
<Compile Include="Lib\ArrayHelper.cs" />
<Compile Include="Lib\BigInteger.cs" />
<Compile Include="Lib\ChunkedCons.cs" />
<Compile Include="Lib\Counted.cs" />
<Compile Include="Lib\EnumeratorSeq.cs" />
<Compile Include="Lib\IChunkedSeq.cs" />
<Compile Include="Lib\IDeref.cs" />
<Compile Include="Lib\Indexed.cs" />
<Compile Include="Lib\LazySeq.cs" />
<Compile Include="Lib\Seqable.cs" />
<Compile Include="Lib\Stream.cs" />
Expand Down
55 changes: 55 additions & 0 deletions Clojure/Clojure/Lib/ArrayChunk.cs
@@ -0,0 +1,55 @@
/**
* Copyright (c) David Miller. All rights reserved.
* The use and distribution terms for this software are covered by the
* Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
* which can be found in the file epl-v10.html 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.Generic;
using System.Linq;
using System.Text;

namespace clojure.lang
{
public class ArrayChunk : Indexed
{
#region Data

readonly object[] _array;
readonly int _off;

#endregion

#region C-tors

public ArrayChunk(object[] array, int off)
{
_array = array;
_off = off;
}

#endregion

#region Indexed Members

public object nth(int i)
{
return _array[_off + i];
}

#endregion

#region Counted Members

public int count()
{
return _array.Length - _off;
}

#endregion
}
}
110 changes: 110 additions & 0 deletions Clojure/Clojure/Lib/ChunkedCons.cs
@@ -0,0 +1,110 @@
/**
* Copyright (c) David Miller. All rights reserved.
* The use and distribution terms for this software are covered by the
* Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
* which can be found in the file epl-v10.html 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.Generic;
using System.Linq;
using System.Text;

namespace clojure.lang
{
public class ChunkedCons : ASeq, IChunkedSeq
{
#region Data

readonly Indexed _chunk;
readonly ISeq _more;
readonly int _offset;

#endregion

#region C-tors

ChunkedCons(IPersistentMap meta, Indexed chunk, int offset, ISeq more)
: base(meta)
{
_chunk = chunk;
_offset = offset;
_more = more;
}

public ChunkedCons(Indexed chunk, ISeq more)
: this(chunk, 0, more)
{
}

public ChunkedCons(Indexed chunk, int offset, ISeq more)
{
_chunk = chunk;
_offset = offset;
_more = more;
}

#endregion

#region IObj methods

public override IObj withMeta(IPersistentMap meta)
{
return (meta == _meta)
? this
:new ChunkedCons(meta, _chunk, _offset, _more);
}

#endregion

#region ISeq methods

public override object first()
{
return _chunk.nth(_offset);
}

public override ISeq next()
{
if (_offset + 1 < _chunk.count())
return new ChunkedCons(_chunk, _offset + 1, _more);
return chunkedNext();
}

#endregion

#region IChunkedSeq Members

public Indexed chunkedFirst()
{
return _chunk;
}

public ISeq chunkedNext()
{
return chunkedMore().seq();
}

public ISeq chunkedMore()
{
if (_more == null)
return PersistentList.EMPTY;
return _more;
}

#endregion

#region IPersistentCollection Members


//public new IPersistentCollection cons(object o)
//{
// throw new NotImplementedException();
//}

#endregion
}
}
24 changes: 24 additions & 0 deletions Clojure/Clojure/Lib/IChunkedSeq.cs
@@ -0,0 +1,24 @@
/**
* Copyright (c) David Miller. All rights reserved.
* The use and distribution terms for this software are covered by the
* Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
* which can be found in the file epl-v10.html 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.Generic;
using System.Linq;
using System.Text;

namespace clojure.lang
{
public interface IChunkedSeq : ISeq
{
Indexed chunkedFirst();
ISeq chunkedNext();
ISeq chunkedMore();
}
}
10 changes: 1 addition & 9 deletions Clojure/Clojure/Lib/IPersistentVector.cs
Expand Up @@ -18,7 +18,7 @@ namespace clojure.lang
/// <summary>
/// Represents an immutable vector (int-indexing).
/// </summary>
public interface IPersistentVector: Associative, Sequential, IPersistentStack, Reversible, Counted
public interface IPersistentVector: Associative, Sequential, IPersistentStack, Reversible, Indexed
{
/// <summary>
/// Gets the number of items in the vector.
Expand All @@ -27,14 +27,6 @@ public interface IPersistentVector: Associative, Sequential, IPersistentStack, R
/// <remarks>Not sure why you wouldn't use <c>count()</c> intead.</remarks>
int length();

/// <summary>
/// Get the i-th item in the vector.
/// </summary>
/// <param name="i">The index of the item to retrieve/</param>
/// <returns>The i-th item</returns>
/// <remarks>Throws an exception if the index <c>i</c> is not in the range of the vector's elements.</remarks>
object nth(int i);

/// <summary>
/// Return a new vector with the i-th value set to <c>val</c>.
/// </summary>
Expand Down
27 changes: 27 additions & 0 deletions Clojure/Clojure/Lib/Indexed.cs
@@ -0,0 +1,27 @@
/**
* Copyright (c) David Miller. All rights reserved.
* The use and distribution terms for this software are covered by the
* Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
* which can be found in the file epl-v10.html 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.Generic;
using System.Linq;
using System.Text;

namespace clojure.lang
{
public interface Indexed : Counted
{
/// <summary>
/// Returns the n-th item.
/// </summary>
/// <param name="i">Index of the item to access</param>
/// <returns>The item at the given index</returns>
object nth(int i);
}
}
23 changes: 21 additions & 2 deletions Clojure/Clojure/Lib/LazySeq.cs
Expand Up @@ -22,6 +22,7 @@ public sealed class LazySeq : Obj, ISeq, ICollection, IList // Should we do ILi
#region Data

private IFn _fn;
private object _sv;
private ISeq _s;

#endregion
Expand Down Expand Up @@ -77,12 +78,30 @@ public override IObj withMeta(IPersistentMap meta)
/// <returns>An <see cref="ISeq"/> for iteration.</returns>
[MethodImpl(MethodImplOptions.Synchronized)]
public ISeq seq()
{
sval();
if (_sv != null)
{
object ls = _sv;
_sv = null;
while (ls is LazySeq)
ls = ((LazySeq)ls).sval();
_s = RT.seq(ls);
}
return _s;
}

[MethodImpl(MethodImplOptions.Synchronized)]
object sval()
{
if (_fn != null)
{
_s = RT.seq(_fn.invoke());
_fn = null;
_sv = _fn.invoke();
_fn = null;
}
if ( _sv != null )
return _sv;

return _s;
}

Expand Down

0 comments on commit f35ce2f

Please sign in to comment.