/usr/include/crystalspace-2.0/csutil/bitarray.h is in libcrystalspace-dev 2.0+dfsg-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 | /*
Copyright (C) 2000 by Andrew Kirmse
2008 by Marten Svanfeldt
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
// A one-dimensional array of bits, similar to STL bitset.
//
// Copyright 2000 Andrew Kirmse. All rights reserved.
//
// Permission is granted to use this code for any purpose, as long as this
// copyright message remains intact.
#ifndef __CS_BITARRAY_H__
#define __CS_BITARRAY_H__
/**\file
* A one-dimensional array of bits, similar to STL bitset.
*/
#include "csextern.h"
#include "csutil/allocator.h"
#include "csutil/bitops.h"
#include "csutil/comparator.h"
#include "csutil/hash.h"
#include "csutil/metautils.h"
#include "csutil/compileassert.h"
#if defined(CS_COMPILER_MSVC) && (CS_PROCESSOR_SIZE == 64)
/* long is 32 bit even on 64-bit MSVC, so use uint64 to utilize a storage in
* 64 bit units.
*/
typedef uint64 csBitArrayStorageType;
#else
/// Storage type utilized by csBitArray to store the bits.
typedef unsigned long csBitArrayStorageType;
#endif
const size_t csBitArrayDefaultInlineBits =
sizeof (csBitArrayStorageType) * 8;
/// Base comparator for bit arrays
template<typename BitArray>
class csComparatorBitArray
{
public:
static int Compare (BitArray const& key1, BitArray const& key2)
{
csBitArrayStorageType const* p0 = key1.GetStore();
csBitArrayStorageType const* p1 = key2.GetStore();
size_t compareNum = MIN (key1.mLength, key2.mLength);
size_t i = 0;
for (; i < compareNum; i++)
if (p0[i] != p1[i])
return (int)p0[i] - (int)p1[i];
if (key1.mLength > key2.mLength)
{
for (; i < key1.mLength; i++)
if (p0[i] != 0)
return (int)p0[i];
}
else
{
for (; i < key2.mLength; i++)
if (p1[i] != 0)
return -((int)p1[i]);
}
return 0;
}
};
/// Base hash computer for bit arrays
template<typename BitArray>
class csHashComputerBitArray
{
public:
static uint ComputeHash (BitArray const& key)
{
const size_t uintCount = sizeof (csBitArrayStorageType) / sizeof (uint);
uint ui[uintCount];
uint hash = 0;
csBitArrayStorageType const* p = key.GetStore();
// \todo Not very good. Find a better hash function; however, it should
// return the same hash for two bit arrays that are the same except for
// the amount of trailing zeros. (e.g. f(10010110) == f(100101100000...))
for (size_t i = 0; i < key.mLength; i++)
{
memcpy (ui, &p[i], sizeof (ui));
for (size_t j = 0; j < uintCount; j++)
hash += ui[j];
}
return hash;
}
};
/**
* A one-dimensional array of bits, similar to STL bitset.
* The amount of bits is dynamic at runtime.
*
* Internally, bits are stored in multiple values of the type
* csBitArrayStorageType. If the number of bits is below a certain threshold,
* the bits are stored in a field inside the class for more performance,
* above that threshold, the data is stored on the heap.
*
* This threshold can be tweaked by changing the \a InlinedBits template
* parameter. At least \a InlinedBits bits will be stored inlined in the
* class (the actual amount is the next bigger multiple of the number of bits
* fitting into one csBitArrayStorageType). In scenarios where you can
* anticipate that the common number of stored bits is larger than the
* default number, you can tweak InlinedBits to gain more performance.
*
* The \a Allocator template allocator allows you to override the logic
* to allocate bits from the heap.
*/
template<int InlinedBits = csBitArrayDefaultInlineBits,
typename Allocator = CS::Memory::AllocatorMalloc>
class csBitArrayTweakable
{
public:
typedef csBitArrayTweakable<InlinedBits, Allocator> ThisType;
typedef Allocator AllocatorType;
protected:
template<typename BitArray> friend class csComparatorBitArray;
template<typename BitArray> friend class csHashComputerBitArray;
enum
{
cellSize = csBitArrayDefaultInlineBits, // This _have_ to be PO2
cellCount = (InlinedBits + (cellSize-1)) / cellSize,
cellMask = (cellSize - 1),
cellShift = CS::Meta::Log2<cellSize>::value
};
CS_COMPILE_ASSERT(CS::Meta::IsLog2<cellSize>::value);
struct Storage : public Allocator
{
union
{
csBitArrayStorageType* heapStore;
csBitArrayStorageType inlineStore[cellCount];
};
Storage()
{
memset (&inlineStore, 0,
MAX(sizeof (heapStore), sizeof (inlineStore)));
}
};
Storage storage;
/// Length of heapStore/inlineStore in units of csBitArrayStorageType
size_t mLength;
size_t mNumBits;
/// Get the GetStore()[] index for a given bit number.
static size_t GetIndex (size_t bit_num)
{
return bit_num >> cellShift;
}
/// Get the offset within GetStore()[GetIndex()] for a given bit number.
static size_t GetOffset (size_t bit_num)
{
return bit_num & cellMask;
}
/// Return whether the inline or heap store is used
bool UseInlineStore () const
{
return mLength <= cellCount;
}
/**
* Get a constant pointer to bit store, which may be internal mSingleWord or
* heap-allocated mpStore.
*/
csBitArrayStorageType const* GetStore() const
{
return UseInlineStore () ? storage.inlineStore : storage.heapStore;
}
/**
* Get a non-constant pointer to bit store, which may be internal mSingleWord
* or heap-allocated mpStore.
*/
csBitArrayStorageType* GetStore()
{
return UseInlineStore () ? storage.inlineStore : storage.heapStore;
}
/// Force overhang bits at the end to 0.
void Trim()
{
size_t extra_bits = mNumBits % cellSize;
if (mLength > 0 && extra_bits != 0)
GetStore()[mLength - 1] &= ~((~(csBitArrayStorageType)0) << extra_bits);
}
/**
* Set the number of stored bits.
* \remarks Does not clear newly added bits.
*/
void SetSizeInternal (size_t newSize)
{
size_t newLength;
if (newSize == 0)
newLength = 0;
else
newLength = 1 + GetIndex (newSize - 1);
if (newLength != mLength)
{
// Avoid allocation if length is 1 (common case)
csBitArrayStorageType* newStore;
if (newLength <= cellCount)
newStore = storage.inlineStore;
else
newStore = (csBitArrayStorageType*)storage.Alloc (
newLength * sizeof (csBitArrayStorageType));
if (newLength > 0)
{
if (mLength > 0)
{
csBitArrayStorageType* oldStore = GetStore();
if (newStore != oldStore)
{
memcpy (newStore, oldStore,
(MIN (mLength, newLength)) * sizeof (csBitArrayStorageType));
if (newLength > mLength)
memset(newStore + mLength, 0,
(newLength - mLength) * sizeof (csBitArrayStorageType));
if (!UseInlineStore ())
storage.Free (oldStore);
}
}
else
memset (newStore, 0, newLength * sizeof (csBitArrayStorageType));
}
mLength = newLength;
if (!UseInlineStore()) storage.heapStore = newStore;
}
mNumBits = newSize;
}
public:
/**
* \internal Bit proxy (for csBitArray::operator[])
*/
class BitProxy
{
private:
csBitArrayTweakable& mArray;
size_t mPos;
public:
/// Constructor.
BitProxy (csBitArrayTweakable& array, size_t pos): mArray(array), mPos(pos)
{}
/// Boolean assignment.
BitProxy &operator= (bool value)
{
mArray.Set (mPos, value);
return *this;
}
/// Proxy assignment.
BitProxy &operator= (const BitProxy &that)
{
mArray.Set (mPos, that.mArray.IsBitSet (that.mPos));
return *this;
}
/// Boolean accessor.
operator bool() const
{
return mArray.IsBitSet (mPos);
}
/**
* Flip state of this bit.
* \return New state of bit.
*/
bool Flip()
{
mArray.FlipBit (mPos);
return mArray.IsBitSet (mPos);
}
};
friend class BitProxy;
/**
* Default constructor.
*/
csBitArrayTweakable () : mLength(0), mNumBits(0)
{
SetSize (0);
}
/**
* Construct with an initial size of \a size bits. These bits will be initialized as false.
*/
explicit csBitArrayTweakable (size_t size) : mLength(0), mNumBits(0)
{
SetSize (size);
}
/**
* Construct as duplicate of \a that (copy constructor).
*/
csBitArrayTweakable (const csBitArrayTweakable& that) : mLength(0), mNumBits(0)
{
*this = that; // Invokes this->operator=().
}
/// Destructor.
~csBitArrayTweakable()
{
if (!UseInlineStore ())
storage.Free (storage.heapStore);
}
/// Return the number of stored bits.
size_t GetSize() const
{
return mNumBits;
}
/**
* Return the number of stored bits.
* \deprecated Deprecated in 1.3. Use GetSize() instead.
*/
CS_DEPRECATED_METHOD_MSG("Use GetSize() instead.")
size_t Length () const
{
return GetSize();
}
/**
* Set the number of stored bits.
* \deprecated Deprecated in 1.3. Use SetSize() instead.
*/
CS_DEPRECATED_METHOD_MSG("Use SetSize() instead.")
void SetLength (size_t newSize)
{
SetSize (newSize);
}
/**
* Set the number of stored bits.
* \remarks If the new size is larger than the old size, the newly added
* bits are cleared.
*/
void SetSize (size_t newSize)
{
SetSizeInternal (newSize);
Trim();
}
//
// Operators
//
/// Copy from other array.
csBitArrayTweakable& operator=(const csBitArrayTweakable& that)
{
if (this != &that)
{
SetSizeInternal (that.mNumBits);
memcpy (GetStore(), that.GetStore(),
mLength * sizeof (csBitArrayStorageType));
}
return *this;
}
/// Return bit at position \a pos.
BitProxy operator[] (size_t pos)
{
CS_ASSERT (pos < mNumBits);
return BitProxy(*this, pos);
}
/// Return bit at position \a pos.
bool operator[] (size_t pos) const
{
return IsBitSet(pos);
}
/// Equal to other array?
bool operator==(const csBitArrayTweakable& that) const
{
if (mNumBits != that.mNumBits)
return false;
csBitArrayStorageType const* p0 = GetStore();
csBitArrayStorageType const* p1 = that.GetStore();
for (unsigned i = 0; i < mLength; i++)
if (p0[i] != p1[i])
return false;
return true;
}
/// Not equal to other array?
bool operator != (const csBitArrayTweakable& that) const
{
return !(*this == that);
}
/// Bit-wise `and'. The arrays must be the same length.
csBitArrayTweakable& operator &= (const csBitArrayTweakable &that)
{
CS_ASSERT (mNumBits == that.mNumBits);
csBitArrayStorageType* p0 = GetStore();
csBitArrayStorageType const* p1 = that.GetStore();
for (size_t i = 0; i < mLength; i++)
p0[i] &= p1[i];
return *this;
}
/// Bit-wise `or'. The arrays must be the same length.
csBitArrayTweakable operator |= (const csBitArrayTweakable& that)
{
CS_ASSERT (mNumBits == that.mNumBits);
csBitArrayStorageType* p0 = GetStore();
csBitArrayStorageType const* p1 = that.GetStore();
for (size_t i = 0; i < mLength; i++)
p0[i] |= p1[i];
return *this;
}
/// Bit-wise `xor'. The arrays must be the same length.
csBitArrayTweakable operator ^= (const csBitArrayTweakable& that)
{
CS_ASSERT (mNumBits == that.mNumBits);
csBitArrayStorageType* p0 = GetStore();
csBitArrayStorageType const* p1 = that.GetStore();
for (size_t i = 0; i < mLength; i++)
p0[i] ^= p1[i];
return *this;
}
/// Return complement bit array in which all bits are flipped from this one.
csBitArrayTweakable operator~() const
{
return csBitArrayTweakable(*this).FlipAllBits();
}
/// Bit-wise `and'. The arrays must be the same length.
friend csBitArrayTweakable operator& (const csBitArrayTweakable& a1,
const csBitArrayTweakable& a2)
{
return csBitArrayTweakable (a1) &= a2;
}
/// Bit-wise `or'. The arrays must be the same length.
friend csBitArrayTweakable operator | (const csBitArrayTweakable& a1,
const csBitArrayTweakable& a2)
{
return csBitArrayTweakable (a1) |= a2;
}
/// Bit-wise `xor'. The arrays must be the same length.
friend csBitArrayTweakable operator ^ (const csBitArrayTweakable& a1,
const csBitArrayTweakable& a2)
{
return csBitArrayTweakable (a1) ^= a2;
}
//
// Plain English interface
//
/// Set all bits to false.
void Clear()
{
memset (GetStore(), 0, mLength * sizeof(csBitArrayStorageType));
}
/// Set all bits to true.
void SetAll()
{
csBitArrayStorageType* store = GetStore();
for (size_t i = 0; i < mNumBits; i++)
store[GetIndex(i)] = ((csBitArrayStorageType)1) << GetOffset(i);
}
/// Set the bit at position pos to true.
void SetBit (size_t pos)
{
CS_ASSERT (pos < mNumBits);
GetStore()[GetIndex(pos)] |= ((csBitArrayStorageType)1) << GetOffset(pos);
}
/// Set the bit at position pos to false.
void ClearBit (size_t pos)
{
CS_ASSERT (pos < mNumBits);
GetStore()[GetIndex(pos)] &= ~(((csBitArrayStorageType)1) << GetOffset(pos));
}
/// Toggle the bit at position pos.
void FlipBit (size_t pos)
{
CS_ASSERT (pos < mNumBits);
GetStore()[GetIndex(pos)] ^= ((csBitArrayStorageType)1) << GetOffset(pos);
}
/// Set the bit at position \a pos to the given value.
void Set (size_t pos, bool val = true)
{
if (val)
SetBit(pos);
else
ClearBit(pos);
}
/// Returns true if the bit at position \a pos is true.
bool IsBitSet (size_t pos) const
{
CS_ASSERT (pos < mNumBits);
return (GetStore()[GetIndex(pos)]
& (((csBitArrayStorageType)1) << GetOffset(pos))) != 0;
}
/**
* Returns true if the bit at position \a pos is true.
* The difference to IsBitSet() is that this methods accepts positions
* outside the bit array (returns \c false) instead of throwing an
* assertion in that case.
*/
bool IsBitSetTolerant (size_t pos) const
{
if (pos >= mNumBits) return false;
return (GetStore()[GetIndex(pos)]
& (((csBitArrayStorageType)1) << GetOffset(pos))) != 0;
}
/// Checks whether at least one of \a count bits is set starting at \a pos.
bool AreSomeBitsSet (size_t pos, size_t count) const
{
CS_ASSERT (pos + count <= mNumBits);
csBitArrayStorageType const* p = GetStore();
while (count > 0)
{
size_t index = GetIndex (pos);
size_t offset = GetOffset (pos);
size_t checkCount = MIN(count, cellSize - offset);
csBitArrayStorageType mask = ((checkCount == cellSize)
? ~(csBitArrayStorageType)0
: ((((csBitArrayStorageType)1) << checkCount) - 1)) << offset;
if (p[index] & mask)
return true;
pos += checkCount;
count -= checkCount;
}
return false;
}
/// Returns true if all bits are false.
bool AllBitsFalse() const
{
csBitArrayStorageType const* p = GetStore();
for (size_t i = 0; i < mLength; i++)
if (p[i] != 0)
return false;
return true;
}
/// Change value of all bits
csBitArrayTweakable& FlipAllBits()
{
csBitArrayStorageType* p = GetStore();
for (size_t i = 0; i < mLength; i++)
p[i] = ~p[i];
Trim();
return *this;
}
/// Count the number of bits that are set.
size_t NumBitsSet() const
{
const size_t ui32perStorage =
sizeof (csBitArrayStorageType) / sizeof (uint32);
union
{
csBitArrayStorageType s;
uint32 ui32[ui32perStorage];
} v;
const csBitArrayStorageType* p = GetStore();
size_t num = 0;
for (size_t i = 0; i < mLength; i++)
{
v.s = p[i];
for (size_t j = 0; j < ui32perStorage; j++)
num += CS::Utility::BitOps::ComputeBitsSet (v.ui32[j]);
}
return num;
}
/**
* Find first bit in array which is set.
* \return First bit set or csArrayItemNotFound if all bits are set.
*/
size_t GetFirstBitSet() const
{
const size_t ui32perStorage =
sizeof (csBitArrayStorageType) / sizeof (uint32);
union
{
csBitArrayStorageType s;
uint32 ui32[ui32perStorage];
} v;
const csBitArrayStorageType* p = GetStore();
size_t ofs = 0, result;
for (size_t i = 0; i < mLength; i++)
{
v.s = p[i];
for (size_t j = 0; j < ui32perStorage; j++)
{
if (CS::Utility::BitOps::ScanBitForward (v.ui32[j], result))
{
size_t r = ofs + result;
if (r >= mNumBits) return csArrayItemNotFound;
return r;
}
ofs += 32;
}
}
return csArrayItemNotFound;
}
/**
* Find first bit in array which is not set.
* \return First bit set or csArrayItemNotFound if all bits are not set.
*/
size_t GetFirstBitUnset() const
{
const size_t ui32perStorage =
sizeof (csBitArrayStorageType) / sizeof (uint32);
union
{
csBitArrayStorageType s;
uint32 ui32[ui32perStorage];
} v;
const csBitArrayStorageType* p = GetStore();
size_t ofs = 0;
unsigned long result;
for (size_t i = 0; i < mLength; i++)
{
v.s = p[i];
for (size_t j = 0; j < ui32perStorage; j++)
{
if (CS::Utility::BitOps::ScanBitForward (~v.ui32[j], result))
{
size_t r = ofs + result;
if (r >= mNumBits) return csArrayItemNotFound;
return r;
}
ofs += 32;
}
}
return csArrayItemNotFound;
}
/**
* Find last bit in array which is set.
* \return First bit set or csArrayItemNotFound if all bits are set.
*/
size_t GetLastBitSet() const
{
const size_t ui32perStorage =
sizeof (csBitArrayStorageType) / sizeof (uint32);
union
{
csBitArrayStorageType s;
uint32 ui32[ui32perStorage];
} v;
const csBitArrayStorageType* p = GetStore();
size_t ofs, result;
ofs = 32 * (mLength*ui32perStorage-1);
for (size_t i = mLength; i-- > 0;)
{
v.s = p[i];
for (size_t j = ui32perStorage; j-- > 0; )
{
if (CS::Utility::BitOps::ScanBitForward (v.ui32[j], result))
{
size_t r = ofs + result;
if (r >= mNumBits) return csArrayItemNotFound;
return r;
}
ofs -= 32;
}
}
return csArrayItemNotFound;
}
/**
* Find last bit in array which is not set.
* \return First bit set or csArrayItemNotFound if all bits are not set.
*/
size_t GetLastBitUnset() const
{
const size_t ui32perStorage =
sizeof (csBitArrayStorageType) / sizeof (uint32);
union
{
csBitArrayStorageType s;
uint32 ui32[ui32perStorage];
} v;
const csBitArrayStorageType* p = GetStore();
size_t ofs, result;
ofs = 32 * (mLength*ui32perStorage-1);
for (size_t i = mLength; i-- > 0;)
{
v.s = p[i];
for (size_t j = ui32perStorage; j-- > 0; )
{
if (CS::Utility::BitOps::ScanBitForward (~v.ui32[j], result))
{
size_t r = ofs + result;
if (r >= mNumBits) return csArrayItemNotFound;
return r;
}
ofs -= 32;
}
}
return csArrayItemNotFound;
}
/**
* Delete from the array \a count bits starting at \a pos, making the array
* shorter.
*/
void Delete(size_t pos, size_t count)
{
if (count > 0)
{
size_t dst = pos;
size_t src = pos + count;
CS_ASSERT(src <= mNumBits);
size_t ntail = mNumBits - src;
while (ntail-- > 0)
Set(dst++, IsBitSet(src++));
SetSize(mNumBits - count);
}
}
/**
* Return a new bit array containing a slice \a count bits in length from
* this array starting at \a pos. Does not modify this array.
*/
csBitArrayTweakable Slice(size_t pos, size_t count) const
{
CS_ASSERT(pos + count <= mNumBits);
csBitArrayTweakable a (count);
for (size_t i = pos, o = 0; i < pos + count; i++)
if (IsBitSet(i))
a.SetBit(o++);
return a;
}
//@{
/// Return the full backing-store.
csBitArrayStorageType* GetArrayBits() { return GetStore(); }
const csBitArrayStorageType* GetArrayBits() const { return GetStore(); }
//@}
//@{
/**
*
*/
class SetBitIterator
{
public:
SetBitIterator (const SetBitIterator& other)
: bitArray (other.bitArray), pos (other.pos), offset (other.offset),
cachedStore (other.cachedStore)
{}
///
size_t Next ()
{
while (offset < 8*sizeof(csBitArrayStorageType))
{
if ((cachedStore & 0x1) != 0)
{
const size_t result = (pos-1)*sizeof(csBitArrayStorageType)*8 + offset;
++offset;
cachedStore >>= 1;
if (!cachedStore)
GetNextCacheItem ();
return result;
}
else
{
++offset;
cachedStore >>= 1;
if (!cachedStore)
GetNextCacheItem ();
}
}
CS_ASSERT_MSG ("Invalid iteration", false);
return 0;
}
///
bool HasNext () const
{
return cachedStore != 0;
}
///
void Reset ()
{
pos = 0;
GetNextCacheItem ();
}
protected:
SetBitIterator (const ThisType& bitArray)
: bitArray (bitArray), pos (0), offset (0), cachedStore (0)
{
Reset ();
}
friend class csBitArrayTweakable<InlinedBits, Allocator>;
void GetNextCacheItem ()
{
offset = 0;
while (pos < bitArray.mLength)
{
cachedStore = bitArray.GetStore ()[pos++];
if (cachedStore)
return;
}
cachedStore = 0;
}
const ThisType& bitArray;
size_t pos, offset;
csBitArrayStorageType cachedStore;
};
friend class SetBitIterator;
SetBitIterator GetSetBitIterator () const
{
return SetBitIterator (*this);
}
//@}
/**\name Serialization
* @{ */
/**
* Get byte stream with the contents of the bit array.
* \param numBytes Number of bytes in the returned buffer.
* \return A byte stream with the contents of the bit array.
* May be 0. Must be freed with cs_free().
*/
uint8* Serialize (size_t& numBytes) const
{
if (mNumBits == 0)
{
numBytes = 0;
return 0;
}
struct SerializeHelper
{
uint8* buf;
size_t bufSize, bufUsed;
SerializeHelper () : buf (0), bufSize (0), bufUsed (0) {}
void PushByte (uint8 b)
{
if (bufUsed >= bufSize)
{
bufSize += 4;
buf = (uint8*)cs_realloc (buf, bufSize);
}
buf[bufUsed++] = b;
}
void TruncZeroes ()
{
while ((bufUsed > 0) && (buf[bufUsed-1] == 0))
bufUsed--;
}
} serHelper;
// Write out bits number
{
size_t remainder = mNumBits;
while (remainder >= 128)
{
uint8 b = (remainder & 0x7f) | 0x80;
serHelper.PushByte (b);
remainder >>= 7;
}
serHelper.PushByte (uint8 (remainder));
}
const size_t ui8Count = sizeof (csBitArrayStorageType) / sizeof (uint8);
uint8 ui8[ui8Count];
csBitArrayStorageType const* p = GetStore();
for (size_t i = 0; i < mLength; i++)
{
memcpy (ui8, &p[i], sizeof (ui8));
#ifdef CS_LITTLE_ENDIAN
for (size_t j = 0; j < ui8Count; j++)
#else
for (size_t j = ui8Count; j-- > 0; )
#endif
{
serHelper.PushByte (ui8[j]);
}
}
serHelper.TruncZeroes();
numBytes = serHelper.bufUsed;
return serHelper.buf;
}
/**
* Create a new instance of a bit array with the contents as given
* in the byte stream.
*/
static ThisType Unserialize (uint8* bytes, size_t numBytes)
{
if ((bytes == 0) || (numBytes == 0))
return ThisType(); // empty bit array
size_t bufPos = 0;
// Read the bits number
size_t numBits = 0;
int shift = 0;
while (bufPos < numBytes)
{
uint8 b = bytes[bufPos++];
numBits |= (b & 0x7f) << shift;
if ((b & 0x80) == 0) break;
shift += 7;
}
ThisType newArray (numBits);
// Read the actual bits
csBitArrayStorageType* p = newArray.GetStore();
size_t storeIndex = 0;
while (bufPos < numBytes)
{
const size_t ui8Count = sizeof (csBitArrayStorageType) / sizeof (uint8);
uint8 ui8[ui8Count];
memset (ui8, 0, sizeof (ui8));
#ifdef CS_LITTLE_ENDIAN
for (size_t j = 0; j < ui8Count; j++)
#else
for (size_t j = ui8Count; j-- > 0; )
#endif
{
ui8[j] = bytes[bufPos++];
if (bufPos >= numBytes) break;
}
memcpy (p + (storeIndex++), ui8, sizeof (ui8));
}
return newArray;
}
/** @} */
};
/**
* A one-dimensional array of bits, similar to STL bitset.
* The amount of bits is dynamic at runtime.
*/
class csBitArray : public csBitArrayTweakable<>
{
public:
/// Default constructor.
csBitArray () { }
///Construct with an initial size of \a size bits. These bits will be initialized as false.
explicit csBitArray (size_t size) : csBitArrayTweakable<> (size) { }
/// Construct as duplicate of \a that (copy constructor).
csBitArray (const csBitArray& that) : csBitArrayTweakable<> (that) { }
/// Copy from other array.
template<int A, typename B>
csBitArray& operator=(const csBitArrayTweakable<A, B>& that)
{
if (this != &that)
{
SetSize (that.GetSize());
memcpy (GetStore(), that.GetArrayBits(),
mLength * sizeof (csBitArrayStorageType));
}
return *this;
}
};
/**
* csComparator<> specialization for csBitArray to allow its use as
* e.g. hash key type.
*/
template<>
class csComparator<csBitArray, csBitArray> :
public csComparatorBitArray<csBitArray> { };
/**
* csHashComputer<> specialization for csBitArray to allow its use as
* hash key type.
*/
template<>
class csHashComputer<csBitArray> :
public csHashComputerBitArray<csBitArray> { };
#endif // __CS_BITARRAY_H__
|