Initial Commit

This commit is contained in:
2023-06-21 12:46:23 -04:00
commit c70248a520
1352 changed files with 336780 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
using System;
namespace System.util.collections
{
/// <summary>
/// Very basic algorithms tool class.
/// </summary>
public class k_Algorithm
{
public static k_Iterator Copy(k_Iterator ak_SrcFirst, k_Iterator ak_BehindSrcLast, k_Iterator ak_DstFirst)
{
k_Iterator lk_Src = ak_SrcFirst.Clone(), lk_Dst = ak_DstFirst.Clone();
while (lk_Src != ak_BehindSrcLast)
{
lk_Dst.Current = lk_Src.Current;
lk_Src.Next(); lk_Dst.Next();
}
return lk_Dst;
}
public static k_Iterator CopyBackward(k_Iterator ak_SrcFirst, k_Iterator ak_BehindSrcLast, k_Iterator ak_BehindDstLast)
{
k_Iterator lk_Src = ak_BehindSrcLast.Clone(), lk_Dst = ak_BehindDstLast.Clone();
while (lk_Src != ak_SrcFirst)
{
lk_Src.Prev(); lk_Dst.Prev();
lk_Dst.Current = lk_Src.Current;
}
return lk_Dst;
}
public static void Fill(k_Iterator ak_DstFirst, k_Iterator ak_BehindDstLast, object ak_Value)
{
for (k_Iterator lk_Iter = ak_DstFirst.Clone(); lk_Iter != ak_BehindDstLast; lk_Iter.Next())
lk_Iter.Current = ak_Value;
}
public static k_Iterator Find(k_Iterator ak_First, k_Iterator ak_Last, object ak_Value)
{
k_Iterator lk_Iter = ak_First.Clone();
for (; lk_Iter != ak_Last; lk_Iter.Next())
{
if (object.Equals(lk_Iter.Current, ak_Value))
break;
}
return lk_Iter;
}
}
}

View File

@@ -0,0 +1,64 @@
using System;
using System.Collections;
namespace System.util.collections
{
/// <summary>
/// Base interface for all containers
/// </summary>
public interface IContainer : ICollection, ICloneable
{
k_Iterator Begin { get; }
k_Iterator End { get; }
bool IsEmpty { get; }
k_Iterator Find(object ak_Value);
k_Iterator Erase(k_Iterator ak_Where);
k_Iterator Erase(k_Iterator ak_First, k_Iterator ak_Last);
}
/// <summary>
/// Interface for non-associative sequential containers (k_Vector, k_Deque, k_List)
/// </summary>
public interface ISequence : IContainer, IList
{
object Front { get; set; }
object Back { get; set; }
void PushFront(object ak_Value);
void PopFront();
void PushBack(object ak_Value);
void PopBack();
void Assign(k_Iterator ak_SrcBegin, k_Iterator ak_SrcEnd);
void Assign(object ak_Value, int ai_Count);
void Insert(k_Iterator ak_Where, object ak_Value);
void Insert(k_Iterator ak_Where, k_Iterator ak_SrcBegin, k_Iterator ak_SrcEnd);
}
/// <summary>
/// Interface for IDictionary derived containers which provide key to value mapping (k_HashTable)
/// </summary>
public interface IMap : IContainer, IDictionary
{
k_Iterator FindKey(object ak_Key);
void Add(DictionaryEntry ar_Item);
void Insert(k_Iterator ak_SrcBegin, k_Iterator ak_SrcEnd);
}
/// <summary>
/// Interface for sorted mapping containers (k_SkipList, k_Tree)
/// </summary>
public interface ISortedMap : IMap
{
IComparer Comparer { get; }
k_Iterator LowerBound(object ak_Key);
k_Iterator UpperBound(object ak_Key);
}
}

View File

@@ -0,0 +1,470 @@
using System;
using System.Collections;
namespace System.util.collections
{
/// <summary>
/// Circular buffer of arrays
/// </summary>
public class k_Deque : ISequence
{
#region k_BlockIterator Implementation
private class k_BlockIterator : k_Iterator
{
private readonly k_Deque mk_Deque;
private int mi_Index;
private int mi_BlockIndex;
private int mi_BlockOffset;
public k_BlockIterator(k_Deque ak_Deque, int ai_Index)
{
mk_Deque = ak_Deque;
mi_Index = ai_Index;
mi_BlockIndex = mk_Deque.CalcBlockAndPos(mi_Index, out mi_BlockOffset);
}
public override void Move(int ai_Count)
{
int li_Index = mi_Index + ai_Count;
if (li_Index > mk_Deque.Count)
throw new InvalidOperationException("Tried to move beyond end element.");
else if (li_Index < 0)
throw new InvalidOperationException("Tried to move before first element.");
mi_Index = li_Index;
mi_BlockOffset += ai_Count;
if (mi_BlockOffset >= k_Deque.mi_BlockSize || mi_BlockOffset < 0)
mi_BlockIndex = mk_Deque.CalcBlockAndPos(mi_Index, out mi_BlockOffset);
}
public override int Distance(k_Iterator ak_Iter)
{
return mi_Index - ((k_BlockIterator)ak_Iter).mi_Index;
}
public override object Collection
{
get { return mk_Deque; }
}
public override object Current
{
get
{
if (mi_Index < 0 || mi_Index >= mk_Deque.mi_Count)
throw new k_InvalidPositionException();
return mk_Deque.mk_Blocks[mi_BlockIndex][mi_BlockOffset];
}
set
{
if (mi_Index < 0 || mi_Index >= mk_Deque.mi_Count)
throw new k_InvalidPositionException();
mk_Deque.mk_Blocks[mi_BlockIndex][mi_BlockOffset] = value;
}
}
public override bool Equals(object ak_Obj)
{
k_BlockIterator lk_Iter = ak_Obj as k_BlockIterator;
if (lk_Iter == null)
return false;
return (mi_Index == lk_Iter.mi_Index) && object.ReferenceEquals(this.Collection, lk_Iter.Collection);
}
public override int GetHashCode()
{
return mk_Deque.GetHashCode() ^ mi_Index;
}
public override k_Iterator Clone()
{
return new k_BlockIterator(mk_Deque, mi_Index);
}
internal int Index
{
get { return mi_Index; }
}
}
private class k_PinnedBlockIterator : k_BlockIterator
{
public k_PinnedBlockIterator(k_Deque ak_Deque, int ai_Index)
: base(ak_Deque, ai_Index)
{
}
public override void Move(int ai_Count)
{
throw new k_IteratorPinnedException();
}
}
#endregion
private const int mi_BlockSize = 16;
private object[][] mk_Blocks;
private int mi_Offset;
private int mi_Count;
public k_Deque()
: this(mi_BlockSize)
{
}
public k_Deque(int ai_Capacity)
{
if (ai_Capacity < 0)
throw new ArgumentException("Capacity must be positive.", "ai_Capacity");
mk_Blocks = new object[(ai_Capacity+mi_BlockSize-1)/mi_BlockSize][];
for (int i=0; i<mk_Blocks.Length; ++i)
mk_Blocks[i] = new object[mi_BlockSize];
}
// IContainer Members
public k_Iterator Begin
{
get { return new k_PinnedBlockIterator(this, 0); }
}
public k_Iterator End
{
get { return new k_PinnedBlockIterator(this, mi_Count); }
}
public bool IsEmpty
{
get { return (this.Count == 0); }
}
public k_Iterator Find(object ak_Value)
{
return k_Algorithm.Find(this.Begin, this.End, ak_Value);
}
public k_Iterator Erase(k_Iterator ak_Where)
{
return Erase(ak_Where, ak_Where + 1);
}
public k_Iterator Erase(k_Iterator ak_First, k_Iterator ak_Last)
{
if (ak_First == ak_Last)
return ak_Last;
int li_FirstIndex = ((k_BlockIterator)ak_First).Index;
int li_Count = ak_Last - ak_First;
int li_LastCount = this.End - ak_Last;
if (li_FirstIndex < li_LastCount)
{
k_Algorithm.CopyBackward(this.Begin, ak_First, ak_Last);
k_Algorithm.Fill(this.Begin, ak_First, null);
mi_Offset += li_Count;
mi_Offset %= (mk_Blocks.Length * mi_BlockSize);
}
else
{
k_Algorithm.Copy(ak_Last, this.End, ak_First);
k_Algorithm.Fill(ak_Last, this.End, null);
}
mi_Count -= li_Count;
return new k_BlockIterator(this, li_FirstIndex);
}
// ISequence Members
public object Front
{
get { return this.Begin.Current; }
set { this.Begin.Current = value; }
}
public object Back
{
get { return (this.End-1).Current; }
set { (this.End-1).Current = value; }
}
public void PushFront(object ak_Value)
{
if (mi_Offset % mi_BlockSize == 0 // currently on block boundary
&& mk_Blocks.Length * mi_BlockSize - mi_Count < mi_BlockSize)
{
AllocateBlock(mi_BlockSize);
}
if (mi_Offset == 0)
mi_Offset = mk_Blocks.Length * mi_BlockSize;
--mi_Offset;
mk_Blocks[mi_Offset/mi_BlockSize][mi_Offset%mi_BlockSize] = ak_Value;
++mi_Count;
}
public void PopFront()
{
Erase(this.Begin);
}
public void PushBack(object ak_Value)
{
if ((mi_Offset+mi_Count) % mi_BlockSize == 0 // currently on block boundary
&& mk_Blocks.Length * mi_BlockSize - mi_Count < mi_BlockSize)
{
AllocateBlock(mi_BlockSize);
}
int li_Pos = mi_Offset + mi_Count;
int li_Block = li_Pos/mi_BlockSize;
if (li_Block >= mk_Blocks.Length)
li_Block -= mk_Blocks.Length;
mk_Blocks[li_Block][li_Pos%mi_BlockSize] = ak_Value;
++mi_Count;
}
public void PopBack()
{
Erase(this.End-1);
}
public void Assign(k_Iterator ak_SrcBegin, k_Iterator ak_SrcEnd)
{
Clear();
Insert(this.End, ak_SrcBegin, ak_SrcEnd);
}
public void Assign(object ak_Value, int ai_Count)
{
Clear();
for (int i = 0; i < ai_Count; ++i)
Insert(this.End, ak_Value);
}
public void Insert(k_Iterator ak_Where, object ak_Value)
{
if (ak_Where == this.Begin)
PushFront(ak_Value);
else if (ak_Where == this.End)
PushBack(ak_Value);
else
{
int li_Index = ((k_BlockIterator)ak_Where).Index;
if (mk_Blocks.Length * mi_BlockSize - mi_Count < mi_BlockSize)
AllocateBlock(mi_BlockSize);
++mi_Count;
if (li_Index < mi_Count/2)
{
if (mi_Offset == 0)
mi_Offset = mk_Blocks.Length * mi_BlockSize;
--mi_Offset;
k_Iterator lk_Dest = k_Algorithm.Copy(this.Begin+1, this.Begin+li_Index+1, this.Begin);
lk_Dest.Current = ak_Value;
}
else
{
// count has been incremented - there is a free element at the end
k_Iterator lk_Dest = this.Begin + li_Index;
k_Algorithm.CopyBackward(lk_Dest, this.End - 1, this.End);
lk_Dest.Current = ak_Value;
}
}
}
public void Insert(k_Iterator ak_Where, k_Iterator ak_SrcBegin, k_Iterator ak_SrcEnd)
{
int li_FirstIndex = ((k_BlockIterator)ak_Where).Index;
int li_Count = ak_SrcEnd - ak_SrcBegin;
if (mk_Blocks.Length * mi_BlockSize <= mi_Count + li_Count + mi_BlockSize)
AllocateBlock(li_Count);
mi_Count += li_Count;
k_Iterator lk_Dest;
if (li_FirstIndex < li_Count/2)
{
if (mi_Offset == 0)
mi_Offset = mk_Blocks.Length * mi_BlockSize;
mi_Offset -= li_Count;
lk_Dest = k_Algorithm.Copy(this.Begin+li_Count, this.Begin+li_FirstIndex+li_Count, this.Begin);
}
else
{
// count has been incremented - there are li_Count free elements at the end
lk_Dest = this.Begin + li_FirstIndex;
k_Algorithm.CopyBackward(lk_Dest, this.End - li_Count, this.End);
}
k_Algorithm.Copy(ak_SrcBegin, ak_SrcEnd, lk_Dest);
}
#region IList Members
public int Add(object ak_Value)
{
PushBack(ak_Value);
return mi_Count;
}
public void Clear()
{
for (int i=0; i<mk_Blocks.Length; ++i)
mk_Blocks[i] = new object[mi_BlockSize];
mi_Count = 0;
mi_Offset = 0;
}
public bool Contains(object ak_Value)
{
return (Find(ak_Value) != this.End);
}
public int IndexOf(object ak_Value)
{
k_Iterator lk_Found = Find(ak_Value);
if (lk_Found == this.End)
return -1;
return ((k_BlockIterator)lk_Found).Index;
}
public void Insert(int ai_Index, object ak_Value)
{
Insert(this.Begin + ai_Index, ak_Value);
}
public void Remove(object ak_Value)
{
Erase(Find(ak_Value));
}
public void RemoveAt(int ai_Index)
{
Erase(this.Begin + ai_Index);
}
public bool IsReadOnly
{
get { return false; }
}
public bool IsFixedSize
{
get { return false; }
}
public object this[int ai_Index]
{
get
{
if (ai_Index >= mi_Count || ai_Index < 0)
throw new ArgumentOutOfRangeException("Position out of boundary");
int li_Pos, li_Block = CalcBlockAndPos(ai_Index, out li_Pos);
return mk_Blocks[li_Block][li_Pos];
}
set
{
if (ai_Index >= mi_Count || ai_Index < 0)
throw new ArgumentOutOfRangeException("Position out of boundary");
int li_Pos, li_Block = CalcBlockAndPos(ai_Index, out li_Pos);
mk_Blocks[li_Block][li_Pos] = value;
}
}
#endregion
#region ICollection Members
public void CopyTo(Array ak_Array, int ai_Index)
{
for (k_Iterator lk_Iter = this.Begin.Clone(); lk_Iter != this.End; lk_Iter.Next())
ak_Array.SetValue(lk_Iter.Current, ai_Index++);
}
public int Count
{
get { return mi_Count; }
}
public bool IsSynchronized
{
get { return false; }
}
public object SyncRoot
{
get { return this; }
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return new k_IteratorEnumerator(this.Begin, this.End);
}
#endregion
#region ICloneable Members
public object Clone()
{
k_Deque lk_Clone = new k_Deque(this.Count);
lk_Clone.Insert(lk_Clone.End, this.Begin, this.End);
return lk_Clone;
}
#endregion
private void AllocateBlock(int ai_MinElements)
{
// number of new blocks - grow by half block count (150%)
int li_Increment = mk_Blocks.Length / 2;
if (ai_MinElements > li_Increment*mi_BlockSize)
li_Increment = (ai_MinElements + mi_BlockSize - 1)/mi_BlockSize;
object[][] lk_NewBlocks = new object[mk_Blocks.Length + li_Increment][];
// first move all blocks after offset to front
int li_StartBlock = mi_Offset / mi_BlockSize;
int li_BackCount = mk_Blocks.Length - li_StartBlock;
Array.Copy(mk_Blocks, li_StartBlock, lk_NewBlocks, 0, li_BackCount);
int li_TotalOld = li_BackCount;
// second move all blocks before offset to end
int li_FrontCount = (mi_Offset + mi_Count + mi_BlockSize - 1) / mi_BlockSize - mk_Blocks.Length;
if (li_FrontCount > 0)
{
Array.Copy(mk_Blocks, 0, lk_NewBlocks, li_BackCount, li_FrontCount);
li_TotalOld += li_FrontCount;
}
// actually create new empty blocks
for (int i=li_TotalOld; i < li_TotalOld+li_Increment; ++i)
lk_NewBlocks[i] = new object[mi_BlockSize];
mk_Blocks = lk_NewBlocks;
mi_Offset %= mi_BlockSize;
}
private int CalcBlockAndPos(int ai_Index, out int ai_Pos)
{
ai_Pos = mi_Offset + ai_Index;
int li_BlockIndex = ai_Pos / mi_BlockSize;
if (li_BlockIndex >= mk_Blocks.Length)
li_BlockIndex -= mk_Blocks.Length;
ai_Pos %= mi_BlockSize;
return li_BlockIndex;
}
}
}

View File

@@ -0,0 +1,658 @@
using System;
using System.Collections;
namespace System.util.collections
{
/// <summary>
/// A HashTable with iterators
/// </summary>
public class k_HashTable : IMap
{
#region static helper functions
private readonly static int[] mk_Primes =
{
11, 17, 23, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197, 239, 293, 353, 431, 521, 631, 761, 919,
1103, 1327, 1597, 1931, 2333, 2801, 3371, 4049, 4861, 5839, 7013, 8419, 10103, 12143, 14591,
17519, 21023, 25229, 30293, 36353, 43627, 52361, 62851, 75431, 90523, 108631, 130363,
156437, 187751, 225307, 270371, 324449, 389357, 467237, 560689, 672827, 807403,
968897, 1162687, 1395263, 1674319, 2009191, 2411033, 2893249, 3471899, 4166287,
4999559, 5999471, 7199369
};
private static bool IsPrime(int ai_Number)
{
if ((ai_Number & 1) == 0)
return (ai_Number == 2);
int li_Max = (int)Math.Sqrt(ai_Number);
for (int li_Div=3; li_Div < li_Max; li_Div+=2)
{
if ((ai_Number % li_Div) == 0)
return false;
}
return true;
}
private static int FindPrimeGreater(int ai_Min)
{
if (ai_Min < 0)
throw new ArgumentException("k_HashTable capacity overflow.");
// do binary search lookup in primes array
int li_Pos = Array.BinarySearch(mk_Primes, ai_Min);
if (li_Pos >= 0)
return mk_Primes[li_Pos];
li_Pos = ~li_Pos;
if (li_Pos < mk_Primes.Length)
return mk_Primes[li_Pos];
// ai_Min is greater than highest number in mk_Primes
for (int i = (ai_Min|1); i <= Int32.MaxValue; i+=2)
{
if (IsPrime(i))
return i;
}
return ai_Min;
}
#endregion
#region Bucket Structure
private struct r_Bucket
{
public object mk_Key;
public object mk_Value;
public int mi_HashCode; // MSB (sign bit) indicates a collision.
}
#endregion
#region k_BucketIterator Implementation
private class k_BucketIterator : k_Iterator
{
private readonly k_HashTable mk_Table;
private int mi_Index;
public k_BucketIterator(k_HashTable ak_Table, int ai_Index)
{
mk_Table = ak_Table;
mi_Index = -1;
if (ai_Index >= 0)
mi_Index = FindNext(ai_Index-1);
}
public override object Current
{
get
{
if (mi_Index < 0 || mk_Table.mk_Buckets[mi_Index].mk_Key == null)
throw new k_InvalidPositionException();
r_Bucket lr_Bucket = mk_Table.mk_Buckets[mi_Index];
return new DictionaryEntry(lr_Bucket.mk_Key, lr_Bucket.mk_Value);
}
set
{
if (mi_Index < 0 || mk_Table.mk_Buckets[mi_Index].mk_Key == null)
throw new k_InvalidPositionException();
DictionaryEntry lr_Entry = (DictionaryEntry)value;
r_Bucket lr_Bucket = mk_Table.mk_Buckets[mi_Index];
if (mk_Table.mk_Comparer.Compare(lr_Entry.Key, lr_Bucket.mk_Key) != 0)
throw new ArgumentException("Key values must not be changed.");
mk_Table.mk_Buckets[mi_Index].mk_Value = lr_Entry.Value;
}
}
public override void Move(int ai_Count)
{
int li_NewIndex = mi_Index;
if (ai_Count > 0)
{
while (ai_Count-- > 0)
{
if (li_NewIndex < 0)
throw new InvalidOperationException("Tried to moved beyond end element.");
li_NewIndex = FindNext(li_NewIndex);
}
}
else
{
while (ai_Count++ < 0)
{
if (li_NewIndex < 0)
li_NewIndex = FindPrev(mk_Table.mk_Buckets.Length);
else
li_NewIndex = FindPrev(li_NewIndex);
if (li_NewIndex < 0)
throw new InvalidOperationException("Tried to move before first element.");
}
}
mi_Index = li_NewIndex;
}
public override int Distance(k_Iterator ak_Iter)
{
k_BucketIterator lk_Iter = ak_Iter as k_BucketIterator;
if (lk_Iter == null || !object.ReferenceEquals(lk_Iter.Collection, this.Collection))
throw new ArgumentException("Cannot determine distance of iterators belonging to different collections.");
k_Iterator lk_End = mk_Table.End;
int li_IndexDiff;
if (this != lk_End && ak_Iter != lk_End)
li_IndexDiff = mi_Index - lk_Iter.mi_Index;
else
li_IndexDiff = (this == lk_End) ? 1 : -1; // 1 is also fine when both are End
if (li_IndexDiff < 0)
{
int li_Diff = 0;
k_Iterator lk_Bck = this.Clone();
for (; lk_Bck != ak_Iter && lk_Bck != lk_End; lk_Bck.Next())
--li_Diff;
if (lk_Bck == ak_Iter)
return li_Diff;
}
else
{
int li_Diff = 0;
k_Iterator lk_Fwd = ak_Iter.Clone();
for (; lk_Fwd != this && lk_Fwd != lk_End; lk_Fwd.Next())
++li_Diff;
if (lk_Fwd == this)
return li_Diff;
}
throw new Exception("Inconsistent state. Concurrency?");
}
public override object Collection
{
get { return mk_Table; }
}
public override bool Equals(object ak_Obj)
{
k_BucketIterator lk_Iter = ak_Obj as k_BucketIterator;
if (lk_Iter == null)
return false;
return (mi_Index == lk_Iter.mi_Index && object.ReferenceEquals(mk_Table, lk_Iter.mk_Table));
}
public override int GetHashCode()
{
return mk_Table.GetHashCode() ^ mi_Index;
}
public override k_Iterator Clone()
{
return new k_BucketIterator(mk_Table, mi_Index);
}
private int FindPrev(int ai_Index)
{
--ai_Index;
r_Bucket[] lk_Buckets = mk_Table.mk_Buckets;
while (ai_Index >= 0 && lk_Buckets[ai_Index].mk_Key == null)
--ai_Index;
if (ai_Index < -1)
return -1;
return ai_Index;
}
private int FindNext(int ai_Index)
{
++ai_Index;
r_Bucket[] lk_Buckets = mk_Table.mk_Buckets;
while (ai_Index < lk_Buckets.Length && lk_Buckets[ai_Index].mk_Key == null)
++ai_Index;
if (ai_Index >= lk_Buckets.Length)
return -1;
return ai_Index;
}
internal int Index
{
get { return mi_Index; }
}
}
private class k_PinnedBucketIterator : k_BucketIterator
{
public k_PinnedBucketIterator(k_HashTable ak_Table, int ai_Index)
: base(ak_Table, ai_Index)
{
}
public override void Move(int ai_Count)
{
throw new k_IteratorPinnedException();
}
}
#endregion
private IHashCodeProvider mk_HashProvider;
private IComparer mk_Comparer;
private double md_LoadFactor;
private int mi_GrowSize;
private r_Bucket[] mk_Buckets;
private int mi_Count;
private readonly k_Iterator mk_End;
public k_HashTable()
: this(0, 0.72)
{
}
public k_HashTable(int ai_Capacity, double ad_LoadFactor)
: this(ai_Capacity, ad_LoadFactor, null, null)
{
}
public k_HashTable(int ai_Capacity, double ad_LoadFactor, IHashCodeProvider ak_HashProvider, IComparer ak_Comparer)
{
if (ad_LoadFactor <= .0 || ad_LoadFactor > 1.0)
throw new ArgumentException("Load factor must be greater than .0 and smaller or equal to 1.0", "ad_LoadFactor");
md_LoadFactor = ad_LoadFactor;
double ld_Size = ai_Capacity/ad_LoadFactor;
if (ld_Size > int.MaxValue)
throw new ArgumentException("k_HashTable overflow");
int li_TableSize = FindPrimeGreater((int)ld_Size);
mk_Buckets = new r_Bucket[li_TableSize];
mi_GrowSize = (md_LoadFactor < 1.0) ? (int)(md_LoadFactor * li_TableSize) : li_TableSize-1;
mk_HashProvider = ak_HashProvider;
mk_Comparer = ak_Comparer;
mk_End = new k_PinnedBucketIterator(this, -1);
}
// IContainer Members
public k_Iterator Begin
{
get
{
if (mi_Count == 0)
return mk_End;
return new k_PinnedBucketIterator(this, 0);
}
}
public k_Iterator End
{
get { return mk_End; }
}
public bool IsEmpty
{
get { return (mi_Count == 0); }
}
public k_Iterator Find(object ak_Value)
{
DictionaryEntry lr_Item = (DictionaryEntry)ak_Value;
int li_Index = FindBucket(lr_Item.Key);
if (li_Index < 0 || !object.Equals(mk_Buckets[li_Index].mk_Value, lr_Item.Value))
return this.End;
return new k_BucketIterator(this, li_Index);
}
public k_Iterator Erase(k_Iterator ak_Where)
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(this, ak_Where.Collection), "Iterator does not belong to this collection.");
k_Iterator lk_Successor = ak_Where + 1;
EmptyBucket(((k_BucketIterator)ak_Where).Index);
return lk_Successor;
}
public k_Iterator Erase(k_Iterator ak_First, k_Iterator ak_Last)
{
if (ak_First == this.Begin && ak_Last == this.End)
{
Clear();
return ak_Last.Clone();
}
k_Iterator lk_Current = ak_First;
while (lk_Current != ak_Last)
lk_Current = Erase(lk_Current);
return lk_Current;
}
// IMap Members
public k_Iterator FindKey(object ak_Key)
{
return new k_BucketIterator(this, FindBucket(ak_Key));
}
public void Add(DictionaryEntry ar_Item)
{
Add(ar_Item.Key, ar_Item.Value);
}
public void Insert(k_Iterator ak_SrcBegin, k_Iterator ak_SrcEnd)
{
for (k_Iterator lk_Iter = ak_SrcBegin.Clone(); lk_Iter != ak_SrcEnd; lk_Iter.Next())
Add((DictionaryEntry)lk_Iter.Current);
}
#region IDictionary Members
public void Add(object ak_Key, object ak_Value)
{
SetValue(ak_Key, ak_Value, true);
}
public void Clear()
{
if (mi_Count == 0)
return;
for (int i=0; i < mk_Buckets.Length; ++i)
mk_Buckets[i] = new r_Bucket();
mi_Count = 0;
}
public bool Contains(object ak_Key)
{
return (FindBucket(ak_Key) >= 0);
}
public void Remove(object ak_Key)
{
EmptyBucket(FindBucket(ak_Key));
}
public IDictionaryEnumerator GetEnumerator()
{
return new k_IteratorDictEnumerator(this.Begin, this.End);
}
public bool IsReadOnly
{
get { return false; }
}
public bool IsFixedSize
{
get { return false; }
}
public object this[object ak_Key]
{
get
{
int li_Index = FindBucket(ak_Key);
if (li_Index < 0)
return null;
return mk_Buckets[li_Index].mk_Value;
}
set
{
SetValue(ak_Key, value, false);
}
}
public ICollection Keys
{
get
{
int i = 0;
object[] lk_Keys = new object[mi_Count];
foreach (DictionaryEntry lr_Entry in this)
lk_Keys[i++] = lr_Entry.Key;
return lk_Keys;
}
}
public ICollection Values
{
get
{
int i=0;
object[] lk_Values = new object[mi_Count];
foreach (DictionaryEntry lr_Entry in this)
lk_Values[i++] = lr_Entry.Value;
return lk_Values;
}
}
#endregion
#region ICollection Members
public void CopyTo(Array ak_Array, int ai_Index)
{
foreach (DictionaryEntry lr_Entry in this)
ak_Array.SetValue(lr_Entry, ai_Index++);
}
public int Count
{
get { return mi_Count; }
}
public bool IsSynchronized
{
get { return false; }
}
public object SyncRoot
{
get { return this; }
}
#endregion
#region IEnumerable Members
IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return new k_IteratorEnumerator(this.Begin, this.End);
}
#endregion
#region ICloneable Members
public object Clone()
{
k_HashTable lk_Clone = new k_HashTable(this.Count, md_LoadFactor, mk_HashProvider, mk_Comparer);
int i = mk_Buckets.Length;
while (i-- > 0)
{
object lk_Key = mk_Buckets[i].mk_Key;
if (lk_Key != null)
lk_Clone[lk_Key] = mk_Buckets[i].mk_Value;
}
return lk_Clone;
}
#endregion
private void EmptyBucket(int ai_Index)
{
if (ai_Index < 0 || ai_Index >= mk_Buckets.Length)
return;
if (mk_Buckets[ai_Index].mk_Key == null)
throw new InvalidOperationException("Key was removed earlier.");
mk_Buckets[ai_Index].mi_HashCode &= unchecked((int)0x80000000);
mk_Buckets[ai_Index].mk_Key = null;
mk_Buckets[ai_Index].mk_Value = null;
--mi_Count;
}
private int FindBucket(object ak_Key)
{
if (ak_Key == null)
throw new ArgumentException("Key must not be null.", "ak_Key");
uint lui_BucketCount = (uint)mk_Buckets.Length;
uint lui_Increment;
uint lui_HashCode = ComputeHashAndStep(ak_Key, out lui_Increment);
uint lui_Walker = lui_HashCode % lui_BucketCount;
r_Bucket lr_Bucket;
do
{
int li_Index = (int)lui_Walker;
lr_Bucket = mk_Buckets[li_Index];
if (lr_Bucket.mk_Key == null && lr_Bucket.mi_HashCode >= 0)
break; // stop on empty non-duplicate
if ((lr_Bucket.mi_HashCode & 0x7fffffff) == lui_HashCode
&& EqualsHelper(lr_Bucket.mk_Key, ak_Key))
return li_Index;
lui_Walker += lui_Increment;
lui_Walker %= lui_BucketCount;
}
while (lr_Bucket.mi_HashCode < 0 && lui_Walker != lui_HashCode);
return -1; // not found
}
private void SetValue(object ak_Key, object ak_Value, bool ab_Add)
{
if (mi_Count >= mi_GrowSize)
ExpandBucketsArray();
uint lui_BucketCount = (uint)mk_Buckets.Length;
uint lui_Increment;
uint lui_HashCode = ComputeHashAndStep(ak_Key, out lui_Increment);
r_Bucket lr_Bucket;
int li_Free = -1;
uint lui_Walker = lui_HashCode % lui_BucketCount;
do
{
int li_Index = (int)lui_Walker;
lr_Bucket = mk_Buckets[li_Index];
if (li_Free < 0 && lr_Bucket.mk_Key == null && lr_Bucket.mi_HashCode < 0)
li_Free = li_Index;
if (lr_Bucket.mk_Key == null && (lr_Bucket.mi_HashCode & unchecked(0x80000000)) == 0)
{
if (li_Free >= 0)
li_Index = li_Free;
mk_Buckets[li_Index].mk_Key = ak_Key;
mk_Buckets[li_Index].mk_Value = ak_Value;
mk_Buckets[li_Index].mi_HashCode |= (int)lui_HashCode;
++mi_Count;
return;
}
if ((lr_Bucket.mi_HashCode & 0x7fffffff) == lui_HashCode
&& EqualsHelper(lr_Bucket.mk_Key, ak_Key))
{
if (ab_Add)
throw new ArgumentException("duplicate key");
mk_Buckets[li_Index].mk_Value = ak_Value;
return;
}
// mark all as dupes as long as we have not found a free bucket
if (li_Free == -1)
mk_Buckets[li_Index].mi_HashCode |= unchecked((int)0x80000000);
lui_Walker += lui_Increment;
lui_Walker %= lui_BucketCount;
}
while (lui_Walker != lui_HashCode);
if (li_Free == -1)
throw new InvalidOperationException("Corrupted hash table. Insert failed.");
mk_Buckets[li_Free].mk_Key = ak_Key;
mk_Buckets[li_Free].mk_Value = ak_Value;
mk_Buckets[li_Free].mi_HashCode |= (int)lui_HashCode;
++mi_Count;
}
private static void InternalExpandInsert(r_Bucket[] ak_Buckets, r_Bucket ar_Bucket)
{
ar_Bucket.mi_HashCode &= 0x7fffffff;
uint lui_BucketCount = (uint)ak_Buckets.Length;
uint lui_Increment = (uint)(1 + ((((uint)ar_Bucket.mi_HashCode >> 5) + 1) % (lui_BucketCount - 1)));
uint lui_Walker = (uint)ar_Bucket.mi_HashCode % lui_BucketCount;
for (;;)
{
int li_Index = (int)lui_Walker;
if (ak_Buckets[li_Index].mk_Key == null)
{
ak_Buckets[li_Index] = ar_Bucket;
return;
}
// since current bucket is occupied mark it as duplicate
ak_Buckets[li_Index].mi_HashCode |= unchecked((int)0x80000000);
lui_Walker += lui_Increment;
lui_Walker %= lui_BucketCount;
}
}
private void ExpandBucketsArray()
{
int li_NewSize = FindPrimeGreater(mk_Buckets.Length * 2);
r_Bucket[] lk_Buckets = new r_Bucket[li_NewSize];
foreach (r_Bucket lr_Bucket in mk_Buckets)
{
if (lr_Bucket.mk_Key == null)
continue;
InternalExpandInsert(lk_Buckets, lr_Bucket);
}
mk_Buckets = lk_Buckets;
mi_GrowSize = (md_LoadFactor < 1.0) ? (int)(md_LoadFactor * li_NewSize) : li_NewSize-1;
}
private uint ComputeHashAndStep(object ak_Key, out uint aui_Increment)
{
// mask the sign bit (our collision indicator)
uint lui_HashCode = (uint)GetHashHelper(ak_Key) & 0x7fffffff;
// calc increment value relatively prime to mk_Buckets.Length
aui_Increment = (uint)(1 + (((lui_HashCode >> 5) + 1) % ((uint)mk_Buckets.Length - 1)));
return lui_HashCode;
}
private int GetHashHelper(object ak_Key)
{
if (mk_HashProvider != null)
return mk_HashProvider.GetHashCode(ak_Key);
return ak_Key.GetHashCode();
}
private bool EqualsHelper(object ak_ObjA, object ak_ObjB)
{
if (mk_Comparer != null)
return (mk_Comparer.Compare(ak_ObjA, ak_ObjB) == 0);
return Object.Equals(ak_ObjA, ak_ObjB);
}
}
}

View File

@@ -0,0 +1,323 @@
using System;
using System.Collections;
namespace System.util.collections
{
public abstract class k_Iterator : IComparable, ICloneable
{
public static k_Iterator operator ++(k_Iterator ak_Iter)
{
return ak_Iter + 1;
}
public static k_Iterator operator --(k_Iterator ak_Iter)
{
return ak_Iter - 1;
}
public static k_Iterator operator +(k_Iterator ak_Iter, int ai_Distance)
{
k_Iterator lk_Clone = ak_Iter.Clone();
lk_Clone.Move(ai_Distance);
return lk_Clone;
}
public static k_Iterator operator -(k_Iterator ak_Iter, int ai_Distance)
{
k_Iterator lk_Clone = ak_Iter.Clone();
lk_Clone.Move(-ai_Distance);
return lk_Clone;
}
public static int operator -(k_Iterator ak_Left, k_Iterator ak_Right)
{
return ak_Left.Distance(ak_Right);
}
public static bool operator ==(k_Iterator ak_Left, k_Iterator ak_Right)
{
return object.Equals(ak_Left, ak_Right);
}
public static bool operator !=(k_Iterator ak_Left, k_Iterator ak_Right)
{
return !object.Equals(ak_Left, ak_Right);
}
public override bool Equals(object ak_Obj)
{
return base.Equals(ak_Obj); // implemented to avoid warning
}
public override int GetHashCode()
{
return base.GetHashCode(); // implemented to avoid warning
}
public int CompareTo(object ak_Obj)
{
k_Iterator lk_Iter = ak_Obj as k_Iterator;
if (lk_Iter == null || !object.ReferenceEquals(lk_Iter.Collection, this.Collection))
throw new ArgumentException("Cannot compare iterators of different origin.");
return Distance(lk_Iter);
}
object ICloneable.Clone()
{
return this.Clone();
}
public void Next()
{
Move(1);
}
public void Prev()
{
Move(-1);
}
public abstract object Current { get; set; }
public abstract object Collection { get; }
public abstract k_Iterator Clone();
public abstract void Move(int ai_Count);
public abstract int Distance(k_Iterator ak_Iter);
}
public class k_IteratorEnumerator : IEnumerator
{
protected k_Iterator mk_Current;
protected k_Iterator mk_Begin, mk_End;
protected bool mb_Fresh;
public k_IteratorEnumerator(k_Iterator ak_Begin, k_Iterator ak_End)
{
mk_Begin = ak_Begin;
mk_End = ak_End;
mb_Fresh = true;
}
#region IEnumerator Members
public bool MoveNext()
{
if (mb_Fresh)
{
mk_Current = mk_Begin.Clone();
mb_Fresh = false;
}
else if (mk_Current != mk_End)
mk_Current.Next();
return (mk_Current != mk_End);
}
public void Reset()
{
mb_Fresh = true;
mk_Current = null;
}
public object Current
{
get
{
if (mb_Fresh || mk_Current == mk_End)
throw new InvalidOperationException("The enumerator is positioned before the first element of the collection or after the last element.");
return mk_Current.Current;
}
}
#endregion
}
public class k_IteratorDictEnumerator : k_IteratorEnumerator, IDictionaryEnumerator
{
public k_IteratorDictEnumerator(k_Iterator ak_Begin, k_Iterator ak_End)
: base(ak_Begin, ak_End)
{
}
#region IDictionaryEnumerator Members
public object Key
{
get { return this.Entry.Key; }
}
public object Value
{
get { return this.Entry.Value; }
}
public DictionaryEntry Entry
{
get { return (DictionaryEntry)this.Current; }
}
#endregion
}
public class k_IListIterator : k_Iterator
{
private readonly IList mk_List;
private int mi_Index;
public static k_IListIterator CreateBegin(IList ak_List)
{
return new k_PinnedIListIterator(ak_List, 0);
}
public static k_IListIterator CreateEnd(IList ak_List)
{
return new k_PinnedIListIterator(ak_List, ak_List.Count);
}
public k_IListIterator(IList ak_List, int ai_Index)
{
mk_List = ak_List;
mi_Index = ai_Index;
}
public override void Move(int ai_Count)
{
int li_Index = mi_Index + ai_Count;
if (li_Index < 0)
throw new InvalidOperationException("Tried to move before first element.");
else if (li_Index > mk_List.Count)
throw new InvalidOperationException("Tried to moved beyond end element.");
mi_Index = li_Index;
}
public override int Distance(k_Iterator ak_Iter)
{
return mi_Index - ((k_IListIterator)ak_Iter).mi_Index;
}
public override object Collection
{
get { return mk_List; }
}
public override object Current
{
get
{
if (mi_Index < 0 || mi_Index >= mk_List.Count)
throw new k_InvalidPositionException();
return mk_List[mi_Index];
}
set
{
if (mi_Index < 0 || mi_Index >= mk_List.Count)
throw new k_InvalidPositionException();
mk_List[mi_Index] = value;
}
}
public override bool Equals(object ak_Obj)
{
k_IListIterator lk_Iter = ak_Obj as k_IListIterator;
if (lk_Iter == null)
return false;
return (mi_Index == lk_Iter.mi_Index) && object.ReferenceEquals(this.Collection, lk_Iter.Collection);
}
public override int GetHashCode()
{
return mk_List.GetHashCode() ^ mi_Index;
}
public override k_Iterator Clone()
{
return new k_IListIterator(mk_List, mi_Index);
}
internal int Index
{
get { return mi_Index; }
}
}
internal class k_PinnedIListIterator : k_IListIterator
{
public k_PinnedIListIterator(IList ak_List, int ai_Index)
: base(ak_List, ai_Index)
{
}
public override void Move(int ai_Count)
{
throw new k_IteratorPinnedException();
}
}
public class k_CollectionOnIterators : ICollection
{
private k_Iterator mk_Begin, mk_End;
private int mi_Count;
public k_CollectionOnIterators(k_Iterator ak_Begin, k_Iterator ak_End)
{
mk_Begin = ak_Begin;
mk_End = ak_End;
mi_Count = mk_End - mk_Begin;
}
#region ICollection Members
public int Count
{
get { return mi_Count; }
}
public bool IsSynchronized
{
get { return false; }
}
public object SyncRoot
{
get { return mk_Begin.Collection; }
}
public void CopyTo(Array ak_Array, int ai_Index)
{
foreach (object lk_Obj in this)
ak_Array.SetValue(lk_Obj, ai_Index++);
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return new k_IteratorEnumerator(mk_Begin, mk_End);
}
#endregion
}
[Serializable]
public class k_IteratorPinnedException : InvalidOperationException
{
public k_IteratorPinnedException()
: base("Cannot move pinned iterator. Use Clone() to create a movable copy.")
{
}
}
[Serializable]
public class k_InvalidPositionException : InvalidOperationException
{
public k_InvalidPositionException()
: base("Iterator positioned on End or invalid element.")
{
}
}
}

