public void OrWith(IBitset otherSet)
{
RLEBitset otherRLESet = (RLEBitset)otherSet; // cast to an RLEBitset - errors if cannot cast
Run current = new Run();
int nextThisIndex, nextOtherIndex;
if(this.length < otherRLESet.length)
{
this.length = otherRLESet.length;
}
if (this.runArray.Count == 0)
{
this.runArray = new List<Run>(otherRLESet.runArray);
nextThisIndex = this.runArray.Count; //this stops the loops
nextOtherIndex = this.runArray.Count;
}
else if (otherRLESet.runArray.Count == 0)
{
nextThisIndex = this.runArray.Count; //this stops the loops
nextOtherIndex = this.runArray.Count;
}
else if (this.runArray[0].StartIndex <= otherRLESet.runArray[0].StartIndex)
{
current = this.runArray[0];
nextThisIndex = 1;
nextOtherIndex = 0;
}
else if (TryCreateUnion(this.runArray[0], otherRLESet.runArray[0], ref current))
{
//first two sets overlap
this.runArray[0] = current;
nextThisIndex = 1;
nextOtherIndex = 1;
}
else
{
this.runArray.Insert(0, otherRLESet.runArray[0]);
nextThisIndex = 1;
nextOtherIndex = 1;
}
while ((nextThisIndex < this.runArray.Count) && (nextOtherIndex < otherRLESet.runArray.Count))
{
if (this.runArray[nextThisIndex].StartIndex >
otherRLESet.runArray[nextOtherIndex].StartIndex)
{
MergeOtherRun(otherRLESet, ref current, ref nextThisIndex, ref nextOtherIndex);
}
else
{
mergeExistingRun(ref current, ref nextThisIndex);
}
}
if (nextThisIndex < this.runArray.Count)
{
//we finished the other, finish this one
while (nextThisIndex < this.runArray.Count)
{
mergeExistingRun(ref current, ref nextThisIndex);
}
}
else
{
//we finished this one, finish the other
while (nextOtherIndex < otherRLESet.runArray.Count)
{
int lastNextIndex = this.runArray.Count;
MergeOtherRun(otherRLESet, ref current, ref lastNextIndex, ref nextOtherIndex);
}
}
}