/// <summary> /// Add to the current bitmap all integers in [rangeStart,rangeEnd). /// </summary> /// <param name="rangeStart">inclusive beginning of range</param> /// <param name="rangeEnd">exclusive ending of range</param> public void add(int rangeStart, int rangeEnd) { if (rangeStart >= rangeEnd) return; // empty range ushort hbStart = Utility.GetHighBits(rangeStart); ushort lbStart = Utility.GetLowBits(rangeStart); ushort hbLast = Utility.GetHighBits(rangeEnd - 1); ushort lbLast = Utility.GetLowBits(rangeEnd - 1); for (ushort hb = hbStart; hb <= hbLast; ++hb) { // first container may contain partial range ushort containerStart = 0; if (hb == hbStart) { containerStart = lbStart; } // last container may contain partial range ushort containerLast = (hb == hbLast) ? lbLast : ushort.MaxValue; int containerIndex = containers.getIndex(hb); if (containerIndex >= 0) { Container c = containers.getContainerAtIndex(containerIndex).add( containerStart, (ushort)(containerLast + 1)); containers.setContainerAtIndex(containerIndex, c); } else { Container newContainer = new ArrayContainer(100); newContainer = newContainer.add(lbStart, lbLast); containers.insertNewKeyValueAt(-containerIndex - 1, hb, newContainer); } } }
public void loadData(ArrayContainer arrayContainer) { this.cardinality = arrayContainer.cardinality; for (int k = 0; k < arrayContainer.cardinality; ++k) { ushort x = arrayContainer.content[k]; bitmap[x / 64] |= (1L << x); } }
/// <summary> /// Deserialize a container from binary format, as written by the Serialize method, minus the first 32 bits giving the cardinality. /// </summary> /// <param name="reader">The reader to deserialize from.</param> /// <returns>The first container represented by reader.</returns> public static ArrayContainer Deserialize(BinaryReader reader, int cardinality) { ArrayContainer container = new ArrayContainer(cardinality); container.cardinality = cardinality; for(int i = 0; i < cardinality; i++) { container.content[i] = (ushort) reader.ReadInt16(); } return container; }
/// <summary> /// Returns the elements of this ArrayContainer that are not in the /// other ArrayContainer. /// </summary> /// <param name="x">the other ArrayContainer</param> /// <returns>A new container with the differences</returns> public override Container andNot (ArrayContainer x) { int desiredCapacity = this.getCardinality(); var answer = new ArrayContainer(desiredCapacity); // Compute the cardinality of the new container answer.cardinality = Utility.unsignedDifference(this.content, desiredCapacity, x.content, x.getCardinality(), answer.content); return answer; }
public void add(int x) { ushort highBits = Utility.GetHighBits(x); int containerIndex = containers.getIndex(highBits); if (containerIndex >= 0) // a container exists at this index already. // find the right container, get the low order bits to add to the container and add them { containers.setContainerAtIndex(containerIndex, containers.getContainerAtIndex(containerIndex).add(Utility.GetLowBits(x)) ); } else { // no container exists for this index // create a new ArrayContainer, since it will only hold one integer to start // get the low order bits and att to the newly created container // add the newly created container to the array of containers ArrayContainer newac = new ArrayContainer(); containers.insertNewKeyValueAt(-containerIndex - 1, highBits, newac.add(Utility.GetLowBits(x))); } }
public override Container and(BitsetContainer value2) { int newCardinality = 0; for (int k = 0; k < this.bitmap.Length; ++k) { newCardinality += Utility.longBitCount( this.bitmap[k] & value2.bitmap[k] ); } if (newCardinality > ArrayContainer.DEFAULT_MAX_SIZE) { BitsetContainer answer = new BitsetContainer(); for (int k = 0; k < answer.bitmap.Length; ++k) { answer.bitmap[k] = this.bitmap[k] & value2.bitmap[k]; } answer.cardinality = newCardinality; return answer; } ArrayContainer ac = new ArrayContainer(newCardinality); Utility.fillArrayAND(ref ac.content, this.bitmap, value2.bitmap); ac.cardinality = newCardinality; return ac; }
/// <summary> /// Computes the bitwise OR of this container with another (union). This /// container as well as the provided container are left unaffected. /// </summary> /// <param name="x">Other container</param> /// <returns>Aggregated container</returns> public abstract Container Or(ArrayContainer x);
public override Container Or(ArrayContainer x) { throw new NotImplementedException(); }
public override Container or(ArrayContainer value2) { BitsetContainer answer = (BitsetContainer) clone(); int c = value2.cardinality; for (int k = 0; k < c ; ++k) { ushort v = value2.content[k]; int i = v >> 6; long w = answer.bitmap[i]; long aft = w | (1L << v); answer.bitmap[i] = aft; //if (USE_BRANCHLESS) { answer.cardinality += (int)((w - aft) >> 63); //} else { // if (w != aft) // answer.cardinality++; //} } return answer; }
/// <summary> /// Returns the elements of this ArrayContainer that are not in the /// other BitSetContainer. /// </summary> /// <param name="x">the BitSetContainer to compare against</param> /// <returns>A new container with the differences</returns> public override Container andNot(BitsetContainer x) { var answer = new ArrayContainer(content.Length); int pos = 0; for (int k = 0; k < cardinality; ++k) { ushort val = this.content[k]; if (!x.contains(val)) answer.content[pos++] = val; } answer.cardinality = pos; return answer; }
/// <summary> /// Returns true if the current container intersects the other container. /// </summary> /// <param name="x">Other container</param> /// <returns>Whether they intersect</returns> public override bool Intersects(ArrayContainer x) { throw new NotImplementedException(); }
/// <summary> /// If the given index is not in the set add it, otherwise remove it. /// </summary> /// <param name="index">The index to flip</param> public void Flip(int x) { ushort hb = Utility.GetHighBits(x); int i = containers.GetIndex(hb); if (i >= 0) { Container c = containers.GetContainerAtIndex(i).Flip(Utility.GetLowBits(x)); if (c.GetCardinality() > 0) { containers.SetContainerAtIndex(i, c); } else { containers.RemoveAtIndex(i); } } else { ArrayContainer newac = new ArrayContainer(); containers.InsertNewKeyValueAt(-i - 1, hb, newac.Add(Utility.GetLowBits(x))); } }
/// <summary> /// Copies the data to an array container /// </summary> /// <returns>The array container</returns> public ArrayContainer ToArrayContainer() { ArrayContainer ac = new ArrayContainer(Cardinality); ac.LoadData(this); return ac; }
/// <summary> /// Computes the in-place bitwise OR of this container with another /// (union). The current container is generally modified, whereas the /// provided container(x) is unaffected.May generate a new container. /// </summary> /// <param name="x">Other container</param> /// <returns>Aggregated container</returns> public override Container IOr(ArrayContainer x) { return this.Or(x); }
/// <summary> /// Computes the bitwise AND of this container with another /// (intersection). This container as well as the provided container are /// left unaffected. /// </summary> /// <param name="x">Other container</param> /// <returns>Aggregated container</returns> public abstract Container And(ArrayContainer x);
/// <summary> /// Returns the elements of this ArrayContainer that are not in the /// other ArrayContainer. /// </summary> /// <param name="x">the other ArrayContainer</param> /// <returns>The modified container</returns> public override Container IAndNot(ArrayContainer x) { this.Cardinality = Utility.UnsignedDifference(this.Content, this.GetCardinality(), x.Content, x.GetCardinality(), this.Content); return this; }
/// <summary> /// Computes the in-place bitwise AND of this container with another /// (intersection). The current container is generally modified, whereas /// the provided container (x) is unaffected. May generate a new container. /// </summary> /// <param name="x">Other container</param> /// <returns>Aggregated container</returns> public override Container IAnd(ArrayContainer other) { Cardinality = Utility.UnsignedIntersect2by2(Content, GetCardinality(), other.Content, other.GetCardinality(), Content); return this; }
/// <summary> /// Computes the in-place bitwise OR of this container with another /// (union). The current container is generally modified, whereas the /// provided container(x) is unaffected.May generate a new container. /// </summary> /// <param name="x">Other container</param> /// <returns>Aggregated container</returns> public override Container IOr(ArrayContainer x) { return(this.Or(x)); }
/// <summary> /// Returns the elements of this BitsetContainer that are not in the /// other BitsetContainer. Depending on the cardinality of the result, /// this will either modify the container in place or return a new ArrayContainer. /// </summary> /// <param name="x">the other BitsetContainer</param> /// <returns>The current container, modified the differences</returns> public override Container IAndNot(BitsetContainer x) { int newCardinality = 0; for (int k = 0; k < this.Bitmap.Length; ++k) { newCardinality += Utility.LongBitCount(this.Bitmap[k] & (~x.Bitmap[k])); } if (newCardinality > ArrayContainer.DEFAULT_MAX_SIZE) { for (int k = 0; k < this.Bitmap.Length; ++k) { this.Bitmap[k] = this.Bitmap[k] & (~x.Bitmap[k]); } this.Cardinality = newCardinality; return this; } ArrayContainer ac = new ArrayContainer(newCardinality); Utility.FillArrayANDNOT(ac.Content, this.Bitmap, x.Bitmap); ac.Cardinality = newCardinality; return ac; }
/// <summary> /// Returns the elements of this BitsetContainer that are not in the /// ArrayContainer by modifying the current container in place. /// </summary> /// <param name="x">the ArrayContainer to compare against</param> /// <returns>A new container with the differences</returns> public override Container iandNot(ArrayContainer x) { for (int k = 0; k < x.cardinality; ++k) { this.remove(x.content[k]); } if (cardinality <= ArrayContainer.DEFAULT_MAX_SIZE) return this.toArrayContainer(); return this; }
/// <summary> /// Computes the bitwise OR of this container with another (union). This /// container as well as the provided container are left unaffected. /// </summary> /// <param name="x">Other container</param> /// <returns>Aggregated container</returns> public override Container Or(ArrayContainer x) { BitsetContainer answer = (BitsetContainer) Clone(); int c = x.Cardinality; for (int k = 0; k < c ; ++k) { ushort v = x.Content[k]; int i = v >> 6; long w = answer.Bitmap[i]; long aft = w | (1L << v); answer.Bitmap[i] = aft; answer.Cardinality += (int)((w - aft) >> 63); } return answer; }
/// <summary> /// Returns the elements of this ArrayContainer that are not in the /// other ArrayContainer. /// </summary> /// <param name="x">the other ArrayContainer</param> /// <returns>The modified container</returns> public override Container iandNot(ArrayContainer x) { this.cardinality = Utility.unsignedDifference(this.content, this.getCardinality(), x.content, x.getCardinality(), this.content); return this; }
public override Container and(ArrayContainer value2) { ArrayContainer value1 = this; int desiredCapacity = Math.Min(value1.getCardinality(), value2.getCardinality()); ArrayContainer answer = new ArrayContainer(desiredCapacity); answer.cardinality = Utility.unsignedIntersect2by2(value1.content, value1.getCardinality(), value2.content, value2.getCardinality(), answer.content); return answer; }
public override Container or(ArrayContainer value2) { ArrayContainer value1 = this; int totalCardinality = value1.getCardinality() + value2.getCardinality(); if (totalCardinality > DEFAULT_MAX_SIZE) { // it could be a bitmap! BitsetContainer bc = new BitsetContainer(); for (int k = 0; k < value2.cardinality; ++k) { ushort v = value2.content[k]; int i = v >> 6; bc.bitmap[i] |= (1L << v); } for (int k = 0; k < this.cardinality; ++k) { ushort v = this.content[k]; int i = v >> 6; bc.bitmap[i] |= (1L << v); } bc.cardinality = 0; foreach (long k in bc.bitmap) { bc.cardinality += Utility.longBitCount(k); } if (bc.cardinality <= DEFAULT_MAX_SIZE) return bc.toArrayContainer(); return bc; } else { // remains an array container int desiredCapacity = totalCardinality; // Math.min(BitmapContainer.MAX_CAPACITY, // totalCardinality); ArrayContainer answer = new ArrayContainer(desiredCapacity); answer.cardinality = Utility.unsignedUnion2by2(value1.content, value1.getCardinality(), value2.content, value2.getCardinality(), answer.content); return answer; } }
/// <summary> /// Performs an in-place intersection with another ArrayContainer. /// </summary> /// <param name="other">the other ArrayContainer to intersect</param> public override Container iand(ArrayContainer other) { cardinality = Utility.unsignedIntersect2by2(content, getCardinality(), other.content, other.getCardinality(), content); return this; }
/// <summary> /// Performs an "in-place" intersection with an ArrayContainer. Since /// no in-place operation is actually possible, this method defaults to /// calling ArrayContainer's and() method with this as input. /// </summary> /// <param name="other">the ArrayContainer to intersect</param> public override Container IAnd(ArrayContainer other) { return(other.And(this)); // No in-place possible }
public override Container ior(ArrayContainer x) { return this.or(x); }
/// <summary> /// Performs an intersection with another BitsetContainer. Depending on /// the cardinality of the result, this will either modify the container /// in place or return a new ArrayContainer. /// </summary> /// <param name="other">the other BitsetContainer to intersect</param> public override Container iand(BitsetContainer other) { int newCardinality = 0; for (int k = 0; k < bitmap.Length; ++k) { newCardinality += Utility.longBitCount( bitmap[k] & other.bitmap[k] ); } if (newCardinality > ArrayContainer.DEFAULT_MAX_SIZE) { for (int k = 0; k < bitmap.Length; ++k) { bitmap[k] = bitmap[k] & other.bitmap[k]; } cardinality = newCardinality; return this; } ArrayContainer ac = new ArrayContainer(newCardinality); Utility.fillArrayAND(ref ac.content, bitmap, other.bitmap); ac.cardinality = newCardinality; return ac; }
public override bool intersects(ArrayContainer x) { throw new NotImplementedException(); }
/// <summary> /// Computes the bitwise ANDNOT of this container with another /// (difference). Modifies the current container in place. /// </summary> /// <param name="x">Other container</param> public abstract Container IAndNot(ArrayContainer x);
/// <summary> /// Performs an "in-place" intersection with an ArrayContainer. Since /// no in-place operation is actually possible, this method defaults to /// calling ArrayContainer's and() method with this as input. /// </summary> /// <param name="other">the ArrayContainer to intersect</param> public override Container iand(ArrayContainer other) { return other.and(this); // No in-place possible }
/// <summary> /// Returns the elements of this BitsetContainer that are not in the /// ArrayContainer. /// </summary> /// <param name="x">the ArrayContainer to compare against</param> /// <returns>A new container with the differences</returns> public override Container AndNot(ArrayContainer x) { BitsetContainer answer = (BitsetContainer) Clone(); int c = x.Cardinality; for (int k = 0; k < c; ++k) { ushort v = x.Content[k]; uint i = (uint) (Utility.ToIntUnsigned(v) >> 6); long w = answer.Bitmap[i]; long aft = w & (~(1L << v)); answer.Bitmap[i] = aft; answer.Cardinality -= (int) ((w ^ aft) >> v); } if (answer.Cardinality <= ArrayContainer.DEFAULT_MAX_SIZE) { return answer.ToArrayContainer(); } return answer; }
public override Container ior(ArrayContainer x) { int c = x.cardinality; for (int k = 0; k < c; ++k) { ushort v = x.content[k]; int i = v >> 6; long w = this.bitmap[i]; long aft = w | (1L << v); this.bitmap[i] = aft; this.cardinality += (int)((w - aft) >> 63); } return this; }
/// <summary> /// Performs an intersection with another BitsetContainer. Depending on /// the cardinality of the result, this will either modify the container /// in place or return a new ArrayContainer. /// </summary> /// <param name="other">the other BitsetContainer to intersect</param> public override Container IAnd(BitsetContainer other) { int newCardinality = 0; for (int k = 0; k < Bitmap.Length; ++k) { newCardinality += Utility.LongBitCount(Bitmap[k] & other.Bitmap[k]); } if (newCardinality > ArrayContainer.DEFAULT_MAX_SIZE) { for (int k = 0; k < Bitmap.Length; ++k) { Bitmap[k] = Bitmap[k] & other.Bitmap[k]; } Cardinality = newCardinality; return this; } ArrayContainer ac = new ArrayContainer(newCardinality); Utility.FillArrayAND(ref ac.Content, Bitmap, other.Bitmap); ac.Cardinality = newCardinality; return ac; }
/** * Copies the data to an array container * * @return the array container */ public ArrayContainer toArrayContainer() { ArrayContainer ac = new ArrayContainer(cardinality); ac.loadData(this); return ac; }
/// <summary> /// Returns the elements of this BitsetContainer that are not in the /// ArrayContainer by modifying the current container in place. /// </summary> /// <param name="x">the ArrayContainer to compare against</param> /// <returns>A new container with the differences</returns> public override Container IAndNot(ArrayContainer x) { for (int k = 0; k < x.Cardinality; ++k) { this.Remove(x.Content[k]); } if (Cardinality <= ArrayContainer.DEFAULT_MAX_SIZE) { return this.ToArrayContainer(); } return this; }
public override Container and(ArrayContainer x) { ArrayContainer answer = new ArrayContainer(x.content.Length); int c = x.cardinality; for (int i=0; i<c; i++) { ushort v = x.content[i]; if (this.contains(v)) answer.content[answer.cardinality++] = v; } return answer; }
/// <summary> /// Returns true if the current container intersects the other container. /// </summary> /// <param name="x">Other container</param> /// <returns>Whether they intersect</returns> public abstract bool Intersects(ArrayContainer x);