View File

@@ -0,0 +1,537 @@
using System;
using System.Collections;
namespace System.util.collections
{
/// <summary>
/// A doubly linked list
/// </summary>
public class k_List : ISequence
{
#region k_Node Implementation
private class k_Node
{
private object mk_Value;
public k_Node mk_Prev, mk_Next;
public k_Node(object ak_Value)
{
mk_Value = ak_Value;
}
public object Value
{
get { return mk_Value; }
set { mk_Value = value; }
}
}
#endregion
#region k_NodeIterator Implementation
private class k_NodeIterator : k_Iterator, ICloneable
{
private readonly k_List mk_List;
private k_Node mk_Current;
public k_NodeIterator(k_List ak_List, k_Node ak_Node)
{
mk_List = ak_List;
mk_Current = ak_Node;
}
public override object Current
{
get
{
if (mk_Current == null)
throw new k_InvalidPositionException();
return mk_Current.Value;
}
set
{
if (mk_Current == null)
throw new k_InvalidPositionException();
mk_Current.Value = value;
}
}
public override object Collection
{
get { return mk_List; }
}
public override void Move(int ai_Count)
{
k_Node lk_NewPos = mk_Current;
int li_Count = ai_Count;
if (li_Count > 0)
{
while (li_Count-- > 0)
{
if (lk_NewPos == null)
throw new InvalidOperationException("Tried to moved beyond end element.");
lk_NewPos = lk_NewPos.mk_Next;
}
}
else
{
while (li_Count++ < 0)
{
if (lk_NewPos == null)
lk_NewPos = mk_List.mk_Tail;
else
lk_NewPos = lk_NewPos.mk_Prev;
if (lk_NewPos == null)
throw new InvalidOperationException("Tried to move before first element.");
}
}
#if (DEBUG)
if (ai_Count != 0 && object.ReferenceEquals(mk_Current, lk_NewPos))
throw new IndexOutOfRangeException("Iterator is positioned on invalid node.");
#endif
mk_Current = lk_NewPos;
}
public override int Distance(k_Iterator ak_Iter)
{
k_NodeIterator lk_Iter = (k_NodeIterator)ak_Iter;
if (!this.IsValid || !lk_Iter.IsValid)
throw new ArgumentException("Iterator is invalid.");
int li_Diff = 0;
k_Iterator lk_End = mk_List.End;
k_Iterator lk_Fwd = lk_Iter.Clone();
for (; lk_Fwd != this && lk_Fwd != lk_End; lk_Fwd.Next())
++li_Diff;
if (lk_Fwd == this)
return li_Diff;
li_Diff = 0;
k_Iterator lk_Bck = this.Clone();
for (; lk_Bck != lk_Iter && lk_Bck != lk_End; lk_Bck.Next())
--li_Diff;
if (lk_Bck == lk_Iter)
return li_Diff;
throw new Exception("Inconsistent state. Concurrency?");
}
public override bool Equals(object ak_Obj)
{
k_NodeIterator lk_Iter = ak_Obj as k_NodeIterator;
if (lk_Iter == null)
return false;
return object.ReferenceEquals(mk_Current, lk_Iter.mk_Current);
}
public override int GetHashCode()
{
if (mk_Current == null)
return mk_List.GetHashCode();
return mk_Current.GetHashCode();
}
public override k_Iterator Clone()
{
return new k_NodeIterator(mk_List, mk_Current);
}
internal k_Node Node
{
get { return mk_Current; }
}
internal bool IsValid
{
get { return (mk_Current == null || (!object.ReferenceEquals(mk_Current.mk_Next, mk_Current) && !object.ReferenceEquals(mk_Current.mk_Prev, mk_Current))); }
}
}
private class k_PinnedNodeIterator : k_NodeIterator
{
public k_PinnedNodeIterator(k_List ak_List, k_Node ak_Node)
: base(ak_List, ak_Node)
{
}
public override void Move(int ai_Count)
{
throw new k_IteratorPinnedException();
}
}
#endregion
private int mi_Count;
private k_Node mk_Head, mk_Tail;
private k_Iterator mk_Begin, mk_End;
public k_List()
{
mk_End = new k_PinnedNodeIterator(this, null);
mk_Begin = mk_End;
}
// IContainer Members
public k_Iterator Begin
{
get
{
if (mi_Count == 0)
return mk_End;
if (mk_Begin == null)
mk_Begin = new k_PinnedNodeIterator(this, mk_Head);
return mk_Begin;
}
}
public k_Iterator End
{
get { return mk_End; }
}
public bool IsEmpty
{
get { return (mi_Count == 0); }
}
public k_Iterator Find(object ak_Value)
{
return k_Algorithm.Find(this.Begin, this.End, ak_Value);
}
public k_Iterator Erase(k_Iterator ak_Where)
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(this, ak_Where.Collection), "Iterator does not belong to this list.");
if (ak_Where == this.End)
return this.End;
return Erase(ak_Where, ak_Where + 1);
}
public k_Iterator Erase(k_Iterator ak_First, k_Iterator ak_Last)
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(this, ak_First.Collection) && object.ReferenceEquals(this, ak_Last.Collection), "Iterators do not belong to this collection.");
int li_Distance = ak_Last - ak_First;
if (li_Distance == 0)
return ak_Last;
k_Node lk_First = ((k_NodeIterator)ak_First).Node;
k_Node lk_Prev = lk_First.mk_Prev;
k_Node lk_Next = (ak_Last != this.End) ? ((k_NodeIterator)ak_Last).Node : null;
if (lk_Prev != null)
lk_Prev.mk_Next = lk_Next;
else
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(mk_Head, lk_First), "Inconsistent list state");
mk_Head = lk_Next;
mk_Begin = null;
}
if (lk_Next != null)
lk_Next.mk_Prev = lk_Prev;
else
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(mk_Tail, ((k_NodeIterator)(ak_Last-1)).Node), "Inconsistent list state");
mk_Tail = lk_Prev;
}
mi_Count -= li_Distance;
#if (DEBUG)
// create invalid nodes linking to itself
k_Node lk_Node = lk_First;
while (lk_Node != null && lk_Node != lk_Next)
{
k_Node lk_Tmp = lk_Node.mk_Next;
lk_Node.mk_Next = lk_Node;
lk_Node.mk_Prev = lk_Node;
lk_Node = lk_Tmp;
}
#endif
return ak_Last;
}
// ISequence Members
public object Front
{
get
{
if (this.IsEmpty)
throw new InvalidOperationException("Empty list");
return mk_Head.Value;
}
set
{
if (this.IsEmpty)
throw new InvalidOperationException("Empty list");
mk_Head.Value = value;
}
}
public object Back
{
get
{
if (this.IsEmpty)
throw new InvalidOperationException("Empty list");
return mk_Tail.Value;
}
set
{
if (this.IsEmpty)
throw new InvalidOperationException("Empty list");
mk_Tail.Value = value;
}
}
public void PushFront(object ak_Value)
{
Insert(this.Begin, ak_Value);
}
public void PopFront()
{
Erase(this.Begin);
}
public void PushBack(object ak_Value)
{
Insert(this.End, ak_Value);
}
public void PopBack()
{
Erase(this.End-1);
}
public void Assign(k_Iterator ak_SrcBegin, k_Iterator ak_SrcEnd)
{
Clear();
Insert(this.End, ak_SrcBegin, ak_SrcEnd);
}
public void Assign(object ak_Value, int ai_Count)
{
Clear();
Insert(this.End, ak_Value, ai_Count);
}
public void Insert(k_Iterator ak_Where, object ak_Value)
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(this, ak_Where.Collection), "Iterator does not belong to this collection.");
k_Node lk_New = new k_Node(ak_Value);
PasteNodeRange((k_NodeIterator)ak_Where, lk_New, lk_New);
++mi_Count;
}
public void Insert(k_Iterator ak_Where, k_Iterator ak_SrcBegin, k_Iterator ak_SrcEnd)
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(this, ak_Where.Collection), "Iterator does not belong to this collection.");
k_Node lk_Start = new k_Node(null), lk_End = lk_Start;
int li_Count = 0;
for (k_Iterator lk_Iter = ak_SrcBegin.Clone(); lk_Iter != ak_SrcEnd; lk_Iter.Next(), ++li_Count)
{
k_Node lk_New = new k_Node(lk_Iter.Current);
lk_End.mk_Next = lk_New;
lk_New.mk_Prev = lk_End;
lk_End = lk_New;
}
if (li_Count > 0)
{
PasteNodeRange((k_NodeIterator)ak_Where, lk_Start.mk_Next, lk_End);
mi_Count += li_Count;
}
}
public void Insert(k_Iterator ak_Where, object ak_Value, int ai_Count)
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(this, ak_Where.Collection), "Iterator does not belong to this collection.");
k_Node lk_Start = new k_Node(null), lk_End = lk_Start;
for (int i=0; i<ai_Count; ++i)
{
k_Node lk_New = new k_Node(ak_Value);
lk_End.mk_Next = lk_New;
lk_New.mk_Prev = lk_End;
lk_End = lk_New;
}
if (ai_Count > 0)
{
PasteNodeRange((k_NodeIterator)ak_Where, lk_Start.mk_Next, lk_End);
mi_Count += ai_Count;
}
}
#region IList Members
public int Add(object ak_Value)
{
Insert(this.End, ak_Value);
return mi_Count;
}
public void Clear()
{
mk_Head = mk_Tail = null;
mk_Begin = mk_End;
mi_Count = 0;
}
public bool Contains(object ak_Value)
{
return (this.Find(ak_Value) != this.End);
}
public int IndexOf(object ak_Value)
{
int li_Index = 0;
foreach (object lk_Val in this)
{
if (object.Equals(lk_Val, ak_Value))
return li_Index;
++li_Index;
}
return -1;
}
void IList.Insert(int ai_Index, object ak_Value)
{
this.Insert(this.Begin + ai_Index, ak_Value);
}
void IList.Remove(object ak_Value)
{
k_NodeIterator lk_Found = (k_NodeIterator)this.Find(ak_Value);
if (lk_Found != this.End)
Erase(lk_Found);
}
public void RemoveAt(int ai_Index)
{
Erase(this.Begin + ai_Index);
}
public bool IsFixedSize
{
get { return false; }
}
bool IList.IsReadOnly
{
get { return false; }
}
public object this[int index]
{
get
{
k_Iterator lk_Iter = this.Begin + index;
return lk_Iter.Current;
}
set
{
k_Iterator lk_Iter = this.Begin + index;
lk_Iter.Current = value;
}
}
#endregion
#region ICollection Members
public void CopyTo(Array ak_Array, int ai_Index)
{
foreach (object lk_Obj in this)
ak_Array.SetValue(lk_Obj, ai_Index++);
}
public int Count
{
get { return mi_Count; }
}
public bool IsSynchronized
{
get { return false; }
}
public object SyncRoot
{
get { return this; }
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return new k_IteratorEnumerator(this.Begin, this.End);
}
#endregion
#region ICloneable Members
public object Clone()
{
k_List lk_Clone = new k_List();
for (k_Iterator lk_Iter = this.Begin.Clone(); lk_Iter != this.End; lk_Iter.Next())
lk_Clone.Add(lk_Iter.Current);
return lk_Clone;
}
#endregion
private void PasteNodeRange(k_NodeIterator ak_Where, k_Node ak_First, k_Node ak_Last)
{
if (ak_Where != this.End)
{
k_Node lk_Next = ak_Where.Node;
k_Node lk_Prev = lk_Next.mk_Prev;
ak_Last.mk_Next = lk_Next;
ak_First.mk_Prev = lk_Prev;
if (lk_Next != null)
lk_Next.mk_Prev = ak_Last;
if (lk_Prev != null)
lk_Prev.mk_Next = ak_First;
}
else
{
if (mk_Tail != null)
{
mk_Tail.mk_Next = ak_First;
ak_First.mk_Prev = mk_Tail;
}
mk_Tail = ak_Last;
}
if (ak_Where == this.Begin)
{
mk_Head = ak_First;
mk_Begin = null; // recalc on next get
}
}
}
}

