/// <summary> /// Primary Reader for PackedBits. /// </summary> public static ulong ReadPackedBits(this uint buffer, ref int bitposition, int bits) { var packsize = BitCounter.UsedBitCount(bits); int cnt = (int)buffer.Read(ref bitposition, (int)packsize); return(buffer.Read(ref bitposition, cnt)); }
protected override void OnCompile() { this.Append(new UB(1, Convert.ToInt32(this.hasAdd))); this.Append(new UB(1, Convert.ToInt32(this.hasMult))); BitCounter.Init(); if (this.hasAdd) { BitCounter.Push(SB.NumBits(this.aR)); BitCounter.Push(SB.NumBits(this.aG)); BitCounter.Push(SB.NumBits(this.aB)); } if (this.hasMult) { BitCounter.Push(SB.NumBits(this.mR)); BitCounter.Push(SB.NumBits(this.mG)); BitCounter.Push(SB.NumBits(this.mB)); } int maxim = BitCounter.Maxim; this.Append(new UB(4, maxim)); if (this.hasAdd) { this.Append(new SB(maxim, this.aR)); this.Append(new SB(maxim, this.aG)); this.Append(new SB(maxim, this.aB)); } if (this.hasMult) { this.Append(new SB(maxim, this.mR)); this.Append(new SB(maxim, this.mG)); this.Append(new SB(maxim, this.mB)); } base.OnCompile(); }
public void CountOnes() { var sut = new BitCounter(); int actual = sut.CountOnes(0b1001_0001_0000_0000, 8); Assert.Equal(3, actual); }
protected override void OnCompile() { int maxim; this.Append(new UB(1, 1)); this.Append(new UB(1, 1)); if (this.General) { BitCounter.Init(); BitCounter.Push(SB.NumBits(this.posX)); BitCounter.Push(SB.NumBits(this.posY)); maxim = BitCounter.Maxim; } else { maxim = SB.NumBits(this.pos); } this.Append(new UB(4, maxim - 2)); this.Append(new UB(1, Convert.ToInt32(this.General))); if (this.General) { this.Append(new SB(maxim, this.posX)); this.Append(new SB(maxim, this.posY)); } else { this.Append(new UB(1, Convert.ToInt32(this.isVert))); this.Append(new SB(maxim, this.pos)); } }
public void CountWithPatternAndHoles2() { var(row, mask, size) = "1100110XXX0XX1".ToRowWithMaskAndSize(); var sut = new BitCounter(); int actual = sut.CountOnes(row, size, mask, includeHoles: true); Assert.Equal(6, actual); }
public void CountZeros() { var sut = new BitCounter(); int actual = sut.CountZeros(0b1001_0001_0000_0000, 8); Assert.Equal(5, actual); actual = sut.CountZeros(0b1100_1010_0000_0000, 8, 0b1111_1110_0000_0000); Assert.Equal(3, actual); }
public void CountHolesCorrectly() { var sut = new BitCounter(); int actual = sut.CountHoles( row: 0b0100_0101_1001_0000, mask: 0b1100_1111_1111_0100, size: 14); Assert.Equal(1, actual); }
/// <summary> /// EXPERIMENTAL: Primary WritePacked Method /// </summary> public static ushort WritePackedBits(this ushort buffer, byte value, ref int bitposition, int bits) { int countbits = BitCounter.UsedBitCount((uint)bits); int cnt = value.UsedBitCount(); buffer = buffer.Write((uint)(cnt), ref bitposition, (int)countbits); buffer = buffer.Write(value, ref bitposition, cnt); //UnityEngine.Debug.Log(value + " = ones : " + cnt + " / " + (int)countbits + " total bits: " + ((int)countbits + cnt)); return(buffer); }
public void CountWithPatternAndHoles() { var sut = new BitCounter(); int actual = sut.CountOnes( row: 0b0100_0101_1001_0000, size: 14, mask: 0b1100_1111_1111_0100, includeHoles: true); Assert.Equal(6, actual); }
public void BbSignalsTestMethod2() { const int length = 1000; const int expandSize = 100; var input = new byte[length]; var output = new byte[length]; var gamma = new byte[length * expandSize]; var buffer = new double[length * BitsPerByte * expandSize]; Rng.GetBytes(input); Rng.GetBytes(gamma); double alpha = 1; double betta = 1; using (var bbSignals = new BbSignals(expandSize, true)) { // Вычисление количества единиц в исходных данных и псевдослучайной последовательности double trueData = BitCounter.Count(input); double trueGamma = BitCounter.Count(gamma); // Вычисление количества нулей в исходных данных и псевдослучайной последовательности var falseData = (long)input.Length * BitsPerByte - trueData; var falseGamma = (long)gamma.Length * BitsPerByte - trueGamma; // Вычисление оценки количества единиц и нулей при смешивании исходных данных и псевдослучайной последовательности var trueCount = trueGamma * falseData + falseGamma * trueData; var falseCount = trueGamma * trueData + falseGamma * falseData; Console.WriteLine("p0 = {0} p1 = {1}", trueCount / (trueCount + falseCount), falseCount / (trueCount + falseCount)); betta = ((falseCount - trueCount) * alpha / (trueCount + falseCount)); Console.WriteLine("alpha = {0} betta = {1}", alpha, betta); bbSignals.Combine(buffer, input, gamma, alpha, betta); ////////////////////////////////////////////////// buffer = buffer.Select(x => x + 1).ToArray(); var e1 = buffer.Average(); var e2 = buffer.Average(x => x * x); betta = e1; alpha = Math.Sqrt(e2 - e1 * e1); Console.WriteLine("alpha = {0} betta = {1}", alpha, betta); bbSignals.Extract(buffer, output, gamma, alpha, betta); ///////////////////////////////////////////////////////// } Assert.IsTrue(input.SequenceEqual(output)); }
protected override void OnCompile() { int val = 0; int num2 = 0; int num3 = 0; if (this.LineIndex > 0) { val = 1; } if (this.FillIndex0 > 0) { num2 = 1; } if (this.FillIndex1 > 0) { num3 = 1; } this.Append(new UB(1, 0)); this.Append(new UB(1, 0)); this.Append(new UB(1, val)); this.Append(new UB(1, num2)); this.Append(new UB(1, num3)); this.Append(new UB(1, Convert.ToInt32(this.MoveTo))); if (this.MoveTo) { BitCounter.Init(); BitCounter.Push(SB.NumBits(this.MoveX)); BitCounter.Push(SB.NumBits(this.MoveY)); int maxim = BitCounter.Maxim; this.Append(new UB(5, maxim)); this.Append(new SB(maxim, this.MoveX)); this.Append(new SB(maxim, this.MoveY)); } if (num2 == 1) { this.Append(new UB(this.Parent.NumFillBits, this.FillIndex0)); } if (num3 == 1) { this.Append(new UB(this.Parent.NumFillBits, this.FillIndex1)); } if (val == 1) { this.Append(new UB(this.Parent.NumLineBits, this.LineIndex)); } }
protected override void OnCompile() { this.Append(new UB(1, 1)); this.Append(new UB(1, 0)); BitCounter.Init(); BitCounter.Push(SB.NumBits(this.posX)); BitCounter.Push(SB.NumBits(this.posY)); BitCounter.Push(SB.NumBits(this.anchX)); BitCounter.Push(SB.NumBits(this.anchY)); int maxim = BitCounter.Maxim; this.Append(new UB(4, maxim - 2)); this.Append(new SB(maxim, this.posX)); this.Append(new SB(maxim, this.posY)); this.Append(new SB(maxim, this.anchX)); this.Append(new SB(maxim, this.anchY)); }
public ISearchState Solve(ISearchState puzzle) { var frontier = Sets.All.Where(p => puzzle[p].HasValue).ToList(); while (frontier.Any()) { var newFrontier = new List <(int x, int y)>(); foreach (var p1 in frontier) { foreach (var set in Sets.ContainingSets(p1)) { foreach (var subSet in BitCounter.PowerSet(set, 1, _n - 1)) { var intersect = puzzle.BitDomain(subSet.First()); foreach (var i in subSet.Skip(1)) { intersect |= puzzle.BitDomain(i); } if (BitCounter.Count(intersect) <= subSet.Count) { foreach (var p2 in set.Where(p => !subSet.Contains(p))) { if (puzzle.DomainMinus(p2, intersect)) { newFrontier.Add(p2); } } } } } } frontier = newFrontier.Distinct().ToList(); } return(puzzle); }
public void CountIntBits() { Assert.AreEqual(0, BitCounter.Count(0x00000000L)); Assert.AreEqual(32, BitCounter.Count(0xFFFFFFFFL)); Assert.AreEqual(16, BitCounter.Count(0x5A5A5A5AL)); Assert.AreEqual(16, BitCounter.Count(0xA5A5A5A5L)); Assert.AreEqual(1, BitCounter.Count(0x00000001L)); Assert.AreEqual(2, BitCounter.Count(0x00000003L)); Assert.AreEqual(3, BitCounter.Count(0x00000007L)); Assert.AreEqual(4, BitCounter.Count(0x0000000FL)); Assert.AreEqual(5, BitCounter.Count(0x0000001FL)); Assert.AreEqual(6, BitCounter.Count(0x0000003FL)); Assert.AreEqual(7, BitCounter.Count(0x0000007FL)); Assert.AreEqual(8, BitCounter.Count(0x000000FFL)); Assert.AreEqual(9, BitCounter.Count(0x000001FFL)); Assert.AreEqual(10, BitCounter.Count(0x000003FFL)); Assert.AreEqual(11, BitCounter.Count(0x000007FFL)); Assert.AreEqual(12, BitCounter.Count(0x00000FFFL)); Assert.AreEqual(13, BitCounter.Count(0x00001FFFL)); Assert.AreEqual(14, BitCounter.Count(0x00003FFFL)); Assert.AreEqual(15, BitCounter.Count(0x00007FFFL)); Assert.AreEqual(16, BitCounter.Count(0x0000FFFFL)); Assert.AreEqual(17, BitCounter.Count(0x0001FFFFL)); Assert.AreEqual(18, BitCounter.Count(0x0003FFFFL)); Assert.AreEqual(19, BitCounter.Count(0x0007FFFFL)); Assert.AreEqual(20, BitCounter.Count(0x000FFFFFL)); Assert.AreEqual(21, BitCounter.Count(0x001FFFFFL)); Assert.AreEqual(22, BitCounter.Count(0x003FFFFFL)); Assert.AreEqual(23, BitCounter.Count(0x007FFFFFL)); Assert.AreEqual(24, BitCounter.Count(0x00FFFFFFL)); Assert.AreEqual(25, BitCounter.Count(0x01FFFFFFL)); Assert.AreEqual(26, BitCounter.Count(0x03FFFFFFL)); Assert.AreEqual(27, BitCounter.Count(0x07FFFFFFL)); Assert.AreEqual(28, BitCounter.Count(0x0FFFFFFFL)); Assert.AreEqual(29, BitCounter.Count(0x1FFFFFFFL)); Assert.AreEqual(30, BitCounter.Count(0x3FFFFFFFL)); Assert.AreEqual(31, BitCounter.Count(0x7FFFFFFFL)); Assert.AreEqual(32, BitCounter.Count(0xFFFFFFFFL)); }
public void CountUlongBits() { Assert.AreEqual(0, BitCounter.Count(0x0000000000000000L)); Assert.AreEqual(64, BitCounter.Count(0xFFFFFFFFFFFFFFFFL)); Assert.AreEqual(32, BitCounter.Count(0x5A5A5A5A5A5A5A5AL)); Assert.AreEqual(32, BitCounter.Count(0xA5A5A5A5A5A5A5A5L)); Assert.AreEqual(1, BitCounter.Count(0x0000000000000001L)); Assert.AreEqual(2, BitCounter.Count(0x0000000000000003L)); Assert.AreEqual(3, BitCounter.Count(0x0000000000000007L)); Assert.AreEqual(4, BitCounter.Count(0x000000000000000FL)); Assert.AreEqual(5, BitCounter.Count(0x000000000000001FL)); Assert.AreEqual(6, BitCounter.Count(0x000000000000003FL)); Assert.AreEqual(7, BitCounter.Count(0x000000000000007FL)); Assert.AreEqual(8, BitCounter.Count(0x00000000000000FFL)); Assert.AreEqual(9, BitCounter.Count(0x00000000000001FFL)); Assert.AreEqual(10, BitCounter.Count(0x00000000000003FFL)); Assert.AreEqual(11, BitCounter.Count(0x00000000000007FFL)); Assert.AreEqual(12, BitCounter.Count(0x0000000000000FFFL)); Assert.AreEqual(13, BitCounter.Count(0x0000000000001FFFL)); Assert.AreEqual(14, BitCounter.Count(0x0000000000003FFFL)); Assert.AreEqual(15, BitCounter.Count(0x0000000000007FFFL)); Assert.AreEqual(16, BitCounter.Count(0x000000000000FFFFL)); Assert.AreEqual(17, BitCounter.Count(0x000000000001FFFFL)); Assert.AreEqual(18, BitCounter.Count(0x000000000003FFFFL)); Assert.AreEqual(19, BitCounter.Count(0x000000000007FFFFL)); Assert.AreEqual(20, BitCounter.Count(0x00000000000FFFFFL)); Assert.AreEqual(21, BitCounter.Count(0x00000000001FFFFFL)); Assert.AreEqual(22, BitCounter.Count(0x00000000003FFFFFL)); Assert.AreEqual(23, BitCounter.Count(0x00000000007FFFFFL)); Assert.AreEqual(24, BitCounter.Count(0x0000000000FFFFFFL)); Assert.AreEqual(25, BitCounter.Count(0x0000000001FFFFFFL)); Assert.AreEqual(26, BitCounter.Count(0x0000000003FFFFFFL)); Assert.AreEqual(27, BitCounter.Count(0x0000000007FFFFFFL)); Assert.AreEqual(28, BitCounter.Count(0x000000000FFFFFFFL)); Assert.AreEqual(29, BitCounter.Count(0x000000001FFFFFFFL)); Assert.AreEqual(30, BitCounter.Count(0x000000003FFFFFFFL)); Assert.AreEqual(31, BitCounter.Count(0x000000007FFFFFFFL)); Assert.AreEqual(32, BitCounter.Count(0x00000000FFFFFFFFL)); Assert.AreEqual(1 + 32, BitCounter.Count(0x00000001FFFFFFFFL)); Assert.AreEqual(2 + 32, BitCounter.Count(0x00000003FFFFFFFFL)); Assert.AreEqual(3 + 32, BitCounter.Count(0x00000007FFFFFFFFL)); Assert.AreEqual(4 + 32, BitCounter.Count(0x0000000FFFFFFFFFL)); Assert.AreEqual(5 + 32, BitCounter.Count(0x0000001FFFFFFFFFL)); Assert.AreEqual(6 + 32, BitCounter.Count(0x0000003FFFFFFFFFL)); Assert.AreEqual(7 + 32, BitCounter.Count(0x0000007FFFFFFFFFL)); Assert.AreEqual(8 + 32, BitCounter.Count(0x000000FFFFFFFFFFL)); Assert.AreEqual(9 + 32, BitCounter.Count(0x000001FFFFFFFFFFL)); Assert.AreEqual(10 + 32, BitCounter.Count(0x000003FFFFFFFFFFL)); Assert.AreEqual(11 + 32, BitCounter.Count(0x000007FFFFFFFFFFL)); Assert.AreEqual(12 + 32, BitCounter.Count(0x00000FFFFFFFFFFFL)); Assert.AreEqual(13 + 32, BitCounter.Count(0x00001FFFFFFFFFFFL)); Assert.AreEqual(14 + 32, BitCounter.Count(0x00003FFFFFFFFFFFL)); Assert.AreEqual(15 + 32, BitCounter.Count(0x00007FFFFFFFFFFFL)); Assert.AreEqual(16 + 32, BitCounter.Count(0x0000FFFFFFFFFFFFL)); Assert.AreEqual(17 + 32, BitCounter.Count(0x0001FFFFFFFFFFFFL)); Assert.AreEqual(18 + 32, BitCounter.Count(0x0003FFFFFFFFFFFFL)); Assert.AreEqual(19 + 32, BitCounter.Count(0x0007FFFFFFFFFFFFL)); Assert.AreEqual(20 + 32, BitCounter.Count(0x000FFFFFFFFFFFFFL)); Assert.AreEqual(21 + 32, BitCounter.Count(0x001FFFFFFFFFFFFFL)); Assert.AreEqual(22 + 32, BitCounter.Count(0x003FFFFFFFFFFFFFL)); Assert.AreEqual(23 + 32, BitCounter.Count(0x007FFFFFFFFFFFFFL)); Assert.AreEqual(24 + 32, BitCounter.Count(0x00FFFFFFFFFFFFFFL)); Assert.AreEqual(25 + 32, BitCounter.Count(0x01FFFFFFFFFFFFFFL)); Assert.AreEqual(26 + 32, BitCounter.Count(0x03FFFFFFFFFFFFFFL)); Assert.AreEqual(27 + 32, BitCounter.Count(0x07FFFFFFFFFFFFFFL)); Assert.AreEqual(28 + 32, BitCounter.Count(0x0FFFFFFFFFFFFFFFL)); Assert.AreEqual(29 + 32, BitCounter.Count(0x1FFFFFFFFFFFFFFFL)); Assert.AreEqual(30 + 32, BitCounter.Count(0x3FFFFFFFFFFFFFFFL)); Assert.AreEqual(31 + 32, BitCounter.Count(0x7FFFFFFFFFFFFFFFL)); Assert.AreEqual(32 + 32, BitCounter.Count(0xFFFFFFFFFFFFFFFFL)); }
public void countBitsTestMinus1() { Assert.AreEqual(32, BitCounter.countBits(-1)); }
public void countBitsTest100() { Assert.AreEqual(3, BitCounter.countBits(100)); }
public void countBitsTest1() { Assert.AreEqual(1, BitCounter.countBits(1)); }
public FiveHoleSolver(BitCounter counter) => this.counter = counter;
public OnesAllDoneSolver(BitCounter bitCounter) => this.counter = bitCounter;
public void TestCount() { Assert.AreEqual(BitCounter.Count(0), 0); Assert.AreEqual(BitCounter.Count(0b1101), 3); Assert.AreEqual(BitCounter.Count(0b1101_0011), 5); }
public ThreeHoleSolver(BitCounter counter) => this.counter = counter;