hxbitset.cpp
上传用户:zhongxx05
上传日期:2007-06-06
资源大小:33641k
文件大小:6k
- /* ***** BEGIN LICENSE BLOCK *****
- * Version: RCSL 1.0/RPSL 1.0
- *
- * Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved.
- *
- * The contents of this file, and the files included with this file, are
- * subject to the current version of the RealNetworks Public Source License
- * Version 1.0 (the "RPSL") available at
- * http://www.helixcommunity.org/content/rpsl unless you have licensed
- * the file under the RealNetworks Community Source License Version 1.0
- * (the "RCSL") available at http://www.helixcommunity.org/content/rcsl,
- * in which case the RCSL will apply. You may also obtain the license terms
- * directly from RealNetworks. You may not use this file except in
- * compliance with the RPSL or, if you have a valid RCSL with RealNetworks
- * applicable to this file, the RCSL. Please see the applicable RPSL or
- * RCSL for the rights, obligations and limitations governing use of the
- * contents of the file.
- *
- * This file is part of the Helix DNA Technology. RealNetworks is the
- * developer of the Original Code and owns the copyrights in the portions
- * it created.
- *
- * This file, and the files included with this file, is distributed and made
- * available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
- * EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES,
- * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
- * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
- *
- * Technology Compatibility Kit Test Suite(s) Location:
- * http://www.helixcommunity.org/content/tck
- *
- * Contributor(s):
- *
- * ***** END LICENSE BLOCK ***** */
- #include "hlxclib/string.h"
- #include "debug.h"
- #include "hxassert.h"
- #include "hxtypes.h"
- #include "hxbitset.h"
- #include "hxheap.h"
- #ifdef _DEBUG
- #undef HX_THIS_FILE
- static const char HX_THIS_FILE[] = __FILE__;
- #endif
- CHXBitset::CHXBitset():
- m_nBitsetSize(_BS_SHORT_LEN),
- m_pBitset(m_pShortBitset)
- {
- memset(m_pShortBitset, 0, HX_SAFESIZE_T(sizeof(_BS_word) * _BS_SHORT_LEN));
- }
- CHXBitset::CHXBitset(INT32 maxBit):
- m_nBitsetSize(0),
- m_pBitset(m_pShortBitset)
- {
- HX_ASSERT(maxBit > 0);
- setBitsetSize(_BS_WORDS_NEEDED(maxBit));
- clear();
- }
- CHXBitset::CHXBitset(BYTE* pBitmap, INT32 nCount):
- m_nBitsetSize(0),
- m_pBitset(m_pShortBitset)
- {
- if (nCount <= 0)
- {
- return;
- }
- setBitsetSize((nCount / 4) + 1); //setBitsetSize also clears the bitset
- INT32 i, j;
- for(i=0, j=0; i<m_nBitsetSize; ++i, j+=4)
- {
- if(nCount - j > 3)
- {
- m_pBitset[i] = pBitmap[j+3] _BS_LEFT 24 |
- pBitmap[j+2] _BS_LEFT 16 |
- pBitmap[j+1] _BS_LEFT 8 |
- pBitmap[j];
- }
- else if(nCount - j > 2)
- {
- m_pBitset[i] = pBitmap[j+2] _BS_LEFT 24 |
- pBitmap[j+1] _BS_LEFT 16 |
- pBitmap[j] _BS_LEFT 8;
- }
- else if(nCount - j > 1)
- {
- m_pBitset[i] = pBitmap[j+1] _BS_LEFT 24 |
- pBitmap[j] _BS_LEFT 16;
- }
- else if(nCount - j > 0)
- {
- m_pBitset[i] = pBitmap[j] _BS_LEFT 24;
- }
- }
- }
- CHXBitset::~CHXBitset()
- {
- if (m_pBitset != m_pShortBitset)
- {
- delete[] m_pBitset;
- }
- }
- void
- CHXBitset::set(INT32 pos)
- {
- HX_ASSERT(pos >= 0);
- setBitsetSize(_BS_WORDS_NEEDED(pos+1));
- INT32 idx = _BS_INDEX(pos);
- m_pBitset[idx] |= _BS_BITMASK(_BS_POS(pos));
- }
- void
- CHXBitset::set(INT32 from, INT32 to)
- {
- HX_ASSERT(from >= 0);
- for(INT32 i=from; i<to; ++i)
- {
- set(i);
- }
- }
- void
- CHXBitset::set()
- {
- memset(m_pBitset, 0xff, HX_SAFESIZE_T(sizeof(_BS_word)*(m_nBitsetSize)));
- }
- void
- CHXBitset::clear(INT32 pos)
- {
- HX_ASSERT(pos >= 0);
- INT32 idx = _BS_INDEX(pos);
- HX_ASSERT(idx < m_nBitsetSize);
- m_pBitset[idx] &= ~(_BS_BITMASK(_BS_POS(pos)));
- }
- void
- CHXBitset::clear(INT32 from, INT32 to)
- {
- HX_ASSERT(from >= 0);
- for(INT32 i=from; i<to; ++i)
- {
- clear(i);
- }
- }
- void
- CHXBitset::clear()
- {
- if (m_nBitsetSize == 0)
- return;
- memset(m_pBitset, 0, HX_SAFESIZE_T(sizeof(_BS_word)*m_nBitsetSize));
- }
- BOOL
- CHXBitset::test(INT32 pos)
- {
- HX_ASSERT(pos >= 0);
- INT32 idx = _BS_INDEX(pos);
- HX_ASSERT(idx < m_nBitsetSize);
- if (!(idx < m_nBitsetSize))
- {
- // changed %d to %ld for long int (WIN16 condsideration)
- // as all values are INT32
- DPRINTF(D_INFO, ("test failure %ld %ld %ldn", pos, idx, m_nBitsetSize));
- }
- return (m_pBitset[idx] & _BS_BITMASK(_BS_POS(pos))) != 0;
- }
- INT32
- CHXBitset::toByteArray(BYTE** pBitmap)
- {
- INT32 nCount = m_nBitsetSize * 4;
- if (nCount == 0)
- {
- return 0;
- }
- *pBitmap = new BYTE[nCount];
- INT32 i,j;
- for(i=0, j=0; i<m_nBitsetSize; ++i, j+=4)
- {
- (*pBitmap)[j+3] = (BYTE)((m_pBitset[i] _BS_RIGHT 24) & 0xffff);
- (*pBitmap)[j+2] = (BYTE)((m_pBitset[i] _BS_RIGHT 16) & 0xffff);
- (*pBitmap)[j+1] = (BYTE)((m_pBitset[i] _BS_RIGHT 8) & 0xffff);
- (*pBitmap)[j] = (BYTE)(m_pBitset[i] & 0xffff);
- }
- return nCount;
- }
- BOOL
- CHXBitset::test(INT32 from, INT32 to)
- {
- HX_ASSERT(from >= 0);
- for(INT32 i=from; i<to; ++i)
- {
- if (!test(i))
- return(FALSE);
- }
- return(TRUE);
- }
- void
- CHXBitset::growsize(INT32 maxBit)
- {
- setBitsetSize(_BS_WORDS_NEEDED(maxBit+1));
- }
- void
- CHXBitset::setBitsetSize(INT32 nBitsetSize)
- {
- if (nBitsetSize > m_nBitsetSize)
- {
- if (nBitsetSize > _BS_SHORT_LEN)
- {
- _BS_word* pTempBitset = new _BS_word[nBitsetSize];
- memcpy(pTempBitset, m_pBitset, m_nBitsetSize); /* Flawfinder: ignore */
- memset(&(pTempBitset[m_nBitsetSize]), 0,
- HX_SAFESIZE_T(sizeof(_BS_word) * (nBitsetSize - m_nBitsetSize)));
- if (m_pBitset != m_pShortBitset)
- {
- delete[] m_pBitset;
- }
- m_pBitset = pTempBitset;
- m_nBitsetSize = nBitsetSize;
- }
- else
- {
- if (m_nBitsetSize == 0)
- {
- memset(m_pShortBitset, 0, HX_SAFESIZE_T(sizeof(_BS_word) * _BS_SHORT_LEN));
- }
- m_nBitsetSize = nBitsetSize;
- }
- }
- }