30 size_t str_size = str.size();
36 for(
size_t i = 0; i < str_size; ++i){
37 if (str[(str_size-1) - i ] ==
'1'){
41 else if (str[(str_size-1) - i ] ==
'0'){
45 std::cerr <<
"Invalid argument: " << str[(str_size-1) - i ] <<
" Only 0's and 1's \
46 allowed in string to initialize dynamic_bitset" << std::endl;
54 size_t vec_size = vec.size();
59 for (
size_t i = 0; i < vec_size; ++i){
110 size_t new_num_ints = ((sz-1)/32)+1;
116 array.resize(new_num_ints);
126 uint32_t mask = (1 << 32 -
buffer) - 1;
139 size_t new_num_ints = ((sz-1)/32)+1;
140 array.reserve(new_num_ints);
185 for(
size_t i = 0; i <
num_ints; ++i){
190 uint32_t mask = (1 << 32-output.
buffer) - 1;
191 output.
array[num_ints-1] &= mask;
211 std::cerr << __FUNCTION__ <<
" called on dynamic_bitsets of different sizes." <<std::endl;
215 for(
size_t i = 0; i <
num_ints; ++i){
220 uint32_t mask = (1 << 32-
buffer) - 1;
221 array[num_ints-1] &= mask;
230 std::cerr << __FUNCTION__ <<
" called on dynamic_bitsets of different sizes." <<std::endl;
235 for(
size_t i = 0; i <
num_ints; ++i){
241 uint32_t mask = (1 << 32-
buffer) - 1;
242 array[num_ints-1] &= mask;
251 std::cerr << __FUNCTION__ <<
" called on dynamic_bitsets of different sizes." <<std::endl;
255 for(
size_t i = 0; i <
num_ints; ++i){
261 uint32_t mask = (1 << 32-
buffer) - 1;
262 array[num_ints-1] &= mask;
281 for(
size_t i = 0; i <
num_ints ;++i){
294 for(
size_t i = 0; i <
num_ints ;++i){
310 size_t ints_to_add = n/32;
320 uint32_t shifted(
lso(
array[start],n));
321 for(
size_t i = start+1; i <
num_ints ; ++i){
326 uint32_t mask = (1 << 32-
buffer) - 1;
327 array[num_ints-1] &= mask;
348 size_t ints_to_delete = n/32;
351 start -= ints_to_delete;
360 uint32_t shifted(
rso(
array[start],n));
363 for(
size_t i = start-1; i !=
SIZE_MAX ; --i){
386 std::cerr <<
"dynamic_bitset::"<<__FUNCTION__ <<
" Error: Position for insert is beyond the range current size\n";
391 size_t remainder = 32 - (pos%32);
394 size_t first_affected = pos/32;
397 size_t next_affected = (pos+n)/32;
399 int64_t next_shift_size = ((pos+n)%32) - (pos%32);
401 int64_t intervening_ints = int64_t (n-remainder) / 32;
402 size_t relative_pos = pos%32;
407 if (intervening_ints != 0 && intervening_ints >= 0){
408 array.insert(
array.begin()+first_affected+1,intervening_ints,0);
414 array.resize(new_num_ints);
419 uint32_t temp_mask =
maskLeft(
array[first_affected], relative_pos);
423 if (next_shift_size == 0){
424 size_t ints_shifted = n/32;
425 for (
size_t i =
num_ints-1; i > first_affected + intervening_ints + 1; --i){
426 array[i] =
array[i-(ints_shifted-intervening_ints)];
428 array[first_affected+ints_shifted] = temp_mask;
430 else if (next_shift_size < 0){
433 uint8_t left_shift = 32 - abs( (uint8_t) next_shift_size);
434 temp_mask >>= abs( (uint8_t) next_shift_size);
436 for(
size_t i = next_affected; i <
num_ints ; ++i){
437 uint32_t tmp =
array[i];
438 array[i] <<= left_shift;
439 array[i] |= temp_mask;
440 temp_mask = tmp >> (32-left_shift);
450 size_t ints_shifted = n/32;
451 for (
size_t i =
num_ints-1; i > first_affected + intervening_ints + 1; --i){
452 array[i] =
array[i-(ints_shifted-intervening_ints)];
453 array[i-(ints_shifted-intervening_ints)]=0;
458 uint32_t shifted =
lso(temp_mask,next_shift_size);
459 array[next_affected] |= temp_mask;
462 for(
size_t i = next_affected+1; i <
num_ints ; ++i){
463 uint32_t tmp =
array[i];
464 array[i] <<= next_shift_size;
466 shifted = tmp >> (32-next_shift_size);
495 size_t first_affected = pos/32;
496 size_t relative_pos = pos%32;
506 uint32_t temp(
array[i]);
508 shifted = temp << 31;
514 uint32_t temp_mask =
maskLeft(
array[first_affected], relative_pos+1);
518 if ((relative_pos - 1) ==
SIZE_MAX){
519 array[first_affected] = 0;
530 temp_mask |= shifted;
533 array[first_affected] |= temp_mask;
537 array.resize(new_num_ints);
565 return *
this & ~rhs ;
576 size_t intIter = pos/32;
577 size_t bitIter = pos - (intIter*32);
578 return array[intIter] & (1 << bitIter);
582 size_t intIter = pos/32;
583 size_t bitIter = pos - (intIter*32);
591 std::cerr <<
"Error: Out of Range.\nFunction: " << __FUNCTION__ << std::endl;
595 size_t intIter = pos/32;
596 size_t bitIter = pos - (intIter*32);
597 return array[intIter] & (1 << bitIter);
607 std::cerr <<
"Error: Out of Range.\nFunction: " << __FUNCTION__ << std::endl;
620 std::cerr <<
"Error: Out of Range.\nFunction: " << __FUNCTION__ << std::endl;
624 size_t intIter = pos/32;
625 size_t bitIter = pos - (intIter*32);
626 array[intIter] |= (1 << bitIter);
636 std::cerr <<
"Error: Out of Range.\nFunction: " << __FUNCTION__ << std::endl;
653 std::cerr <<
"Error: Out of Range.\nFunction: " << __FUNCTION__ << std::endl;
657 size_t intIter = pos/32;
658 size_t bitIter = pos - (intIter*32);
659 array[intIter] &= ~(1 << bitIter);
698 for(
size_t i = 0; i <
num_ints; i++){
710 for(
size_t i = 0; i <
num_ints; i++){
759 for(
size_t i = 0; i <
num_ints ; ++i){
764 uint32_t val =
array[i];
766 static const size_t MultiplyDeBruijnBitPosition[32] =
769 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
770 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
781 return i*32 + MultiplyDeBruijnBitPosition[val];
794 static const size_t MultiplyDeBruijnBitPosition[32] =
797 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
798 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
807 size_t iter = pos/32;
810 size_t int_pos = pos%32;
812 int32_t val =
array[iter];
815 val = val & ~((1 << int_pos) - 1);
826 return iter * 32 + MultiplyDeBruijnBitPosition[val];
831 for(
size_t i = iter+1; i <
num_ints ; ++i){
836 uint32_t val =
array[i];
846 return i * 32 + MultiplyDeBruijnBitPosition[val];
864 uint32_t val =
array[i];
865 const uint32_t mask[] = {
878 for (uint32_t j = 0; j < 5; j++) {
879 int mi = lo + (hi - lo) / 2;
881 if ((val >> mi) != 0)
883 else if ((val & (mask[j] << lo)) != 0)
900 size_t iter = pos/32;
903 size_t int_pos = pos%32;
905 int32_t val =
array[iter];
909 val = val & ((1 << int_pos+1) - 1);
917 const uint32_t mask[] = {
930 for (uint32_t j = 0; j < 5; j++) {
931 int mi = lo + (hi - lo) / 2;
933 if ((val >> mi) != 0)
935 else if ((val & (mask[j] << lo)) != 0)
938 return lo + 32 * iter;
941 for(
size_t i = iter-1; i !=
SIZE_MAX ; --i){
948 uint32_t val =
array[i];
949 const uint32_t mask[] = {
962 for (uint32_t j = 0; j < 5; j++) {
963 int mi = lo + (hi - lo) / 2;
965 if ((val >> mi) != 0)
967 else if ((val & (mask[j] << lo)) != 0)
983 for(
size_t i = 0; i <
num_ints; ++i){
984 uint32_t temp =
array[i];
989 val ^= ((temp >> 28) & 1) != 0;
998 size_t population(0);
999 for (
size_t i=0; i <
num_ints; i++){
1000 uint32_t x =
array[i];
1002 x = x - ((x >> 1) & 0x55555555);
1004 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
1010 population+= (x * 0x01010101) >> 24;
1020 size_t current_int = pos/32;
1021 size_t current_pos = pos%32;
1023 uint32_t x(
array[current_int]);
1026 uint32_t mask = (1 << current_pos) - 1;
1030 x = x - ((x >> 1) & 0x55555555);
1031 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
1032 size_t population(((x + (x >> 4) & 0xF0F0F0F) * 0x1010101) >> 24);
1035 for (
size_t i=0; i < current_int; i++){
1038 x = x - ((x >> 1) & 0x55555555);
1040 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
1046 population+= (x * 0x01010101) >> 24;
1055 std::string output(
"");
1078 std::string output(
"");
1104 if ((rhs.
val >> pos) & 1){
1125 if (!value && ((this->val >> pos) & 1)){
1151 return !((this->val >> pos) & 1);