Prettify scan result screen, I think
[zxing.git] / csharp / common / BitArray.cs
1 /*\r
2 * Copyright 2007 ZXing authors\r
3 *\r
4 * Licensed under the Apache License, Version 2.0 (the "License");\r
5 * you may not use this file except in compliance with the License.\r
6 * You may obtain a copy of the License at\r
7 *\r
8 *      http://www.apache.org/licenses/LICENSE-2.0\r
9 *\r
10 * Unless required by applicable law or agreed to in writing, software\r
11 * distributed under the License is distributed on an "AS IS" BASIS,\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13 * See the License for the specific language governing permissions and\r
14 * limitations under the License.\r
15 */\r
16 using System;\r
17 namespace com.google.zxing.common\r
18 {\r
19         \r
20         /// <summary> <p>A simple, fast array of bits, represented compactly by an array of ints internally.</p>\r
21         /// \r
22         /// </summary>\r
23         /// <author>  Sean Owen\r
24         /// </author>\r
25         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
26         /// </author>\r
27         public sealed class BitArray\r
28         {\r
29                 public int Size\r
30                 {\r
31                         get\r
32                         {\r
33                                 return size;\r
34                         }\r
35                         \r
36                 }\r
37                 \r
38                 // TODO: I have changed these members to be public so ProGuard can inline get() and set(). Ideally\r
39                 // they'd be private and we'd use the -allowaccessmodification flag, but Dalvik rejects the\r
40                 // resulting binary at runtime on Android. If we find a solution to this, these should be changed\r
41                 // back to private.\r
42                 public int[] bits;\r
43                 //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
44                 public int size;\r
45                 \r
46                 public BitArray(int size)\r
47                 {\r
48                         if (size < 1)\r
49                         {\r
50                                 throw new System.ArgumentException("size must be at least 1");\r
51                         }\r
52                         this.size = size;\r
53                         this.bits = makeArray(size);\r
54                 }\r
55                 \r
56                 /// <param name="i">bit to get\r
57                 /// </param>\r
58                 /// <returns> true iff bit i is set\r
59                 /// </returns>\r
60                 public bool get_Renamed(int i)\r
61                 {\r
62                         return (bits[i >> 5] & (1 << (i & 0x1F))) != 0;\r
63                 }\r
64                 \r
65                 /// <summary> Sets bit i.\r
66                 /// \r
67                 /// </summary>\r
68                 /// <param name="i">bit to set\r
69                 /// </param>\r
70                 public void  set_Renamed(int i)\r
71                 {\r
72                         bits[i >> 5] |= 1 << (i & 0x1F);\r
73                 }\r
74                 \r
75                 /// <summary> Flips bit i.\r
76                 /// \r
77                 /// </summary>\r
78                 /// <param name="i">bit to set\r
79                 /// </param>\r
80                 public void  flip(int i)\r
81                 {\r
82                         bits[i >> 5] ^= 1 << (i & 0x1F);\r
83                 }\r
84                 \r
85                 /// <summary> Sets a block of 32 bits, starting at bit i.\r
86                 /// \r
87                 /// </summary>\r
88                 /// <param name="i">first bit to set\r
89                 /// </param>\r
90                 /// <param name="newBits">the new value of the next 32 bits. Note again that the least-significant bit\r
91                 /// corresponds to bit i, the next-least-significant to i+1, and so on.\r
92                 /// </param>\r
93                 public void  setBulk(int i, int newBits)\r
94                 {\r
95                         bits[i >> 5] = newBits;\r
96                 }\r
97                 \r
98                 /// <summary> Clears all bits (sets to false).</summary>\r
99                 public void  clear()\r
100                 {\r
101                         int max = bits.Length;\r
102                         for (int i = 0; i < max; i++)\r
103                         {\r
104                                 bits[i] = 0;\r
105                         }\r
106                 }\r
107                 \r
108                 /// <summary> Efficient method to check if a range of bits is set, or not set.\r
109                 /// \r
110                 /// </summary>\r
111                 /// <param name="start">start of range, inclusive.\r
112                 /// </param>\r
113                 /// <param name="end">end of range, exclusive\r
114                 /// </param>\r
115                 /// <param name="value">if true, checks that bits in range are set, otherwise checks that they are not set\r
116                 /// </param>\r
117                 /// <returns> true iff all bits are set or not set in range, according to value argument\r
118                 /// </returns>\r
119                 /// <throws>  IllegalArgumentException if end is less than or equal to start </throws>\r
120                 public bool isRange(int start, int end, bool value_Renamed)\r
121                 {\r
122                         if (end < start)\r
123                         {\r
124                                 throw new System.ArgumentException();\r
125                         }\r
126                         if (end == start)\r
127                         {\r
128                                 return true; // empty range matches\r
129                         }\r
130                         end--; // will be easier to treat this as the last actually set bit -- inclusive    \r
131                         int firstInt = start >> 5;\r
132                         int lastInt = end >> 5;\r
133                         for (int i = firstInt; i <= lastInt; i++)\r
134                         {\r
135                                 int firstBit = i > firstInt?0:start & 0x1F;\r
136                                 int lastBit = i < lastInt?31:end & 0x1F;\r
137                                 int mask;\r
138                                 if (firstBit == 0 && lastBit == 31)\r
139                                 {\r
140                                         mask = - 1;\r
141                                 }\r
142                                 else\r
143                                 {\r
144                                         mask = 0;\r
145                                         for (int j = firstBit; j <= lastBit; j++)\r
146                                         {\r
147                                                 mask |= 1 << j;\r
148                                         }\r
149                                 }\r
150                                 \r
151                                 // Return false if we're looking for 1s and the masked bits[i] isn't all 1s (that is,\r
152                                 // equals the mask, or we're looking for 0s and the masked portion is not all 0s\r
153                                 if ((bits[i] & mask) != (value_Renamed?mask:0))\r
154                                 {\r
155                                         return false;\r
156                                 }\r
157                         }\r
158                         return true;\r
159                 }\r
160                 \r
161                 /// <returns> underlying array of ints. The first element holds the first 32 bits, and the least\r
162                 /// significant bit is bit 0.\r
163                 /// </returns>\r
164                 public int[] getBitArray()\r
165                 {\r
166                         return bits;\r
167                 }\r
168                 \r
169                 /// <summary> Reverses all bits in the array.</summary>\r
170                 public void  reverse()\r
171                 {\r
172                         int[] newBits = new int[bits.Length];\r
173                         int size = this.size;\r
174                         for (int i = 0; i < size; i++)\r
175                         {\r
176                                 if (get_Renamed(size - i - 1))\r
177                                 {\r
178                                         newBits[i >> 5] |= 1 << (i & 0x1F);\r
179                                 }\r
180                         }\r
181                         bits = newBits;\r
182                 }\r
183                 \r
184                 private static int[] makeArray(int size)\r
185                 {\r
186                         int arraySize = size >> 5;\r
187                         if ((size & 0x1F) != 0)\r
188                         {\r
189                                 arraySize++;\r
190                         }\r
191                         return new int[arraySize];\r
192                 }\r
193                 \r
194                 public override System.String ToString()\r
195                 {\r
196                         System.Text.StringBuilder result = new System.Text.StringBuilder(size);\r
197                         for (int i = 0; i < size; i++)\r
198                         {\r
199                                 if ((i & 0x07) == 0)\r
200                                 {\r
201                                         result.Append(' ');\r
202                                 }\r
203                                 result.Append(get_Renamed(i)?'X':'.');\r
204                         }\r
205                         return result.ToString();\r
206                 }\r
207         }\r
208 }