public IBitset Or(IBitset otherSet)
{
RLEBitset otherRLESet = (RLEBitset)otherSet; // cast to an RLEBitset - errors if cannot cast
RLEBitset rtnVal = new RLEBitset(); // instantiate the return value
rtnVal.length = (this.length > otherRLESet.length) ? this.length : otherRLESet.length;
List<Run> runsA = this.runArray;
List<Run> runsB = otherRLESet.runArray;
int i = 0;
int j = 0;
while (i < runsA.Count && j < runsB.Count)
{
Run currRun = new Run();
if (TryCreateUnion(runsA[i], runsB[j], ref currRun))
{
//there is an overlap
//now compare the overlapping run you created to the previous run you added to see if they should be merged
AddRunToRLE(ref rtnVal, currRun);
//now move the counters.
if (runsA[i].EndIndex < runsB[j].EndIndex)
{
i++;
}
else if (runsA[i].EndIndex > runsB[j].EndIndex)
{
j++;
}
else
{
i++;
j++;
}
}
else
{
//no overlap, so let's just add lower run and step that counter.
if (runsA[i].StartIndex < runsB[j].StartIndex)
{
AddRunToRLE(ref rtnVal, runsA[i]);
i++;
}
else
{
AddRunToRLE(ref rtnVal, runsB[j]);
j++;
}
}
}
//account for remaining runs in one of our sets.
int remCounter = 0;
List<Run> remRuns = new List<Run>();
if (i < runsA.Count)
{
remCounter = i;
remRuns = runsA;
}
else if (j < runsB.Count)
{
remCounter = j;
remRuns = runsB;
}
while (remCounter < remRuns.Count)
{
AddRunToRLE(ref rtnVal, remRuns[remCounter]);
remCounter++;
}
return rtnVal;
}