View File

@@ -0,0 +1,108 @@
using System;
using System.Collections;
namespace System.util.collections
{
/// <summary>
/// k_Queue is a first-in, first-out (FIFO) data structure.
/// It hides functionality of the underlying container (e.g. k_List, k_Deque)
/// and provides a a basic queue class.
/// </summary>
public class k_Queue : ICollection
{
private ISequence mk_Container;
public k_Queue()
: this(typeof(k_Deque))
{
}
public k_Queue(Type ak_ContainerType)
{
mk_Container = Activator.CreateInstance(ak_ContainerType) as ISequence;
if (mk_Container == null)
throw new ArgumentException("Container type must implement ISequence.", "ak_ContainerType");
}
public k_Iterator Begin
{
get { return mk_Container.Begin; }
}
public k_Iterator End
{
get { return mk_Container.End; }
}
public object Front
{
get { return mk_Container.Front; }
}
public object Back
{
get { return mk_Container.Back; }
}
public bool IsEmpty
{
get { return mk_Container.IsEmpty; }
}
public k_Iterator Erase(k_Iterator ak_Where)
{
return mk_Container.Erase(ak_Where);
}
public void Push(object ak_Value)
{
mk_Container.PushBack(ak_Value);
}
public object Pop()
{
object lk_Obj = mk_Container.Front;
mk_Container.PopFront();
return lk_Obj;
}
public IContainer UnderlyingContainer
{
get { return mk_Container; }
}
#region ICollection Members
public void CopyTo(Array ak_Array, int ai_Index)
{
foreach (object lk_Obj in this)
ak_Array.SetValue(lk_Obj, ai_Index++);
}
public int Count
{
get { return mk_Container.Count; }
}
public bool IsSynchronized
{
get { return false; }
}
public object SyncRoot
{
get { return this; }
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return new k_IteratorEnumerator(mk_Container.Begin, mk_Container.End);
}
#endregion
}
}

View File

