LibreOffice Module sfx2 (master) 1
bitset.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <sal/log.hxx>
21#include <sal/types.h>
22
23#include <bitset.hxx>
24
25#include <string.h>
26
27// creates the asymmetric difference with another bitset
28
30{
31 sal_uInt16 nBlock = nBit / 32;
32 sal_uInt32 nBitVal = 1U << (nBit % 32);
33
34 if ( nBlock >= nBlocks )
35 return *this;
36
37 if ( pBitmap[nBlock] & nBitVal )
38 {
39 pBitmap[nBlock] &= ~nBitVal;
40 }
41
42 return *this;
43}
44
45// unify with a single bit
46
48{
49 sal_uInt16 nBlock = nBit / 32;
50 sal_uInt32 nBitVal = 1U << (nBit % 32);
51
52 if ( nBlock >= nBlocks )
53 {
54 sal_uInt32 *pNewMap = new sal_uInt32[nBlock+1];
55 memset( pNewMap + nBlocks, 0, 4 * (nBlock - nBlocks + 1) );
56
57 if ( pBitmap )
58 {
59 memcpy( pNewMap, pBitmap.get(), 4 * nBlocks );
60 }
61 pBitmap.reset(pNewMap);
62 nBlocks = nBlock+1;
63 }
64
65 if ( (pBitmap[nBlock] & nBitVal) == 0 )
66 {
67 pBitmap[nBlock] |= nBitVal;
68 }
69
70 return *this;
71}
72
73
74// determines if the bit is set (may be the only one)
75
76bool IndexBitSet::Contains( sal_uInt16 nBit ) const
77{
78 sal_uInt16 nBlock = nBit / 32;
79 sal_uInt32 nBitVal = 1U << (nBit % 32);
80
81 if ( nBlock >= nBlocks )
82 return false;
83 return ( nBitVal & pBitmap[nBlock] ) == nBitVal;
84}
85
87{
88 nBlocks = 0;
89}
90
92{
93}
94
96{
97 for(sal_uInt16 i=0;i<SAL_MAX_UINT16;i++)
98 if(!Contains(i))
99 {
100 *this|=i;
101 return i;
102 }
103 SAL_WARN( "sfx", "IndexBitSet contains more than SAL_MAX_UINT16 entries");
104 return 0;
105}
106
107
108/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
IndexBitSet()
Definition: bitset.cxx:86
std::unique_ptr< sal_uInt32[]> pBitmap
Definition: bitset.hxx:30
sal_uInt16 GetFreeIndex()
Definition: bitset.cxx:95
~IndexBitSet()
Definition: bitset.cxx:91
IndexBitSet & operator-=(sal_uInt16 nBit)
Definition: bitset.cxx:29
bool Contains(sal_uInt16 nBit) const
Definition: bitset.cxx:76
sal_uInt16 nBlocks
Definition: bitset.hxx:29
IndexBitSet & operator|=(sal_uInt16 nBit)
Definition: bitset.cxx:47
#define SAL_WARN(area, stream)
sal_Int16 nBit
int i
#define SAL_MAX_UINT16