| 1 |
|
|---|
| 2 |
=head1 NAME |
|---|
| 3 |
|
|---|
| 4 |
Bit::Vector - Efficient bit vector, set of integers and "big int" math library |
|---|
| 5 |
|
|---|
| 6 |
=head1 SYNOPSIS |
|---|
| 7 |
|
|---|
| 8 |
=head2 OVERLOADED OPERATORS |
|---|
| 9 |
|
|---|
| 10 |
See L<Bit::Vector::Overload(3)>. |
|---|
| 11 |
|
|---|
| 12 |
=head2 CLASS METHODS |
|---|
| 13 |
|
|---|
| 14 |
Version |
|---|
| 15 |
$version = Bit::Vector->Version(); |
|---|
| 16 |
|
|---|
| 17 |
Word_Bits |
|---|
| 18 |
$bits = Bit::Vector->Word_Bits(); # bits in a machine word |
|---|
| 19 |
|
|---|
| 20 |
Long_Bits |
|---|
| 21 |
$bits = Bit::Vector->Long_Bits(); # bits in an unsigned long |
|---|
| 22 |
|
|---|
| 23 |
new |
|---|
| 24 |
$vector = Bit::Vector->new($bits); # bit vector constructor |
|---|
| 25 |
|
|---|
| 26 |
@veclist = Bit::Vector->new($bits,$count); |
|---|
| 27 |
|
|---|
| 28 |
new_Hex |
|---|
| 29 |
$vector = Bit::Vector->new_Hex($bits,$string); |
|---|
| 30 |
|
|---|
| 31 |
new_Bin |
|---|
| 32 |
$vector = Bit::Vector->new_Bin($bits,$string); |
|---|
| 33 |
|
|---|
| 34 |
new_Dec |
|---|
| 35 |
$vector = Bit::Vector->new_Dec($bits,$string); |
|---|
| 36 |
|
|---|
| 37 |
new_Enum |
|---|
| 38 |
$vector = Bit::Vector->new_Enum($bits,$string); |
|---|
| 39 |
|
|---|
| 40 |
Concat_List |
|---|
| 41 |
$vector = Bit::Vector->Concat_List(@vectors); |
|---|
| 42 |
|
|---|
| 43 |
=head2 OBJECT METHODS |
|---|
| 44 |
|
|---|
| 45 |
new |
|---|
| 46 |
$vec2 = $vec1->new($bits); # alternative call of constructor |
|---|
| 47 |
|
|---|
| 48 |
@veclist = $vec->new($bits,$count); |
|---|
| 49 |
|
|---|
| 50 |
Shadow |
|---|
| 51 |
$vec2 = $vec1->Shadow(); # new vector, same size but empty |
|---|
| 52 |
|
|---|
| 53 |
Clone |
|---|
| 54 |
$vec2 = $vec1->Clone(); # new vector, exact duplicate |
|---|
| 55 |
|
|---|
| 56 |
Concat |
|---|
| 57 |
$vector = $vec1->Concat($vec2); |
|---|
| 58 |
|
|---|
| 59 |
Concat_List |
|---|
| 60 |
$vector = $vec1->Concat_List($vec2,$vec3,...); |
|---|
| 61 |
|
|---|
| 62 |
Size |
|---|
| 63 |
$bits = $vector->Size(); |
|---|
| 64 |
|
|---|
| 65 |
Resize |
|---|
| 66 |
$vector->Resize($bits); |
|---|
| 67 |
$vector->Resize($vector->Size()+5); |
|---|
| 68 |
$vector->Resize($vector->Size()-5); |
|---|
| 69 |
|
|---|
| 70 |
Copy |
|---|
| 71 |
$vec2->Copy($vec1); |
|---|
| 72 |
|
|---|
| 73 |
Empty |
|---|
| 74 |
$vector->Empty(); |
|---|
| 75 |
|
|---|
| 76 |
Fill |
|---|
| 77 |
$vector->Fill(); |
|---|
| 78 |
|
|---|
| 79 |
Flip |
|---|
| 80 |
$vector->Flip(); |
|---|
| 81 |
|
|---|
| 82 |
Primes |
|---|
| 83 |
$vector->Primes(); # Sieve of Erathostenes |
|---|
| 84 |
|
|---|
| 85 |
Reverse |
|---|
| 86 |
$vec2->Reverse($vec1); |
|---|
| 87 |
|
|---|
| 88 |
Interval_Empty |
|---|
| 89 |
$vector->Interval_Empty($min,$max); |
|---|
| 90 |
|
|---|
| 91 |
Interval_Fill |
|---|
| 92 |
$vector->Interval_Fill($min,$max); |
|---|
| 93 |
|
|---|
| 94 |
Interval_Flip |
|---|
| 95 |
$vector->Interval_Flip($min,$max); |
|---|
| 96 |
|
|---|
| 97 |
Interval_Reverse |
|---|
| 98 |
$vector->Interval_Reverse($min,$max); |
|---|
| 99 |
|
|---|
| 100 |
Interval_Scan_inc |
|---|
| 101 |
if (($min,$max) = $vector->Interval_Scan_inc($start)) |
|---|
| 102 |
|
|---|
| 103 |
Interval_Scan_dec |
|---|
| 104 |
if (($min,$max) = $vector->Interval_Scan_dec($start)) |
|---|
| 105 |
|
|---|
| 106 |
Interval_Copy |
|---|
| 107 |
$vec2->Interval_Copy($vec1,$offset2,$offset1,$length); |
|---|
| 108 |
|
|---|
| 109 |
Interval_Substitute |
|---|
| 110 |
$vec2->Interval_Substitute($vec1,$off2,$len2,$off1,$len1); |
|---|
| 111 |
|
|---|
| 112 |
is_empty |
|---|
| 113 |
if ($vector->is_empty()) |
|---|
| 114 |
|
|---|
| 115 |
is_full |
|---|
| 116 |
if ($vector->is_full()) |
|---|
| 117 |
|
|---|
| 118 |
equal |
|---|
| 119 |
if ($vec1->equal($vec2)) |
|---|
| 120 |
|
|---|
| 121 |
Lexicompare (unsigned) |
|---|
| 122 |
if ($vec1->Lexicompare($vec2) == 0) |
|---|
| 123 |
if ($vec1->Lexicompare($vec2) != 0) |
|---|
| 124 |
if ($vec1->Lexicompare($vec2) < 0) |
|---|
| 125 |
if ($vec1->Lexicompare($vec2) <= 0) |
|---|
| 126 |
if ($vec1->Lexicompare($vec2) > 0) |
|---|
| 127 |
if ($vec1->Lexicompare($vec2) >= 0) |
|---|
| 128 |
|
|---|
| 129 |
Compare (signed) |
|---|
| 130 |
if ($vec1->Compare($vec2) == 0) |
|---|
| 131 |
if ($vec1->Compare($vec2) != 0) |
|---|
| 132 |
if ($vec1->Compare($vec2) < 0) |
|---|
| 133 |
if ($vec1->Compare($vec2) <= 0) |
|---|
| 134 |
if ($vec1->Compare($vec2) > 0) |
|---|
| 135 |
if ($vec1->Compare($vec2) >= 0) |
|---|
| 136 |
|
|---|
| 137 |
to_Hex |
|---|
| 138 |
$string = $vector->to_Hex(); |
|---|
| 139 |
|
|---|
| 140 |
from_Hex |
|---|
| 141 |
$vector->from_Hex($string); |
|---|
| 142 |
|
|---|
| 143 |
to_Bin |
|---|
| 144 |
$string = $vector->to_Bin(); |
|---|
| 145 |
|
|---|
| 146 |
from_Bin |
|---|
| 147 |
$vector->from_Bin($string); |
|---|
| 148 |
|
|---|
| 149 |
to_Dec |
|---|
| 150 |
$string = $vector->to_Dec(); |
|---|
| 151 |
|
|---|
| 152 |
from_Dec |
|---|
| 153 |
$vector->from_Dec($string); |
|---|
| 154 |
|
|---|
| 155 |
to_Enum |
|---|
| 156 |
$string = $vector->to_Enum(); # e.g. "2,3,5-7,11,13-19" |
|---|
| 157 |
|
|---|
| 158 |
from_Enum |
|---|
| 159 |
$vector->from_Enum($string); |
|---|
| 160 |
|
|---|
| 161 |
Bit_Off |
|---|
| 162 |
$vector->Bit_Off($index); |
|---|
| 163 |
|
|---|
| 164 |
Bit_On |
|---|
| 165 |
$vector->Bit_On($index); |
|---|
| 166 |
|
|---|
| 167 |
bit_flip |
|---|
| 168 |
$bit = $vector->bit_flip($index); |
|---|
| 169 |
|
|---|
| 170 |
bit_test |
|---|
| 171 |
contains |
|---|
| 172 |
$bit = $vector->bit_test($index); |
|---|
| 173 |
$bit = $vector->contains($index); |
|---|
| 174 |
if ($vector->bit_test($index)) |
|---|
| 175 |
if ($vector->contains($index)) |
|---|
| 176 |
|
|---|
| 177 |
Bit_Copy |
|---|
| 178 |
$vector->Bit_Copy($index,$bit); |
|---|
| 179 |
|
|---|
| 180 |
LSB (least significant bit) |
|---|
| 181 |
$vector->LSB($bit); |
|---|
| 182 |
|
|---|
| 183 |
MSB (most significant bit) |
|---|
| 184 |
$vector->MSB($bit); |
|---|
| 185 |
|
|---|
| 186 |
lsb (least significant bit) |
|---|
| 187 |
$bit = $vector->lsb(); |
|---|
| 188 |
|
|---|
| 189 |
msb (most significant bit) |
|---|
| 190 |
$bit = $vector->msb(); |
|---|
| 191 |
|
|---|
| 192 |
rotate_left |
|---|
| 193 |
$carry = $vector->rotate_left(); |
|---|
| 194 |
|
|---|
| 195 |
rotate_right |
|---|
| 196 |
$carry = $vector->rotate_right(); |
|---|
| 197 |
|
|---|
| 198 |
shift_left |
|---|
| 199 |
$carry = $vector->shift_left($carry); |
|---|
| 200 |
|
|---|
| 201 |
shift_right |
|---|
| 202 |
$carry = $vector->shift_right($carry); |
|---|
| 203 |
|
|---|
| 204 |
Move_Left |
|---|
| 205 |
$vector->Move_Left($bits); # shift left "$bits" positions |
|---|
| 206 |
|
|---|
| 207 |
Move_Right |
|---|
| 208 |
$vector->Move_Right($bits); # shift right "$bits" positions |
|---|
| 209 |
|
|---|
| 210 |
Insert |
|---|
| 211 |
$vector->Insert($offset,$bits); |
|---|
| 212 |
|
|---|
| 213 |
Delete |
|---|
| 214 |
$vector->Delete($offset,$bits); |
|---|
| 215 |
|
|---|
| 216 |
increment |
|---|
| 217 |
$carry = $vector->increment(); |
|---|
| 218 |
|
|---|
| 219 |
decrement |
|---|
| 220 |
$carry = $vector->decrement(); |
|---|
| 221 |
|
|---|
| 222 |
inc |
|---|
| 223 |
$overflow = $vec2->inc($vec1); |
|---|
| 224 |
|
|---|
| 225 |
dec |
|---|
| 226 |
$overflow = $vec2->dec($vec1); |
|---|
| 227 |
|
|---|
| 228 |
add |
|---|
| 229 |
$carry = $vec3->add($vec1,$vec2,$carry); |
|---|
| 230 |
($carry,$overflow) = $vec3->add($vec1,$vec2,$carry); |
|---|
| 231 |
|
|---|
| 232 |
subtract |
|---|
| 233 |
$carry = $vec3->subtract($vec1,$vec2,$carry); |
|---|
| 234 |
($carry,$overflow) = $vec3->subtract($vec1,$vec2,$carry); |
|---|
| 235 |
|
|---|
| 236 |
Neg |
|---|
| 237 |
Negate |
|---|
| 238 |
$vec2->Neg($vec1); |
|---|
| 239 |
$vec2->Negate($vec1); |
|---|
| 240 |
|
|---|
| 241 |
Abs |
|---|
| 242 |
Absolute |
|---|
| 243 |
$vec2->Abs($vec1); |
|---|
| 244 |
$vec2->Absolute($vec1); |
|---|
| 245 |
|
|---|
| 246 |
Sign |
|---|
| 247 |
if ($vector->Sign() == 0) |
|---|
| 248 |
if ($vector->Sign() != 0) |
|---|
| 249 |
if ($vector->Sign() < 0) |
|---|
| 250 |
if ($vector->Sign() <= 0) |
|---|
| 251 |
if ($vector->Sign() > 0) |
|---|
| 252 |
if ($vector->Sign() >= 0) |
|---|
| 253 |
|
|---|
| 254 |
Multiply |
|---|
| 255 |
$vec3->Multiply($vec1,$vec2); |
|---|
| 256 |
|
|---|
| 257 |
Divide |
|---|
| 258 |
$quot->Divide($vec1,$vec2,$rest); |
|---|
| 259 |
|
|---|
| 260 |
GCD (Greatest Common Divisor) |
|---|
| 261 |
$vecgcd->GCD($veca,$vecb); |
|---|
| 262 |
$vecgcd->GCD($vecx,$vecy,$veca,$vecb); |
|---|
| 263 |
|
|---|
| 264 |
Power |
|---|
| 265 |
$vec3->Power($vec1,$vec2); |
|---|
| 266 |
|
|---|
| 267 |
Block_Store |
|---|
| 268 |
$vector->Block_Store($buffer); |
|---|
| 269 |
|
|---|
| 270 |
Block_Read |
|---|
| 271 |
$buffer = $vector->Block_Read(); |
|---|
| 272 |
|
|---|
| 273 |
Word_Size |
|---|
| 274 |
$size = $vector->Word_Size(); # number of words in "$vector" |
|---|
| 275 |
|
|---|
| 276 |
Word_Store |
|---|
| 277 |
$vector->Word_Store($offset,$word); |
|---|
| 278 |
|
|---|
| 279 |
Word_Read |
|---|
| 280 |
$word = $vector->Word_Read($offset); |
|---|
| 281 |
|
|---|
| 282 |
Word_List_Store |
|---|
| 283 |
$vector->Word_List_Store(@words); |
|---|
| 284 |
|
|---|
| 285 |
Word_List_Read |
|---|
| 286 |
@words = $vector->Word_List_Read(); |
|---|
| 287 |
|
|---|
| 288 |
Word_Insert |
|---|
| 289 |
$vector->Word_Insert($offset,$count); |
|---|
| 290 |
|
|---|
| 291 |
Word_Delete |
|---|
| 292 |
$vector->Word_Delete($offset,$count); |
|---|
| 293 |
|
|---|
| 294 |
Chunk_Store |
|---|
| 295 |
$vector->Chunk_Store($chunksize,$offset,$chunk); |
|---|
| 296 |
|
|---|
| 297 |
Chunk_Read |
|---|
| 298 |
$chunk = $vector->Chunk_Read($chunksize,$offset); |
|---|
| 299 |
|
|---|
| 300 |
Chunk_List_Store |
|---|
| 301 |
$vector->Chunk_List_Store($chunksize,@chunks); |
|---|
| 302 |
|
|---|
| 303 |
Chunk_List_Read |
|---|
| 304 |
@chunks = $vector->Chunk_List_Read($chunksize); |
|---|
| 305 |
|
|---|
| 306 |
Index_List_Remove |
|---|
| 307 |
$vector->Index_List_Remove(@indices); |
|---|
| 308 |
|
|---|
| 309 |
Index_List_Store |
|---|
| 310 |
$vector->Index_List_Store(@indices); |
|---|
| 311 |
|
|---|
| 312 |
Index_List_Read |
|---|
| 313 |
@indices = $vector->Index_List_Read(); |
|---|
| 314 |
|
|---|
| 315 |
Or |
|---|
| 316 |
Union |
|---|
| 317 |
$vec3->Or($vec1,$vec2); |
|---|
| 318 |
$set3->Union($set1,$set2); |
|---|
| 319 |
|
|---|
| 320 |
And |
|---|
| 321 |
Intersection |
|---|
| 322 |
$vec3->And($vec1,$vec2); |
|---|
| 323 |
$set3->Intersection($set1,$set2); |
|---|
| 324 |
|
|---|
| 325 |
AndNot |
|---|
| 326 |
Difference |
|---|
| 327 |
$vec3->AndNot($vec1,$vec2); |
|---|
| 328 |
$set3->Difference($set1,$set2); |
|---|
| 329 |
|
|---|
| 330 |
Xor |
|---|
| 331 |
ExclusiveOr |
|---|
| 332 |
$vec3->Xor($vec1,$vec2); |
|---|
| 333 |
$set3->ExclusiveOr($set1,$set2); |
|---|
| 334 |
|
|---|
| 335 |
Not |
|---|
| 336 |
Complement |
|---|
| 337 |
$vec2->Not($vec1); |
|---|
| 338 |
$set2->Complement($set1); |
|---|
| 339 |
|
|---|
| 340 |
subset |
|---|
| 341 |
if ($set1->subset($set2)) # true if $set1 is subset of $set2 |
|---|
| 342 |
|
|---|
| 343 |
Norm |
|---|
| 344 |
$norm = $set->Norm(); |
|---|
| 345 |
|
|---|
| 346 |
Min |
|---|
| 347 |
$min = $set->Min(); |
|---|
| 348 |
|
|---|
| 349 |
Max |
|---|
| 350 |
$max = $set->Max(); |
|---|
| 351 |
|
|---|
| 352 |
Multiplication |
|---|
| 353 |
$matrix3->Multiplication($rows3,$cols3, |
|---|
| 354 |
$matrix1,$rows1,$cols1, |
|---|
| 355 |
$matrix2,$rows2,$cols2); |
|---|
| 356 |
|
|---|
| 357 |
Product |
|---|
| 358 |
$matrix3->Product($rows3,$cols3, |
|---|
| 359 |
$matrix1,$rows1,$cols1, |
|---|
| 360 |
$matrix2,$rows2,$cols2); |
|---|
| 361 |
|
|---|
| 362 |
Closure |
|---|
| 363 |
$matrix->Closure($rows,$cols); |
|---|
| 364 |
|
|---|
| 365 |
Transpose |
|---|
| 366 |
$matrix2->Transpose($rows2,$cols2,$matrix1,$rows1,$cols1); |
|---|
| 367 |
|
|---|
| 368 |
=head1 IMPORTANT NOTES |
|---|
| 369 |
|
|---|
| 370 |
=over 2 |
|---|
| 371 |
|
|---|
| 372 |
=item * |
|---|
| 373 |
|
|---|
| 374 |
Method naming conventions |
|---|
| 375 |
|
|---|
| 376 |
Method names completely in lower case indicate a boolean return value. |
|---|
| 377 |
|
|---|
| 378 |
(Except for the bit vector constructor method "C<new()>", of course.) |
|---|
| 379 |
|
|---|
| 380 |
=item * |
|---|
| 381 |
|
|---|
| 382 |
Boolean values |
|---|
| 383 |
|
|---|
| 384 |
Boolean values in this module are always a numeric zero ("C<0>") for |
|---|
| 385 |
"false" and a numeric one ("C<1>") for "true". |
|---|
| 386 |
|
|---|
| 387 |
=item * |
|---|
| 388 |
|
|---|
| 389 |
Negative numbers |
|---|
| 390 |
|
|---|
| 391 |
All numeric input parameters passed to any of the methods in this module |
|---|
| 392 |
are regarded as being B<UNSIGNED> (as opposed to the contents of the |
|---|
| 393 |
bit vectors themselves, which are usually considered to be B<SIGNED>). |
|---|
| 394 |
|
|---|
| 395 |
As a consequence, whenever you pass a negative number as an argument to |
|---|
| 396 |
some method of this module, it will be treated as a (usually very large) |
|---|
| 397 |
positive number due to its internal two's complement binary representation, |
|---|
| 398 |
usually resulting in an "index out of range" error message and program |
|---|
| 399 |
abortion. |
|---|
| 400 |
|
|---|
| 401 |
=item * |
|---|
| 402 |
|
|---|
| 403 |
Bit order |
|---|
| 404 |
|
|---|
| 405 |
Note that bit vectors are stored least order bit and least order word first |
|---|
| 406 |
internally. |
|---|
| 407 |
|
|---|
| 408 |
I.e., bit #0 of any given bit vector corresponds to bit #0 of word #0 in the |
|---|
| 409 |
array of machine words representing the bit vector. |
|---|
| 410 |
|
|---|
| 411 |
(Where word #0 comes first in memory, i.e., it is stored at the least memory |
|---|
| 412 |
address in the allocated block of memory holding the given bit vector.) |
|---|
| 413 |
|
|---|
| 414 |
Note however that machine words can be stored least order byte first or last, |
|---|
| 415 |
depending on your system's implementation. |
|---|
| 416 |
|
|---|
| 417 |
When you are exporting or importing a whole bit vector at once using the |
|---|
| 418 |
methods "C<Block_Read()>" and "C<Block_Store()>" (the only time in this |
|---|
| 419 |
module where this could make any difference), however, a conversion to and |
|---|
| 420 |
from "least order byte first" order is automatically supplied. |
|---|
| 421 |
|
|---|
| 422 |
In other words, what "C<Block_Read()>" provides and what "C<Block_Store()>" |
|---|
| 423 |
expects is always in "least order byte first" order, regardless of the order |
|---|
| 424 |
in which words are stored internally on your machine. |
|---|
| 425 |
|
|---|
| 426 |
This is to make sure that what you export on one machine using "C<Block_Read()>" |
|---|
| 427 |
can always be read in correctly with "C<Block_Store()>" on a different machine. |
|---|
| 428 |
|
|---|
| 429 |
Note further that whenever bit vectors are converted to and from (binary or |
|---|
| 430 |
hexadecimal) strings, the B<RIGHTMOST> bit is always the B<LEAST SIGNIFICANT> |
|---|
| 431 |
one, and the B<LEFTMOST> bit is always the B<MOST SIGNIFICANT> bit. |
|---|
| 432 |
|
|---|
| 433 |
This is because in our western culture, numbers are always represented in this |
|---|
| 434 |
way (least significant to most significant digits go from right to left). |
|---|
| 435 |
|
|---|
| 436 |
Of course this requires an internal reversion of order, which the corresponding |
|---|
| 437 |
conversion methods perform automatically (without any additional overhead, it's |
|---|
| 438 |
just a matter of starting the internal loop at the bottom or the top end). |
|---|
| 439 |
|
|---|
| 440 |
=item * |
|---|
| 441 |
|
|---|
| 442 |
"Word" related methods |
|---|
| 443 |
|
|---|
| 444 |
Note that all methods whose names begin with "C<Word_>" are |
|---|
| 445 |
B<MACHINE-DEPENDENT>! |
|---|
| 446 |
|
|---|
| 447 |
They depend on the size (number of bits) of an "unsigned int" (C type) on |
|---|
| 448 |
your machine. |
|---|
| 449 |
|
|---|
| 450 |
Therefore, you should only use these methods if you are B<ABSOLUTELY CERTAIN> |
|---|
| 451 |
that portability of your code is not an issue! |
|---|
| 452 |
|
|---|
| 453 |
Note that you can use arbitrarily large chunks (i.e., fragments of bit vectors) |
|---|
| 454 |
of up to 32 bits B<IN A PORTABLE WAY> using the methods whose names begin with |
|---|
| 455 |
"C<Chunk_>". |
|---|
| 456 |
|
|---|
| 457 |
=item * |
|---|
| 458 |
|
|---|
| 459 |
Chunk sizes |
|---|
| 460 |
|
|---|
| 461 |
Note that legal chunk sizes for all methods whose names begin with "C<Chunk_>" |
|---|
| 462 |
range from "C<1>" to "C<Bit::Vector-E<gt>Long_Bits();>" bits ("C<0>" is B<NOT> |
|---|
| 463 |
allowed!). |
|---|
| 464 |
|
|---|
| 465 |
In order to make your programs portable, however, you shouldn't use chunk sizes |
|---|
| 466 |
larger than 32 bits, since this is the minimum size of an "unsigned long" |
|---|
| 467 |
(C type) on all systems, as prescribed by S<ANSI C>. |
|---|
| 468 |
|
|---|
| 469 |
=item * |
|---|
| 470 |
|
|---|
| 471 |
Matching sizes |
|---|
| 472 |
|
|---|
| 473 |
In general, for methods involving several bit vectors at the same time, all |
|---|
| 474 |
bit vector arguments must have identical sizes (number of bits), or a fatal |
|---|
| 475 |
"size mismatch" error will occur. |
|---|
| 476 |
|
|---|
| 477 |
Exceptions from this rule are the methods "C<Concat()>", "C<Concat_List()>", |
|---|
| 478 |
"C<Copy()>", "C<Interval_Copy()>" and "C<Interval_Substitute()>", where no |
|---|
| 479 |
conditions at all are imposed on the size of their bit vector arguments. |
|---|
| 480 |
|
|---|
| 481 |
In method "C<Multiply()>", all three bit vector arguments must in principle |
|---|
| 482 |
obey the rule of matching sizes, but the bit vector in which the result of |
|---|
| 483 |
the multiplication is to be stored may be larger than the two bit vector |
|---|
| 484 |
arguments containing the factors for the multiplication. |
|---|
| 485 |
|
|---|
| 486 |
In method "C<Power()>", the bit vector for the result must be the same |
|---|
| 487 |
size or greater than the base of the exponentiation term. The exponent |
|---|
| 488 |
can be any size. |
|---|
| 489 |
|
|---|
| 490 |
=item * |
|---|
| 491 |
|
|---|
| 492 |
Index ranges |
|---|
| 493 |
|
|---|
| 494 |
All indices for any given bits must lie between "C<0>" and |
|---|
| 495 |
"C<$vector-E<gt>Size()-1>", or a fatal "index out of range" |
|---|
| 496 |
error will occur. |
|---|
| 497 |
|
|---|
| 498 |
=back |
|---|
| 499 |
|
|---|
| 500 |
=head1 DESCRIPTION |
|---|
| 501 |
|
|---|
| 502 |
=head2 OVERLOADED OPERATORS |
|---|
| 503 |
|
|---|
| 504 |
See L<Bit::Vector::Overload(3)>. |
|---|
| 505 |
|
|---|
| 506 |
=head2 CLASS METHODS |
|---|
| 507 |
|
|---|
| 508 |
=over 2 |
|---|
| 509 |
|
|---|
| 510 |
=item * |
|---|
| 511 |
|
|---|
| 512 |
C<$version = Bit::Vector-E<gt>Version();> |
|---|
| 513 |
|
|---|
| 514 |
Returns the current version number of this module. |
|---|
| 515 |
|
|---|
| 516 |
=item * |
|---|
| 517 |
|
|---|
| 518 |
C<$bits = Bit::Vector-E<gt>Word_Bits();> |
|---|
| 519 |
|
|---|
| 520 |
Returns the number of bits of an "unsigned int" (C type) |
|---|
| 521 |
on your machine. |
|---|
| 522 |
|
|---|
| 523 |
(An "unsigned int" is also called a "machine word", |
|---|
| 524 |
hence the name of this method.) |
|---|
| 525 |
|
|---|
| 526 |
=item * |
|---|
| 527 |
|
|---|
| 528 |
C<$bits = Bit::Vector-E<gt>Long_Bits();> |
|---|
| 529 |
|
|---|
| 530 |
Returns the number of bits of an "unsigned long" (C type) |
|---|
| 531 |
on your machine. |
|---|
| 532 |
|
|---|
| 533 |
=item * |
|---|
| 534 |
|
|---|
| 535 |
C<$vector = Bit::Vector-E<gt>new($bits);> |
|---|
| 536 |
|
|---|
| 537 |
This is the bit vector constructor method. |
|---|
| 538 |
|
|---|
| 539 |
Call this method to create a new bit vector containing "C<$bits>" |
|---|
| 540 |
bits (with indices ranging from "C<0>" to "C<$bits-1>"). |
|---|
| 541 |
|
|---|
| 542 |
Note that - in contrast to previous versions - bit vectors |
|---|
| 543 |
of length zero (i.e., with C<$bits = 0>) are permitted now. |
|---|
| 544 |
|
|---|
| 545 |
The method returns a reference to the newly created bit vector. |
|---|
| 546 |
|
|---|
| 547 |
A new bit vector is always initialized so that all bits are cleared |
|---|
| 548 |
(turned off). |
|---|
| 549 |
|
|---|
| 550 |
An exception will be raised if the method is unable to allocate the |
|---|
| 551 |
necessary memory. |
|---|
| 552 |
|
|---|
| 553 |
Note that if you specify a negative number for "C<$bits>" it will be |
|---|
| 554 |
interpreted as a large positive number due to its internal two's |
|---|
| 555 |
complement binary representation. |
|---|
| 556 |
|
|---|
| 557 |
In such a case, the bit vector constructor method will obediently attempt |
|---|
| 558 |
to create a bit vector of that size, probably resulting in an exception, |
|---|
| 559 |
as explained above. |
|---|
| 560 |
|
|---|
| 561 |
=item * |
|---|
| 562 |
|
|---|
| 563 |
C<@veclist = Bit::Vector-E<gt>new($bits,$count);> |
|---|
| 564 |
|
|---|
| 565 |
You can also create more than one bit vector at a time if you specify the |
|---|
| 566 |
optional second parameter "C<$count>". |
|---|
| 567 |
|
|---|
| 568 |
The method returns a list of "C<$count>" bit vectors which all have the |
|---|
| 569 |
same number of bits "C<$bits>" (and which are all initialized, i.e., |
|---|
| 570 |
all bits are cleared). |
|---|
| 571 |
|
|---|
| 572 |
If "C<$count>" is zero, an empty list is returned. |
|---|
| 573 |
|
|---|
| 574 |
If "C<$bits>" is zero, a list of null-sized bit vectors is returned. |
|---|
| 575 |
|
|---|
| 576 |
Note again that if you specify a negative number for "C<$count>" it will |
|---|
| 577 |
be interpreted as a large positive number due to its internal two's |
|---|
| 578 |
complement binary representation. |
|---|
| 579 |
|
|---|
| 580 |
In such a case, the bit vector constructor method will obediently attempt |
|---|
| 581 |
to create that many bit vectors, probably resulting in an exception ("out |
|---|
| 582 |
of memory"). |
|---|
| 583 |
|
|---|
| 584 |
=item * |
|---|
| 585 |
|
|---|
| 586 |
C<$vector = Bit::Vector-E<gt>new_Hex($bits,$string);> |
|---|
| 587 |
|
|---|
| 588 |
This method is an alternative constructor which allows you to create |
|---|
| 589 |
a new bit vector object (with "C<$bits>" bits) and to initialize it |
|---|
| 590 |
all in one go. |
|---|
| 591 |
|
|---|
| 592 |
The method is more efficient than performing these two steps separately |
|---|
| 593 |
first because in this method, the memory area occupied by the new bit |
|---|
| 594 |
vector is not initialized to zeros (which is pointless in this case), |
|---|
| 595 |
and second because it saves you from the associated overhead of one |
|---|
| 596 |
additional method invocation. |
|---|
| 597 |
|
|---|
| 598 |
The method first calls the bit vector constructor method "C<new()>" |
|---|
| 599 |
internally, and then passes the given string to the method "C<from_Hex()>". |
|---|
| 600 |
|
|---|
| 601 |
An exception will be raised if the necessary memory cannot be allocated |
|---|
| 602 |
(see the description of the method "C<new()>" immediately above for |
|---|
| 603 |
possible causes) or if the given string cannot be converted successfully |
|---|
| 604 |
(see the description of the method "C<from_Hex()>" further below for |
|---|
| 605 |
details). |
|---|
| 606 |
|
|---|
| 607 |
In the latter case, the memory occupied by the new bit vector is |
|---|
| 608 |
released first (i.e., "free"d) before the exception is actually |
|---|
| 609 |
raised. |
|---|
| 610 |
|
|---|
| 611 |
=item * |
|---|
| 612 |
|
|---|
| 613 |
C<$vector = Bit::Vector-E<gt>new_Bin($bits,$string);> |
|---|
| 614 |
|
|---|
| 615 |
This method is an alternative constructor which allows you to create |
|---|
| 616 |
a new bit vector object (with "C<$bits>" bits) and to initialize it |
|---|
| 617 |
all in one go. |
|---|
| 618 |
|
|---|
| 619 |
The method is more efficient than performing these two steps separately |
|---|
| 620 |
first because in this method, the memory area occupied by the new bit |
|---|
| 621 |
vector is not initialized to zeros (which is pointless in this case), |
|---|
| 622 |
and second because it saves you from the associated overhead of one |
|---|
| 623 |
additional method invocation. |
|---|
| 624 |
|
|---|
| 625 |
The method first calls the bit vector constructor method "C<new()>" |
|---|
| 626 |
internally, and then passes the given string to the method "C<from_Bin()>". |
|---|
| 627 |
|
|---|
| 628 |
An exception will be raised if the necessary memory cannot be allocated |
|---|
| 629 |
(see the description of the method "C<new()>" above for possible causes) |
|---|
| 630 |
or if the given string cannot be converted successfully (see the |
|---|
| 631 |
description of the method "C<from_Bin()>" further below for details). |
|---|
| 632 |
|
|---|
| 633 |
In the latter case, the memory occupied by the new bit vector is |
|---|
| 634 |
released first (i.e., "free"d) before the exception is actually |
|---|
| 635 |
raised. |
|---|
| 636 |
|
|---|
| 637 |
=item * |
|---|
| 638 |
|
|---|
| 639 |
C<$vector = Bit::Vector-E<gt>new_Dec($bits,$string);> |
|---|
| 640 |
|
|---|
| 641 |
This method is an alternative constructor which allows you to create |
|---|
| 642 |
a new bit vector object (with "C<$bits>" bits) and to initialize it |
|---|
| 643 |
all in one go. |
|---|
| 644 |
|
|---|
| 645 |
The method is more efficient than performing these two steps separately |
|---|
| 646 |
first because in this method, the memory area occupied by the new bit |
|---|
| 647 |
vector is not initialized to zeros (which is pointless in this case), |
|---|
| 648 |
and second because it saves you from the associated overhead of one |
|---|
| 649 |
additional method invocation. |
|---|
| 650 |
|
|---|
| 651 |
The method first calls the bit vector constructor method "C<new()>" |
|---|
| 652 |
internally, and then passes the given string to the method "C<from_Dec()>". |
|---|
| 653 |
|
|---|
| 654 |
An exception will be raised if the necessary memory cannot be allocated |
|---|
| 655 |
(see the description of the method "C<new()>" above for possible causes) |
|---|
| 656 |
or if the given string cannot be converted successfully (see the |
|---|
| 657 |
description of the method "C<from_Dec()>" further below for details). |
|---|
| 658 |
|
|---|
| 659 |
In the latter case, the memory occupied by the new bit vector is |
|---|
| 660 |
released first (i.e., "free"d) before the exception is actually |
|---|
| 661 |
raised. |
|---|
| 662 |
|
|---|
| 663 |
=item * |
|---|
| 664 |
|
|---|
| 665 |
C<$vector = Bit::Vector-E<gt>new_Enum($bits,$string);> |
|---|
| 666 |
|
|---|
| 667 |
This method is an alternative constructor which allows you to create |
|---|
| 668 |
a new bit vector object (with "C<$bits>" bits) and to initialize it |
|---|
| 669 |
all in one go. |
|---|
| 670 |
|
|---|
| 671 |
The method is more efficient than performing these two steps separately |
|---|
| 672 |
first because in this method, the memory area occupied by the new bit |
|---|
| 673 |
vector is not initialized to zeros (which is pointless in this case), |
|---|
| 674 |
and second because it saves you from the associated overhead of one |
|---|
| 675 |
additional method invocation. |
|---|
| 676 |
|
|---|
| 677 |
The method first calls the bit vector constructor method "C<new()>" |
|---|
| 678 |
internally, and then passes the given string to the method "C<from_Enum()>". |
|---|
| 679 |
|
|---|
| 680 |
An exception will be raised if the necessary memory cannot be allocated |
|---|
| 681 |
(see the description of the method "C<new()>" above for possible causes) |
|---|
| 682 |
or if the given string cannot be converted successfully (see the |
|---|
| 683 |
description of the method "C<from_Enum()>" further below for details). |
|---|
| 684 |
|
|---|
| 685 |
In the latter case, the memory occupied by the new bit vector is |
|---|
| 686 |
released first (i.e., "free"d) before the exception is actually |
|---|
| 687 |
raised. |
|---|
| 688 |
|
|---|
| 689 |
=item * |
|---|
| 690 |
|
|---|
| 691 |
C<$vector = Bit::Vector-E<gt>Concat_List(@vectors);> |
|---|
| 692 |
|
|---|
| 693 |
This method creates a new vector containing all bit vectors from the |
|---|
| 694 |
argument list in concatenated form. |
|---|
| 695 |
|
|---|
| 696 |
The argument list may contain any number of arguments (including |
|---|
| 697 |
zero); the only condition is that all arguments must be bit vectors. |
|---|
| 698 |
|
|---|
| 699 |
There is no condition concerning the length (in number of bits) of |
|---|
| 700 |
these arguments. |
|---|
| 701 |
|
|---|
| 702 |
The vectors from the argument list are not changed in any way. |
|---|
| 703 |
|
|---|
| 704 |
If the argument list is empty or if all arguments have length zero, |
|---|
| 705 |
the resulting bit vector will also have length zero. |
|---|
| 706 |
|
|---|
| 707 |
Note that the B<RIGHTMOST> bit vector from the argument list will |
|---|
| 708 |
become the B<LEAST> significant part of the resulting bit vector, |
|---|
| 709 |
and the B<LEFTMOST> bit vector from the argument list will |
|---|
| 710 |
become the B<MOST> significant part of the resulting bit vector. |
|---|
| 711 |
|
|---|
| 712 |
=back |
|---|
| 713 |
|
|---|
| 714 |
=head2 OBJECT METHODS |
|---|
| 715 |
|
|---|
| 716 |
=over 2 |
|---|
| 717 |
|
|---|
| 718 |
=item * |
|---|
| 719 |
|
|---|
| 720 |
C<$vec2 = $vec1-E<gt>new($bits);> |
|---|
| 721 |
|
|---|
| 722 |
C<@veclist = $vec-E<gt>new($bits);> |
|---|
| 723 |
|
|---|
| 724 |
This is an alternative way of calling the bit vector constructor method. |
|---|
| 725 |
|
|---|
| 726 |
Vector "C<$vec1>" (or "C<$vec>") is not affected by this, it just serves |
|---|
| 727 |
as an anchor for the method invocation mechanism. |
|---|
| 728 |
|
|---|
| 729 |
In fact B<ALL> class methods in this module can be called this way, |
|---|
| 730 |
even though this is probably considered to be "politically incorrect" |
|---|
| 731 |
by OO ("object-orientation") aficionados. ;-) |
|---|
| 732 |
|
|---|
| 733 |
So even if you are too lazy to type "C<Bit::Vector-E<gt>>" instead of |
|---|
| 734 |
"C<$vec1-E<gt>>" (and even though laziness is - allegedly - a programmer's |
|---|
| 735 |
virtue C<:-)>), maybe it is better not to use this feature if you don't |
|---|
| 736 |
want to get booed at. ;-) |
|---|
| 737 |
|
|---|
| 738 |
=item * |
|---|
| 739 |
|
|---|
| 740 |
C<$vec2 = $vec1-E<gt>Shadow();> |
|---|
| 741 |
|
|---|
| 742 |
Creates a B<NEW> bit vector "C<$vec2>" of the B<SAME SIZE> as "C<$vec1>" |
|---|
| 743 |
but which is B<EMPTY>. |
|---|
| 744 |
|
|---|
| 745 |
Just like a shadow that has the same shape as the object it |
|---|
| 746 |
originates from, but is flat and has no volume, i.e., contains |
|---|
| 747 |
nothing. |
|---|
| 748 |
|
|---|
| 749 |
=item * |
|---|
| 750 |
|
|---|
| 751 |
C<$vec2 = $vec1-E<gt>Clone();> |
|---|
| 752 |
|
|---|
| 753 |
Creates a B<NEW> bit vector "C<$vec2>" of the B<SAME SIZE> as "C<$vec1>" |
|---|
| 754 |
which is an B<EXACT COPY> of "C<$vec1>". |
|---|
| 755 |
|
|---|
| 756 |
=item * |
|---|
| 757 |
|
|---|
| 758 |
C<$vector = $vec1-E<gt>Concat($vec2);> |
|---|
| 759 |
|
|---|
| 760 |
This method returns a new bit vector object which is the result of the |
|---|
| 761 |
concatenation of the contents of "C<$vec1>" and "C<$vec2>". |
|---|
| 762 |
|
|---|
| 763 |
Note that the contents of "C<$vec1>" become the B<MOST> significant part |
|---|
| 764 |
of the resulting bit vector, and "C<$vec2>" the B<LEAST> significant part. |
|---|
| 765 |
|
|---|
| 766 |
If both bit vector arguments have length zero, the resulting bit vector |
|---|
| 767 |
will also have length zero. |
|---|
| 768 |
|
|---|
| 769 |
=item * |
|---|
| 770 |
|
|---|
| 771 |
C<$vector = $vec1-E<gt>Concat_List($vec2,$vec3,...);> |
|---|
| 772 |
|
|---|
| 773 |
This is an alternative way of calling this (class) method as an |
|---|
| 774 |
object method. |
|---|
| 775 |
|
|---|
| 776 |
The method returns a new bit vector object which is the result of |
|---|
| 777 |
the concatenation of the contents of C<$vec1 . $vec2 . $vec3 . ...> |
|---|
| 778 |
|
|---|
| 779 |
See the section "class methods" above for a detailed description of |
|---|
| 780 |
this method. |
|---|
| 781 |
|
|---|
| 782 |
Note that the argument list may be empty and that all arguments |
|---|
| 783 |
must be bit vectors if it isn't. |
|---|
| 784 |
|
|---|
| 785 |
=item * |
|---|
| 786 |
|
|---|
| 787 |
C<$bits = $vector-E<gt>Size();> |
|---|
| 788 |
|
|---|
| 789 |
Returns the size (number of bits) the given vector was created with |
|---|
| 790 |
(or "C<Resize()>"d to). |
|---|
| 791 |
|
|---|
| 792 |
=item * |
|---|
| 793 |
|
|---|
| 794 |
C<$vector-E<gt>Resize($bits);> |
|---|
| 795 |
|
|---|
| 796 |
Changes the size of the given vector to the specified number of bits. |
|---|
| 797 |
|
|---|
| 798 |
This method allows you to change the size of an existing bit vector, |
|---|
| 799 |
preserving as many bits from the old vector as will fit into the |
|---|
| 800 |
new one (i.e., all bits with indices smaller than the minimum of the |
|---|
| 801 |
sizes of both vectors, old and new). |
|---|
| 802 |
|
|---|
| 803 |
If the number of machine words needed to store the new vector is smaller |
|---|
| 804 |
than or equal to the number of words needed to store the old vector, the |
|---|
| 805 |
memory allocated for the old vector is reused for the new one, and only |
|---|
| 806 |
the relevant book-keeping information is adjusted accordingly. |
|---|
| 807 |
|
|---|
| 808 |
This means that even if the number of bits increases, new memory is not |
|---|
| 809 |
necessarily being allocated (i.e., if the old and the new number of bits |
|---|
| 810 |
fit into the same number of machine words). |
|---|
| 811 |
|
|---|
| 812 |
If the number of machine words needed to store the new vector is greater |
|---|
| 813 |
than the number of words needed to store the old vector, new memory is |
|---|
| 814 |
allocated for the new vector, the old vector is copied to the new one, |
|---|
| 815 |
the remaining bits in the new vector are cleared (turned off) and the old |
|---|
| 816 |
vector is deleted, i.e., the memory that was allocated for it is released. |
|---|
| 817 |
|
|---|
| 818 |
(An exception will be raised if the method is unable to allocate the |
|---|
| 819 |
necessary memory for the new vector.) |
|---|
| 820 |
|
|---|
| 821 |
As a consequence, if you decrease the size of a given vector so that |
|---|
| 822 |
it will use fewer machine words, and increase it again later so that it |
|---|
| 823 |
will use more words than immediately before but still less than the |
|---|
| 824 |
original vector, new memory will be allocated anyway because the |
|---|
| 825 |
information about the size of the original vector is lost whenever |
|---|
| 826 |
you resize it. |
|---|
| 827 |
|
|---|
| 828 |
Note also that if you specify a negative number for "C<$bits>" it will |
|---|
| 829 |
be interpreted as a large positive number due to its internal two's |
|---|
| 830 |
complement binary representation. |
|---|
| 831 |
|
|---|
| 832 |
In such a case, "Resize()" will obediently attempt to create a bit |
|---|
| 833 |
vector of that size, probably resulting in an exception, as explained |
|---|
| 834 |
above. |
|---|
| 835 |
|
|---|
| 836 |
Finally, note that - in contrast to previous versions - resizing a bit |
|---|
| 837 |
vector to a size of zero bits (length zero) is now permitted. |
|---|
| 838 |
|
|---|
| 839 |
=item * |
|---|
| 840 |
|
|---|
| 841 |
C<$vec2-E<gt>Copy($vec1);> |
|---|
| 842 |
|
|---|
| 843 |
Copies the contents of bit vector "C<$vec1>" to bit vector "C<$vec2>". |
|---|
| 844 |
|
|---|
| 845 |
The previous contents of bit vector "C<$vec2>" get overwritten, i.e., |
|---|
| 846 |
are lost. |
|---|
| 847 |
|
|---|
| 848 |
Both vectors must exist beforehand, i.e., this method does not B<CREATE> |
|---|
| 849 |
any new bit vector object. |
|---|
| 850 |
|
|---|
| 851 |
The two vectors may be of any size. |
|---|
| 852 |
|
|---|
| 853 |
If the source bit vector is larger than the target, this method will copy |
|---|
| 854 |
as much of the least significant bits of the source vector as will fit into |
|---|
| 855 |
the target vector, thereby discarding any extraneous most significant bits. |
|---|
| 856 |
|
|---|
| 857 |
BEWARE that this causes a brutal cutoff in the middle of your data, and it |
|---|
| 858 |
will also leave you with an almost unpredictable sign if subsequently the |
|---|
| 859 |
number in the target vector is going to be interpreted as a number! (You |
|---|
| 860 |
have been warned!) |
|---|
| 861 |
|
|---|
| 862 |
If the target bit vector is larger than the source, this method fills up |
|---|
| 863 |
the remaining most significant bits in the target bit vector with either |
|---|
| 864 |
0's or 1's, depending on the sign (= the most significant bit) of the |
|---|
| 865 |
source bit vector. This is also known as "sign extension". |
|---|
| 866 |
|
|---|
| 867 |
This makes it possible to copy numbers from a smaller bit vector into |
|---|
| 868 |
a larger one while preserving the number's absolute value as well as |
|---|
| 869 |
its sign (due to the two's complement binary representation of numbers). |
|---|
| 870 |
|
|---|
| 871 |
=item * |
|---|
| 872 |
|
|---|
| 873 |
C<$vector-E<gt>Empty();> |
|---|
| 874 |
|
|---|
| 875 |
Clears all bits in the given vector. |
|---|
| 876 |
|
|---|
| 877 |
=item * |
|---|
| 878 |
|
|---|
| 879 |
C<$vector-E<gt>Fill();> |
|---|
| 880 |
|
|---|
| 881 |
Sets all bits in the given vector. |
|---|
| 882 |
|
|---|
| 883 |
=item * |
|---|
| 884 |
|
|---|
| 885 |
C<$vector-E<gt>Flip();> |
|---|
| 886 |
|
|---|
| 887 |
Flips (i.e., complements) all bits in the given vector. |
|---|
| 888 |
|
|---|
| 889 |
=item * |
|---|
| 890 |
|
|---|
| 891 |
C<$vector-E<gt>Primes();> |
|---|
| 892 |
|
|---|
| 893 |
Clears the given bit vector and sets all bits whose |
|---|
| 894 |
indices are prime numbers. |
|---|
| 895 |
|
|---|
| 896 |
This method uses the algorithm known as the "Sieve of |
|---|
| 897 |
Erathostenes" internally. |
|---|
| 898 |
|
|---|
| 899 |
=item * |
|---|
| 900 |
|
|---|
| 901 |
C<$vec2-E<gt>Reverse($vec1);> |
|---|
| 902 |
|
|---|
| 903 |
This method copies the given vector "C<$vec1>" to |
|---|
| 904 |
the vector "C<$vec2>", thereby reversing the order |
|---|
| 905 |
of all bits. |
|---|
| 906 |
|
|---|
| 907 |
I.e., the least significant bit of "C<$vec1>" becomes the |
|---|
| 908 |
most significant bit of "C<$vec2>", whereas the most |
|---|
| 909 |
significant bit of "C<$vec1>" becomes the least |
|---|
| 910 |
significant bit of "C<$vec2>", and so forth |
|---|
| 911 |
for all bits in between. |
|---|
| 912 |
|
|---|
| 913 |
Note that in-place processing is also possible, i.e., |
|---|
| 914 |
"C<$vec1>" and "C<$vec2>" may be identical. |
|---|
| 915 |
|
|---|
| 916 |
(Internally, this is the same as |
|---|
| 917 |
C<$vec1-E<gt>Interval_Reverse(0,$vec1-E<gt>Size()-1);>.) |
|---|
| 918 |
|
|---|
| 919 |
=item * |
|---|
| 920 |
|
|---|
| 921 |
C<$vector-E<gt>Interval_Empty($min,$max);> |
|---|
| 922 |
|
|---|
| 923 |
Clears all bits in the interval C<[$min..$max]> (including both limits) |
|---|
| 924 |
in the given vector. |
|---|
| 925 |
|
|---|
| 926 |
"C<$min>" and "C<$max>" may have the same value; this is the same |
|---|
| 927 |
as clearing a single bit with "C<Bit_Off()>" (but less efficient). |
|---|
| 928 |
|
|---|
| 929 |
Note that C<$vector-E<gt>Interval_Empty(0,$vector-E<gt>Size()-1);> |
|---|
| 930 |
is the same as C<$vector-E<gt>Empty();> (but less efficient). |
|---|
| 931 |
|
|---|
| 932 |
=item * |
|---|
| 933 |
|
|---|
| 934 |
C<$vector-E<gt>Interval_Fill($min,$max);> |
|---|
| 935 |
|
|---|
| 936 |
Sets all bits in the interval C<[$min..$max]> (including both limits) |
|---|
| 937 |
in the given vector. |
|---|
| 938 |
|
|---|
| 939 |
"C<$min>" and "C<$max>" may have the same value; this is the same |
|---|
| 940 |
as setting a single bit with "C<Bit_On()>" (but less efficient). |
|---|
| 941 |
|
|---|
| 942 |
Note that C<$vector-E<gt>Interval_Fill(0,$vector-E<gt>Size()-1);> |
|---|
| 943 |
is the same as C<$vector-E<gt>Fill();> (but less efficient). |
|---|
| 944 |
|
|---|
| 945 |
=item * |
|---|
| 946 |
|
|---|
| 947 |
C<$vector-E<gt>Interval_Flip($min,$max);> |
|---|
| 948 |
|
|---|
| 949 |
Flips (i.e., complements) all bits in the interval C<[$min..$max]> |
|---|
| 950 |
(including both limits) in the given vector. |
|---|
| 951 |
|
|---|
| 952 |
"C<$min>" and "C<$max>" may have the same value; this is the same |
|---|
| 953 |
as flipping a single bit with "C<bit_flip()>" (but less efficient). |
|---|
| 954 |
|
|---|
| 955 |
Note that C<$vector-E<gt>Interval_Flip(0,$vector-E<gt>Size()-1);> |
|---|
| 956 |
is the same as C<$vector-E<gt>Flip();> and |
|---|
| 957 |
C<$vector-E<gt>Complement($vector);> |
|---|
| 958 |
(but less efficient). |
|---|
| 959 |
|
|---|
| 960 |
=item * |
|---|
| 961 |
|
|---|
| 962 |
C<$vector-E<gt>Interval_Reverse($min,$max);> |
|---|
| 963 |
|
|---|
| 964 |
Reverses the order of all bits in the interval C<[$min..$max]> |
|---|
| 965 |
(including both limits) in the given vector. |
|---|
| 966 |
|
|---|
| 967 |
I.e., bits "C<$min>" and "C<$max>" swap places, and so forth |
|---|
| 968 |
for all bits in between. |
|---|
| 969 |
|
|---|
| 970 |
"C<$min>" and "C<$max>" may have the same value; this has no |
|---|
| 971 |
effect whatsoever, though. |
|---|
| 972 |
|
|---|
| 973 |
=item * |
|---|
| 974 |
|
|---|
| 975 |
C<if (($min,$max) = $vector-E<gt>Interval_Scan_inc($start))> |
|---|
| 976 |
|
|---|
| 977 |
Returns the minimum and maximum indices of the next contiguous block |
|---|
| 978 |
of set bits (i.e., bits in the "on" state). |
|---|
| 979 |
|
|---|
| 980 |
The search starts at index "C<$start>" (i.e., C<"$min" E<gt>= "$start">) |
|---|
| 981 |
and proceeds upwards (i.e., C<"$max" E<gt>= "$min">), thus repeatedly |
|---|
| 982 |
increments the search pointer "C<$start>" (internally). |
|---|
| 983 |
|
|---|
| 984 |
Note though that the contents of the variable (or scalar literal value) |
|---|
| 985 |
"C<$start>" is B<NOT> altered. I.e., you have to set it to the desired |
|---|
| 986 |
value yourself prior to each call to "C<Interval_Scan_inc()>" (see also |
|---|
| 987 |
the example given below). |
|---|
| 988 |
|
|---|
| 989 |
Actually, the bit vector is not searched bit by bit, but one machine |
|---|
| 990 |
word at a time, in order to speed up execution (which means that this |
|---|
| 991 |
method is quite efficient). |
|---|
| 992 |
|
|---|
| 993 |
An empty list is returned if no such block can be found. |
|---|
| 994 |
|
|---|
| 995 |
Note that a single set bit (surrounded by cleared bits) is a valid |
|---|
| 996 |
block by this definition. In that case the return values for "C<$min>" |
|---|
| 997 |
and "C<$max>" are the same. |
|---|
| 998 |
|
|---|
| 999 |
Typical use: |
|---|
| 1000 |
|
|---|
| 1001 |
$start = 0; |
|---|
| 1002 |
while (($start < $vector->Size()) && |
|---|
| 1003 |
(($min,$max) = $vector->Interval_Scan_inc($start))) |
|---|
| 1004 |
{ |
|---|
| 1005 |
$start = $max + 2; |
|---|
| 1006 |
|
|---|
| 1007 |
# do something with $min and $max |
|---|
| 1008 |
} |
|---|
| 1009 |
|
|---|
| 1010 |
=item * |
|---|
| 1011 |
|
|---|
| 1012 |
C<if (($min,$max) = $vector-E<gt>Interval_Scan_dec($start))> |
|---|
| 1013 |
|
|---|
| 1014 |
Returns the minimum and maximum indices of the next contiguous block |
|---|
| 1015 |
of set bits (i.e., bits in the "on" state). |
|---|
| 1016 |
|
|---|
| 1017 |
The search starts at index "C<$start>" (i.e., C<"$max" E<lt>= "$start">) |
|---|
| 1018 |
and proceeds downwards (i.e., C<"$min" E<lt>= "$max">), thus repeatedly |
|---|
| 1019 |
decrements the search pointer "C<$start>" (internally). |
|---|
| 1020 |
|
|---|
| 1021 |
Note though that the contents of the variable (or scalar literal value) |
|---|
| 1022 |
"C<$start>" is B<NOT> altered. I.e., you have to set it to the desired |
|---|
| 1023 |
value yourself prior to each call to "C<Interval_Scan_dec()>" (see also |
|---|
| 1024 |
the example given below). |
|---|
| 1025 |
|
|---|
| 1026 |
Actually, the bit vector is not searched bit by bit, but one machine |
|---|
| 1027 |
word at a time, in order to speed up execution (which means that this |
|---|
| 1028 |
method is quite efficient). |
|---|
| 1029 |
|
|---|
| 1030 |
An empty list is returned if no such block can be found. |
|---|
| 1031 |
|
|---|
| 1032 |
Note that a single set bit (surrounded by cleared bits) is a valid |
|---|
| 1033 |
block by this definition. In that case the return values for "C<$min>" |
|---|
| 1034 |
and "C<$max>" are the same. |
|---|
| 1035 |
|
|---|
| 1036 |
Typical use: |
|---|
| 1037 |
|
|---|
| 1038 |
$start = $vector->Size() - 1; |
|---|
| 1039 |
while (($start >= 0) && |
|---|
| 1040 |
(($min,$max) = $vector->Interval_Scan_dec($start))) |
|---|
| 1041 |
{ |
|---|
| 1042 |
$start = $min - 2; |
|---|
| 1043 |
|
|---|
| 1044 |
# do something with $min and $max |
|---|
| 1045 |
} |
|---|
| 1046 |
|
|---|
| 1047 |
=item * |
|---|
| 1048 |
|
|---|
| 1049 |
C<$vec2-E<gt>Interval_Copy($vec1,$offset2,$offset1,$length);> |
|---|
| 1050 |
|
|---|
| 1051 |
This method allows you to copy a stretch of contiguous bits (starting |
|---|
| 1052 |
at any position "C<$offset1>" you choose, with a length of "C<$length>" |
|---|
| 1053 |
bits) from a given "source" bit vector "C<$vec1>" to another position |
|---|
| 1054 |
"C<$offset2>" in a "target" bit vector "C<$vec2>". |
|---|
| 1055 |
|
|---|
| 1056 |
Note that the two bit vectors "C<$vec1>" and "C<$vec2>" do B<NOT> |
|---|
| 1057 |
need to have the same (matching) size! |
|---|
| 1058 |
|
|---|
| 1059 |
Consequently, any of the two terms "C<$offset1 + $length>" and |
|---|
| 1060 |
"C<$offset2 + $length>" (or both) may exceed the actual length |
|---|
| 1061 |
of its corresponding bit vector ("C<$vec1-E<gt>Size()>" and |
|---|
| 1062 |
"C<$vec2-E<gt>Size()>", respectively). |
|---|
| 1063 |
|
|---|
| 1064 |
In such a case, the "C<$length>" parameter is automatically reduced |
|---|
| 1065 |
internally so that both terms above are bounded by the number of bits |
|---|
| 1066 |
of their corresponding bit vector. |
|---|
| 1067 |
|
|---|
| 1068 |
This may even result in a length of zero, in which case nothing is |
|---|
| 1069 |
copied at all. |
|---|
| 1070 |
|
|---|
| 1071 |
(Of course the value of the "C<$length>" parameter, supplied by you |
|---|
| 1072 |
in the initial method call, may also be zero right from the start!) |
|---|
| 1073 |
|
|---|
| 1074 |
Note also that "C<$offset1>" and "C<$offset2>" must lie within the |
|---|
| 1075 |
range "C<0>" and, respectively, "C<$vec1-E<gt>Size()-1>" or |
|---|
| 1076 |
"C<$vec2-E<gt>Size()-1>", or a fatal "offset out of range" error |
|---|
| 1077 |
will occur. |
|---|
| 1078 |
|
|---|
| 1079 |
Note further that "C<$vec1>" and "C<$vec2>" may be identical, i.e., |
|---|
| 1080 |
you may copy a stretch of contiguous bits from one part of a given |
|---|
| 1081 |
bit vector to another part. |
|---|
| 1082 |
|
|---|
| 1083 |
The source and the target interval may even overlap, in which case |
|---|
| 1084 |
the copying is automatically performed in ascending or descending |
|---|
| 1085 |
order (depending on the direction of the copy - downwards or upwards |
|---|
| 1086 |
in the bit vector, respectively) to handle this situation correctly, |
|---|
| 1087 |
i.e., so that no bits are being overwritten before they have been |
|---|
| 1088 |
copied themselves. |
|---|
| 1089 |
|
|---|
| 1090 |
=item * |
|---|
| 1091 |
|
|---|
| 1092 |
C<$vec2-E<gt>Interval_Substitute($vec1,$off2,$len2,$off1,$len1);> |
|---|
| 1093 |
|
|---|
| 1094 |
This method is (roughly) the same for bit vectors (i.e., arrays |
|---|
| 1095 |
of booleans) as what the "splice" function in Perl is for lists |
|---|
| 1096 |
(i.e., arrays of scalars). |
|---|
| 1097 |
|
|---|
| 1098 |
(See L<perlfunc/splice> for more details about this function.) |
|---|
| 1099 |
|
|---|
| 1100 |
The method allows you to substitute a stretch of contiguous bits |
|---|
| 1101 |
(defined by a position (offset) "C<$off1>" and a length of "C<$len1>" |
|---|
| 1102 |
bits) from a given "source" bit vector "C<$vec1>" for a different |
|---|
| 1103 |
stretch of contiguous bits (defined by a position (offset) "C<$off2>" |
|---|
| 1104 |
and a length of "C<$len2>" bits) in another, "target" bit vector |
|---|
| 1105 |
"C<$vec2>". |
|---|
| 1106 |
|
|---|
| 1107 |
Note that the two bit vectors "C<$vec1>" and "C<$vec2>" do B<NOT> |
|---|
| 1108 |
need to have the same (matching) size! |
|---|
| 1109 |
|
|---|
| 1110 |
Note further that "C<$off1>" and "C<$off2>" must lie within the |
|---|
| 1111 |
range "C<0>" and, respectively, "C<$vec1-E<gt>Size()>" or |
|---|
| 1112 |
"C<$vec2-E<gt>Size()>", or a fatal "offset out of range" error |
|---|
| 1113 |
will occur. |
|---|
| 1114 |
|
|---|
| 1115 |
Alert readers will have noticed that these upper limits are B<NOT> |
|---|
| 1116 |
"C<$vec1-E<gt>Size()-1>" and "C<$vec2-E<gt>Size()-1>", as they would |
|---|
| 1117 |
be for any other method in this module, but that these offsets may |
|---|
| 1118 |
actually point to one position B<PAST THE END> of the corresponding |
|---|
| 1119 |
bit vector. |
|---|
| 1120 |
|
|---|
| 1121 |
This is necessary in order to make it possible to B<APPEND> a given |
|---|
| 1122 |
stretch of bits to the target bit vector instead of B<REPLACING> |
|---|
| 1123 |
something in it. |
|---|
| 1124 |
|
|---|
| 1125 |
For reasons of symmetry and generality, the same applies to the offset |
|---|
| 1126 |
in the source bit vector, even though such an offset (one position past |
|---|
| 1127 |
the end of the bit vector) does not serve any practical purpose there |
|---|
| 1128 |
(but does not cause any harm either). |
|---|
| 1129 |
|
|---|
| 1130 |
(Actually this saves you from the need of testing for this special case, |
|---|
| 1131 |
in certain circumstances.) |
|---|
| 1132 |
|
|---|
| 1133 |
Note that whenever the term "C<$off1 + $len1>" exceeds the size |
|---|
| 1134 |
"C<$vec1-E<gt>Size()>" of bit vector "C<$vec1>" (or if "C<$off2 + $len2>" |
|---|
| 1135 |
exceeds "C<$vec2-E<gt>Size()>"), the corresponding length ("C<$len1>" |
|---|
| 1136 |
or "C<$len2>", respectively) is automatically reduced internally |
|---|
| 1137 |
so that "C<$off1 + $len1 E<lt>= $vec1-E<gt>Size()>" (and |
|---|
| 1138 |
"C<$off2 + $len2 E<lt>= $vec2-E<gt>Size()>") holds. |
|---|
| 1139 |
|
|---|
| 1140 |
(Note that this does B<NOT> alter the intended result, even though |
|---|
| 1141 |
this may seem counter-intuitive at first!) |
|---|
| 1142 |
|
|---|
| 1143 |
This may even result in a length ("C<$len1>" or "C<$len2>") of zero. |
|---|
| 1144 |
|
|---|
| 1145 |
A length of zero for the interval in the B<SOURCE> bit vector |
|---|
| 1146 |
("C<$len1 == 0>") means that the indicated stretch of bits in |
|---|
| 1147 |
the target bit vector (starting at position "C<$off2>") is to |
|---|
| 1148 |
be replaced by B<NOTHING>, i.e., is to be B<DELETED>. |
|---|
| 1149 |
|
|---|
| 1150 |
A length of zero for the interval in the B<TARGET> bit vector |
|---|
| 1151 |
("C<$len2> == 0") means that B<NOTHING> is replaced, and that the |
|---|
| 1152 |
stretch of bits from the source bit vector is simply B<INSERTED> |
|---|
| 1153 |
into the target bit vector at the indicated position ("C<$off2>"). |
|---|
| 1154 |
|
|---|
| 1155 |
If both length parameters are zero, nothing is done at all. |
|---|
| 1156 |
|
|---|
| 1157 |
Note that in contrast to any other method in this module (especially |
|---|
| 1158 |
"C<Interval_Copy()>", "C<Insert()>" and "C<Delete()>"), this method |
|---|
| 1159 |
B<IMPLICITLY> and B<AUTOMATICALLY> adapts the length of the resulting |
|---|
| 1160 |
bit vector as needed, as given by |
|---|
| 1161 |
|
|---|
| 1162 |
$size = $vec2->Size(); # before |
|---|
| 1163 |
$size += $len1 - $len2; # after |
|---|
| 1164 |
|
|---|
| 1165 |
(The only other method in this module that changes the size of a bit |
|---|
| 1166 |
vector is the method "C<Resize()>".) |
|---|
| 1167 |
|
|---|
| 1168 |
In other words, replacing a given interval of bits in the target bit |
|---|
| 1169 |
vector with a longer or shorter stretch of bits from the source bit |
|---|
| 1170 |
vector, or simply inserting ("C<$len2 == 0>") a stretch of bits into |
|---|
| 1171 |
or deleting ("C<$len1 == 0>") an interval of bits from the target bit |
|---|
| 1172 |
vector will automatically increase or decrease, respectively, the size |
|---|
| 1173 |
of the target bit vector accordingly. |
|---|
| 1174 |
|
|---|
| 1175 |
For the sake of generality, this may even result in a bit vector with |
|---|
| 1176 |
a size of zero (containing no bits at all). |
|---|
| 1177 |
|
|---|
| 1178 |
This is also the reason why bit vectors of length zero are permitted |
|---|
| 1179 |
in this module in the first place, starting with version 5.0. |
|---|
| 1180 |
|
|---|
| 1181 |
Finally, note that "C<$vec1>" and "C<$vec2>" may be identical, i.e., |
|---|
| 1182 |
in-place processing is possible. |
|---|
| 1183 |
|
|---|
| 1184 |
(If you think about that for a while or if you look at the code, |
|---|
| 1185 |
you will see that this is far from trivial!) |
|---|
| 1186 |
|
|---|
| 1187 |
=item * |
|---|
| 1188 |
|
|---|
| 1189 |
C<if ($vector-E<gt>is_empty())> |
|---|
| 1190 |
|
|---|
| 1191 |
Tests whether the given bit vector is empty, i.e., whether B<ALL> of |
|---|
| 1192 |
its bits are cleared (in the "off" state). |
|---|
| 1193 |
|
|---|
| 1194 |
In "big integer" arithmetic, this is equivalent to testing whether |
|---|
| 1195 |
the number stored in the bit vector is zero ("C<0>"). |
|---|
| 1196 |
|
|---|
| 1197 |
Returns "true" ("C<1>") if the bit vector is empty and "false" ("C<0>") |
|---|
| 1198 |
otherwise. |
|---|
| 1199 |
|
|---|
| 1200 |
Note that this method also returns "true" ("C<1>") if the given bit |
|---|
| 1201 |
vector has a length of zero, i.e., if it contains no bits at all. |
|---|
| 1202 |
|
|---|
| 1203 |
=item * |
|---|
| 1204 |
|
|---|
| 1205 |
C<if ($vector-E<gt>is_full())> |
|---|
| 1206 |
|
|---|
| 1207 |
Tests whether the given bit vector is full, i.e., whether B<ALL> of |
|---|
| 1208 |
its bits are set (in the "on" state). |
|---|
| 1209 |
|
|---|
| 1210 |
In "big integer" arithmetic, this is equivalent to testing whether |
|---|
| 1211 |
the number stored in the bit vector is minus one ("-1"). |
|---|
| 1212 |
|
|---|
| 1213 |
Returns "true" ("C<1>") if the bit vector is full and "false" ("C<0>") |
|---|
| 1214 |
otherwise. |
|---|
| 1215 |
|
|---|
| 1216 |
If the given bit vector has a length of zero (i.e., if it contains |
|---|
| 1217 |
no bits at all), this method returns "false" ("C<0>"). |
|---|
| 1218 |
|
|---|
| 1219 |
=item * |
|---|
| 1220 |
|
|---|
| 1221 |
C<if ($vec1-E<gt>equal($vec2))> |
|---|
| 1222 |
|
|---|
| 1223 |
Tests the two given bit vectors for equality. |
|---|
| 1224 |
|
|---|
| 1225 |
Returns "true" ("C<1>") if the two bit vectors are exact |
|---|
| 1226 |
copies of one another and "false" ("C<0>") otherwise. |
|---|
| 1227 |
|
|---|
| 1228 |
=item * |
|---|
| 1229 |
|
|---|
| 1230 |
C<$cmp = $vec1-E<gt>Lexicompare($vec2);> |
|---|
| 1231 |
|
|---|
| 1232 |
Compares the two given bit vectors, which are |
|---|
| 1233 |
regarded as B<UNSIGNED> numbers in binary representation. |
|---|
| 1234 |
|
|---|
| 1235 |
The method returns "C<-1>" if the first bit vector is smaller |
|---|
| 1236 |
than the second bit vector, "C<0>" if the two bit vectors are |
|---|
| 1237 |
exact copies of one another and "C<1>" if the first bit vector |
|---|
| 1238 |
is greater than the second bit vector. |
|---|
| 1239 |
|
|---|
| 1240 |
=item * |
|---|
| 1241 |
|
|---|
| 1242 |
C<$cmp = $vec1-E<gt>Compare($vec2);> |
|---|
| 1243 |
|
|---|
| 1244 |
Compares the two given bit vectors, which are |
|---|
| 1245 |
regarded as B<SIGNED> numbers in binary representation. |
|---|
| 1246 |
|
|---|
| 1247 |
The method returns "C<-1>" if the first bit vector is smaller |
|---|
| 1248 |
than the second bit vector, "C<0>" if the two bit vectors are |
|---|
| 1249 |
exact copies of one another and "C<1>" if the first bit vector |
|---|
| 1250 |
is greater than the second bit vector. |
|---|
| 1251 |
|
|---|
| 1252 |
=item * |
|---|
| 1253 |
|
|---|
| 1254 |
C<$string = $vector-E<gt>to_Hex();> |
|---|
| 1255 |
|
|---|
| 1256 |
Returns a hexadecimal string representing the given bit vector. |
|---|
| 1257 |
|
|---|
| 1258 |
Note that this representation is quite compact, in that it only |
|---|
| 1259 |
needs at most twice the number of bytes needed to store the bit |
|---|
| 1260 |
vector itself, internally. |
|---|
| 1261 |
|
|---|
| 1262 |
Note also that since a hexadecimal digit is always worth four bits, |
|---|
| 1263 |
the length of the resulting string is always a multiple of four bits, |
|---|
| 1264 |
regardless of the true length (in bits) of the given bit vector. |
|---|
| 1265 |
|
|---|
| 1266 |
Finally, note that the B<LEAST> significant hexadecimal digit is |
|---|
| 1267 |
located at the B<RIGHT> end of the resulting string, and the B<MOST> |
|---|
| 1268 |
significant digit at the B<LEFT> end. |
|---|
| 1269 |
|
|---|
| 1270 |
=item * |
|---|
| 1271 |
|
|---|
| 1272 |
C<$vector-E<gt>from_Hex($string);> |
|---|
| 1273 |
|
|---|
| 1274 |
Allows to read in the contents of a bit vector from a hexadecimal |
|---|
| 1275 |
string, such as returned by the method "C<to_Hex()>" (see above). |
|---|
| 1276 |
|
|---|
| 1277 |
Remember that the least significant bits are always to the right of a |
|---|
| 1278 |
hexadecimal string, and the most significant bits to the left. Therefore, |
|---|
| 1279 |
the string is actually read in from right to left while the bit vector |
|---|
| 1280 |
is filled accordingly, 4 bits at a time, starting with the least significant |
|---|
| 1281 |
bits and going upward to the most significant bits. |
|---|
| 1282 |
|
|---|
| 1283 |
If the given string contains less hexadecimal digits than are needed |
|---|
| 1284 |
to completely fill the given bit vector, the remaining (most significant) |
|---|
| 1285 |
bits are all cleared. |
|---|
| 1286 |
|
|---|
| 1287 |
This also means that, even if the given string does not contain enough digits |
|---|
| 1288 |
to completely fill the given bit vector, the previous contents of the |
|---|
| 1289 |
bit vec |
|---|