@@ -0,0 +1,699 @@
using System;
using System.Collections;
namespace System.util.collections
{
/// <summary>
/// A Skip List
/// </summary>
public class k_SkipList : ISortedMap
{
#region k_Node Implementation
private class k_Node
{
private object mk_Key;
private object mk_Value;
private k_Node[] mk_Next;
public k_Node(object ak_Key, object ak_Value, int ai_Height)
{
mk_Next = new k_Node[ai_Height];
mk_Key = ak_Key;
mk_Value = ak_Value;
}
public object Key
{
get { return mk_Key; }
}
public object Value
{
get { return mk_Value; }
set { mk_Value = Value; }
}
public DictionaryEntry Item
{
get { return new DictionaryEntry(mk_Key, mk_Value); }
}
public k_Node[] Next
{
get { return mk_Next; }
}
public int Height
{
get { return mk_Next.Length; }
}
}
#endregion
#region k_NodeIterator Implementation
private class k_NodeIterator : k_Iterator
{
private readonly k_SkipList mk_List;
private k_Node mk_Current;
public k_NodeIterator(k_SkipList ak_List, k_Node ak_Node)
{
mk_List = ak_List;
mk_Current = ak_Node;
}
public override object Current
{
get
{
if (mk_Current == null)
throw new k_InvalidPositionException();
return mk_Current.Item;
}
set
{
DictionaryEntry lr_Entry = (DictionaryEntry)value;
if (mk_List.mk_Comparer.Compare(lr_Entry.Key, mk_Current.Key) != 0)
throw new ArgumentException("Key values must not be changed.");
mk_Current.Value = lr_Entry.Value;
}
}
public override object Collection
{
get { return mk_List; }
}
public override void Move(int ai_Count)
{
k_Node lk_NewPos = mk_Current;
if (ai_Count > 0)
{
while (ai_Count-- > 0)
{
if (lk_NewPos == null)
throw new InvalidOperationException("Tried to moved beyond end element.");
lk_NewPos = mk_List.Next(lk_NewPos);
}
}
else
{
while (ai_Count++ < 0)
{
if (lk_NewPos == null)
lk_NewPos = mk_List.RightMost();
else
lk_NewPos = mk_List.Previous(lk_NewPos);
if (lk_NewPos == null)
throw new InvalidOperationException("Tried to move before first element.");
}
}
mk_Current = lk_NewPos;
}
public override int Distance(k_Iterator ak_Iter)
{
k_NodeIterator lk_Iter = (k_NodeIterator)ak_Iter;
k_Iterator lk_End = mk_List.End;
int li_KeyDiff;
if (this == lk_End || ak_Iter == lk_End)
li_KeyDiff = (this == lk_End && this != ak_Iter) ? 1 : 0;
else
li_KeyDiff = mk_List.mk_Comparer.Compare(mk_Current.Key, lk_Iter.mk_Current.Key);
if (li_KeyDiff <= 0)
{
int li_Diff = 0;
k_Iterator lk_Bck = this.Clone();
for (; lk_Bck != lk_Iter && lk_Bck != lk_End; lk_Bck.Next())
--li_Diff;
if (lk_Bck == lk_Iter)
return li_Diff;
}
if (li_KeyDiff >= 0)
{
int li_Diff = 0;
k_Iterator lk_Fwd = lk_Iter.Clone();
for (; lk_Fwd != this && lk_Fwd != lk_End; lk_Fwd.Next())
++li_Diff;
if (lk_Fwd == this)
return li_Diff;
}
throw new Exception("Inconsistent state. Concurrency?");
}
public override bool Equals(object ak_Obj)
{
k_NodeIterator lk_Iter = ak_Obj as k_NodeIterator;
if (lk_Iter == null)
return false;
return object.ReferenceEquals(mk_Current, lk_Iter.mk_Current);
}
public override int GetHashCode()
{
if (mk_Current == null)
return mk_List.GetHashCode();
return mk_Current.GetHashCode();
}
public override k_Iterator Clone()
{
return new k_NodeIterator(mk_List, mk_Current);
}
internal k_Node Node
{
get { return mk_Current; }
}
}
private class k_PinnedNodeIterator : k_NodeIterator
{
public k_PinnedNodeIterator(k_SkipList ak_List, k_Node ak_Node)
: base(ak_List, ak_Node)
{
}
public override void Move(int ai_Count)
{
throw new k_IteratorPinnedException();
}
}
#endregion
private static Random mk_Rand = new Random();
private IComparer mk_Comparer;
private double md_Prob;
private int mi_MaxLevel;
private int mi_HighestNode;
private k_Node mk_Head;
private int mi_Count;
private k_Iterator mk_End;
public k_SkipList()
: this(System.Collections.Comparer.Default)
{
}
public k_SkipList(IComparer ak_Comparer)
: this(ak_Comparer, 1.0/Math.E, 16)
{
}
public k_SkipList(IComparer ak_Comparer, double ad_Prob, int ai_MaxLevel)
{
if (ad_Prob >= 1.0 || ad_Prob <= 0)
throw new ArgumentException("Invalid probability. Must be (0-1).", "ad_Prob");
md_Prob = ad_Prob;
mi_MaxLevel = ai_MaxLevel;
mk_Comparer = ak_Comparer;
mk_Head = new k_Node(null, null, ai_MaxLevel);
mk_End = new k_PinnedNodeIterator(this, null);
}
// IContainer Members
public k_Iterator Begin
{
get
{
if (mi_Count == 0)
return this.End;
return new k_NodeIterator(this, this.LeftMost());
}
}
public k_Iterator End
{
get { return mk_End; }
}
public bool IsEmpty
{
get { return (mi_Count == 0); }
}
public k_Iterator Find(object ak_Value)
{
DictionaryEntry lr_Item = (DictionaryEntry)ak_Value;
k_NodeIterator lk_Found = (k_NodeIterator)LowerBound(lr_Item.Key);
if (lk_Found != this.End
&& mk_Comparer.Compare(lr_Item.Key, lk_Found.Node.Key) == 0 && mk_Comparer.Compare(lr_Item.Value, lk_Found.Node.Value) == 0)
return lk_Found;
return this.End;
}
public k_Iterator Erase(k_Iterator ak_Where)
{
return Erase(ak_Where, ak_Where+1);
}
public k_Iterator Erase(k_Iterator ak_First, k_Iterator ak_Last)
{
if (ak_First == ak_Last)
return ak_Last.Clone();
int li_Count = ak_Last - ak_First;
k_Node lk_First = ((k_NodeIterator)ak_First).Node;
k_Node lk_Last = (ak_Last != this.End) ? ((k_NodeIterator)ak_Last).Node : null;
k_Node lk_Node = new k_Node(null, null, mi_HighestNode);
k_Node lk_Current = mk_Head;
for (int li_Level = mi_HighestNode-1; li_Level >= 0; --li_Level)
{
while (lk_Current.Next[li_Level] != null)
{
if (ComparePos(lk_Current.Next[li_Level], lk_First) >= 0)
break;
lk_Current = lk_Current.Next[li_Level];
}
lk_Node.Next[li_Level] = lk_Current;
}
if (lk_Last == null)
{
for (int i=0; i<lk_Node.Height; ++i)
{
k_Node lk_Left = lk_Node.Next[i];
lk_Left.Next[i] = null;
}
}
else
{
for (int i=0; i<lk_Node.Height; ++i)
{
k_Node lk_Left = lk_Node.Next[i];
// for each level skip over erased range
lk_Current = lk_Left.Next[i];
while (lk_Current != null)
{
if (ComparePos(lk_Current, lk_Last) >= 0)
break;
lk_Current = lk_Current.Next[i];
}
lk_Left.Next[i] = lk_Current;
}
}
mi_Count -= li_Count;
while (mi_HighestNode > 0 && mk_Head.Next[mi_HighestNode-1] == null)
--mi_HighestNode;
return ak_Last;
}
// IMap Members
public k_Iterator FindKey(object ak_Key)
{
k_NodeIterator lk_Found = (k_NodeIterator)LowerBound(ak_Key);
if (lk_Found != this.End && mk_Comparer.Compare(ak_Key, lk_Found.Node.Key) == 0)
return lk_Found;
return this.End;
}
public void Add(DictionaryEntry ar_Entry)
{
Add(ar_Entry.Key, ar_Entry.Value);
}
public void Insert(k_Iterator ak_SrcBegin, k_Iterator ak_SrcEnd)
{
for (k_Iterator lk_Iter = ak_SrcBegin.Clone(); lk_Iter != ak_SrcEnd; lk_Iter.Next())
Add((DictionaryEntry)lk_Iter.Current);
}
// ISortedMap Members
public IComparer Comparer
{
get { return mk_Comparer; }
}
/// <summary>
/// Returns an iterator to the first element in a list with a key value
/// that is equal to or greater than that of a specified key.
/// </summary>
/// <param name="ak_Key">
/// The argument key value to be compared with the sort key of an element
/// from the list being searched.
/// </param>
/// <returns>
/// Location of an element in a list that with a key that is equal to
/// or greater than the argument key, or this.End if no match is found for the key.
/// </returns>
public k_Iterator LowerBound(object ak_Key)
{
k_Node lk_Found = null;
k_Node lk_Current = mk_Head;
for (int li_Level = mi_HighestNode-1; li_Level >= 0; --li_Level)
{
k_Node lk_Next = lk_Current.Next[li_Level];
while (lk_Next != null)
{
int li_Diff = mk_Comparer.Compare(lk_Next.Key, ak_Key);
if (li_Diff >= 0)
{
lk_Found = lk_Next;
break;
}
lk_Current = lk_Next;
lk_Next = lk_Next.Next[li_Level];
}
}
return new k_NodeIterator(this, lk_Found);
}
/// <summary>
/// Returns an iterator to the first element in a list with a key value
/// that is greater than that of a specified key.
/// </summary>
/// <param name="ak_Key">
/// The argument key value to be compared with the sort key of an element
/// from the list being searched.
/// </param>
/// <returns>
/// Location of an element in a list that with a key that is greater
/// than the argument key, or this.End if no match is found for the key.
/// </returns>
public k_Iterator UpperBound(object ak_Key)
{
k_Node lk_Found = null;
k_Node lk_Current = mk_Head;
for (int li_Level = mi_HighestNode-1; li_Level >= 0; --li_Level)
{
k_Node lk_Next = lk_Current.Next[li_Level];
while (lk_Next != null)
{
int li_Diff = mk_Comparer.Compare(lk_Next.Key, ak_Key);
if (li_Diff > 0)
{
lk_Found = lk_Next;
break;
}
lk_Current = lk_Next;
lk_Next = lk_Next.Next[li_Level];
}
}
return new k_NodeIterator(this, lk_Found);
}
#region IDictionary Members
public void Add(object ak_Key, object ak_Value)
{
k_Node lk_Node = new k_Node(ak_Key, ak_Value, CalcNewNodeHeight());
if (lk_Node.Height > mi_HighestNode)
mi_HighestNode = lk_Node.Height;
FindInsertPos(lk_Node);
for (int i=0; i<lk_Node.Height; ++i)
{
k_Node lk_Left = lk_Node.Next[i];
k_Node lk_Tmp = lk_Left.Next[i];
lk_Left.Next[i] = lk_Node;
lk_Node.Next[i] = lk_Tmp;
}
++mi_Count;
}
public void Clear()
{
Array.Clear(mk_Head.Next, 0, mk_Head.Next.Length);
mi_HighestNode = 0;
mi_Count = 0;
}
public bool Contains(object ak_Key)
{
return (FindKey(ak_Key) != this.End);
}
public IDictionaryEnumerator GetEnumerator()
{
return new k_IteratorDictEnumerator(this.Begin, this.End);
}
public void Remove(object ak_Key)
{
Erase(LowerBound(ak_Key), UpperBound(ak_Key));
}
public bool IsFixedSize
{
get { return false; }
}
public bool IsReadOnly
{
get { return false; }
}
public object this[object ak_Key]
{
get
{
k_NodeIterator lk_Iter = (k_NodeIterator)FindKey(ak_Key);
if (lk_Iter == this.End)
return null;
return lk_Iter.Node.Value;
}
set
{
k_NodeIterator lk_Iter = (k_NodeIterator)FindKey(ak_Key);
if (lk_Iter == this.End)
throw new ArgumentException("No element for key was found.", "ak_Key");
lk_Iter.Node.Value = value;
}
}
public ICollection Keys
{
get
{
object[] lk_Keys = new object[mi_Count];
int i = 0;
for (k_Iterator lk_Iter = this.Begin.Clone(); lk_Iter != this.End; lk_Iter.Next())
lk_Keys[i++] = ((k_NodeIterator)lk_Iter).Node.Key;
return lk_Keys;
}
}
public ICollection Values
{
get
{
object[] lk_Values = new object[mi_Count];
int i=0;
for (k_Iterator lk_Iter = this.Begin.Clone(); lk_Iter != this.End; lk_Iter.Next())
lk_Values[i++] = ((k_NodeIterator)lk_Iter).Node.Value;
return lk_Values;
}
}
#endregion
#region ICollection Members
public void CopyTo(Array ak_Array, int ai_Index)
{
foreach (object lk_Obj in this)
ak_Array.SetValue(lk_Obj, ai_Index++);
}
public int Count
{
get { return mi_Count; }
}
public bool IsSynchronized
{
get { return false; }
}
public object SyncRoot
{
get { return this; }
}
#endregion
#region IEnumerable Members
IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return new k_IteratorEnumerator(this.Begin, this.End);
}
#endregion
#region ICloneable Members
public object Clone()
{
k_SkipList lk_Clone = new k_SkipList(mk_Comparer, md_Prob, mi_MaxLevel);
lk_Clone.mi_Count = mi_Count;
lk_Clone.mi_HighestNode = mi_HighestNode;
lk_Clone.mk_Head = CloneR(mk_Head, null);
return lk_Clone;
}
#endregion
private k_Node Previous(k_Node ak_Node)
{
k_Node lk_Current = mk_Head;
for (int li_Level = mi_HighestNode-1; li_Level >= 0; --li_Level)
{
while (lk_Current.Next[li_Level] != null)
{
int li_Diff = mk_Comparer.Compare(lk_Current.Next[li_Level].Key, ak_Node.Key);
if (li_Diff > 0)
break;
if (li_Diff == 0)
{
k_Node lk_Next = lk_Current;
while (lk_Next != null && !object.ReferenceEquals(lk_Next.Next[0], ak_Node))
{
if (mk_Comparer.Compare(lk_Next.Key, ak_Node.Key) > 0)
lk_Next = null;
else
lk_Next = lk_Next.Next[0];
}
if (lk_Next == null)
break;
return lk_Next; // found previous node during right-scan of nodes with equal key value
}
lk_Current = lk_Current.Next[li_Level];
}
}
if (object.ReferenceEquals(mk_Head, lk_Current))
return null;
return lk_Current;
}
private k_Node Next(k_Node ak_Node)
{
return ak_Node.Next[0];
}
/// <summary>
/// Return leftmost node in list.
/// </summary>
/// <returns>Found node</returns>
private k_Node LeftMost()
{
return mk_Head.Next[0];
}
/// <summary>
/// Return rightmost node in list.
/// </summary>
/// <returns>Found node</returns>
private k_Node RightMost()
{
k_Node lk_Current = mk_Head.Next[mi_HighestNode-1];
for (int li_Level = mi_HighestNode-1; li_Level >= 0; --li_Level)
{
while (lk_Current.Next[li_Level] != null)
lk_Current = lk_Current.Next[li_Level];
}
return lk_Current;
}
private void FindInsertPos(k_Node ak_Node)
{
k_Node lk_Current = mk_Head;
for (int li_Level = mi_HighestNode-1; li_Level >= 0; --li_Level)
{
while (lk_Current.Next[li_Level] != null && mk_Comparer.Compare(lk_Current.Next[li_Level].Key, ak_Node.Key) < 0)
lk_Current = lk_Current.Next[li_Level];
if (li_Level < ak_Node.Height)
ak_Node.Next[li_Level] = lk_Current;
}
}
private int CalcNewNodeHeight()
{
double ld_Rnd = mk_Rand.NextDouble();
int li_Level = 1;
for (double ld_Pow = md_Prob; li_Level < mi_MaxLevel; ++li_Level, ld_Pow*=md_Prob)
{
if (ld_Pow < ld_Rnd)
break;
}
return li_Level;
}
private int ComparePos(k_Node ak_Left, k_Node ak_Right)
{
if (object.ReferenceEquals(ak_Left, ak_Right))
return 0;
int li_Diff = mk_Comparer.Compare(ak_Left.Key, ak_Right.Key);
if (li_Diff != 0)
return li_Diff;
k_Node lk_Current = ak_Left;
for (;;)
{
if (lk_Current == null || mk_Comparer.Compare(lk_Current.Key, ak_Right.Key) > 0)
return 1;
else if (object.ReferenceEquals(lk_Current, ak_Right))
return -1;
lk_Current = lk_Current.Next[0];
}
}
private k_Node CloneR(k_Node ak_Node, k_Node ak_NextHigher)
{
k_Node lk_New = new k_Node(ak_Node.Key, ak_Node.Value, ak_Node.Height);
for (int i=ak_Node.Height-1; i>=0; --i)
{
// simply copy two links with equal target next to each other
if (i < ak_Node.Height-1 && object.ReferenceEquals(ak_Node.Next[i], ak_Node.Next[i+1]))
{
lk_New.Next[i] = lk_New.Next[i+1];
continue;
}
k_Node lk_Next = ak_Node.Next[i];
if (lk_Next != null && lk_Next.Height-1 <= i)
{
k_Node lk_Higher = (i < ak_Node.Height-1) ? ak_Node.Next[i+1] : ak_NextHigher;
lk_New.Next[i] = CloneR(lk_Next, lk_Higher);
}
else
lk_New.Next[i] = ak_NextHigher;
}
return lk_New;
}
}
}

View File

@@ -0,0 +1,101 @@
using System;
using System.Collections;
namespace System.util.collections
{
/// <summary>
/// A push-down stack using an underlying k_Vector.
/// Last in first out (LIFO).
/// </summary>
public class k_Stack : ICollection
{
private ISequence mk_Items; // stack container
private int mi_MaxSize;
public k_Stack()
{
mk_Items = new k_Vector();
mi_MaxSize = int.MaxValue;
}
public k_Stack(int ai_Capacity, bool ab_FixedSize)
{
mk_Items = new k_Vector(ai_Capacity);
mi_MaxSize = (ab_FixedSize) ? ai_Capacity : int.MaxValue;
}
public object Top
{
get { return mk_Items.Back; }
set { mk_Items.Back = value; }
}
public object this[int ai_Index]
{
get { return (mk_Items.Begin+ai_Index).Current; }
set { (mk_Items.Begin+ai_Index).Current = value; }
}
public void Push(object ak_Value)
{
if (mk_Items.Count >= mi_MaxSize)
throw new StackOverflowException("Stack overflow");
mk_Items.PushBack(ak_Value);
}
public object Pop()
{
if (mk_Items.Count == 0)
throw new StackOverflowException("Stack underflow");
object lk_Obj = mk_Items.Back;
mk_Items.PopBack();
return lk_Obj;
}
public bool IsEmpty
{
get { return mk_Items.IsEmpty; }
}
public void Clear()
{
mk_Items.Clear();
}
#region ICollection Members
public void CopyTo(Array ak_Array, int ai_Index)
{
for (k_Iterator lk_Iter = mk_Items.Begin.Clone(); lk_Iter != mk_Items.End; lk_Iter.Next())
ak_Array.SetValue(lk_Iter.Current, ai_Index++);
}
public int Count
{
get { return mk_Items.Count; }
}
public bool IsSynchronized
{
get { return false; }
}
public object SyncRoot
{
get { return this; }
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return new k_IteratorEnumerator(mk_Items.Begin, mk_Items.End);
}
#endregion
}
}

View File

