-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBitMap.cs
More file actions
149 lines (136 loc) · 3.65 KB
/
BitMap.cs
File metadata and controls
149 lines (136 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
using System;
#if NET461_OR_GREATER || NETSTANDARD2_0
using Grax32.Extensions;
#endif
public class BitMap
{
private static byte[] BIT_UTIL = new byte[] { 1, 2, 4, 8, 16, 32, 64, unchecked((byte)-128) };
private static byte[] UNMARK_BIT_UTIL =
new byte[] {
(byte) 0XFE, // 11111110
(byte) 0XFD, // 11111101
(byte) 0XFB, // 11111011
(byte) 0XF7, // 11110111
(byte) 0XEF, // 11101111
(byte) 0XDF, // 11011111
(byte) 0XBF, // 10111111
(byte) 0X7F // 01111111
};
private byte[] bits;
private int size;
/** Initialize a BitMap with given size. */
public BitMap(int size)
{
this.size = size;
bits = new byte[size / 8 + 1];
#if NET461_OR_GREATER || NETSTANDARD2_0
bits.Fill((byte)0);
#else
Array.Fill<byte>(bits, (byte)0);
#endif
}
/** Initialize a BitMap with given size and bytes. */
public BitMap(int size, byte[] bits)
{
this.size = size;
this.bits = bits;
}
public byte[] getByteArray()
{
return this.bits;
}
public int getSize()
{
return this.size;
}
/** returns the value of the bit with the specified index. */
public bool isMarked(int position)
{
return (bits[position / 8] & BIT_UTIL[position % 8]) != 0;
}
/** mark as 1 at all positions. */
public void markAll()
{
#if NET461_OR_GREATER || NETSTANDARD2_0
bits.Fill((byte)0xFF);
#else
Array.Fill(bits, (byte)0xFF);
#endif
}
/** mark as 1 at the given bit position. */
public void mark(int position)
{
bits[position / 8] |= BIT_UTIL[position % 8];
}
/** mark as 0 at all positions. */
public void reset()
{
#if NET461_OR_GREATER || NETSTANDARD2_0
bits.Fill((byte)0);
#else
Array.Fill(bits, (byte)0);
#endif
}
public void unmark(int position)
{
bits[position / 8] &= UNMARK_BIT_UTIL[position % 8];
}
/** whether all bits are zero, i.e., no Null value */
public bool isAllUnmarked()
{
int j;
for (j = 0; j < size / 8; j++)
{
if (bits[j] != (byte)0)
{
return false;
}
}
for (j = 0; j < size % 8; j++)
{
if ((bits[size / 8] & BIT_UTIL[j]) != 0)
{
return false;
}
}
return true;
}
/** whether all bits are one, i.e., all are Null */
public bool isAllMarked()
{
int j;
for (j = 0; j < size / 8; j++)
{
if (bits[j] != (byte)0XFF)
{
return false;
}
}
for (j = 0; j < size % 8; j++)
{
if ((bits[size / 8] & BIT_UTIL[j]) == 0)
{
return false;
}
}
return true;
}
}