build fix
[LibreOffice.git] / sfx2 / source / bastyp / bitset.cxx
blobd03a7d22da9ce2fd855bc69902ec9f9b311ae6d7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <tools/debug.hxx>
21 #include <sal/log.hxx>
23 #include "bitset.hxx"
25 #include <string.h>
26 #include <limits.h>
28 // creates the asymmetric difference with another bitset
30 IndexBitSet& IndexBitSet::operator-=(sal_uInt16 nBit)
32 sal_uInt16 nBlock = nBit / 32;
33 sal_uInt32 nBitVal = 1L << (nBit % 32);
35 if ( nBlock >= nBlocks )
36 return *this;
38 if ( (*(pBitmap+nBlock) & nBitVal) )
40 *(pBitmap+nBlock) &= ~nBitVal;
41 --nCount;
44 return *this;
47 // unites with a single bit
49 IndexBitSet& IndexBitSet::operator|=( sal_uInt16 nBit )
51 sal_uInt16 nBlock = nBit / 32;
52 sal_uInt32 nBitVal = 1L << (nBit % 32);
54 if ( nBlock >= nBlocks )
56 sal_uInt32 *pNewMap = new sal_uInt32[nBlock+1];
57 memset( pNewMap + nBlocks, 0, 4 * (nBlock - nBlocks + 1) );
59 if ( pBitmap )
61 memcpy( pNewMap, pBitmap, 4 * nBlocks );
62 delete [] pBitmap;
64 pBitmap = pNewMap;
65 nBlocks = nBlock+1;
68 if ( (*(pBitmap+nBlock) & nBitVal) == 0 )
70 *(pBitmap+nBlock) |= nBitVal;
71 ++nCount;
74 return *this;
78 // determines if the bit is set (may be the only one)
80 bool IndexBitSet::Contains( sal_uInt16 nBit ) const
82 sal_uInt16 nBlock = nBit / 32;
83 sal_uInt32 nBitVal = 1L << (nBit % 32);
85 if ( nBlock >= nBlocks )
86 return false;
87 return ( nBitVal & *(pBitmap+nBlock) ) == nBitVal;
90 IndexBitSet::IndexBitSet()
92 nCount = 0;
93 nBlocks = 0;
94 pBitmap = nullptr;
97 IndexBitSet::~IndexBitSet()
99 delete [] pBitmap;
102 sal_uInt16 IndexBitSet::GetFreeIndex()
104 for(int i=0;i<USHRT_MAX;i++)
105 if(!Contains(i))
107 *this|=i;
108 return i;
110 SAL_WARN( "sfx", "IndexBitSet enthaelt mehr als USHRT_MAX Eintraege");
111 return 0;
115 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */