|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace ProbabilisticDataStructures |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Buckets64 is a fast, space-efficient array of buckets where each bucket can store |
| 11 | + /// up to a configured maximum value. |
| 12 | + /// </summary> |
| 13 | + public class Buckets64 |
| 14 | + { |
| 15 | + // The largest C# array to create; the largest power of 2 that C# can support. |
| 16 | + private const uint maxArraySize = 1U << 30; |
| 17 | + private byte[][] Data { get; set; } |
| 18 | + private int arrayCount { get; set; } |
| 19 | + private byte bucketSize { get; set; } |
| 20 | + private byte _max; |
| 21 | + private int Max |
| 22 | + { |
| 23 | + get |
| 24 | + { |
| 25 | + return _max; |
| 26 | + } |
| 27 | + set |
| 28 | + { |
| 29 | + // TODO: Figure out this truncation thing. |
| 30 | + // I'm not sure if MaxValue is always supposed to be capped at 255 via |
| 31 | + // a byte conversion or not... |
| 32 | + if (value > byte.MaxValue) |
| 33 | + _max = byte.MaxValue; |
| 34 | + else |
| 35 | + _max = (byte)value; |
| 36 | + } |
| 37 | + } |
| 38 | + internal ulong count { get; set; } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Creates a new Buckets64 with the provided number of buckets where each bucket |
| 42 | + /// is the specified number of bits. |
| 43 | + /// </summary> |
| 44 | + /// <param name="count">Number of buckets.</param> |
| 45 | + /// <param name="bucketSize">Number of bits per bucket.</param> |
| 46 | + internal Buckets64(ulong count, byte bucketSize) |
| 47 | + { |
| 48 | + this.count = count; |
| 49 | + this.bucketSize = bucketSize; |
| 50 | + AllocateArray(count, bucketSize); |
| 51 | + this.Max = (1 << bucketSize) - 1; |
| 52 | + } |
| 53 | + |
| 54 | + private void AllocateArray(ulong count, byte bucketSize) |
| 55 | + { |
| 56 | + this.arrayCount = (int)(count / maxArraySize + 1); |
| 57 | + this.Data = new byte[this.arrayCount][]; |
| 58 | + var bytesToAllocate = (count * bucketSize + 7) / 8; |
| 59 | + for (int i = 0; i < this.arrayCount; i++) |
| 60 | + { |
| 61 | + var arraySize = Math.Min(bytesToAllocate, maxArraySize); |
| 62 | + this.Data[i] = new byte[arraySize]; |
| 63 | + bytesToAllocate -= arraySize; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Returns the maximum value that can be stored in a bucket. |
| 69 | + /// </summary> |
| 70 | + /// <returns>The bucket max value.</returns> |
| 71 | + internal byte MaxBucketValue() |
| 72 | + { |
| 73 | + return this._max; |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Increment the value in the specified bucket by the provided delta. A bucket |
| 78 | + /// can be decremented by providing a negative delta. |
| 79 | + /// <para> |
| 80 | + /// The value is clamped to zero and the maximum bucket value. Returns itself |
| 81 | + /// to allow for chaining. |
| 82 | + /// </para> |
| 83 | + /// </summary> |
| 84 | + /// <param name="bucket">The bucket to increment.</param> |
| 85 | + /// <param name="delta">The amount to increment the bucket by.</param> |
| 86 | + /// <returns>The modified bucket.</returns> |
| 87 | + internal Buckets64 Increment(uint bucket, int delta) |
| 88 | + { |
| 89 | + int val = (int)(GetBits(bucket * this.bucketSize, this.bucketSize) + delta); |
| 90 | + |
| 91 | + if (val > this.Max) |
| 92 | + val = this.Max; |
| 93 | + else if (val < 0) |
| 94 | + val = 0; |
| 95 | + |
| 96 | + SetBits((uint)bucket * (uint)this.bucketSize, this.bucketSize, (uint)val); |
| 97 | + return this; |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Set the bucket value. The value is clamped to zero and the maximum bucket |
| 102 | + /// value. Returns itself to allow for chaining. |
| 103 | + /// </summary> |
| 104 | + /// <param name="bucket">The bucket to change the value of.</param> |
| 105 | + /// <param name="value">The value to set.</param> |
| 106 | + /// <returns>The modified bucket.</returns> |
| 107 | + internal Buckets64 Set(ulong bucket, byte value) |
| 108 | + { |
| 109 | + if (value > this._max) |
| 110 | + value = this._max; |
| 111 | + |
| 112 | + SetBits(bucket * this.bucketSize, this.bucketSize, value); |
| 113 | + return this; |
| 114 | + } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Returns the value in the specified bucket. |
| 118 | + /// </summary> |
| 119 | + /// <param name="bucket">The bucket to get.</param> |
| 120 | + /// <returns>The specified bucket.</returns> |
| 121 | + internal uint Get(ulong bucket) |
| 122 | + { |
| 123 | + return GetBits(bucket * this.bucketSize, this.bucketSize); |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Restores the Buckets64 to the original state. Returns itself to allow for |
| 128 | + /// chaining. |
| 129 | + /// </summary> |
| 130 | + /// <returns>The Buckets64 object the reset operation was performed on.</returns> |
| 131 | + internal Buckets64 Reset() |
| 132 | + { |
| 133 | + AllocateArray(this.count, this.bucketSize); |
| 134 | + return this; |
| 135 | + } |
| 136 | + |
| 137 | + /// <summary> |
| 138 | + /// Returns the bits at the specified offset and length. |
| 139 | + /// </summary> |
| 140 | + /// <param name="offset">The position to start reading at.</param> |
| 141 | + /// <param name="length">The distance to read from the offset.</param> |
| 142 | + /// <returns>The bits at the specified offset and length.</returns> |
| 143 | + internal uint GetBits(ulong offset, int length) |
| 144 | + { |
| 145 | + ulong byteIndex = offset / 8; |
| 146 | + int byteOffset = (int)(offset % 8); |
| 147 | + |
| 148 | + if ((byteOffset + length) > 8) |
| 149 | + { |
| 150 | + int rem = 8 - byteOffset; |
| 151 | + return GetBits(offset, rem) |
| 152 | + | (GetBits(offset + (ulong)rem, length - rem) << rem); |
| 153 | + } |
| 154 | + |
| 155 | + var dataArray = this.Data[byteIndex / maxArraySize]; |
| 156 | + var dataArrayByteIndex = byteIndex % maxArraySize; |
| 157 | + int bitMask = (1 << length) - 1; |
| 158 | + return (uint)((dataArray[dataArrayByteIndex] & (bitMask << byteOffset)) >> byteOffset); |
| 159 | + } |
| 160 | + |
| 161 | + /// <summary> |
| 162 | + /// Sets bits at the specified offset and length. |
| 163 | + /// </summary> |
| 164 | + /// <param name="offset">The position to start writing at.</param> |
| 165 | + /// <param name="length">The distance to write from the offset.</param> |
| 166 | + /// <param name="bits">The bits to write.</param> |
| 167 | + internal void SetBits(ulong offset, int length, uint bits) |
| 168 | + { |
| 169 | + ulong byteIndex = offset / 8; |
| 170 | + int byteOffset = (int)(offset % 8); |
| 171 | + |
| 172 | + if ((byteOffset + length) > 8) |
| 173 | + { |
| 174 | + int rem = 8 - byteOffset; |
| 175 | + SetBits(offset, (byte)rem, bits); |
| 176 | + SetBits(offset + (ulong)rem, length - rem, bits >> rem); |
| 177 | + return; |
| 178 | + } |
| 179 | + |
| 180 | + var dataArray = this.Data[(uint)(byteIndex / maxArraySize)]; |
| 181 | + var dataArrayByteIndex = (uint)(byteIndex % maxArraySize); |
| 182 | + int bitMask = (1 << length) - 1; |
| 183 | + dataArray[dataArrayByteIndex] = |
| 184 | + (byte)((dataArray[dataArrayByteIndex]) & ~(bitMask << byteOffset)); |
| 185 | + dataArray[dataArrayByteIndex] = |
| 186 | + (byte)((dataArray[dataArrayByteIndex]) | ((bits & bitMask) << byteOffset)); |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments