bitVectorW.h

Engine/source/core/bitVectorW.h

More...

Classes:

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _BITVECTORW_H_
 25#define _BITVECTORW_H_
 26
 27#ifndef _PLATFORM_H_
 28#include "platform/platform.h"
 29#endif
 30
 31/// @see BitVector
 32class BitVectorW
 33{
 34      U32   mNumEntries;
 35      U32   mBitWidth;
 36      U32   mBitMask;
 37      U8 *  mDataPtr;
 38
 39   public:
 40      BitVectorW()   {mDataPtr=<a href="/coding/file/types_8lint_8h/#types_8lint_8h_1a070d2ce7b6bb7e5c05602aa8c308d0c4">NULL</a>; setDims(0,0);}
 41      ~BitVectorW()  {if(mDataPtr) delete [] mDataPtr;}
 42
 43      U32   bitWidth() const     {return mBitWidth;}
 44      U32   numEntries() const   {return mNumEntries;}
 45      U8 *  dataPtr() const      {return mDataPtr;}
 46
 47      U32   getU17(U32 idx) const;        // get and set for bit widths
 48      void  setU17(U32 idx, U32 val);     // of 17 or less
 49      U32   numBytes() const;
 50      void  setDims(U32 sz, U32 w);
 51};
 52
 53//-------------------------------------------------------------------------------------
 54
 55inline U32 BitVectorW::numBytes() const
 56{
 57   if (mNumEntries > 0)
 58      return (mBitWidth * mNumEntries) + 32 >> 3;
 59   else
 60      return 0;
 61}
 62
 63// Alloc the data - note it does work for a bit width of zero (lookups return zero)
 64inline void BitVectorW::setDims(U32 size, U32 width)
 65{
 66   if (mDataPtr)
 67      delete [] mDataPtr;
 68
 69   if (size > 0 && width <= 17)
 70   {
 71      mBitWidth = width;
 72      mNumEntries = size;
 73      mBitMask = (1 << width) - 1;
 74      U32 dataSize = numBytes();
 75      mDataPtr = new U8 [dataSize];
 76      dMemset(mDataPtr, 0, dataSize);
 77   }
 78   else
 79   {
 80      mDataPtr = NULL;
 81      mBitWidth = mBitMask = mNumEntries = 0;
 82   }
 83}
 84
 85//-------------------------------------------------------------------------------------
 86// For coding ease, the get and set methods might read or write an extra byte or two.
 87// If more or less max bit width is ever needed, add or remove the x[] expressions.
 88
 89inline U32 BitVectorW::getU17(U32 i) const
 90{
 91   if (mDataPtr) {
 92      register U8 *  x = &mDataPtr[(i *= mBitWidth) >> 3];
 93      return (U32(*x) + (U32(x[1])<<8) + (U32(x[2])<<16) >> (i&7)) & mBitMask;
 94   }
 95   return 0;
 96}
 97
 98inline void BitVectorW::setU17(U32 i, U32 value)
 99{
100   if (mDataPtr) {
101      register U8 *  x = &mDataPtr[(i *= mBitWidth) >> 3];
102      register U32   mask = mBitMask << (i &= 7);
103      x[0] = (x[0] & (~mask >>  0))  |  ((value <<= i) & (mask >>  0));
104      x[1] = (x[1] & (~mask >>  8))  |  ((value >>  8) & (mask >>  8));
105      x[2] = (x[2] & (~mask >> 16))  |  ((value >> 16) & (mask >> 16));
106   }
107}
108
109#endif //_BITVECTORW_H_
110