New C# port from Suraj Supekar
[zxing.git] / csharp / common / BitArray.cs
diff --git a/csharp/common/BitArray.cs b/csharp/common/BitArray.cs
new file mode 100755 (executable)
index 0000000..51f2776
--- /dev/null
@@ -0,0 +1,208 @@
+/*\r
+* Copyright 2007 ZXing authors\r
+*\r
+* Licensed under the Apache License, Version 2.0 (the "License");\r
+* you may not use this file except in compliance with the License.\r
+* You may obtain a copy of the License at\r
+*\r
+*      http://www.apache.org/licenses/LICENSE-2.0\r
+*\r
+* Unless required by applicable law or agreed to in writing, software\r
+* distributed under the License is distributed on an "AS IS" BASIS,\r
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+* See the License for the specific language governing permissions and\r
+* limitations under the License.\r
+*/\r
+using System;\r
+namespace com.google.zxing.common\r
+{\r
+       \r
+       /// <summary> <p>A simple, fast array of bits, represented compactly by an array of ints internally.</p>\r
+       /// \r
+       /// </summary>\r
+       /// <author>  Sean Owen\r
+       /// </author>\r
+       /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
+       /// </author>\r
+       public sealed class BitArray\r
+       {\r
+               public int Size\r
+               {\r
+                       get\r
+                       {\r
+                               return size;\r
+                       }\r
+                       \r
+               }\r
+               \r
+               // TODO: I have changed these members to be public so ProGuard can inline get() and set(). Ideally\r
+               // they'd be private and we'd use the -allowaccessmodification flag, but Dalvik rejects the\r
+               // resulting binary at runtime on Android. If we find a solution to this, these should be changed\r
+               // back to private.\r
+               public int[] bits;\r
+               //UPGRADE_NOTE: Final was removed from the declaration of 'size '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+               public int size;\r
+               \r
+               public BitArray(int size)\r
+               {\r
+                       if (size < 1)\r
+                       {\r
+                               throw new System.ArgumentException("size must be at least 1");\r
+                       }\r
+                       this.size = size;\r
+                       this.bits = makeArray(size);\r
+               }\r
+               \r
+               /// <param name="i">bit to get\r
+               /// </param>\r
+               /// <returns> true iff bit i is set\r
+               /// </returns>\r
+               public bool get_Renamed(int i)\r
+               {\r
+                       return (bits[i >> 5] & (1 << (i & 0x1F))) != 0;\r
+               }\r
+               \r
+               /// <summary> Sets bit i.\r
+               /// \r
+               /// </summary>\r
+               /// <param name="i">bit to set\r
+               /// </param>\r
+               public void  set_Renamed(int i)\r
+               {\r
+                       bits[i >> 5] |= 1 << (i & 0x1F);\r
+               }\r
+               \r
+               /// <summary> Flips bit i.\r
+               /// \r
+               /// </summary>\r
+               /// <param name="i">bit to set\r
+               /// </param>\r
+               public void  flip(int i)\r
+               {\r
+                       bits[i >> 5] ^= 1 << (i & 0x1F);\r
+               }\r
+               \r
+               /// <summary> Sets a block of 32 bits, starting at bit i.\r
+               /// \r
+               /// </summary>\r
+               /// <param name="i">first bit to set\r
+               /// </param>\r
+               /// <param name="newBits">the new value of the next 32 bits. Note again that the least-significant bit\r
+               /// corresponds to bit i, the next-least-significant to i+1, and so on.\r
+               /// </param>\r
+               public void  setBulk(int i, int newBits)\r
+               {\r
+                       bits[i >> 5] = newBits;\r
+               }\r
+               \r
+               /// <summary> Clears all bits (sets to false).</summary>\r
+               public void  clear()\r
+               {\r
+                       int max = bits.Length;\r
+                       for (int i = 0; i < max; i++)\r
+                       {\r
+                               bits[i] = 0;\r
+                       }\r
+               }\r
+               \r
+               /// <summary> Efficient method to check if a range of bits is set, or not set.\r
+               /// \r
+               /// </summary>\r
+               /// <param name="start">start of range, inclusive.\r
+               /// </param>\r
+               /// <param name="end">end of range, exclusive\r
+               /// </param>\r
+               /// <param name="value">if true, checks that bits in range are set, otherwise checks that they are not set\r
+               /// </param>\r
+               /// <returns> true iff all bits are set or not set in range, according to value argument\r
+               /// </returns>\r
+               /// <throws>  IllegalArgumentException if end is less than or equal to start </throws>\r
+               public bool isRange(int start, int end, bool value_Renamed)\r
+               {\r
+                       if (end < start)\r
+                       {\r
+                               throw new System.ArgumentException();\r
+                       }\r
+                       if (end == start)\r
+                       {\r
+                               return true; // empty range matches\r
+                       }\r
+                       end--; // will be easier to treat this as the last actually set bit -- inclusive    \r
+                       int firstInt = start >> 5;\r
+                       int lastInt = end >> 5;\r
+                       for (int i = firstInt; i <= lastInt; i++)\r
+                       {\r
+                               int firstBit = i > firstInt?0:start & 0x1F;\r
+                               int lastBit = i < lastInt?31:end & 0x1F;\r
+                               int mask;\r
+                               if (firstBit == 0 && lastBit == 31)\r
+                               {\r
+                                       mask = - 1;\r
+                               }\r
+                               else\r
+                               {\r
+                                       mask = 0;\r
+                                       for (int j = firstBit; j <= lastBit; j++)\r
+                                       {\r
+                                               mask |= 1 << j;\r
+                                       }\r
+                               }\r
+                               \r
+                               // Return false if we're looking for 1s and the masked bits[i] isn't all 1s (that is,\r
+                               // equals the mask, or we're looking for 0s and the masked portion is not all 0s\r
+                               if ((bits[i] & mask) != (value_Renamed?mask:0))\r
+                               {\r
+                                       return false;\r
+                               }\r
+                       }\r
+                       return true;\r
+               }\r
+               \r
+               /// <returns> underlying array of ints. The first element holds the first 32 bits, and the least\r
+               /// significant bit is bit 0.\r
+               /// </returns>\r
+               public int[] getBitArray()\r
+               {\r
+                       return bits;\r
+               }\r
+               \r
+               /// <summary> Reverses all bits in the array.</summary>\r
+               public void  reverse()\r
+               {\r
+                       int[] newBits = new int[bits.Length];\r
+                       int size = this.size;\r
+                       for (int i = 0; i < size; i++)\r
+                       {\r
+                               if (get_Renamed(size - i - 1))\r
+                               {\r
+                                       newBits[i >> 5] |= 1 << (i & 0x1F);\r
+                               }\r
+                       }\r
+                       bits = newBits;\r
+               }\r
+               \r
+               private static int[] makeArray(int size)\r
+               {\r
+                       int arraySize = size >> 5;\r
+                       if ((size & 0x1F) != 0)\r
+                       {\r
+                               arraySize++;\r
+                       }\r
+                       return new int[arraySize];\r
+               }\r
+               \r
+               public override System.String ToString()\r
+               {\r
+                       System.Text.StringBuilder result = new System.Text.StringBuilder(size);\r
+                       for (int i = 0; i < size; i++)\r
+                       {\r
+                               if ((i & 0x07) == 0)\r
+                               {\r
+                                       result.Append(' ');\r
+                               }\r
+                               result.Append(get_Renamed(i)?'X':'.');\r
+                       }\r
+                       return result.ToString();\r
+               }\r
+       }\r
+}
\ No newline at end of file