Committed C# port from Mohamad
[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..84a8fe4
--- /dev/null
@@ -0,0 +1,178 @@
+/*\r
+* Copyright 2008 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
+namespace com.google.zxing.common\r
+{\r
+    using System;\r
+    using System.Text;\r
+\r
+    /// <summary> A class which wraps a 2D array of bytes. The default usage is signed. If you want to use it as a\r
+    /// unsigned container, it's up to you to do byteValue & 0xff at each location.\r
+    /// *\r
+    /// JAVAPORT: I'm not happy about the argument ordering throughout the file, as I always like to have\r
+    /// the horizontal component first, but this is for compatibility with the C++ code. The original\r
+    /// code was a 2D array of ints, but since it only ever gets assigned -1, 0, and 1, I'm going to use\r
+    /// less memory and go with bytes.\r
+    /// *\r
+    /// </summary>\r
+    /// <author>  dswitkin@google.com (Daniel Switkin)\r
+    /// \r
+    /// </author>\r
+    public sealed class BitArray\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
+      public int Size;\r
+\r
+      public BitArray(int size) {\r
+        if (size < 1) {\r
+          throw new Exception("size must be at least 1");\r
+        }\r
+        this.Size = size;\r
+        this.bits = makeArray(size);\r
+      }\r
+\r
+      public int getSize() {\r
+        return Size;\r
+      }\r
+\r
+      /**\r
+       * @param i bit to get\r
+       * @return true iff bit i is set\r
+       */\r
+      public bool get(int i) {\r
+        return (bits[i >> 5] & (1 << (i & 0x1F))) != 0;\r
+      }\r
+\r
+      /**\r
+       * Sets bit i.\r
+       *\r
+       * @param i bit to set\r
+       */\r
+      public void set(int i) {\r
+        bits[i >> 5] |= 1 << (i & 0x1F);\r
+      }\r
+\r
+      /**\r
+       * Sets a block of 32 bits, starting at bit i.\r
+       *\r
+       * @param i first bit to set\r
+       * @param 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
+       */\r
+      public void setBulk(int i, int newBits) {\r
+        bits[i >> 5] = newBits;\r
+      }\r
+\r
+      /**\r
+       * Clears all bits (sets to false).\r
+       */\r
+      public void clear() {\r
+        int max = bits.Length;\r
+        for (int i = 0; i < max; i++) {\r
+          bits[i] = 0;\r
+        }\r
+      }\r
+\r
+      /**\r
+       * Efficient method to check if a range of bits is set, or not set.\r
+       *\r
+       * @param start start of range, inclusive.\r
+       * @param end end of range, exclusive\r
+       * @param value if true, checks that bits in range are set, otherwise checks that they are not set\r
+       * @return true iff all bits are set or not set in range, according to value argument\r
+       * @throws IllegalArgumentException if end is less than or equal to start\r
+       */\r
+      public bool isRange(int start, int end, bool value) {\r
+        if (end < start) {\r
+          throw new Exception();\r
+        }\r
+        if (end == start) {\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
+          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
+            mask = -1;\r
+          } else {\r
+            mask = 0;\r
+            for (int j = firstBit; j <= lastBit; j++) {\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 ? mask : 0)) {\r
+            return false;\r
+          }\r
+        }\r
+        return true;\r
+      }\r
+\r
+      /**\r
+       * @return underlying array of ints. The first element holds the first 32 bits, and the least\r
+       *         significant bit is bit 0.\r
+       */\r
+      public int[] getBitArray() {\r
+        return bits;\r
+      }\r
+\r
+      /**\r
+       * Reverses all bits in the array.\r
+       */\r
+      public void reverse() {\r
+        int[] newBits = makeArray(Size);\r
+        int max = newBits.Length;\r
+        for (int i = 0; i < max; i++) {\r
+          newBits[i] = 0;\r
+        }\r
+        int size = this.Size;\r
+        for (int i = 0; i < size; i++) {\r
+          if (get(size - i - 1)) {\r
+            newBits[i >> 5] |= 1 << (i & 0x1F);\r
+          }\r
+        }\r
+        bits = newBits;\r
+      }\r
+\r
+      private static int[] makeArray(int size) {\r
+        int arraySize = size >> 5;\r
+        if ((size & 0x1F) != 0) {\r
+          arraySize++;\r
+        }\r
+        return new int[arraySize];\r
+      }\r
+      \r
+      public String toString() {\r
+        StringBuilder  result = new StringBuilder(Size);\r
+        for (int i = 0; i < Size; i++) {\r
+          if (i % 8 == 0) {\r
+            result.Append(' ');\r
+          }\r
+          result.Append(get(i) ? 'X' : '.');\r
+        }\r
+        return result.ToString();\r
+      }\r
+        }\r
+}
\ No newline at end of file