@@ -0,0 +1,830 @@
using System;
using System.Collections;
namespace System.util.collections
{
/// <summary>
/// k_Tree is a red-black balanced search tree (BST) implementation.
/// Complexity of find, insert and erase operations is near O(lg n).
/// </summary>
public class k_Tree : ISortedMap
{
#region k_Node Implementation
private class k_Node
{
private object mk_Key;
private object mk_Value;
private bool mb_Red;
public k_Node mk_Left, mk_Right, mk_Parent; // public to simplify fixup & clone (passing by ref)
public k_Node(object ak_Key, object ak_Value, k_Node ak_Parent)
{
mk_Key = ak_Key;
mk_Value = ak_Value;
mk_Parent = ak_Parent;
mb_Red = true;
}
public object Key
{
get { return mk_Key; }
}
public object Value
{
get { return mk_Value; }
set { mk_Value = value; }
}
public DictionaryEntry Item
{
get { return new DictionaryEntry(mk_Key, mk_Value); }
}
public bool Red
{
get { return mb_Red; }
set { mb_Red = value; }
}
public static void SwapItems(k_Node ak_A, k_Node ak_B)
{
object lk_Tmp = ak_A.mk_Key;
ak_A.mk_Key = ak_B.mk_Key;
ak_B.mk_Key = lk_Tmp;
lk_Tmp = ak_A.mk_Value;
ak_A.mk_Value = ak_B.mk_Value;
ak_B.mk_Value = lk_Tmp;
}
}
#endregion
#region k_NodeIterator Implementation
private class k_NodeIterator : k_Iterator
{
private readonly k_Tree mk_Tree;
private k_Node mk_Current;
public k_NodeIterator(k_Tree ak_Tree, k_Node ak_Node)
{
mk_Tree = ak_Tree;
mk_Current = ak_Node;
}
public override object Current
{
get
{
if (mk_Current == null)
throw new k_InvalidPositionException();
return mk_Current.Item;
}
set
{
DictionaryEntry lr_Entry = (DictionaryEntry)value;
if (mk_Tree.mk_Comparer.Compare(lr_Entry.Key, mk_Current.Key) != 0)
throw new ArgumentException("Key values must not be changed.");
mk_Current.Value = lr_Entry.Value;
}
}
public override object Collection
{
get { return mk_Tree; }
}
public override void Move(int ai_Count)
{
k_Node lk_NewPos = mk_Current;
if (ai_Count > 0)
{
while (ai_Count-- > 0)
{
if (lk_NewPos == null)
throw new InvalidOperationException("Tried to moved beyond end element.");
lk_NewPos = k_Tree.Next(lk_NewPos);
}
}
else
{
while (ai_Count++ < 0)
{
if (lk_NewPos == null)
lk_NewPos = mk_Tree.mk_Right;
else
lk_NewPos = k_Tree.Previous(lk_NewPos);
if (lk_NewPos == null)
throw new InvalidOperationException("Tried to move before first element.");
}
}
mk_Current = lk_NewPos;
}
public override int Distance(k_Iterator ak_Iter)
{
k_NodeIterator lk_Iter = ak_Iter as k_NodeIterator;
if (lk_Iter == null || !object.ReferenceEquals(lk_Iter.Collection, this.Collection))
throw new ArgumentException("Cannot determine distance of iterators belonging to different collections.");
k_Iterator lk_End = mk_Tree.End;
int li_KeyDiff;
if (this == lk_End || ak_Iter == lk_End)
li_KeyDiff = (this == lk_End && this != ak_Iter) ? 1 : 0;
else
li_KeyDiff = mk_Tree.mk_Comparer.Compare(mk_Current.Key, lk_Iter.mk_Current.Key);
if (li_KeyDiff <= 0)
{
int li_Diff = 0;
k_Iterator lk_Bck = this.Clone();
for (; lk_Bck != ak_Iter && lk_Bck != lk_End; lk_Bck.Next())
--li_Diff;
if (lk_Bck == ak_Iter)
return li_Diff;
}
if (li_KeyDiff >= 0)
{
int li_Diff = 0;
k_Iterator lk_Fwd = ak_Iter.Clone();
for (; lk_Fwd != this && lk_Fwd != lk_End; lk_Fwd.Next())
++li_Diff;
if (lk_Fwd == this)
return li_Diff;
}
throw new Exception("Inconsistent state. Concurrency?");
}
public override bool Equals(object ak_Obj)
{
k_NodeIterator lk_Iter = ak_Obj as k_NodeIterator;
if (lk_Iter == null)
return false;
return object.ReferenceEquals(mk_Current, lk_Iter.mk_Current);
}
public override int GetHashCode()
{
if (mk_Current == null)
return mk_Tree.GetHashCode();
return mk_Current.GetHashCode();
}
public override k_Iterator Clone()
{
return new k_NodeIterator(mk_Tree, mk_Current);
}
internal k_Node Node
{
get { return mk_Current; }
}
}
private class k_PinnedNodeIterator : k_NodeIterator
{
public k_PinnedNodeIterator(k_Tree ak_Tree, k_Node ak_Node)
: base(ak_Tree, ak_Node)
{
}
public override void Move(int ai_Count)
{
throw new k_IteratorPinnedException();
}
}
#endregion
private k_Node mk_Head, mk_Left, mk_Right;
private k_Iterator mk_End;
private int mi_Count;
private IComparer mk_Comparer;
private bool mb_AllowDuplicateKeys;
public k_Tree()
: this(false)
{
}
public k_Tree(bool ab_AllowDuplicateKeys)
: this(ab_AllowDuplicateKeys, System.Collections.Comparer.Default)
{
}
public k_Tree(bool ab_AllowDuplicateKeys, IComparer ak_Comparer)
{
mb_AllowDuplicateKeys = ab_AllowDuplicateKeys;
mk_Comparer = ak_Comparer;
mk_End = new k_PinnedNodeIterator(this, null);
}
// IContainer Members
public k_Iterator Begin
{
get
{
if (mi_Count == 0)
return this.End;
return new k_NodeIterator(this, mk_Left);
}
}
public k_Iterator End
{
get { return mk_End; }
}
public bool IsEmpty
{
get { return (mi_Count == 0); }
}
public k_Iterator Find(object ak_Value)
{
DictionaryEntry lr_Item = (DictionaryEntry)ak_Value;
k_Node lk_Found = FindInternal(mk_Head, lr_Item.Key);
if (lk_Found != null && mk_Comparer.Compare(lk_Found.Value, lr_Item.Value) == 0)
return new k_NodeIterator(this, lk_Found);
return this.End;
}
public k_Iterator Erase(k_Iterator ak_Where)
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(this, ak_Where.Collection), "Iterator does not belong to this tree.");
k_Iterator lk_Successor = ak_Where + 1;
RemoveNode(((k_NodeIterator)ak_Where).Node);
return lk_Successor;
}
public k_Iterator Erase(k_Iterator ak_First, k_Iterator ak_Last)
{
if (ak_First == this.Begin && ak_Last == this.End)
{
Clear();
return ak_Last.Clone();
}
k_Iterator lk_Current = ak_First;
while (lk_Current != ak_Last)
lk_Current = Erase(lk_Current);
return lk_Current;
}
// IMap Members
public void Add(DictionaryEntry ar_Item)
{
Add(ar_Item.Key, ar_Item.Value);
}
public k_Iterator FindKey(object ak_Key)
{
return new k_NodeIterator(this, FindInternal(mk_Head, ak_Key));
}
public void Insert(k_Iterator ak_SrcBegin, k_Iterator ak_SrcEnd)
{
for (k_Iterator lk_Iter = ak_SrcBegin.Clone(); lk_Iter != ak_SrcEnd; lk_Iter.Next())
Add((DictionaryEntry)lk_Iter.Current);
}
// ISortedMap Members
public IComparer Comparer
{
get { return mk_Comparer; }
}
public k_Iterator LowerBound(object ak_Key)
{
k_Node lk_Node = mk_Head;
k_Node lk_Found = null;
while (lk_Node != null)
{
if (mk_Comparer.Compare(lk_Node.Key, ak_Key) < 0)
lk_Node = lk_Node.mk_Right;
else
{
lk_Found = lk_Node;
lk_Node = lk_Node.mk_Left;
}
}
return new k_NodeIterator(this, lk_Found);
}
public k_Iterator UpperBound(object ak_Key)
{
k_Node lk_Node = mk_Head;
k_Node lk_Found = null;
while (lk_Node != null)
{
if (mk_Comparer.Compare(lk_Node.Key, ak_Key) > 0)
{
lk_Found = lk_Node;
lk_Node = lk_Node.mk_Left;
}
else
lk_Node = lk_Node.mk_Right;
}
return new k_NodeIterator(this, lk_Found);
}
#region IDictionary Members
public void Add(object ak_Key, object ak_Value)
{
Insert(ref mk_Head, null, ak_Key, ak_Value, false);
mk_Head.Red = false;
++mi_Count;
}
public void Clear()
{
mi_Count = 0;
mk_Head = null;
mk_Left = null;
mk_Right = null;
}
public bool Contains(object ak_Key)
{
return (FindInternal(mk_Head, ak_Key) != null);
}
public IDictionaryEnumerator GetEnumerator()
{
return new k_IteratorDictEnumerator(this.Begin, this.End);
}
public void Remove(object ak_Key)
{
RemoveNode(FindInternal(mk_Head, ak_Key));
}
public bool IsFixedSize
{
get { return false; }
}
public bool IsReadOnly
{
get { return false; }
}
public object this[object ak_Key]
{
get
{
k_Node lk_Node = FindInternal(mk_Head, ak_Key);
if (lk_Node == null)
return null;
return lk_Node.Value;
}
set
{
k_Node lk_Node = FindInternal(mk_Head, ak_Key);
if (lk_Node == null)
Add(new DictionaryEntry(ak_Key, value));
else
lk_Node.Value = value;
}
}
public ICollection Keys
{
get
{
int i=0;
object[] lk_Keys = new object[mi_Count];
foreach (DictionaryEntry lr_Entry in this)
lk_Keys[i++] = lr_Entry.Key;
return lk_Keys;
}
}
public ICollection Values
{
get
{
int i=0;
object[] lk_Values = new object[mi_Count];
foreach (DictionaryEntry lr_Entry in this)
lk_Values[i++] = lr_Entry.Value;
return lk_Values;
}
}
#endregion
#region ICollection Members
public void CopyTo(Array ak_Array, int ai_Index)
{
foreach (DictionaryEntry lr_Entry in this)
ak_Array.SetValue(lr_Entry, ai_Index++);
}
public int Count
{
get { return mi_Count; }
}
public bool IsSynchronized
{
get { return false; }
}
public object SyncRoot
{
get { return this; }
}
#endregion
#region IEnumerable Members
IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return new k_IteratorEnumerator(this.Begin, this.End);
}
#endregion
#region ICloneable Members
public object Clone()
{
k_Tree lk_Clone = new k_Tree(mb_AllowDuplicateKeys, mk_Comparer);
lk_Clone.mi_Count = mi_Count;
CloneRecursive(mk_Head, null, ref lk_Clone.mk_Head);
lk_Clone.mk_Left = k_Tree.LeftMost(lk_Clone.mk_Head);
lk_Clone.mk_Right = k_Tree.RightMost(lk_Clone.mk_Head);
return lk_Clone;
}
#endregion
private void CloneRecursive(k_Node ak_Node, k_Node ak_Parent, ref k_Node ak_Link)
{
if (ak_Node == null)
return;
ak_Link = new k_Node(ak_Node.Key, ak_Node.Value, ak_Parent);
ak_Link.Red = ak_Node.Red;
CloneRecursive(ak_Node.mk_Left, ak_Link, ref ak_Link.mk_Left);
CloneRecursive(ak_Node.mk_Right, ak_Link, ref ak_Link.mk_Right);
}
private bool IsRed(k_Node ak_Node)
{
return (ak_Node != null && ak_Node.Red);
}
private k_Node FindInternal(k_Node ak_Node, object ak_Key)
{
while (ak_Node != null)
{
int li_Diff = mk_Comparer.Compare(ak_Key, ak_Node.Key);
if (li_Diff == 0)
return ak_Node;
ak_Node = (li_Diff < 0) ? ak_Node.mk_Left : ak_Node.mk_Right;
}
return null;
}
/// <summary>
/// Return leftmost node in subtree.
/// </summary>
/// <param name="ak_Node">Node where to start search</param>
/// <returns>Found node</returns>
private static k_Node LeftMost(k_Node ak_Node)
{
if (ak_Node == null)
return null;
while (ak_Node.mk_Left != null)
ak_Node = ak_Node.mk_Left;
return ak_Node;
}
/// <summary>
/// Return rightmost node in subtree.
/// </summary>
/// <param name="ak_Node">Node where to start search</param>
/// <returns>Found node</returns>
private static k_Node RightMost(k_Node ak_Node)
{
if (ak_Node == null)
return null;
while (ak_Node.mk_Right != null)
ak_Node = ak_Node.mk_Right;
return ak_Node;
}
private static k_Node Previous(k_Node ak_Node) // the next smaller
{
if (ak_Node.mk_Left != null)
return RightMost(ak_Node.mk_Left);
k_Node lk_Parent = ak_Node.mk_Parent;
while (lk_Parent != null && lk_Parent.mk_Left == ak_Node)
{
ak_Node = lk_Parent;
lk_Parent = lk_Parent.mk_Parent;
}
return lk_Parent;
}
private static k_Node Next(k_Node ak_Node)
{
if (ak_Node.mk_Right != null)
return LeftMost(ak_Node.mk_Right);
k_Node lk_Parent = ak_Node.mk_Parent;
while (lk_Parent != null && lk_Parent.mk_Right == ak_Node)
{
ak_Node = lk_Parent;
lk_Parent = lk_Parent.mk_Parent;
}
return lk_Parent;
}
private void RemoveNode(k_Node ak_Node)
{
if (ak_Node == null)
return;
if (ak_Node == mk_Head)
UnlinkNode(ref mk_Head);
else if (ak_Node == ak_Node.mk_Parent.mk_Right)
UnlinkNode(ref ak_Node.mk_Parent.mk_Right);
else
UnlinkNode(ref ak_Node.mk_Parent.mk_Left);
}
private void UnlinkNode(ref k_Node ak_Node)
{
bool lb_Red = ak_Node.Red;
k_Node lk_Erased = ak_Node;
k_Node lk_PatchNode = null;
if (ak_Node.mk_Right == null)
lk_PatchNode = ak_Node.mk_Left;
else if (ak_Node.mk_Left == null)
lk_PatchNode = ak_Node.mk_Right;
else
lk_PatchNode = ak_Node;
k_Node lk_PatchParent = null, lk_FixNode = null;
if (lk_PatchNode == null)
{
lk_PatchParent = ak_Node.mk_Parent;
ak_Node = null;
}
else if (lk_PatchNode != ak_Node)
{
lk_PatchNode.mk_Parent = ak_Node.mk_Parent;
ak_Node = lk_PatchNode;
lk_PatchParent = lk_PatchNode.mk_Parent;
}
else
{
// two subtrees
lk_PatchNode = RightMost(ak_Node.mk_Left);
if (lk_PatchNode.mk_Parent.mk_Right == lk_PatchNode)
lk_PatchNode.mk_Parent.mk_Right = lk_PatchNode.mk_Left;
else
lk_PatchNode.mk_Parent.mk_Left = lk_PatchNode.mk_Left;
lb_Red = lk_PatchNode.Red;
if (lk_PatchNode.mk_Left != null)
lk_PatchNode.mk_Left.mk_Parent = lk_PatchNode.mk_Parent;
lk_PatchParent = lk_PatchNode.mk_Parent;
lk_FixNode = lk_PatchNode.mk_Left;
k_Node.SwapItems(ak_Node, lk_PatchNode);
// ensure that mk_Left and/or mk_Right are corrected after unlink
lk_Erased = lk_PatchNode;
}
if (!lb_Red && lk_PatchParent != null)
{
// erased node was black link - rebalance the tree
while (!IsRed(lk_FixNode) && lk_FixNode != mk_Head)
{
if (lk_PatchParent.mk_Left != null || lk_PatchParent.mk_Right != null)
{
if (lk_PatchParent.mk_Left == lk_FixNode)
{
// fixup right subtree
k_Node lk_Node = lk_PatchParent.mk_Right;
if (IsRed(lk_Node))
{
lk_Node.Red = false;
lk_PatchParent.Red = true;
RotateLeft(lk_PatchParent);
lk_Node = lk_PatchParent.mk_Right;
}
if (lk_Node != null)
{
if (!IsRed(lk_Node.mk_Left) && !IsRed(lk_Node.mk_Right))
lk_Node.Red = true;
else
{
if (!IsRed(lk_Node.mk_Right))
{
lk_Node.Red = true;
lk_Node.mk_Left.Red = false;
RotateRight(lk_Node);
lk_Node = lk_PatchParent.mk_Right;
}
lk_Node.Red = lk_PatchParent.Red;
lk_PatchParent.Red = false;
lk_Node.mk_Right.Red = false;
RotateLeft(lk_PatchParent);
break;
}
}
}
else
{
// fixup leftsubtree
k_Node lk_Node = lk_PatchParent.mk_Left;
if (IsRed(lk_Node))
{
lk_Node.Red = false;
lk_PatchParent.Red = true;
RotateRight(lk_PatchParent);
lk_Node = lk_PatchParent.mk_Left;
}
if (lk_Node != null)
{
if (!IsRed(lk_Node.mk_Left) && !IsRed(lk_Node.mk_Right))
lk_Node.Red = true;
else
{
if (!IsRed(lk_Node.mk_Left))
{
lk_Node.Red = true;
lk_Node.mk_Right.Red = false;
RotateLeft(lk_Node);
lk_Node = lk_PatchParent.mk_Left;
}
lk_Node.Red = lk_PatchParent.Red;
lk_PatchParent.Red = false;
lk_Node.mk_Left.Red = false;
RotateRight(lk_PatchParent);
break;
}
}
}
}
lk_FixNode = lk_PatchParent;
lk_PatchParent = lk_PatchParent.mk_Parent;
}
if (lk_FixNode != null)
lk_FixNode.Red = false;
}
--mi_Count;
if (object.ReferenceEquals(lk_Erased, mk_Right))
mk_Right = k_Tree.RightMost(mk_Head);
if (object.ReferenceEquals(lk_Erased, mk_Left))
mk_Left = k_Tree.LeftMost(mk_Head);
}
private void Insert(ref k_Node ak_Node, k_Node ak_Parent, object ak_Key, object ak_Value, bool ab_RightMove)
{
if (ak_Node == null)
{
ak_Node = new k_Node(ak_Key, ak_Value, ak_Parent);
if (object.ReferenceEquals(ak_Parent, mk_Right) && (ak_Parent == null || ab_RightMove))
mk_Right = ak_Node;
if (object.ReferenceEquals(ak_Parent, mk_Left) && (ak_Parent == null || !ab_RightMove))
mk_Left = ak_Node;
return;
}
if (IsRed(ak_Node.mk_Left) && IsRed(ak_Node.mk_Right))
{
ak_Node.Red = true;
ak_Node.mk_Left.Red = false;
ak_Node.mk_Right.Red = false;
}
int li_Diff = mk_Comparer.Compare(ak_Key, ak_Node.Key);
if (!mb_AllowDuplicateKeys && li_Diff == 0)
throw new ArgumentException("An element with the same key already exists in the tree.");
if (li_Diff < 0)
{
Insert(ref ak_Node.mk_Left, ak_Node, ak_Key, ak_Value, false);
if (IsRed(ak_Node) && IsRed(ak_Node.mk_Left) && ab_RightMove)
ak_Node = RotateRight(ak_Node);
if (IsRed(ak_Node.mk_Left) && IsRed(ak_Node.mk_Left.mk_Left))
{
ak_Node = RotateRight(ak_Node);
ak_Node.Red = false;
ak_Node.mk_Right.Red = true;
}
}
else
{
Insert(ref ak_Node.mk_Right, ak_Node, ak_Key, ak_Value, true);
if (IsRed(ak_Node) && IsRed(ak_Node.mk_Right) && !ab_RightMove)
ak_Node = RotateLeft(ak_Node);
if (IsRed(ak_Node.mk_Right) && IsRed(ak_Node.mk_Right.mk_Right))
{
ak_Node = RotateLeft(ak_Node);
ak_Node.Red = false;
ak_Node.mk_Left.Red = true;
}
}
}
/*
A right rotation: ak_Node.Left takes old position of ak_Node.
Makes the old root the right subtree of the new root.
5 2
2 7 -> 1 5
1 3 6 8 3 7
6 8
*/
private k_Node RotateRight(k_Node ak_Node)
{
k_Node lk_Tmp = ak_Node.mk_Left;
lk_Tmp.mk_Parent = ak_Node.mk_Parent;
ak_Node.mk_Parent = lk_Tmp;
ak_Node.mk_Left = lk_Tmp.mk_Right;
if (ak_Node.mk_Left != null)
ak_Node.mk_Left.mk_Parent = ak_Node;
lk_Tmp.mk_Right = ak_Node;
// correct parent
if (lk_Tmp.mk_Parent == null)
mk_Head = lk_Tmp;
else if (lk_Tmp.mk_Parent.mk_Right == ak_Node)
lk_Tmp.mk_Parent.mk_Right = lk_Tmp;
else
lk_Tmp.mk_Parent.mk_Left = lk_Tmp;
return lk_Tmp;
}
/*
A left rotation: ak_Node.Right takes old position of ak_Node.
Makes the old root the left subtree of the new root.
5 7
2 7 -> 5 8
1 3 6 8 2 6
1 3
*/
private k_Node RotateLeft(k_Node ak_Node)
{
k_Node lk_Tmp = ak_Node.mk_Right;
lk_Tmp.mk_Parent = ak_Node.mk_Parent;
ak_Node.mk_Parent = lk_Tmp;
ak_Node.mk_Right = lk_Tmp.mk_Left;
if (ak_Node.mk_Right != null)
ak_Node.mk_Right.mk_Parent = ak_Node;
lk_Tmp.mk_Left = ak_Node;
// correct parent
if (lk_Tmp.mk_Parent == null)
mk_Head = lk_Tmp;
else if (lk_Tmp.mk_Parent.mk_Right == ak_Node)
lk_Tmp.mk_Parent.mk_Right = lk_Tmp;
else
lk_Tmp.mk_Parent.mk_Left = lk_Tmp;
return lk_Tmp;
}
}
}

