public IBitset Difference(IBitset otherSet) { UncompressedBitArray workset = null; if (otherSet is UncompressedBitArray) { workset = (UncompressedBitArray)otherSet; } else { throw new InvalidOperationException("otherSet is not an UncompressedBitArray"); } UncompressedBitArray newArray = (UncompressedBitArray)this.Clone(); for (int i = 0; i < workset._Array.Length; i++) { if (workset._Array[i] && i < this.Length()) { newArray.Set(i, false); } } return(newArray); }
public IBitset And(IBitset otherSet) { IBitset result = this.Clone(); result.AndWith(otherSet); return(result); }
public void SetEdgeCaseTest() { IBitset testSet = CreateSetFromIndices(new int[] { }, TEST_SET_LENGTH); testSet.Set(0, 1, true); Assert.True(testSet.Get(0)); }
public IBitset Or(IBitset otherSet) { IBitset result = this.Clone(); result.OrWith(otherSet); return(result); }
/// <summary> /// Performs the set difference, defined as the set in A and not in B. /// </summary> /// <param name="otherSet">the other IBitset</param> /// <returns>The set difference of this set and the other.</returns> public IBitset Difference(IBitset otherSet) { RLEBitset otherRLESet = (RLEBitset)otherSet; // cast to an RLEBitset - errors if cannot cast IBitset rtnVal = And(otherRLESet.Not()); return(rtnVal); }
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 //List<Run> runsA = this._RunArray; //List<Run> runsB = otherRLESet._RunArray; //int i = 0; //int j = 0; //while (i < runsA.Count && j < runsB.Count) //{ // //check for overlap of the runs. // Run currRun = overlapOr(runsA[i], runsB[j]); // if (currRun.StartIndex <= currRun.EndIndex) // { // rtnVal._RunArray.Add(currRun); // } //} //return rtnVal; throw new NotImplementedException(); }
public virtual void EqualsTest() { int[] set = SetGenerator.GetRandomArray(TEST_SET_LENGTH); IBitset testSet = CreateSetFromIndices(set, TEST_SET_LENGTH); IBitset otherSet = CreateSetFromIndices(set, TEST_SET_LENGTH); Assert.AreEqual <IBitset>(testSet, otherSet); }
/// <summary> /// Finds members of this bitset that are not in the other set (ANDNOT). /// This does not modify either bitset. /// </summary> /// <param name="otherSet">The set to compare against</param> /// <returns>A new IBitset containing the members that are in /// this bitset but not in the other.</returns> public IBitset Difference(IBitset otherSet) { if (otherSet is RoaringBitset) { return(this.andNot((RoaringBitset)otherSet)); } throw new ArgumentOutOfRangeException("Other set must be a roaring bitset"); }
public IBitset And(IBitset otherSet) { if (otherSet is RoaringBitset) { return(and(this, (RoaringBitset)otherSet)); } throw new ArgumentOutOfRangeException("otherSet must be a RoaringBitset"); }
public virtual void CloneTest() { int[] set = SetGenerator.GetRandomArray(TEST_SET_LENGTH); IBitset testSet = CreateSetFromIndices(set, TEST_SET_LENGTH); var clone = testSet.Clone(); Assert.AreEqual(clone, testSet); }
public virtual void GetTest() { int[] set = SetGenerator.GetRandomArray(TEST_SET_LENGTH); IBitset testSet = CreateSetFromIndices(set, TEST_SET_LENGTH); bool expected = set.Contains(2); bool result = testSet.Get(2); Assert.AreEqual(expected, result); }
public virtual void CardinalityTest() { int[] set = SetGenerator.GetContiguousArray(1, 5000); IBitset testSet = CreateSetFromIndices(set, set.Max() + 1); int expected = set.Length; int actual = testSet.Cardinality(); Assert.AreEqual(expected, actual); }
/// <summary> /// Performs an in-place intersection of two Roaring Bitsets. /// </summary> /// <param name="otherSet">the second Roaring Bitset to intersect</param> public void AndWith(IBitset otherSet) { RoaringBitset otherRoaring = otherSet as RoaringBitset; if (otherRoaring == null) { throw new ArgumentOutOfRangeException("otherSet must be a RoaringBitset"); } AndWith((RoaringBitset)otherSet); }
/// <summary> /// Creates a new bitset that is the bitwise AND of this bitset with another /// </summary> /// <param name="otherSet">Other bitset</param> /// <returns>A new roaring bitset</returns> public IBitset And(IBitset otherSet) { RoaringBitset otherRoaring = otherSet as RoaringBitset; if (otherRoaring == null) { throw new ArgumentOutOfRangeException("otherSet must be a RoaringBitset"); } return(And(this, otherRoaring)); }
public virtual void OrTest() { int[] first = SetGenerator.GetRandomArray(TEST_SET_LENGTH); int[] second = SetGenerator.GetRandomArray(TEST_SET_LENGTH); int[] result = first.Union(second).ToArray(); IBitset expected = CreateSetFromIndices(result, TEST_SET_LENGTH); IBitset actual = CreateSetFromIndices(first, TEST_SET_LENGTH).Or(CreateSetFromIndices(second, TEST_SET_LENGTH)); Assert.AreEqual(expected, actual, generateMessage("OrWith", first, second, result)); }
public virtual void AndTest() { int[] first = SetGenerator.GetRandomArray(TEST_SET_LENGTH); int[] second = SetGenerator.GetRandomArray(TEST_SET_LENGTH); int[] result = first.Intersect(second).ToArray(); IBitset expected = CreateSetFromIndices(result, TEST_SET_LENGTH); IBitset actual = CreateSetFromIndices(first, TEST_SET_LENGTH).And(CreateSetFromIndices(second, TEST_SET_LENGTH)); Assert.AreEqual(expected, actual); }
public virtual void GetHashCodeEqualityTest() { int[] set = SetGenerator.GetRandomArray(TEST_SET_LENGTH); IBitset testSet = CreateSetFromIndices(set, TEST_SET_LENGTH); IBitset otherTestSet = CreateSetFromIndices(set, TEST_SET_LENGTH); int hash = testSet.GetHashCode(); int otherHash = otherTestSet.GetHashCode(); Assert.AreEqual(hash, otherHash); }
public void FlipFalseTest() { int[] set = { 1, 2, 3, 5 }; IBitset testSet = CreateSetFromIndices(set, 6); testSet.Flip(2); bool expected = false; bool result = testSet.Get(2); Assert.Equal(expected, result); }
public virtual void SetRangeFalseLargeTest() { int[] set = SetGenerator.GetContiguousArray(0, 5000);; IBitset testSet = CreateSetFromIndices(set, 5000); testSet.Set(1, 3, false); bool expected = false; bool result = testSet.Get(2); Assert.AreEqual(expected, result); }
public void FlipRangeTrueTest() { int[] set = { 1, 2, 3, 7 }; IBitset testSet = CreateSetFromIndices(set, 8); testSet.Flip(4, 6); bool expected = true; bool result = testSet.Get(5); Assert.Equal(expected, result); }
/// <summary> /// Finds members of this bitset that are not in the other set (ANDNOT). /// Places the results in the current bitset (modifies in place). /// </summary> /// <param name="otherSet">The set to compare against</param> /// <returns>A new IBitset containing the members that are in /// this bitset but not in the other.</returns> public void DifferenceWith(IBitset otherSet) { if (otherSet is RoaringBitset) { this.iandNot((RoaringBitset)otherSet); } else { throw new ArgumentOutOfRangeException("Other set must be a roaring bitset"); } }
public virtual void FlipRangeFalseTest() { int[] set = { 1, 2, 3, 7 }; IBitset testSet = CreateSetFromIndices(set, 8); testSet.Flip(2, 4); bool expected = false; bool result = testSet.Get(3); Assert.AreEqual(expected, result); }
public virtual void FlipTrueTest() { int[] set = { 1, 2, 3, 5 }; IBitset testSet = CreateSetFromIndices(set, 6); testSet.Flip(4); bool expected = true; bool result = testSet.Get(4); Assert.AreEqual(expected, result); }
public virtual void AndWithTest() { int[] first = SetGenerator.GetRandomArray(TEST_SET_LENGTH); int[] second = SetGenerator.GetRandomArray(TEST_SET_LENGTH); int[] result = first.Intersect(second).ToArray(); IBitset testSet = CreateSetFromIndices(first, TEST_SET_LENGTH); testSet.AndWith(CreateSetFromIndices(second, TEST_SET_LENGTH)); Assert.AreEqual(CreateSetFromIndices(result, TEST_SET_LENGTH), testSet); }
public virtual void SetRangeTrueLargeTest() { int[] set = SetGenerator.GetContiguousArray(0, 5000); IBitset testSet = CreateSetFromIndices(set, 5000); testSet.Set(5007, 5009, true); bool expected = true; bool result = testSet.Get(8); Assert.AreEqual(expected, result); }
public virtual void SetTrueTest() { int[] set = SetGenerator.GetRandomArray(TEST_SET_LENGTH); IBitset testSet = CreateSetFromIndices(set, TEST_SET_LENGTH); testSet.Set(8, true); bool expected = true; bool result = testSet.Get(8); Assert.AreEqual(expected, result); }
public virtual void SetRangeFalseTest() { int[] set = { 1, 2, 3 }; IBitset testSet = CreateSetFromIndices(set, 4); testSet.Set(1, 3, false); bool expected = false; bool result = testSet.Get(2); Assert.AreEqual(expected, result); }
/// <summary> /// Performs an in-place intersection of two Roaring Bitsets. /// </summary> /// <param name="otherSet">the second Roaring Bitset to intersect</param> public void AndWith(IBitset otherSet) { if (otherSet is RoaringBitset) { andWith((RoaringBitset)otherSet); } else { throw new ArgumentOutOfRangeException("otherSet must be a RoaringBitset"); } }
public virtual void EnumerationTest() { int[] set = SetGenerator.GetRandomArray(TEST_SET_LENGTH); IBitset testSet = CreateSetFromIndices(set, TEST_SET_LENGTH); List <int> enumeratedList = new List <int>(); foreach (int i in testSet) { enumeratedList.Add(i); } CollectionAssert.AreEquivalent(enumeratedList.ToArray(), set); }
public virtual void OrWithTest() { int[] first = SetGenerator.GetRandomArray(TEST_SET_LENGTH); int[] second = SetGenerator.GetRandomArray(TEST_SET_LENGTH); int[] result = first.Union(second).ToArray(); IBitset testSet = CreateSetFromIndices(first, TEST_SET_LENGTH); testSet.OrWith(CreateSetFromIndices(second, TEST_SET_LENGTH)); Assert.AreEqual(CreateSetFromIndices(result, TEST_SET_LENGTH), testSet, generateMessage("OrWith", first, second, result)); }
public IBitset Difference(IBitset otherSet) { UncompressedBitArray workset = null; if (otherSet is UncompressedBitArray) workset = (UncompressedBitArray)otherSet; else throw new InvalidOperationException("otherSet is not an UncompressedBitArray"); UncompressedBitArray newArray = (UncompressedBitArray) this.Clone(); for (int i = 0; i < workset._Array.Length; i++) { if (workset._Array[i] && i < this.Length()) { newArray.Set(i, false); } } return newArray; }
public IBitset And(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; List<Run> runsA = this._RunArray; List<Run> runsB = otherRLESet._RunArray; int i = 0; int j = 0; while (i < runsA.Count && j < runsB.Count) { // check for overlap of the runs. Run currRun = new Run(); if (tryCreateIntersection(runsA[i], runsB[j], ref currRun)) { rtnVal._RunArray.Add(currRun); } // iterate the counters appropriately to compare the next set of runs for overlap. if (runsA[i].EndIndex > runsB[j].EndIndex + 1) { j++; } else if (runsA[i].EndIndex < runsB[j].EndIndex - 1) { i++; } else { i++; j++; } } return rtnVal; }
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._RunArray.Count == 0) { this._RunArray = new List<Run>(otherRLESet._RunArray); this._Length = otherRLESet._Length; 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); } } }
public IBitset And(IBitset otherSet) { if (otherSet is RoaringBitset) { return and(this, (RoaringBitset)otherSet); } throw new ArgumentOutOfRangeException("otherSet must be a RoaringBitset"); }
/// <summary> /// Creates a new bitset that is the bitwise OR of this bitset with another bitset. /// </summary> /// <param name="otherSet">Other bitset</param> /// <returns>A new IBitset</returns> public IBitset Or(IBitset otherSet) { IBitset result = this.Clone(); result.OrWith(otherSet); return result; }
public void OrWith(IBitset otherSet) { if (!(otherSet is RoaringBitset)) throw new ArgumentOutOfRangeException("otherSet must be a RoaringBitSet"); RoaringBitset x2 = (RoaringBitset)otherSet; int pos1 = 0, pos2 = 0; int length1 = this.containers.size, length2 = x2.containers.size; if (pos1 < length1 && pos2 < length2) { ushort s1 = this.containers.getKeyAtIndex(pos1); ushort s2 = x2.containers.getKeyAtIndex(pos2); while (true) { if (s1 == s2) { this.containers.setContainerAtIndex( pos1, this.containers.getContainerAtIndex(pos1).ior( x2.containers.getContainerAtIndex(pos2) ) ); pos1++; pos2++; if ((pos1 == length1) || (pos2 == length2)) { break; } s1 = this.containers.getKeyAtIndex(pos1); s2 = x2.containers.getKeyAtIndex(pos2); } else if (s1 < s2) { pos1++; if (pos1 == length1) { break; } s1 = this.containers.getKeyAtIndex(pos1); } else { // s1 > s2 this.containers.insertNewKeyValueAt(pos1, s2, x2.containers.getContainerAtIndex(pos2)); pos1++; length1++; pos2++; if (pos2 == length2) { break; } s2 = x2.containers.getKeyAtIndex(pos2); } } } if (pos1 == length1) { this.containers.appendCopy(x2.containers, pos2, length2); } }
public IBitset Or(IBitset otherSet) { if (!(otherSet is RoaringBitset)) throw new ArgumentOutOfRangeException("otherSet must be a RoaringBitSet"); RoaringBitset answer = new RoaringBitset(); RoaringBitset x2 = (RoaringBitset) otherSet; int pos1 = 0, pos2 = 0; int length1 = this.containers.size, length2 = x2.containers.size; if (pos1 < length1 && pos2 < length2) { ushort s1 = this.containers.getKeyAtIndex(pos1); ushort s2 = x2.containers.getKeyAtIndex(pos2); while (true) { if (s1 == s2) { answer.containers.append( s1, this.containers.getContainerAtIndex(pos1).or( x2.containers.getContainerAtIndex(pos2) ) ); pos1++; pos2++; if ((pos1 == length1) || (pos2 == length2)) { break; } s1 = this.containers.getKeyAtIndex(pos1); s2 = x2.containers.getKeyAtIndex(pos2); } else if (s1 < s2) { answer.containers.appendCopy(this.containers, pos1); pos1++; if (pos1 == length1) { break; } s1 = this.containers.getKeyAtIndex(pos1); } else { // s1 > s2 answer.containers.appendCopy(x2.containers, pos2); pos2++; if (pos2 == length2) { break; } s2 = x2.containers.getKeyAtIndex(pos2); } } } if (pos1 == length1) { answer.containers.appendCopy(x2.containers, pos2, length2); } else if (pos2 == length2) { answer.containers.appendCopy(this.containers, pos1, length1); } return answer; }
/// <summary> /// Finds members of this bitset that are not in the other set (ANDNOT). /// This does not modify either bitset. /// </summary> /// <param name="otherSet">The set to compare against</param> /// <returns>A new IBitset containing the members that are in /// this bitset but not in the other.</returns> public IBitset Difference(IBitset otherSet) { if (otherSet is RoaringBitset) { return this.andNot((RoaringBitset) otherSet); } throw new ArgumentOutOfRangeException("Other set must be a roaring bitset"); }
/// <summary> /// Computes the in-place bitwise OR of this bitset with another bitset. /// </summary> /// <param name="otherSet">Other bitset</param> public void OrWith(IBitset otherSet) { array = array.Or(((UncompressedBitArray)otherSet).array); }
/// <summary> /// Creates a new bitset that is the bitwise AND of this bitset with another /// </summary> /// <param name="otherSet">Other bitset</param> /// <returns>A new IBitset</returns> public IBitset And(IBitset otherSet) { IBitset result = this.Clone(); result.AndWith(otherSet); return result; }
/// <summary> /// Performs the set difference, defined as the set in A and not in B. /// </summary> /// <param name="otherSet">the other IBitset</param> /// <returns>The set difference of this set and the other.</returns> public IBitset Difference(IBitset otherSet) { RLEBitset otherRLESet = (RLEBitset)otherSet; // cast to an RLEBitset - errors if cannot cast IBitset rtnVal = And(otherRLESet.Not()); return rtnVal; }
/// <summary> /// Creates a new bitset that is the bitwise OR of this bitset with another /// </summary> /// <param name="otherSet">Other bitset</param> /// <returns>A new IBitset</returns> public IBitset Or(IBitset otherSet) { if (!(otherSet is RoaringBitset)) { throw new ArgumentOutOfRangeException("otherSet must be a RoaringBitSet"); } RoaringBitset answer = new RoaringBitset(); RoaringBitset x2 = (RoaringBitset) otherSet; int pos1 = 0, pos2 = 0; int thisSize = this.containers.Size; int otherSetSize = x2.containers.Size; if (pos1 < thisSize && pos2 < otherSetSize) { ushort s1 = this.containers.GetKeyAtIndex(pos1); ushort s2 = x2.containers.GetKeyAtIndex(pos2); while (true) { if (s1 == s2) { Container newContainer = this.containers.GetContainerAtIndex(pos1) .Or(x2.containers.GetContainerAtIndex(pos2)); answer.containers.Append(s1, newContainer); pos1++; pos2++; if ((pos1 == thisSize) || (pos2 == otherSetSize)) { break; } s1 = this.containers.GetKeyAtIndex(pos1); s2 = x2.containers.GetKeyAtIndex(pos2); } else if (s1 < s2) { answer.containers.AppendCopy(this.containers, pos1); pos1++; if (pos1 == thisSize) { break; } s1 = this.containers.GetKeyAtIndex(pos1); } else // s1 > s2 { answer.containers.AppendCopy(x2.containers, pos2); pos2++; if (pos2 == otherSetSize) { break; } s2 = x2.containers.GetKeyAtIndex(pos2); } } } if (pos1 == thisSize) { answer.containers.AppendCopy(x2.containers, pos2, otherSetSize); } else if (pos2 == otherSetSize) { answer.containers.AppendCopy(this.containers, pos1, thisSize); } return answer; }
/// <summary> /// Performs an in-place intersection of two Roaring Bitsets. /// </summary> /// <param name="otherSet">the second Roaring Bitset to intersect</param> public void AndWith(IBitset otherSet) { array = array.And(((UncompressedBitArray)otherSet).array); }
/// <summary> /// Intersects an IBitset with another IBitset, modifying the first IBitset rather /// than creating a new IBitset /// </summary> /// <param name="otherSet">the other IBitset</param> /// <returns>void</returns> public void AndWith(IBitset otherSet) { RLEBitset otherRLESet = (RLEBitset)otherSet; // cast to an RLEBitset List<Run> runsA = this._RunArray; List<Run> runsB = otherRLESet._RunArray; int i = 0; int j = 0; while (i < runsA.Count && j < runsB.Count) { int x = runsA[i].StartIndex; int y = runsA[i].EndIndex; int w = runsB[j].StartIndex; int z = runsB[j].EndIndex; if (x < w) { if (y < w) { runsA.RemoveAt(i); } else // (y >= w) { // crops the current run in runsA from the left to align with // the start of the current run in runsB Run ithRun = runsA[i]; ithRun.StartIndex = w; runsA[i] = ithRun; var what = this._RunArray[i]; if (y <= z) { i++; } else // (y > z ) { // splits the run from runsA into two runs Run newRun = new Run(z + 1, y); Run newRun2 = runsA[i]; newRun2.EndIndex = z; runsA[i] = newRun2; runsA.Insert(i + 1, newRun); i++; j++; } } } else // (x >= w) { if (y <= z) { i++; } else // (y > z) { if (x <= z) { // splits the run from runsA into two runs Run newRun = new Run(z + 1, y); Run newRun2 = runsA[i]; newRun2.EndIndex = z; runsA[i] = newRun2; runsA.Insert(i + 1, newRun); i++; j++; } else { j++; } } } } //this truncates runsA if we've considered all of the runs in runsB this._RunArray = this._RunArray.Take(i).ToList(); }
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; }
/// <summary> /// Performs the set difference, defined as the set in A and not in B. /// </summary> /// <param name="otherSet">the other IBitset</param> public void DifferenceWith(IBitset otherSet) { RLEBitset otherRLESet = (RLEBitset)otherSet; // cast to an RLEBitset - errors if cannot cast AndWith(otherRLESet.Not()); }