Skip to content

Commit

Permalink
Updated to Java commit f8856dc (2012.05.04): better vector iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
dmiller committed Jun 30, 2012
1 parent 356de6d commit 4671e8a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
18 changes: 16 additions & 2 deletions Clojure/Clojure/Lib/APersistentVector.cs
Expand Up @@ -503,7 +503,7 @@ public object SyncRoot

#region IEnumerable<Object>, IEnumerable Members

public IEnumerator<object> GetEnumerator()
public virtual IEnumerator<object> GetEnumerator()
{
for (ISeq s = seq(); s != null; s = s.next())
yield return s.first();
Expand Down Expand Up @@ -861,7 +861,7 @@ public object reduce(IFn f, object start)
/// Internal class providing subvector functionality for <see cref="APersistentVector">APersistentVector</see>.
/// </summary>
[Serializable]
public sealed class SubVector : APersistentVector, IPersistentCollection, IObj
public sealed class SubVector : APersistentVector, IPersistentCollection, IObj, IEnumerable
{
#region Data

Expand Down Expand Up @@ -1035,6 +1035,20 @@ public override IPersistentStack pop()
}

#endregion

#region IEnumerable members

IEnumerator IEnumerable.GetEnumerator()
{
return ((PersistentVector)_v).RangedIterator(_start, _end);
}

public override IEnumerator<object> GetEnumerator()
{
return ((PersistentVector)_v).RangedIteratorT(_start, _end);
}

#endregion
}

}
Expand Down
52 changes: 51 additions & 1 deletion Clojure/Clojure/Lib/PersistentVector.cs
Expand Up @@ -15,6 +15,7 @@
using System;
using System.Collections;
using System.Threading;
using System.Collections.Generic;

namespace clojure.lang
{
Expand All @@ -23,7 +24,7 @@ namespace clojure.lang
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1708:IdentifiersShouldDifferByMoreThanCase")]
[Serializable]
public class PersistentVector: APersistentVector, IObj, IEditableCollection
public class PersistentVector: APersistentVector, IObj, IEditableCollection, IEnumerable
{
#region Node class

Expand Down Expand Up @@ -953,5 +954,54 @@ public object kvreduce(IFn f, object init)
}

#endregion

#region Ranged iterator

public IEnumerator RangedIterator(int start, int end)
{
int i = start;
int b = i - (i%32);
object[] arr = (start < count()) ? ArrayFor(i) : null;

while (i < end)
{
if (i - b == 32)
{
arr = ArrayFor(i);
b += 32;
}
yield return arr[i++ & 0x01f];
}
}

public IEnumerator<object> RangedIteratorT(int start, int end)
{
int i = start;
int b = i - (i % 32);
object[] arr = (start < count()) ? ArrayFor(i) : null;

while (i < end)
{
if (i - b == 32)
{
arr = ArrayFor(i);
b += 32;
}
yield return arr[i++ & 0x01f];
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return RangedIterator(0, count());
}

public override IEnumerator<object> GetEnumerator()
{
return RangedIteratorT(0, count());

}

#endregion
}
}

0 comments on commit 4671e8a

Please sign in to comment.