source: icXML/icXML-devel/src/xercesc/util/BitSet.cpp @ 2732

Last change on this file since 2732 was 2722, checked in by cameron, 7 years ago

Original Xerces files with import mods for icxercesc

File size: 7.4 KB
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/*
19 * $Id: BitSet.cpp 676911 2008-07-15 13:27:32Z amassari $
20 */
21
22
23// ---------------------------------------------------------------------------
24//  Includes
25// ---------------------------------------------------------------------------
26#include <xercesc/util/BitSet.hpp>
27#include <xercesc/framework/MemoryManager.hpp>
28
29XERCES_CPP_NAMESPACE_BEGIN
30
31// ---------------------------------------------------------------------------
32//  Local const data
33//
34//  kBitsPerUnit
35//      The number of bits in each of our allocation units, which is a 32
36//      bit value in this case.
37//
38//  kGrowBy
39//      The minimum allocation units to grow the buffer by.
40// ---------------------------------------------------------------------------
41const XMLSize_t kBitsPerUnit    = 32;
42const XMLSize_t kGrowBy         = 1;
43
44
45
46// ---------------------------------------------------------------------------
47//  BitSet: Constructors and Destructor
48// ---------------------------------------------------------------------------
49BitSet::BitSet( const XMLSize_t size
50              , MemoryManager* const manager) :
51
52    fMemoryManager(manager)
53    , fBits(0)
54    , fUnitLen(0)
55{
56    ensureCapacity(size);
57}
58
59BitSet::BitSet(const BitSet& toCopy) :
60    XMemory(toCopy)
61    , fMemoryManager(toCopy.fMemoryManager)
62    , fBits(0)
63    , fUnitLen(toCopy.fUnitLen)
64{
65    fBits = (unsigned long*) fMemoryManager->allocate
66    (
67        fUnitLen * sizeof(unsigned long)
68    ); //new unsigned long[fUnitLen];
69    for (XMLSize_t i = 0; i < fUnitLen; i++)
70        fBits[i] = toCopy.fBits[i];
71}
72
73BitSet::~BitSet()
74{
75    fMemoryManager->deallocate(fBits); //delete [] fBits;
76}
77
78
79// ---------------------------------------------------------------------------
80//  BitSet: Equality methods
81// ---------------------------------------------------------------------------
82bool BitSet::equals(const BitSet& other) const
83{
84    if (this == &other)
85        return true;
86
87    if (fUnitLen != other.fUnitLen)
88        return false;
89
90    for (XMLSize_t i = 0; i < fUnitLen; i++)
91    {
92        if (fBits[i] != other.fBits[i])
93            return false;
94    }
95    return true;
96}
97
98
99// ---------------------------------------------------------------------------
100//  BitSet: Getter methods
101// ---------------------------------------------------------------------------
102bool BitSet::get(const XMLSize_t index) const
103{
104    const XMLSize_t unitOfBit = (index / kBitsPerUnit);
105    const XMLSize_t bitWithinUnit = index % kBitsPerUnit;
106
107    //
108    //  If the index is beyond our size, don't actually expand. Just return
109    //  false, which is what the state would be if we did expand it.
110    //
111    bool retVal = false;
112    if (unitOfBit <= fUnitLen)
113    {
114        if (fBits[unitOfBit] & (1 << bitWithinUnit))
115            retVal = true;
116    }
117    return retVal;
118}
119
120
121XMLSize_t BitSet::size() const
122{
123    return fUnitLen * kBitsPerUnit;
124}
125
126
127
128// ---------------------------------------------------------------------------
129//  BitSet: Setter methods
130// ---------------------------------------------------------------------------
131bool BitSet::allAreCleared() const
132{
133    for (XMLSize_t index = 0; index < fUnitLen; index++)
134    {
135        if (fBits[index])
136            return false;
137    }
138    return true;
139}
140
141bool BitSet::allAreSet() const
142{
143    for (XMLSize_t index = 0; index < fUnitLen; index++)
144    {
145        if (fBits[index] != 0xFFFFFFFF)
146            return false;
147    }
148    return true;
149}
150
151void BitSet::clearAll()
152{
153    // Just zero out all the units
154    for (XMLSize_t index = 0; index < fUnitLen; index++)
155        fBits[index] = 0;
156}
157
158void BitSet::clear(const XMLSize_t index)
159{
160    ensureCapacity(index+1);
161
162    const XMLSize_t unitOfBit = (index / kBitsPerUnit);
163    const XMLSize_t bitWithinUnit = index % kBitsPerUnit;
164
165    fBits[unitOfBit] &= ~(1UL << bitWithinUnit);
166}
167
168
169void BitSet::set(const XMLSize_t index)
170{
171    ensureCapacity(index+1);
172
173    const XMLSize_t unitOfBit = (index / kBitsPerUnit);
174    const XMLSize_t bitWithinUnit = index % kBitsPerUnit;
175
176    fBits[unitOfBit] |= (1UL << bitWithinUnit);
177}
178
179
180
181// ---------------------------------------------------------------------------
182//  BitSet: Bitwise logical methods
183// ---------------------------------------------------------------------------
184void BitSet::andWith(const BitSet& other)
185{
186    if (fUnitLen < other.fUnitLen)
187        ensureCapacity(other.fUnitLen * kBitsPerUnit);
188
189    for (XMLSize_t index = 0; index < other.fUnitLen; index++)
190        fBits[index] &= other.fBits[index];
191}
192
193
194void BitSet::orWith(const BitSet& other)
195{
196    if (fUnitLen < other.fUnitLen)
197        ensureCapacity(other.fUnitLen * kBitsPerUnit);
198
199    for (XMLSize_t index = 0; index < other.fUnitLen; index++)
200        fBits[index] |= other.fBits[index];
201}
202
203
204void BitSet::xorWith(const BitSet& other)
205{
206    if (fUnitLen < other.fUnitLen)
207        ensureCapacity(other.fUnitLen * kBitsPerUnit);
208
209    for (XMLSize_t index = 0; index < other.fUnitLen; index++)
210        fBits[index] ^= other.fBits[index];
211}
212
213
214
215// ---------------------------------------------------------------------------
216//  BitSet: Miscellaneous methods
217// ---------------------------------------------------------------------------
218XMLSize_t BitSet::hash(const XMLSize_t hashModulus) const
219{
220    const unsigned char* pBytes = (const unsigned char*)fBits;
221    const XMLSize_t len = fUnitLen * sizeof(unsigned long);
222
223    XMLSize_t hashVal = 0;
224    for (XMLSize_t index = 0; index < len; index++)
225    {
226        hashVal <<= 1;
227        hashVal ^= *pBytes;
228    }
229    return hashVal % hashModulus;
230}
231
232
233
234// ---------------------------------------------------------------------------
235//  BitSet: Private methods
236// ---------------------------------------------------------------------------
237void BitSet::ensureCapacity(const XMLSize_t size)
238{
239    // If we have enough space, do nothing
240    if(fUnitLen * kBitsPerUnit >= size)
241        return;
242
243    // Calculate the units required to hold the passed bit count.
244    XMLSize_t unitsNeeded = size / kBitsPerUnit;
245    if (size % kBitsPerUnit)
246        unitsNeeded++;
247
248    // Regrow the unit length by at least the expansion unit
249    if (unitsNeeded < (fUnitLen + kGrowBy))
250        unitsNeeded = fUnitLen + kGrowBy;
251
252    // Allocate the array, copy the old stuff, and zero the new stuff
253    unsigned long* newBits = (unsigned long*) fMemoryManager->allocate
254    (
255        unitsNeeded * sizeof(unsigned long)
256    ); //new unsigned long[unitsNeeded];
257
258    XMLSize_t index;
259    for (index = 0; index < fUnitLen; index++)
260        newBits[index] = fBits[index];
261
262    for (; index < unitsNeeded; index++)
263        newBits[index] = 0;
264
265    fMemoryManager->deallocate(fBits); //delete [] fBits;
266    fBits = newBits;
267    fUnitLen = unitsNeeded;
268}
269
270XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.