-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitPool.cpp
More file actions
165 lines (136 loc) · 3.41 KB
/
BitPool.cpp
File metadata and controls
165 lines (136 loc) · 3.41 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2017 Sathyanesh Krishnan
// Licensed under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
#include "BitPool.h"
///////////////////////// public /////////////////
BitPool::BitPool(const unsigned MaxElements)
{
unsigned i=0;
//Zero based index, four bytes storage can accommodate bit 0 to bit 31.
unsigned ArraySize = FindMaskArrayIndex(MaxElements) + 1;
mMaxElements = (ArraySize * mArrayElementBitSizei) - 1; //Zero based index
mpBitMaskArray = new unsigned[ArraySize];
for (i = 0; i < ArraySize; ++i)
{
*(mpBitMaskArray + i) = 0;
}
}
BitPool::~BitPool()
{
delete mpBitMaskArray;
mpBitMaskArray = NULL;
}
///////////////////////// protected /////////////////
char BitPool::GetFlag(unsigned BitIndex)
{
char flag = 0;
if (BitIndex > mMaxElements)
{
// Error
flag = -1;
}
else
{
// The array element the bit belongs
unsigned ArrayIndex = FindMaskArrayIndex(BitIndex);
// Offset of the bit withing this array element.
unsigned BitOffset = BitIndex % mArrayElementBitSizei;
unsigned Mask = 1 << BitOffset;
// lock
{
unsigned *pElement = (mpBitMaskArray + ArrayIndex);
if (*pElement & Mask)
{
flag = 1;
}
}
// UnLock
}
return(flag);
}
///////////////////////// protected /////////////////
char BitPool::SetFlag(unsigned BitIndex, unsigned char flag)
{
if (BitIndex > mMaxElements)
{
return(-1);
}
else
{
// The array element the bit belongs
unsigned ArrayIndex = FindMaskArrayIndex(BitIndex);
// Offset of the bit withing this array element.
unsigned BitOffset = BitIndex % mArrayElementBitSizei;
unsigned Mask = 1 << BitOffset;
// lock
{
unsigned *pElement = (mpBitMaskArray + ArrayIndex);
if (flag)
{
// Set the flag
*pElement = *pElement | Mask;
}
else
{
// Clear the flag
*pElement = *pElement & (~Mask);
}
}
// UnLock
}
return(0);
}
///////////////////////// private /////////////////
unsigned BitPool::FindMaskArrayIndex(unsigned BitIndex)
{
//Zero based index, four bytes storage can accommodate bit 0 to bit 31.
//return(BitMaskArraySize(BitIndex) - 1);
unsigned Index = (BitIndex / mArrayElementBitSizei);
return(Index);
}
char *BitPool::TestModulePrintBits(const unsigned val)
{
static char Buff[sizeof(unsigned)* 8 + 1];
char *cp;
unsigned Mask = 1;
unsigned i = 0;
for (i = 0; i < sizeof(Buff); ++i)
{
Buff[i] = 0;
}
cp = Buff;
for (i = 0; i < sizeof(unsigned)* 8; ++i)
{
*cp = '0';
unsigned tmp = val & Mask;
if (tmp)
{
*cp = '1';
}
Mask = Mask << 1;
++cp;
}
*cp = 0;
return(Buff);
}
void BitPool::TestModulePrintBits2(const char *Buff)
{
const char *cp = Buff;
unsigned Mask = 1;
unsigned i = 0;
printf("\n");
for (i = 1; *cp; ++i)
{
printf("%c", *cp);
if (i % 8 == 0)
{
printf(", ");
}
else if (i % 4 == 0)
{
printf(" ");
}
++cp;
}
printf(" \n");
}