/// <summary> /// Adds the specified value to the current bitmap /// </summary> /// <param name="x">Value to be added</param> 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 ac = new ArrayContainer(); containers.InsertNewKeyValueAt(-containerIndex - 1, highBits, ac.Add(Utility.GetLowBits(x))); } }
/// <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); } } }
/// <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> /// Adds the specified value to the current bitmap /// </summary> /// <param name="x">Value to be added</param> 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))); } }
/// <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> /// 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); } } }