New ordered tree to replace k_Tree.
git-svn-id: svn://svn.code.sf.net/p/itextsharp/code/trunk@46 820d3149-562b-4f88-9aa4-a8e61a3485cfmaster
parent
1ea1146eb1
commit
e3807b1d00
|
@ -1,49 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,64 +0,0 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -1,470 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,658 +0,0 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,323 +0,0 @@
|
|||
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.")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,537 +0,0 @@
|
|||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,931 @@
|
|||
using System.Collections;
|
||||
using System.Text;
|
||||
using System;
|
||||
|
||||
/*
|
||||
* $Id: $
|
||||
*
|
||||
* Copyright 2009 by Paulo Soares.
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
* (the "License"); you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is 'iText, a free JAVA-PDF library'.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
|
||||
* the Initial Developer are Copyright (C) 1999-2009 by Bruno Lowagie.
|
||||
* All Rights Reserved.
|
||||
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
|
||||
* are Copyright (C) 2000-2009 by Paulo Soares. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s): all the names of the contributors are added in the source code
|
||||
* where applicable.
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of the
|
||||
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
|
||||
* provisions of LGPL are applicable instead of those above. If you wish to
|
||||
* allow use of your version of this file only under the terms of the LGPL
|
||||
* License and not to allow others to use your version of this file under
|
||||
* the MPL, indicate your decision by deleting the provisions above and
|
||||
* replace them with the notice and other provisions required by the LGPL.
|
||||
* If you do not delete the provisions above, a recipient may use your version
|
||||
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the MPL as stated above or under the terms of the GNU
|
||||
* Library General Public License as published by the Free Software Foundation;
|
||||
* either version 2 of the License, or any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
|
||||
* details.
|
||||
*
|
||||
* If you didn't download this code from the following link, you should check if
|
||||
* you aren't using an obsolete version:
|
||||
* http://www.lowagie.com/iText/
|
||||
*/
|
||||
|
||||
namespace System.util.collections {
|
||||
public class OrderedTree {
|
||||
// the number of nodes contained in the tree
|
||||
private int intCount;
|
||||
// the tree
|
||||
private OrderedTreeNode rbTree;
|
||||
// sentinelNode is convenient way of indicating a leaf node.
|
||||
public static OrderedTreeNode sentinelNode;
|
||||
// the node that was last found; used to optimize searches
|
||||
private OrderedTreeNode lastNodeFound;
|
||||
private Random rand = new Random();
|
||||
|
||||
static OrderedTree() {
|
||||
// set up the sentinel node. the sentinel node is the key to a successfull
|
||||
// implementation and for understanding the red-black tree properties.
|
||||
sentinelNode = new OrderedTreeNode();
|
||||
sentinelNode.Left = sentinelNode.Right = sentinelNode;
|
||||
sentinelNode.Parent = null;
|
||||
sentinelNode.Color = OrderedTreeNode.BLACK;
|
||||
}
|
||||
|
||||
public OrderedTree() {
|
||||
rbTree = sentinelNode;
|
||||
lastNodeFound = sentinelNode;
|
||||
}
|
||||
|
||||
public object this[IComparable key] {
|
||||
get {
|
||||
return GetData(key);
|
||||
}
|
||||
set {
|
||||
if(key == null)
|
||||
throw new ArgumentNullException("Key is null");
|
||||
|
||||
// traverse tree - find where node belongs
|
||||
int result = 0;
|
||||
// create new node
|
||||
OrderedTreeNode node = new OrderedTreeNode();
|
||||
OrderedTreeNode temp = rbTree; // grab the rbTree node of the tree
|
||||
|
||||
while(temp != sentinelNode) {
|
||||
// find Parent
|
||||
node.Parent = temp;
|
||||
result = key.CompareTo(temp.Key);
|
||||
if(result == 0) {
|
||||
lastNodeFound = temp;
|
||||
temp.Data = value;
|
||||
return;
|
||||
}
|
||||
if(result > 0)
|
||||
temp = temp.Right;
|
||||
else
|
||||
temp = temp.Left;
|
||||
}
|
||||
|
||||
// setup node
|
||||
node.Key = key;
|
||||
node.Data = value;
|
||||
node.Left = sentinelNode;
|
||||
node.Right = sentinelNode;
|
||||
|
||||
// insert node into tree starting at parent's location
|
||||
if(node.Parent != null) {
|
||||
result = node.Key.CompareTo(node.Parent.Key);
|
||||
if(result > 0)
|
||||
node.Parent.Right = node;
|
||||
else
|
||||
node.Parent.Left = node;
|
||||
}
|
||||
else
|
||||
rbTree = node; // first node added
|
||||
|
||||
RestoreAfterInsert(node); // restore red-black properities
|
||||
|
||||
lastNodeFound = node;
|
||||
|
||||
intCount = intCount + 1;
|
||||
}
|
||||
}
|
||||
|
||||
///<summary>
|
||||
/// Add
|
||||
/// args: ByVal key As IComparable, ByVal data As Object
|
||||
/// key is object that implements IComparable interface
|
||||
/// performance tip: change to use use int type (such as the hashcode)
|
||||
///</summary>
|
||||
public void Add(IComparable key, object data) {
|
||||
if(key == null)
|
||||
throw(new ArgumentNullException("Key is null"));
|
||||
|
||||
// traverse tree - find where node belongs
|
||||
int result = 0;
|
||||
// create new node
|
||||
OrderedTreeNode node = new OrderedTreeNode();
|
||||
OrderedTreeNode temp = rbTree; // grab the rbTree node of the tree
|
||||
|
||||
while(temp != sentinelNode) {
|
||||
// find Parent
|
||||
node.Parent = temp;
|
||||
result = key.CompareTo(temp.Key);
|
||||
if(result == 0)
|
||||
throw new ArgumentException("Key duplicated");
|
||||
if(result > 0)
|
||||
temp = temp.Right;
|
||||
else
|
||||
temp = temp.Left;
|
||||
}
|
||||
|
||||
// setup node
|
||||
node.Key = key;
|
||||
node.Data = data;
|
||||
node.Left = sentinelNode;
|
||||
node.Right = sentinelNode;
|
||||
|
||||
// insert node into tree starting at parent's location
|
||||
if(node.Parent != null) {
|
||||
result = node.Key.CompareTo(node.Parent.Key);
|
||||
if(result > 0)
|
||||
node.Parent.Right = node;
|
||||
else
|
||||
node.Parent.Left = node;
|
||||
}
|
||||
else
|
||||
rbTree = node; // first node added
|
||||
|
||||
RestoreAfterInsert(node); // restore red-black properities
|
||||
|
||||
lastNodeFound = node;
|
||||
|
||||
intCount = intCount + 1;
|
||||
}
|
||||
///<summary>
|
||||
/// RestoreAfterInsert
|
||||
/// Additions to red-black trees usually destroy the red-black
|
||||
/// properties. Examine the tree and restore. Rotations are normally
|
||||
/// required to restore it
|
||||
///</summary>
|
||||
private void RestoreAfterInsert(OrderedTreeNode x) {
|
||||
// x and y are used as variable names for brevity, in a more formal
|
||||
// implementation, you should probably change the names
|
||||
|
||||
OrderedTreeNode y;
|
||||
|
||||
// maintain red-black tree properties after adding x
|
||||
while(x != rbTree && x.Parent.Color == OrderedTreeNode.RED) {
|
||||
// Parent node is .Colored red;
|
||||
if(x.Parent == x.Parent.Parent.Left) { // determine traversal path
|
||||
// is it on the Left or Right subtree?
|
||||
y = x.Parent.Parent.Right; // get uncle
|
||||
if(y!= null && y.Color == OrderedTreeNode.RED) {
|
||||
// uncle is red; change x's Parent and uncle to black
|
||||
x.Parent.Color = OrderedTreeNode.BLACK;
|
||||
y.Color = OrderedTreeNode.BLACK;
|
||||
// grandparent must be red. Why? Every red node that is not
|
||||
// a leaf has only black children
|
||||
x.Parent.Parent.Color = OrderedTreeNode.RED;
|
||||
x = x.Parent.Parent; // continue loop with grandparent
|
||||
}
|
||||
else {
|
||||
// uncle is black; determine if x is greater than Parent
|
||||
if(x == x.Parent.Right) {
|
||||
// yes, x is greater than Parent; rotate Left
|
||||
// make x a Left child
|
||||
x = x.Parent;
|
||||
RotateLeft(x);
|
||||
}
|
||||
// no, x is less than Parent
|
||||
x.Parent.Color = OrderedTreeNode.BLACK; // make Parent black
|
||||
x.Parent.Parent.Color = OrderedTreeNode.RED; // make grandparent black
|
||||
RotateRight(x.Parent.Parent); // rotate right
|
||||
}
|
||||
}
|
||||
else {
|
||||
// x's Parent is on the Right subtree
|
||||
// this code is the same as above with "Left" and "Right" swapped
|
||||
y = x.Parent.Parent.Left;
|
||||
if(y!= null && y.Color == OrderedTreeNode.RED) {
|
||||
x.Parent.Color = OrderedTreeNode.BLACK;
|
||||
y.Color = OrderedTreeNode.BLACK;
|
||||
x.Parent.Parent.Color = OrderedTreeNode.RED;
|
||||
x = x.Parent.Parent;
|
||||
}
|
||||
else {
|
||||
if(x == x.Parent.Left) {
|
||||
x = x.Parent;
|
||||
RotateRight(x);
|
||||
}
|
||||
x.Parent.Color = OrderedTreeNode.BLACK;
|
||||
x.Parent.Parent.Color = OrderedTreeNode.RED;
|
||||
RotateLeft(x.Parent.Parent);
|
||||
}
|
||||
}
|
||||
}
|
||||
rbTree.Color = OrderedTreeNode.BLACK; // rbTree should always be black
|
||||
}
|
||||
|
||||
///<summary>
|
||||
/// RotateLeft
|
||||
/// Rebalance the tree by rotating the nodes to the left
|
||||
///</summary>
|
||||
public void RotateLeft(OrderedTreeNode x) {
|
||||
// pushing node x down and to the Left to balance the tree. x's Right child (y)
|
||||
// replaces x (since y > x), and y's Left child becomes x's Right child
|
||||
// (since it's < y but > x).
|
||||
|
||||
OrderedTreeNode y = x.Right; // get x's Right node, this becomes y
|
||||
|
||||
// set x's Right link
|
||||
x.Right = y.Left; // y's Left child's becomes x's Right child
|
||||
|
||||
// modify parents
|
||||
if(y.Left != sentinelNode)
|
||||
y.Left.Parent = x; // sets y's Left Parent to x
|
||||
|
||||
if(y != sentinelNode)
|
||||
y.Parent = x.Parent; // set y's Parent to x's Parent
|
||||
|
||||
if(x.Parent != null) {
|
||||
// determine which side of it's Parent x was on
|
||||
if(x == x.Parent.Left)
|
||||
x.Parent.Left = y; // set Left Parent to y
|
||||
else
|
||||
x.Parent.Right = y; // set Right Parent to y
|
||||
}
|
||||
else
|
||||
rbTree = y; // at rbTree, set it to y
|
||||
|
||||
// link x and y
|
||||
y.Left = x; // put x on y's Left
|
||||
if(x != sentinelNode) // set y as x's Parent
|
||||
x.Parent = y;
|
||||
}
|
||||
///<summary>
|
||||
/// RotateRight
|
||||
/// Rebalance the tree by rotating the nodes to the right
|
||||
///</summary>
|
||||
public void RotateRight(OrderedTreeNode x) {
|
||||
// pushing node x down and to the Right to balance the tree. x's Left child (y)
|
||||
// replaces x (since x < y), and y's Right child becomes x's Left child
|
||||
// (since it's < x but > y).
|
||||
|
||||
OrderedTreeNode y = x.Left; // get x's Left node, this becomes y
|
||||
|
||||
// set x's Right link
|
||||
x.Left = y.Right; // y's Right child becomes x's Left child
|
||||
|
||||
// modify parents
|
||||
if(y.Right != sentinelNode)
|
||||
y.Right.Parent = x; // sets y's Right Parent to x
|
||||
|
||||
if(y != sentinelNode)
|
||||
y.Parent = x.Parent; // set y's Parent to x's Parent
|
||||
|
||||
if(x.Parent != null) { // null=rbTree, could also have used rbTree
|
||||
// determine which side of it's Parent x was on
|
||||
if(x == x.Parent.Right)
|
||||
x.Parent.Right = y; // set Right Parent to y
|
||||
else
|
||||
x.Parent.Left = y; // set Left Parent to y
|
||||
}
|
||||
else
|
||||
rbTree = y; // at rbTree, set it to y
|
||||
|
||||
// link x and y
|
||||
y.Right = x; // put x on y's Right
|
||||
if(x != sentinelNode) // set y as x's Parent
|
||||
x.Parent = y;
|
||||
}
|
||||
|
||||
public bool ContainsKey(IComparable key) {
|
||||
OrderedTreeNode treeNode = rbTree; // begin at root
|
||||
int result = 0;
|
||||
// traverse tree until node is found
|
||||
while(treeNode != sentinelNode) {
|
||||
result = key.CompareTo(treeNode.Key);
|
||||
if(result == 0) {
|
||||
lastNodeFound = treeNode;
|
||||
return true;
|
||||
}
|
||||
if(result < 0)
|
||||
treeNode = treeNode.Left;
|
||||
else
|
||||
treeNode = treeNode.Right;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
///<summary>
|
||||
/// GetData
|
||||
/// Gets the data object associated with the specified key
|
||||
///<summary>
|
||||
public object GetData(IComparable key) {
|
||||
if(key == null)
|
||||
throw new ArgumentNullException("Key is null");
|
||||
int result;
|
||||
|
||||
OrderedTreeNode treeNode = rbTree; // begin at root
|
||||
|
||||
// traverse tree until node is found
|
||||
while(treeNode != sentinelNode) {
|
||||
result = key.CompareTo(treeNode.Key);
|
||||
if(result == 0) {
|
||||
lastNodeFound = treeNode;
|
||||
return treeNode.Data;
|
||||
}
|
||||
if(result < 0)
|
||||
treeNode = treeNode.Left;
|
||||
else
|
||||
treeNode = treeNode.Right;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
///<summary>
|
||||
/// GetMinKey
|
||||
/// Returns the minimum key value
|
||||
///<summary>
|
||||
public IComparable GetMinKey() {
|
||||
OrderedTreeNode treeNode = rbTree;
|
||||
|
||||
if(treeNode == null || treeNode == sentinelNode)
|
||||
throw(new InvalidOperationException("Tree is empty"));
|
||||
|
||||
// traverse to the extreme left to find the smallest key
|
||||
while(treeNode.Left != sentinelNode)
|
||||
treeNode = treeNode.Left;
|
||||
|
||||
lastNodeFound = treeNode;
|
||||
|
||||
return treeNode.Key;
|
||||
|
||||
}
|
||||
///<summary>
|
||||
/// GetMaxKey
|
||||
/// Returns the maximum key value
|
||||
///<summary>
|
||||
public IComparable GetMaxKey() {
|
||||
OrderedTreeNode treeNode = rbTree;
|
||||
|
||||
if(treeNode == null || treeNode == sentinelNode)
|
||||
throw(new InvalidOperationException("Tree is empty"));
|
||||
|
||||
// traverse to the extreme right to find the largest key
|
||||
while(treeNode.Right != sentinelNode)
|
||||
treeNode = treeNode.Right;
|
||||
|
||||
lastNodeFound = treeNode;
|
||||
|
||||
return treeNode.Key;
|
||||
|
||||
}
|
||||
///<summary>
|
||||
/// GetMinValue
|
||||
/// Returns the object having the minimum key value
|
||||
///<summary>
|
||||
public object GetMinValue() {
|
||||
return GetData(GetMinKey());
|
||||
}
|
||||
///<summary>
|
||||
/// GetMaxValue
|
||||
/// Returns the object having the maximum key
|
||||
///<summary>
|
||||
public object GetMaxValue() {
|
||||
return GetData(GetMaxKey());
|
||||
}
|
||||
///<summary>
|
||||
/// GetEnumerator
|
||||
/// return an enumerator that returns the tree nodes in order
|
||||
///<summary>
|
||||
public OrderedTreeEnumerator GetEnumerator() {
|
||||
// elements is simply a generic name to refer to the
|
||||
// data objects the nodes contain
|
||||
return Elements(true);
|
||||
}
|
||||
///<summary>
|
||||
/// Keys
|
||||
/// if(ascending is true, the keys will be returned in ascending order, else
|
||||
/// the keys will be returned in descending order.
|
||||
///<summary>
|
||||
public OrderedTreeEnumerator Keys {
|
||||
get {
|
||||
return KeyElements(true);
|
||||
}
|
||||
}
|
||||
public OrderedTreeEnumerator KeyElements(bool ascending) {
|
||||
return new OrderedTreeEnumerator(rbTree, true, ascending);
|
||||
}
|
||||
///<summary>
|
||||
/// Values
|
||||
/// Provided for .NET compatibility.
|
||||
///<summary>
|
||||
public OrderedTreeEnumerator Values {
|
||||
get {
|
||||
return Elements(true);
|
||||
}
|
||||
}
|
||||
///<summary>
|
||||
/// Elements
|
||||
/// Returns an enumeration of the data objects.
|
||||
/// if(ascending is true, the objects will be returned in ascending order,
|
||||
/// else the objects will be returned in descending order.
|
||||
///<summary>
|
||||
public OrderedTreeEnumerator Elements() {
|
||||
return Elements(true);
|
||||
}
|
||||
public OrderedTreeEnumerator Elements(bool ascending) {
|
||||
return new OrderedTreeEnumerator(rbTree, false, ascending);
|
||||
}
|
||||
///<summary>
|
||||
/// IsEmpty
|
||||
/// Is the tree empty?
|
||||
///<summary>
|
||||
public bool IsEmpty() {
|
||||
return (rbTree == null || rbTree == sentinelNode);
|
||||
}
|
||||
///<summary>
|
||||
/// Remove
|
||||
/// removes the key and data object (delete)
|
||||
///<summary>
|
||||
public void Remove(IComparable key) {
|
||||
if(key == null)
|
||||
throw new ArgumentNullException("Key is null");
|
||||
|
||||
// find node
|
||||
int result;
|
||||
OrderedTreeNode node;
|
||||
|
||||
// see if node to be deleted was the last one found
|
||||
result = key.CompareTo(lastNodeFound.Key);
|
||||
if(result == 0)
|
||||
node = lastNodeFound;
|
||||
else {
|
||||
// not found, must search
|
||||
node = rbTree;
|
||||
while(node != sentinelNode) {
|
||||
result = key.CompareTo(node.Key);
|
||||
if(result == 0)
|
||||
break;
|
||||
if(result < 0)
|
||||
node = node.Left;
|
||||
else
|
||||
node = node.Right;
|
||||
}
|
||||
|
||||
if(node == sentinelNode)
|
||||
return; // key not found
|
||||
}
|
||||
|
||||
Delete(node);
|
||||
|
||||
intCount = intCount - 1;
|
||||
}
|
||||
///<summary>
|
||||
/// Delete
|
||||
/// Delete a node from the tree and restore red black properties
|
||||
///<summary>
|
||||
private void Delete(OrderedTreeNode z) {
|
||||
// A node to be deleted will be:
|
||||
// 1. a leaf with no children
|
||||
// 2. have one child
|
||||
// 3. have two children
|
||||
// If the deleted node is red, the red black properties still hold.
|
||||
// If the deleted node is black, the tree needs rebalancing
|
||||
|
||||
OrderedTreeNode x = new OrderedTreeNode(); // work node to contain the replacement node
|
||||
OrderedTreeNode y; // work node
|
||||
|
||||
// find the replacement node (the successor to x) - the node one with
|
||||
// at *most* one child.
|
||||
if(z.Left == sentinelNode || z.Right == sentinelNode)
|
||||
y = z; // node has sentinel as a child
|
||||
else {
|
||||
// z has two children, find replacement node which will
|
||||
// be the leftmost node greater than z
|
||||
y = z.Right; // traverse right subtree
|
||||
while(y.Left != sentinelNode) // to find next node in sequence
|
||||
y = y.Left;
|
||||
}
|
||||
|
||||
// at this point, y contains the replacement node. it's content will be copied
|
||||
// to the valules in the node to be deleted
|
||||
|
||||
// x (y's only child) is the node that will be linked to y's old parent.
|
||||
if(y.Left != sentinelNode)
|
||||
x = y.Left;
|
||||
else
|
||||
x = y.Right;
|
||||
|
||||
// replace x's parent with y's parent and
|
||||
// link x to proper subtree in parent
|
||||
// this removes y from the chain
|
||||
x.Parent = y.Parent;
|
||||
if(y.Parent != null)
|
||||
if(y == y.Parent.Left)
|
||||
y.Parent.Left = x;
|
||||
else
|
||||
y.Parent.Right = x;
|
||||
else
|
||||
rbTree = x; // make x the root node
|
||||
|
||||
// copy the values from y (the replacement node) to the node being deleted.
|
||||
// note: this effectively deletes the node.
|
||||
if(y != z) {
|
||||
z.Key = y.Key;
|
||||
z.Data = y.Data;
|
||||
}
|
||||
|
||||
if(y.Color == OrderedTreeNode.BLACK)
|
||||
RestoreAfterDelete(x);
|
||||
|
||||
lastNodeFound = sentinelNode;
|
||||
}
|
||||
|
||||
///<summary>
|
||||
/// RestoreAfterDelete
|
||||
/// Deletions from red-black trees may destroy the red-black
|
||||
/// properties. Examine the tree and restore. Rotations are normally
|
||||
/// required to restore it
|
||||
///</summary>
|
||||
private void RestoreAfterDelete(OrderedTreeNode x) {
|
||||
// maintain Red-Black tree balance after deleting node
|
||||
|
||||
OrderedTreeNode y;
|
||||
|
||||
while(x != rbTree && x.Color == OrderedTreeNode.BLACK) {
|
||||
if(x == x.Parent.Left) { // determine sub tree from parent
|
||||
y = x.Parent.Right; // y is x's sibling
|
||||
if(y.Color == OrderedTreeNode.RED) {
|
||||
// x is black, y is red - make both black and rotate
|
||||
y.Color = OrderedTreeNode.BLACK;
|
||||
x.Parent.Color = OrderedTreeNode.RED;
|
||||
RotateLeft(x.Parent);
|
||||
y = x.Parent.Right;
|
||||
}
|
||||
if(y.Left.Color == OrderedTreeNode.BLACK &&
|
||||
y.Right.Color == OrderedTreeNode.BLACK) {
|
||||
// children are both black
|
||||
y.Color = OrderedTreeNode.RED; // change parent to red
|
||||
x = x.Parent; // move up the tree
|
||||
}
|
||||
else {
|
||||
if(y.Right.Color == OrderedTreeNode.BLACK) {
|
||||
y.Left.Color = OrderedTreeNode.BLACK;
|
||||
y.Color = OrderedTreeNode.RED;
|
||||
RotateRight(y);
|
||||
y = x.Parent.Right;
|
||||
}
|
||||
y.Color = x.Parent.Color;
|
||||
x.Parent.Color = OrderedTreeNode.BLACK;
|
||||
y.Right.Color = OrderedTreeNode.BLACK;
|
||||
RotateLeft(x.Parent);
|
||||
x = rbTree;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// right subtree - same as code above with right and left swapped
|
||||
y = x.Parent.Left;
|
||||
if(y.Color == OrderedTreeNode.RED) {
|
||||
y.Color = OrderedTreeNode.BLACK;
|
||||
x.Parent.Color = OrderedTreeNode.RED;
|
||||
RotateRight (x.Parent);
|
||||
y = x.Parent.Left;
|
||||
}
|
||||
if(y.Right.Color == OrderedTreeNode.BLACK &&
|
||||
y.Left.Color == OrderedTreeNode.BLACK) {
|
||||
y.Color = OrderedTreeNode.RED;
|
||||
x = x.Parent;
|
||||
}
|
||||
else {
|
||||
if(y.Left.Color == OrderedTreeNode.BLACK) {
|
||||
y.Right.Color = OrderedTreeNode.BLACK;
|
||||
y.Color = OrderedTreeNode.RED;
|
||||
RotateLeft(y);
|
||||
y = x.Parent.Left;
|
||||
}
|
||||
y.Color = x.Parent.Color;
|
||||
x.Parent.Color = OrderedTreeNode.BLACK;
|
||||
y.Left.Color = OrderedTreeNode.BLACK;
|
||||
RotateRight(x.Parent);
|
||||
x = rbTree;
|
||||
}
|
||||
}
|
||||
}
|
||||
x.Color = OrderedTreeNode.BLACK;
|
||||
}
|
||||
|
||||
///<summary>
|
||||
/// RemoveMin
|
||||
/// removes the node with the minimum key
|
||||
///<summary>
|
||||
public void RemoveMin() {
|
||||
if(rbTree == null || rbTree == sentinelNode)
|
||||
return;
|
||||
Remove(GetMinKey());
|
||||
}
|
||||
///<summary>
|
||||
/// RemoveMax
|
||||
/// removes the node with the maximum key
|
||||
///<summary>
|
||||
public void RemoveMax() {
|
||||
if(rbTree == null || rbTree == sentinelNode)
|
||||
return;
|
||||
Remove(GetMaxKey());
|
||||
}
|
||||
///<summary>
|
||||
/// Clear
|
||||
/// Empties or clears the tree
|
||||
///<summary>
|
||||
public void Clear () {
|
||||
rbTree = sentinelNode;
|
||||
intCount = 0;
|
||||
}
|
||||
|
||||
public int Count {
|
||||
get {
|
||||
return intCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class OrderedTreeEnumerator : IEnumerator {
|
||||
// the treap uses the stack to order the nodes
|
||||
private Stack stack;
|
||||
// return the keys
|
||||
private bool keys;
|
||||
// return in ascending order (true) or descending (false)
|
||||
private bool ascending;
|
||||
private OrderedTreeNode tnode;
|
||||
bool pre = true;
|
||||
|
||||
// key
|
||||
private IComparable ordKey;
|
||||
// the data or value associated with the key
|
||||
private object objValue;
|
||||
|
||||
///<summary>
|
||||
///Key
|
||||
///</summary>
|
||||
public IComparable Key {
|
||||
get {
|
||||
return ordKey;
|
||||
}
|
||||
|
||||
set {
|
||||
ordKey = value;
|
||||
}
|
||||
}
|
||||
///<summary>
|
||||
///Data
|
||||
///</summary>
|
||||
public object Value {
|
||||
get {
|
||||
return objValue;
|
||||
}
|
||||
|
||||
set {
|
||||
objValue = value;
|
||||
}
|
||||
}
|
||||
|
||||
private OrderedTreeEnumerator() {
|
||||
}
|
||||
///<summary>
|
||||
/// Determine order, walk the tree and push the nodes onto the stack
|
||||
///</summary>
|
||||
public OrderedTreeEnumerator(OrderedTreeNode tnode, bool keys, bool ascending) {
|
||||
|
||||
stack = new Stack();
|
||||
this.keys = keys;
|
||||
this.ascending = ascending;
|
||||
this.tnode = tnode;
|
||||
Reset();
|
||||
}
|
||||
|
||||
public void Reset() {
|
||||
pre = true;
|
||||
stack.Clear();
|
||||
// use depth-first traversal to push nodes into stack
|
||||
// the lowest node will be at the top of the stack
|
||||
if(ascending) {
|
||||
// find the lowest node
|
||||
while(tnode != OrderedTree.sentinelNode) {
|
||||
stack.Push(tnode);
|
||||
tnode = tnode.Left;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// the highest node will be at top of stack
|
||||
while(tnode != OrderedTree.sentinelNode) {
|
||||
stack.Push(tnode);
|
||||
tnode = tnode.Right;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public object Current {
|
||||
get {
|
||||
if (pre)
|
||||
throw new InvalidOperationException("Current");
|
||||
return keys == true ? Key : Value;
|
||||
}
|
||||
}
|
||||
|
||||
///<summary>
|
||||
/// HasMoreElements
|
||||
///</summary>
|
||||
public bool HasMoreElements() {
|
||||
return (stack.Count > 0);
|
||||
}
|
||||
///<summary>
|
||||
/// NextElement
|
||||
///</summary>
|
||||
public object NextElement() {
|
||||
if(stack.Count == 0)
|
||||
|
||||
throw new InvalidOperationException("Element not found");
|
||||
|
||||
// the top of stack will always have the next item
|
||||
// get top of stack but don't remove it as the next nodes in sequence
|
||||
// may be pushed onto the top
|
||||
// the stack will be popped after all the nodes have been returned
|
||||
OrderedTreeNode node = (OrderedTreeNode) stack.Peek(); //next node in sequence
|
||||
|
||||
if(ascending) {
|
||||
if(node.Right == OrderedTree.sentinelNode) {
|
||||
// yes, top node is lowest node in subtree - pop node off stack
|
||||
OrderedTreeNode tn = (OrderedTreeNode) stack.Pop();
|
||||
// peek at right node's parent
|
||||
// get rid of it if it has already been used
|
||||
while(HasMoreElements()&& ((OrderedTreeNode) stack.Peek()).Right == tn)
|
||||
tn = (OrderedTreeNode) stack.Pop();
|
||||
}
|
||||
else {
|
||||
// find the next items in the sequence
|
||||
// traverse to left; find lowest and push onto stack
|
||||
OrderedTreeNode tn = node.Right;
|
||||
while(tn != OrderedTree.sentinelNode) {
|
||||
stack.Push(tn);
|
||||
tn = tn.Left;
|
||||
}
|
||||
}
|
||||
}
|
||||
else { // descending, same comments as above apply
|
||||
if(node.Left == OrderedTree.sentinelNode) {
|
||||
// walk the tree
|
||||
OrderedTreeNode tn = (OrderedTreeNode) stack.Pop();
|
||||
while(HasMoreElements() && ((OrderedTreeNode)stack.Peek()).Left == tn)
|
||||
tn = (OrderedTreeNode) stack.Pop();
|
||||
}
|
||||
else {
|
||||
// determine next node in sequence
|
||||
// traverse to left subtree and find greatest node - push onto stack
|
||||
OrderedTreeNode tn = node.Left;
|
||||
while(tn != OrderedTree.sentinelNode) {
|
||||
stack.Push(tn);
|
||||
tn = tn.Right;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// the following is for .NET compatibility (see MoveNext())
|
||||
Key = node.Key;
|
||||
Value = node.Data;
|
||||
// ******** testing only ********
|
||||
|
||||
return keys == true ? node.Key : node.Data;
|
||||
}
|
||||
///<summary>
|
||||
/// MoveNext
|
||||
/// For .NET compatibility
|
||||
///</summary>
|
||||
public bool MoveNext() {
|
||||
if(HasMoreElements()) {
|
||||
NextElement();
|
||||
pre = false;
|
||||
return true;
|
||||
}
|
||||
pre = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public OrderedTreeEnumerator GetEnumerator() {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public class OrderedTreeNode {
|
||||
// tree node colors
|
||||
public const bool RED = false;
|
||||
public const bool BLACK = true;
|
||||
|
||||
// key provided by the calling class
|
||||
private IComparable ordKey;
|
||||
// the data or value associated with the key
|
||||
private object objData;
|
||||
// color - used to balance the tree
|
||||
private bool intColor;
|
||||
// left node
|
||||
private OrderedTreeNode rbnLeft;
|
||||
// right node
|
||||
private OrderedTreeNode rbnRight;
|
||||
// parent node
|
||||
private OrderedTreeNode rbnParent;
|
||||
|
||||
///<summary>
|
||||
///Key
|
||||
///</summary>
|
||||
public IComparable Key {
|
||||
get {
|
||||
return ordKey;
|
||||
}
|
||||
|
||||
set {
|
||||
ordKey = value;
|
||||
}
|
||||
}
|
||||
///<summary>
|
||||
///Data
|
||||
///</summary>
|
||||
public object Data {
|
||||
get {
|
||||
return objData;
|
||||
}
|
||||
|
||||
set {
|
||||
objData = value;
|
||||
}
|
||||
}
|
||||
///<summary>
|
||||
///Color
|
||||
///</summary>
|
||||
public bool Color {
|
||||
get {
|
||||
return intColor;
|
||||
}
|
||||
|
||||
set {
|
||||
intColor = value;
|
||||
}
|
||||
}
|
||||
///<summary>
|
||||
///Left
|
||||
///</summary>
|
||||
public OrderedTreeNode Left {
|
||||
get {
|
||||
return rbnLeft;
|
||||
}
|
||||
|
||||
set {
|
||||
rbnLeft = value;
|
||||
}
|
||||
}
|
||||
///<summary>
|
||||
/// Right
|
||||
///</summary>
|
||||
public OrderedTreeNode Right {
|
||||
get {
|
||||
return rbnRight;
|
||||
}
|
||||
|
||||
set {
|
||||
rbnRight = value;
|
||||
}
|
||||
}
|
||||
public OrderedTreeNode Parent {
|
||||
get {
|
||||
return rbnParent;
|
||||
}
|
||||
|
||||
set {
|
||||
rbnParent = value;
|
||||
}
|
||||
}
|
||||
|
||||
public OrderedTreeNode() {
|
||||
Color = RED;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,108 +0,0 @@
|
|||
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
|
||||
}
|
||||
}
|
|
@ -1,699 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,101 +0,0 @@
|
|||
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
|
||||
}
|
||||
}
|
|
@ -1,830 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,143 +0,0 @@
|
|||
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
|
||||
}
|
||||
}
|
|
@ -224,15 +224,14 @@ namespace iTextSharp.text.pdf {
|
|||
* @param documentJavaScript the javascript used in the document
|
||||
* @param writer the writer the catalog applies to
|
||||
*/
|
||||
internal void AddNames(k_Tree localDestinations, Hashtable documentLevelJS, Hashtable documentFileAttachment, PdfWriter writer) {
|
||||
internal void AddNames(OrderedTree localDestinations, Hashtable documentLevelJS, Hashtable documentFileAttachment, PdfWriter writer) {
|
||||
if (localDestinations.Count == 0 && documentLevelJS.Count == 0 && documentFileAttachment.Count == 0)
|
||||
return;
|
||||
PdfDictionary names = new PdfDictionary();
|
||||
if (localDestinations.Count > 0) {
|
||||
PdfArray ar = new PdfArray();
|
||||
foreach (DictionaryEntry entry in localDestinations) {
|
||||
String name = (String)entry.Key;
|
||||
Object[] obj = (Object[])entry.Value;
|
||||
foreach (String name in localDestinations.Keys) {
|
||||
Object[] obj = (Object[])localDestinations[name];
|
||||
if (obj[2] == null) //no destination
|
||||
continue;
|
||||
PdfIndirectReference refi = (PdfIndirectReference)obj[1];
|
||||
|
@ -1966,7 +1965,7 @@ namespace iTextSharp.text.pdf {
|
|||
* Stores the destinations keyed by name. Value is
|
||||
* <CODE>Object[]{PdfAction,PdfIndirectReference,PdfDestintion}</CODE>.
|
||||
*/
|
||||
protected internal k_Tree localDestinations = new k_Tree();
|
||||
protected internal OrderedTree localDestinations = new OrderedTree();
|
||||
|
||||
internal PdfAction GetLocalGotoAction(String name) {
|
||||
PdfAction action;
|
||||
|
|
|
@ -225,7 +225,7 @@ namespace iTextSharp.text.pdf {
|
|||
private const int OBJSINSTREAM = 200;
|
||||
|
||||
/** array containing the cross-reference table of the normal objects. */
|
||||
private k_Tree xrefs;
|
||||
private OrderedTree xrefs;
|
||||
private int refnum;
|
||||
/** the current byteposition in the body. */
|
||||
private int position;
|
||||
|
@ -242,7 +242,7 @@ namespace iTextSharp.text.pdf {
|
|||
* @param writer
|
||||
*/
|
||||
internal PdfBody(PdfWriter writer) {
|
||||
xrefs = new k_Tree();
|
||||
xrefs = new OrderedTree();
|
||||
xrefs[new PdfCrossReference(0, 0, GENERATION_MAX)] = null;
|
||||
position = writer.Os.Counter;
|
||||
refnum = 1;
|
||||
|
@ -401,9 +401,7 @@ namespace iTextSharp.text.pdf {
|
|||
|
||||
internal int Size {
|
||||
get {
|
||||
k_Iterator it = xrefs.End.Clone();
|
||||
it.Prev();
|
||||
return Math.Max(((PdfCrossReference)((DictionaryEntry)it.Current).Key).Refnum + 1, refnum);
|
||||
return Math.Max(((PdfCrossReference)xrefs.GetMaxKey()).Refnum + 1, refnum);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -425,12 +423,10 @@ namespace iTextSharp.text.pdf {
|
|||
refNumber = IndirectReferenceNumber;
|
||||
xrefs[new PdfCrossReference(refNumber, position)] = null;
|
||||
}
|
||||
PdfCrossReference entry = (PdfCrossReference)((DictionaryEntry)xrefs.Begin.Current).Key;
|
||||
int first = entry.Refnum;
|
||||
int first = ((PdfCrossReference)xrefs.GetMinKey()).Refnum;
|
||||
int len = 0;
|
||||
ArrayList sections = new ArrayList();
|
||||
for (k_Iterator i = xrefs.Begin.Clone(); i != xrefs.End; i.Next()) {
|
||||
entry = (PdfCrossReference)((DictionaryEntry)i.Current).Key;
|
||||
foreach (PdfCrossReference entry in xrefs.Keys) {
|
||||
if (first + len == entry.Refnum)
|
||||
++len;
|
||||
else {
|
||||
|
@ -452,8 +448,7 @@ namespace iTextSharp.text.pdf {
|
|||
}
|
||||
ByteBuffer buf = new ByteBuffer();
|
||||
|
||||
for (k_Iterator i = xrefs.Begin.Clone(); i != xrefs.End; i.Next()) {
|
||||
entry = (PdfCrossReference)((DictionaryEntry)i.Current).Key;
|
||||
foreach (PdfCrossReference entry in xrefs.Keys) {
|
||||
entry.ToPdf(mid, buf);
|
||||
}
|
||||
PdfStream xr = new PdfStream(buf.ToByteArray());
|
||||
|
@ -485,7 +480,8 @@ namespace iTextSharp.text.pdf {
|
|||
else {
|
||||
byte[] tmp = GetISOBytes("xref\n");
|
||||
os.Write(tmp, 0, tmp.Length);
|
||||
k_Iterator i = xrefs.Begin.Clone();
|
||||
IEnumerator i = xrefs.Keys;
|
||||
i.MoveNext();
|
||||
for (int k = 0; k < sections.Count; k += 2) {
|
||||
first = (int)sections[k];
|
||||
len = (int)sections[k + 1];
|
||||
|
@ -496,9 +492,8 @@ namespace iTextSharp.text.pdf {
|
|||
os.Write(tmp, 0, tmp.Length);
|
||||
os.WriteByte((byte)'\n');
|
||||
while (len-- > 0) {
|
||||
entry = (PdfCrossReference)((DictionaryEntry)i.Current).Key;
|
||||
entry.ToPdf(os);
|
||||
i.Next();
|
||||
((PdfCrossReference)i.Current).ToPdf(os);
|
||||
i.MoveNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -743,7 +738,7 @@ namespace iTextSharp.text.pdf {
|
|||
* @param dest the <CODE>Hashtable</CODE> containing the destinations
|
||||
* @throws IOException on error
|
||||
*/
|
||||
internal void AddLocalDestinations(k_Tree dest) {
|
||||
internal void AddLocalDestinations(OrderedTree dest) {
|
||||
foreach (String name in dest.Keys) {
|
||||
Object[] obj = (Object[])dest[name];
|
||||
PdfDestination destination = (PdfDestination)obj[2];
|
||||
|
|
|
@ -92,9 +92,9 @@ namespace iTextSharp.text.pdf.codec {
|
|||
public const int TABLES = 53; //see 7.4.13.
|
||||
public const int EXTENSION = 62; //see 7.4.14.
|
||||
|
||||
private k_Tree segments = new k_Tree();
|
||||
private k_Tree pages = new k_Tree();
|
||||
private k_Tree globals = new k_Tree();
|
||||
private OrderedTree segments = new OrderedTree();
|
||||
private OrderedTree pages = new OrderedTree();
|
||||
private OrderedTree globals = new OrderedTree();
|
||||
private RandomAccessFileOrArray ra;
|
||||
private bool sequential;
|
||||
private bool number_of_pages_known;
|
||||
|
@ -140,7 +140,7 @@ namespace iTextSharp.text.pdf.codec {
|
|||
public class JBIG2Page {
|
||||
public int page;
|
||||
private JBIG2SegmentReader sr;
|
||||
private k_Tree segs = new k_Tree();
|
||||
private OrderedTree segs = new OrderedTree();
|
||||
public int pageBitmapWidth = -1;
|
||||
public int pageBitmapHeight = -1;
|
||||
public JBIG2Page(int page, JBIG2SegmentReader sr) {
|
||||
|
@ -337,7 +337,7 @@ namespace iTextSharp.text.pdf.codec {
|
|||
s.page_association_size = page_association_size;
|
||||
s.page_association_offset = page_association_offset;
|
||||
|
||||
if ( segment_page_association > 0 && ! pages.Contains(segment_page_association) ) {
|
||||
if ( segment_page_association > 0 && ! pages.ContainsKey(segment_page_association) ) {
|
||||
pages[segment_page_association] = new JBIG2Page(segment_page_association, this);
|
||||
}
|
||||
if ( segment_page_association > 0 ) {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<VisualStudioProject>
|
||||
<CSHARP
|
||||
ProjectType = "Local"
|
||||
ProductVersion = "7.10.3077"
|
||||
ProductVersion = "7.10.6030"
|
||||
SchemaVersion = "2.0"
|
||||
ProjectGuid = "{84C4FDD9-3ED7-453B-B9DA-B3ED52CB071C}"
|
||||
>
|
||||
|
@ -6982,57 +6982,7 @@
|
|||
BuildAction = "Compile"
|
||||
/>
|
||||
<File
|
||||
RelPath = "System\util\collections\Algorithm.cs"
|
||||
SubType = "Code"
|
||||
BuildAction = "Compile"
|
||||
/>
|
||||
<File
|
||||
RelPath = "System\util\collections\Container.cs"
|
||||
SubType = "Code"
|
||||
BuildAction = "Compile"
|
||||
/>
|
||||
<File
|
||||
RelPath = "System\util\collections\Deque.cs"
|
||||
SubType = "Code"
|
||||
BuildAction = "Compile"
|
||||
/>
|
||||
<File
|
||||
RelPath = "System\util\collections\HashTable.cs"
|
||||
SubType = "Code"
|
||||
BuildAction = "Compile"
|
||||
/>
|
||||
<File
|
||||
RelPath = "System\util\collections\Iterator.cs"
|
||||
SubType = "Code"
|
||||
BuildAction = "Compile"
|
||||
/>
|
||||
<File
|
||||
RelPath = "System\util\collections\List.cs"
|
||||
SubType = "Code"
|
||||
BuildAction = "Compile"
|
||||
/>
|
||||
<File
|
||||
RelPath = "System\util\collections\Queue.cs"
|
||||
SubType = "Code"
|
||||
BuildAction = "Compile"
|
||||
/>
|
||||
<File
|
||||
RelPath = "System\util\collections\SkipList.cs"
|
||||
SubType = "Code"
|
||||
BuildAction = "Compile"
|
||||
/>
|
||||
<File
|
||||
RelPath = "System\util\collections\Stack.cs"
|
||||
SubType = "Code"
|
||||
BuildAction = "Compile"
|
||||
/>
|
||||
<File
|
||||
RelPath = "System\util\collections\Tree.cs"
|
||||
SubType = "Code"
|
||||
BuildAction = "Compile"
|
||||
/>
|
||||
<File
|
||||
RelPath = "System\util\collections\Vector.cs"
|
||||
RelPath = "System\util\collections\OrderedTree.cs"
|
||||
SubType = "Code"
|
||||
BuildAction = "Compile"
|
||||
/>
|
||||
|
|
Loading…
Reference in New Issue