bitVectorW.h 3.6 KB

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