View File

@@ -0,0 +1,143 @@
using System;
using System.Collections;
namespace System.util.collections
{
/// <summary>
/// One dimensional array of variable size
/// </summary>
public class k_Vector : ArrayList, ISequence
{
public k_Vector()
: base()
{
}
public k_Vector(int ai_Capacity)
: base(ai_Capacity)
{
}
public k_Vector(ICollection ak_Collection)
: base(ak_Collection)
{
}
// IContainer Members
public k_Iterator Begin
{
get { return k_IListIterator.CreateBegin(this); }
}
public k_Iterator End
{
get { return k_IListIterator.CreateEnd(this); }
}
public bool IsEmpty
{
get { return (this.Count == 0); }
}
public k_Iterator Find(object ak_Value)
{
int li_Index = this.IndexOf(ak_Value);
if (li_Index < 0)
return this.End;
return new k_IListIterator(this, li_Index);
}
public k_Iterator Erase(k_Iterator ak_Where)
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(this, ak_Where.Collection), "Iterator does not belong to this collection.");
int li_Index = ((k_IListIterator)ak_Where).Index;
if (li_Index < this.Count)
base.RemoveAt(li_Index);
return new k_IListIterator(this, li_Index);
}
public k_Iterator Erase(k_Iterator ak_First, k_Iterator ak_Last)
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(this, ak_First.Collection) && object.ReferenceEquals(this, ak_Last.Collection), "Iterators do not belong to this collection.");
int li_FirstIndex = ((k_IListIterator)ak_First).Index;
int li_Count = ak_Last - ak_First;
base.RemoveRange(li_FirstIndex, li_Count);
return new k_IListIterator(this, li_FirstIndex);
}
// ISequence Members
public object Front
{
get { return this.Begin.Current; }
set { this.Begin.Current = value; }
}
public object Back
{
get { return (this.End-1).Current; }
set { (this.End-1).Current = value; }
}
public void PushFront(object ak_Value)
{
Insert(this.Begin, ak_Value);
}
public void PopFront()
{
Erase(this.Begin);
}
public void PushBack(object ak_Value)
{
Insert(this.End, ak_Value);
}
public void PopBack()
{
Erase(this.End-1);
}
public void Assign(k_Iterator ak_SrcBegin, k_Iterator ak_SrcEnd)
{
Clear();
Insert(this.End, ak_SrcBegin, ak_SrcEnd);
}
public void Assign(object ak_Value, int ai_Count)
{
Clear();
Insert(this.End, ak_Value, ai_Count);
}
public void Insert(k_Iterator ak_Where, object ak_Value)
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(this, ak_Where.Collection), "Iterator does not belong to this collection.");
this.Insert(((k_IListIterator)ak_Where).Index, ak_Value);
}
public void Insert(k_Iterator ak_Where, k_Iterator ak_SrcBegin, k_Iterator ak_SrcEnd)
{
//System.Diagnostics.Debug.Assert(object.ReferenceEquals(this, ak_Where.Collection), "Iterator does not belong to this collection.");
InsertRange(((k_IListIterator)ak_Where).Index, new k_CollectionOnIterators(ak_SrcBegin, ak_SrcEnd));
}
public void Insert(k_Iterator ak_Where, object ak_Value, int ai_Count)
{
int li_Pos = ((k_IListIterator)ak_Where).Index;
for (int i=0; i<ai_Count; ++i)
base.Insert(li_Pos+i, ak_Value);
}
#region ICloneable Members
public override object Clone()
{
return new k_Vector(this);
}
#endregion
}
}