Skip to content

Commit

Permalink
VarArrayを実装 refs #2
Browse files Browse the repository at this point in the history
  • Loading branch information
bleis-tift committed Oct 8, 2013
1 parent a54acd0 commit 37ebdab
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
47 changes: 47 additions & 0 deletions SeqExt/IEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,51 @@ namespace SeqExt
/// シーケンスに対する反復処理をサポートします。
/// </summary>
public interface IEnumerator<out T> : System.Collections.Generic.IEnumerator<T> { }

/// <summary>
/// 既存のSystem.Collections.Generic.IEnumeratorをラップしたSeqExt.IEnumerator実装です。
/// </summary>
public sealed class Enumerator<T> : IEnumerator<T>
{
readonly System.Collections.Generic.IEnumerator<T> value;
/// <summary>
/// オブジェクトを生成します。
/// </summary>
public Enumerator(System.Collections.Generic.IEnumerator<T> value)
{
this.value = value;
}

/// <summary>
/// ラップされたIEnumeratorのCurrentを呼び出します。
/// </summary>
public T Current { get { return this.value.Current; } }

object System.Collections.IEnumerator.Current { get { return this.value.Current; } }

/// <summary>
/// ラップされたIEnumeratorのMoveNextを呼び出します。
/// </summary>
public bool MoveNext()
{
return this.value.MoveNext();
}

/// <summary>
/// ラップされたIEnumeratorのResetを呼び出します。
/// </summary>
public void Reset()
{
this.value.Reset();
}

/// <summary>
/// ラップされたIEnumeratorのDisposeを呼び出します。
/// </summary>
public void Dispose()
{
this.value.Dispose();
}
}

}
79 changes: 79 additions & 0 deletions SeqExt/Mutable/VarArray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using LangExt;

namespace SeqExt.Mutable
{
public sealed class VarArray<T> : SeqBase<T>, RandomAccessSeq<T>
{
readonly System.Collections.Generic.List<T> value;

public VarArray()
{
this.value = new System.Collections.Generic.List<T>();
}

public VarArray(Seq<T> seq)
{
this.value = new System.Collections.Generic.List<T>(seq);
}

public int Size { get { return this.value.Count; } }

public void Add(T item) { this.value.Add(item); }

public void AddAll(Seq<T> seq) { this.value.AddRange(seq); }

public void Insert(int index, T item) { this.value.Insert(index, item); }

public void InsertAll(int index, Seq<T> seq) { this.value.InsertRange(index, seq); }

public bool RemoveFirst(T item) { return this.value.Remove(item); }

public int RemoveAll(Func<T, bool> pred) { return this.value.RemoveAll(x => pred(x)); }

public void RemoveAt(int index) { this.value.RemoveAt(index); }

public void RemoveRange(Range range)
{
if (range.Length != 0)
this.value.RemoveRange(range.Increasing ? range.Begin : range.Last, range.Length);
}

public void Clear() { this.value.Clear(); }

protected override IEnumerator<T> GetEnumeratorImpl()
{
return new Enumerator<T>(this.value.GetEnumerator());
}

public T this[int index]
{
get { return this.value[index]; }
set { this.value[index] = value; }
}

public Option<T> TryGet(int index)
{
if (index < this.value.Count && index >= 0)
return Option.Some(this.value[index]);
return Option.None;
}

public void SortThis() { this.value.Sort(); }

public void SortThisWith(System.Collections.Generic.IComparer<T> comparer)
{
this.value.Sort(comparer);
}

public void SortThisWith(Func<T, T, int> comparison)
{
this.value.Sort((a, b) => comparison(a, b));
}

public void ReverseThis()
{
this.value.Reverse();
}
}
}
10 changes: 10 additions & 0 deletions SeqExt/RandomAccessSeq.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using LangExt;

namespace SeqExt
{
public interface RandomAccessSeq<out T> : Seq<T>
{
T this[int index] { get; }
}
}
2 changes: 2 additions & 0 deletions SeqExt/SeqExt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
<Compile Include="ConsList.cs" />
<Compile Include="IEnumerator.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RandomAccessSeq.cs" />
<Compile Include="SeqBase.cs" />
<Compile Include="Mutable\VarArray.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="LangExt">
Expand Down

0 comments on commit 37ebdab

Please sign in to comment.