public Dictionary <long, long> ApplyMask(string mask, int address, string value) { string reverseMask = new string(mask.Reverse().ToArray()); BitArray orMask = new BitArray(reverseMask.Replace('X', '0').Select(x => x == '1').ToArray()); BitArray bitValue = BitArrayHelper.GetBitArrayFor(address, orMask.Length); BitArray orValue = new BitArray(bitValue).Or(orMask); BitArray xMask = new BitArray(reverseMask.Replace('1', '0').Replace('X', '1').Select(x => x == '1').ToArray()); List <BitArray> addresses = new List <BitArray> { orValue }; for (int index = 0; index < xMask.Length; index++) { if (xMask[index]) { List <BitArray> testList = addresses.ToList(); foreach (BitArray currentAddress in testList) { currentAddress.Set(index, false); BitArray newAddress = new BitArray(currentAddress); newAddress.Set(index, true); addresses.Add(newAddress); } } } long newValue = long.Parse(value); Dictionary <long, long> result = new Dictionary <long, long>(); addresses.ForEach(x => result.Add( key: BitArrayHelper.GetBitArrayValue(x), value: newValue )); return(result); }
public void TestToBitArray() { string data = "10001101001"; BitArray bits = BitArrayHelper.ToBitArray(data); AssertBits(data, bits); }
public Dictionary <long, long> ApplyMask(string mask, int address, string value) { string reverseMask = new string(mask.Reverse().ToArray()); BitArray andMask = new BitArray(reverseMask.Replace('X', '1').Select(x => x == '1').ToArray()); BitArray orMask = new BitArray(reverseMask.Replace('X', '0').Select(x => x == '1').ToArray()); BitArray bitValue = BitArrayHelper.GetBitArrayFor(int.Parse(value), andMask.Length); Dictionary <long, long> result = new Dictionary <long, long>(); result.Add(address, BitArrayHelper.GetBitArrayValue(bitValue.Or(orMask).And(andMask))); return(result); }
public void TestToBitMatrix() { string[] data = new string[3]; data[0] = "1001001001000111010"; data[1] = "1111100000"; data[2] = new string('1', 700); BitArray[] bits = BitArrayHelper.ToBitMatrix(data); Assert.AreEqual(data.Length, bits.Length); for (int i = 0; i < data.Length; ++i) { AssertBits(data[i], bits[i]); } }
public void TestPopBack() { BitArray[] bits = new BitArray[3]; BitArray first = bits[0] = new BitArray(new int[] { 1 }); BitArray second = bits[1] = new BitArray(new int[] { 2 }); BitArray last = bits[2] = new BitArray(new int[] { 4 }); BitArray result = BitArrayHelper.PopBack(ref bits); Assert.AreSame(last, result); Assert.AreEqual(2, bits.Length); Assert.AreSame(first, bits[0]); Assert.AreSame(second, bits[1]); }
public void ReverseTest() { BitArray _array = new BitArray(8); for (int i = 0; i < 5; i++) { _array[i] = true; } byte[] _expected = new byte[1] { 0x1F }; CollectionAssert.AreEqual(_expected, BitArrayHelper.Reverse(_array).ToBytes()); }
public void ToBytesTest() { BitArray _array = new BitArray(8); for (int i = 0; i < _array.Length; i++) { _array[i] = true; } byte[] _expected = new byte[1] { 0xFF }; CollectionAssert.AreEqual(_expected, BitArrayHelper.ToBytes(_array)); }
public void TestToBitArrayWrongData() { BitArrayHelper.ToBitArray("10010010100102"); }
public void ToBinaryStringTest() { Assert.AreEqual("00000000", BitArrayHelper.ToBinaryString(new BitArray(8))); }
/// <summary> /// /// </summary> /// <param name="currentNode"></param> /// <param name="trieIndexHeader"></param> /// <param name="index"></param> /// <remarks>Don't forget to dispose stream</remarks> /// <returns></returns> public static int SerializeIndexWithBinaryWriter(TrieNode rootNode, TrieIndexHeader trieIndexHeader, Stream index) { int processedNodeCount = 0; Queue <TrieNode> serializerQueue = new Queue <TrieNode>(); serializerQueue.Enqueue(rootNode); TrieNode currentNode = null; BinaryWriter binaryWriter = new BinaryWriter(index); //uint position = 0; //var sb = new StringBuilder(); while (serializerQueue.Count > 0) { currentNode = serializerQueue.Dequeue(); if (currentNode == null) { throw new InvalidDataException(string.Format("Value cannot be null ", processedNodeCount)); } long currentPositionOfStream = binaryWriter.BaseStream.Position; // write character //bw.Write(Encoding.Unicode.GetBytes(node.Character.ToString())); UInt16?characterIndex = TrieIndexHeaderCharacterReader.Instance.GetCharacterIndex(trieIndexHeader, currentNode.Character); if (characterIndex != null && characterIndex.HasValue) { binaryWriter.Write(characterIndex.Value); } else { binaryWriter.Write(Convert.ToUInt16(0)); // Its root } binaryWriter.Write(currentNode.IsTerminal); //if (currentNode.IsTerminal) //{ // //sb.AppendLine(currentNode.Character); //} // write children flags // convert 512 bool value to 64 byte value for efficient storage BitArray baChildren = new BitArray(trieIndexHeader.COUNT_OF_CHARSET); if (currentNode.Children != null) { foreach (var item in currentNode.Children) { UInt16?itemIndex = TrieIndexHeaderCharacterReader.Instance.GetCharacterIndex(trieIndexHeader, item.Key); baChildren.Set(itemIndex.Value, true); } } int[] childrenFlags = new int[trieIndexHeader.COUNT_OF_CHILDREN_FLAGS_IN_BYTES]; BitArrayHelper.CopyToInt32Array(baChildren, childrenFlags, 0); for (int i = 0; i < childrenFlags.Length; i++) { binaryWriter.Write(childrenFlags[i]); } // write children offset binaryWriter.Write(currentNode.ChildrenCount * trieIndexHeader.LENGTH_OF_STRUCT); // todo:position of text file if (currentNode.PositionOnTextFile.HasValue) { binaryWriter.Write((uint)currentNode.PositionOnTextFile.Value); } else { binaryWriter.Write((uint)0); } if (currentNode.Children != null) { foreach (var childNode in currentNode.Children) { serializerQueue.Enqueue(childNode.Value); } } ++processedNodeCount; } return(processedNodeCount); }
public void TestToBitArrayWrongData() { Assert.Throws <ArgumentException>(() => BitArrayHelper.ToBitArray("10010010100102")); }