Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perlmod / SunOS-sparc / Bit / Vector.pod
CommitLineData
86530b38
AT
1
2=head1 NAME
3
4Bit::Vector - Efficient bit vector, set of integers and "big int" math library
5
6=head1 SYNOPSIS
7
8=head2 OVERLOADED OPERATORS
9
10See L<Bit::Vector::Overload(3)>.
11
12=head2 MORE STRING IMPORT/EXPORT
13
14See L<Bit::Vector::String(3)>.
15
16=head2 CLASS METHODS
17
18 Version
19 $version = Bit::Vector->Version();
20
21 Word_Bits
22 $bits = Bit::Vector->Word_Bits(); # bits in a machine word
23
24 Long_Bits
25 $bits = Bit::Vector->Long_Bits(); # bits in an unsigned long
26
27 new
28 $vector = Bit::Vector->new($bits); # bit vector constructor
29
30 @veclist = Bit::Vector->new($bits,$count);
31
32 new_Hex
33 $vector = Bit::Vector->new_Hex($bits,$string);
34
35 new_Bin
36 $vector = Bit::Vector->new_Bin($bits,$string);
37
38 new_Dec
39 $vector = Bit::Vector->new_Dec($bits,$string);
40
41 new_Enum
42 $vector = Bit::Vector->new_Enum($bits,$string);
43
44 Concat_List
45 $vector = Bit::Vector->Concat_List(@vectors);
46
47=head2 OBJECT METHODS
48
49 new
50 $vec2 = $vec1->new($bits); # alternative call of constructor
51
52 @veclist = $vec->new($bits,$count);
53
54 Shadow
55 $vec2 = $vec1->Shadow(); # new vector, same size but empty
56
57 Clone
58 $vec2 = $vec1->Clone(); # new vector, exact duplicate
59
60 Concat
61 $vector = $vec1->Concat($vec2);
62
63 Concat_List
64 $vector = $vec1->Concat_List($vec2,$vec3,...);
65
66 Size
67 $bits = $vector->Size();
68
69 Resize
70 $vector->Resize($bits);
71 $vector->Resize($vector->Size()+5);
72 $vector->Resize($vector->Size()-5);
73
74 Copy
75 $vec2->Copy($vec1);
76
77 Empty
78 $vector->Empty();
79
80 Fill
81 $vector->Fill();
82
83 Flip
84 $vector->Flip();
85
86 Primes
87 $vector->Primes(); # Sieve of Erathostenes
88
89 Reverse
90 $vec2->Reverse($vec1);
91
92 Interval_Empty
93 $vector->Interval_Empty($min,$max);
94
95 Interval_Fill
96 $vector->Interval_Fill($min,$max);
97
98 Interval_Flip
99 $vector->Interval_Flip($min,$max);
100
101 Interval_Reverse
102 $vector->Interval_Reverse($min,$max);
103
104 Interval_Scan_inc
105 if (($min,$max) = $vector->Interval_Scan_inc($start))
106
107 Interval_Scan_dec
108 if (($min,$max) = $vector->Interval_Scan_dec($start))
109
110 Interval_Copy
111 $vec2->Interval_Copy($vec1,$offset2,$offset1,$length);
112
113 Interval_Substitute
114 $vec2->Interval_Substitute($vec1,$off2,$len2,$off1,$len1);
115
116 is_empty
117 if ($vector->is_empty())
118
119 is_full
120 if ($vector->is_full())
121
122 equal
123 if ($vec1->equal($vec2))
124
125 Lexicompare (unsigned)
126 if ($vec1->Lexicompare($vec2) == 0)
127 if ($vec1->Lexicompare($vec2) != 0)
128 if ($vec1->Lexicompare($vec2) < 0)
129 if ($vec1->Lexicompare($vec2) <= 0)
130 if ($vec1->Lexicompare($vec2) > 0)
131 if ($vec1->Lexicompare($vec2) >= 0)
132
133 Compare (signed)
134 if ($vec1->Compare($vec2) == 0)
135 if ($vec1->Compare($vec2) != 0)
136 if ($vec1->Compare($vec2) < 0)
137 if ($vec1->Compare($vec2) <= 0)
138 if ($vec1->Compare($vec2) > 0)
139 if ($vec1->Compare($vec2) >= 0)
140
141 to_Hex
142 $string = $vector->to_Hex();
143
144 from_Hex
145 $vector->from_Hex($string);
146
147 to_Bin
148 $string = $vector->to_Bin();
149
150 from_Bin
151 $vector->from_Bin($string);
152
153 to_Dec
154 $string = $vector->to_Dec();
155
156 from_Dec
157 $vector->from_Dec($string);
158
159 to_Enum
160 $string = $vector->to_Enum(); # e.g. "2,3,5-7,11,13-19"
161
162 from_Enum
163 $vector->from_Enum($string);
164
165 Bit_Off
166 $vector->Bit_Off($index);
167
168 Bit_On
169 $vector->Bit_On($index);
170
171 bit_flip
172 $bit = $vector->bit_flip($index);
173
174 bit_test
175 contains
176 $bit = $vector->bit_test($index);
177 $bit = $vector->contains($index);
178 if ($vector->bit_test($index))
179 if ($vector->contains($index))
180
181 Bit_Copy
182 $vector->Bit_Copy($index,$bit);
183
184 LSB (least significant bit)
185 $vector->LSB($bit);
186
187 MSB (most significant bit)
188 $vector->MSB($bit);
189
190 lsb (least significant bit)
191 $bit = $vector->lsb();
192
193 msb (most significant bit)
194 $bit = $vector->msb();
195
196 rotate_left
197 $carry = $vector->rotate_left();
198
199 rotate_right
200 $carry = $vector->rotate_right();
201
202 shift_left
203 $carry = $vector->shift_left($carry);
204
205 shift_right
206 $carry = $vector->shift_right($carry);
207
208 Move_Left
209 $vector->Move_Left($bits); # shift left "$bits" positions
210
211 Move_Right
212 $vector->Move_Right($bits); # shift right "$bits" positions
213
214 Insert
215 $vector->Insert($offset,$bits);
216
217 Delete
218 $vector->Delete($offset,$bits);
219
220 increment
221 $carry = $vector->increment();
222
223 decrement
224 $carry = $vector->decrement();
225
226 inc
227 $overflow = $vec2->inc($vec1);
228
229 dec
230 $overflow = $vec2->dec($vec1);
231
232 add
233 $carry = $vec3->add($vec1,$vec2,$carry);
234 ($carry,$overflow) = $vec3->add($vec1,$vec2,$carry);
235
236 subtract
237 $carry = $vec3->subtract($vec1,$vec2,$carry);
238 ($carry,$overflow) = $vec3->subtract($vec1,$vec2,$carry);
239
240 Neg
241 Negate
242 $vec2->Neg($vec1);
243 $vec2->Negate($vec1);
244
245 Abs
246 Absolute
247 $vec2->Abs($vec1);
248 $vec2->Absolute($vec1);
249
250 Sign
251 if ($vector->Sign() == 0)
252 if ($vector->Sign() != 0)
253 if ($vector->Sign() < 0)
254 if ($vector->Sign() <= 0)
255 if ($vector->Sign() > 0)
256 if ($vector->Sign() >= 0)
257
258 Multiply
259 $vec3->Multiply($vec1,$vec2);
260
261 Divide
262 $quot->Divide($vec1,$vec2,$rest);
263
264 GCD (Greatest Common Divisor)
265 $vecgcd->GCD($veca,$vecb);
266 $vecgcd->GCD($vecx,$vecy,$veca,$vecb);
267
268 Power
269 $vec3->Power($vec1,$vec2);
270
271 Block_Store
272 $vector->Block_Store($buffer);
273
274 Block_Read
275 $buffer = $vector->Block_Read();
276
277 Word_Size
278 $size = $vector->Word_Size(); # number of words in "$vector"
279
280 Word_Store
281 $vector->Word_Store($offset,$word);
282
283 Word_Read
284 $word = $vector->Word_Read($offset);
285
286 Word_List_Store
287 $vector->Word_List_Store(@words);
288
289 Word_List_Read
290 @words = $vector->Word_List_Read();
291
292 Word_Insert
293 $vector->Word_Insert($offset,$count);
294
295 Word_Delete
296 $vector->Word_Delete($offset,$count);
297
298 Chunk_Store
299 $vector->Chunk_Store($chunksize,$offset,$chunk);
300
301 Chunk_Read
302 $chunk = $vector->Chunk_Read($chunksize,$offset);
303
304 Chunk_List_Store
305 $vector->Chunk_List_Store($chunksize,@chunks);
306
307 Chunk_List_Read
308 @chunks = $vector->Chunk_List_Read($chunksize);
309
310 Index_List_Remove
311 $vector->Index_List_Remove(@indices);
312
313 Index_List_Store
314 $vector->Index_List_Store(@indices);
315
316 Index_List_Read
317 @indices = $vector->Index_List_Read();
318
319 Or
320 Union
321 $vec3->Or($vec1,$vec2);
322 $set3->Union($set1,$set2);
323
324 And
325 Intersection
326 $vec3->And($vec1,$vec2);
327 $set3->Intersection($set1,$set2);
328
329 AndNot
330 Difference
331 $vec3->AndNot($vec1,$vec2);
332 $set3->Difference($set1,$set2);
333
334 Xor
335 ExclusiveOr
336 $vec3->Xor($vec1,$vec2);
337 $set3->ExclusiveOr($set1,$set2);
338
339 Not
340 Complement
341 $vec2->Not($vec1);
342 $set2->Complement($set1);
343
344 subset
345 if ($set1->subset($set2)) # true if $set1 is subset of $set2
346
347 Norm
348 $norm = $set->Norm();
349 $norm = $set->Norm2();
350 $norm = $set->Norm3();
351
352 Min
353 $min = $set->Min();
354
355 Max
356 $max = $set->Max();
357
358 Multiplication
359 $matrix3->Multiplication($rows3,$cols3,
360 $matrix1,$rows1,$cols1,
361 $matrix2,$rows2,$cols2);
362
363 Product
364 $matrix3->Product($rows3,$cols3,
365 $matrix1,$rows1,$cols1,
366 $matrix2,$rows2,$cols2);
367
368 Closure
369 $matrix->Closure($rows,$cols);
370
371 Transpose
372 $matrix2->Transpose($rows2,$cols2,$matrix1,$rows1,$cols1);
373
374=head1 IMPORTANT NOTES
375
376=over 2
377
378=item *
379
380Method naming conventions
381
382Method names completely in lower case indicate a boolean return value.
383
384(Except for the bit vector constructor method "C<new()>", of course.)
385
386=item *
387
388Boolean values
389
390Boolean values in this module are always a numeric zero ("C<0>") for
391"false" and a numeric one ("C<1>") for "true".
392
393=item *
394
395Negative numbers
396
397All numeric input parameters passed to any of the methods in this module
398are regarded as being B<UNSIGNED> (as opposed to the contents of the
399bit vectors themselves, which are usually considered to be B<SIGNED>).
400
401As a consequence, whenever you pass a negative number as an argument to
402some method of this module, it will be treated as a (usually very large)
403positive number due to its internal two's complement binary representation,
404usually resulting in an "index out of range" error message and program
405abortion.
406
407=item *
408
409Bit order
410
411Note that bit vectors are stored least order bit and least order word first
412internally.
413
414I.e., bit #0 of any given bit vector corresponds to bit #0 of word #0 in the
415array of machine words representing the bit vector.
416
417(Where word #0 comes first in memory, i.e., it is stored at the least memory
418address in the allocated block of memory holding the given bit vector.)
419
420Note however that machine words can be stored least order byte first or last,
421depending on your system's implementation.
422
423When you are exporting or importing a whole bit vector at once using the
424methods "C<Block_Read()>" and "C<Block_Store()>" (the only time in this
425module where this could make any difference), however, a conversion to and
426from "least order byte first" order is automatically supplied.
427
428In other words, what "C<Block_Read()>" provides and what "C<Block_Store()>"
429expects is always in "least order byte first" order, regardless of the order
430in which words are stored internally on your machine.
431
432This is to make sure that what you export on one machine using "C<Block_Read()>"
433can always be read in correctly with "C<Block_Store()>" on a different machine.
434
435Note further that whenever bit vectors are converted to and from (binary or
436hexadecimal) strings, the B<RIGHTMOST> bit is always the B<LEAST SIGNIFICANT>
437one, and the B<LEFTMOST> bit is always the B<MOST SIGNIFICANT> bit.
438
439This is because in our western culture, numbers are always represented in this
440way (least significant to most significant digits go from right to left).
441
442Of course this requires an internal reversion of order, which the corresponding
443conversion methods perform automatically (without any additional overhead, it's
444just a matter of starting the internal loop at the bottom or the top end).
445
446=item *
447
448"Word" related methods
449
450Note that all methods whose names begin with "C<Word_>" are
451B<MACHINE-DEPENDENT>!
452
453They depend on the size (number of bits) of an "unsigned int" (C type) on
454your machine.
455
456Therefore, you should only use these methods if you are B<ABSOLUTELY CERTAIN>
457that portability of your code is not an issue!
458
459Note that you can use arbitrarily large chunks (i.e., fragments of bit vectors)
460of up to 32 bits B<IN A PORTABLE WAY> using the methods whose names begin with
461"C<Chunk_>".
462
463=item *
464
465Chunk sizes
466
467Note that legal chunk sizes for all methods whose names begin with "C<Chunk_>"
468range from "C<1>" to "C<Bit::Vector-E<gt>Long_Bits();>" bits ("C<0>" is B<NOT>
469allowed!).
470
471In order to make your programs portable, however, you shouldn't use chunk sizes
472larger than 32 bits, since this is the minimum size of an "unsigned long"
473(C type) on all systems, as prescribed by S<ANSI C>.
474
475=item *
476
477Matching sizes
478
479In general, for methods involving several bit vectors at the same time, all
480bit vector arguments must have identical sizes (number of bits), or a fatal
481"size mismatch" error will occur.
482
483Exceptions from this rule are the methods "C<Concat()>", "C<Concat_List()>",
484"C<Copy()>", "C<Interval_Copy()>" and "C<Interval_Substitute()>", where no
485conditions at all are imposed on the size of their bit vector arguments.
486
487In method "C<Multiply()>", all three bit vector arguments must in principle
488obey the rule of matching sizes, but the bit vector in which the result of
489the multiplication is to be stored may be larger than the two bit vector
490arguments containing the factors for the multiplication.
491
492In method "C<Power()>", the bit vector for the result must be the same
493size or greater than the base of the exponentiation term. The exponent
494can be any size.
495
496=item *
497
498Index ranges
499
500All indices for any given bits must lie between "C<0>" and
501"C<$vector-E<gt>Size()-1>", or a fatal "index out of range"
502error will occur.
503
504=back
505
506=head1 DESCRIPTION
507
508=head2 OVERLOADED OPERATORS
509
510See L<Bit::Vector::Overload(3)>.
511
512=head2 MORE STRING IMPORT/EXPORT
513
514See L<Bit::Vector::String(3)>.
515
516=head2 CLASS METHODS
517
518=over 2
519
520=item *
521
522C<$version = Bit::Vector-E<gt>Version();>
523
524Returns the current version number of this module.
525
526=item *
527
528C<$bits = Bit::Vector-E<gt>Word_Bits();>
529
530Returns the number of bits of an "unsigned int" (C type)
531on your machine.
532
533(An "unsigned int" is also called a "machine word",
534hence the name of this method.)
535
536=item *
537
538C<$bits = Bit::Vector-E<gt>Long_Bits();>
539
540Returns the number of bits of an "unsigned long" (C type)
541on your machine.
542
543=item *
544
545C<$vector = Bit::Vector-E<gt>new($bits);>
546
547This is the bit vector constructor method.
548
549Call this method to create a new bit vector containing "C<$bits>"
550bits (with indices ranging from "C<0>" to "C<$bits-1>").
551
552Note that - in contrast to previous versions - bit vectors
553of length zero (i.e., with C<$bits = 0>) are permitted now.
554
555The method returns a reference to the newly created bit vector.
556
557A new bit vector is always initialized so that all bits are cleared
558(turned off).
559
560An exception will be raised if the method is unable to allocate the
561necessary memory.
562
563Note that if you specify a negative number for "C<$bits>" it will be
564interpreted as a large positive number due to its internal two's
565complement binary representation.
566
567In such a case, the bit vector constructor method will obediently attempt
568to create a bit vector of that size, probably resulting in an exception,
569as explained above.
570
571=item *
572
573C<@veclist = Bit::Vector-E<gt>new($bits,$count);>
574
575You can also create more than one bit vector at a time if you specify the
576optional second parameter "C<$count>".
577
578The method returns a list of "C<$count>" bit vectors which all have the
579same number of bits "C<$bits>" (and which are all initialized, i.e.,
580all bits are cleared).
581
582If "C<$count>" is zero, an empty list is returned.
583
584If "C<$bits>" is zero, a list of null-sized bit vectors is returned.
585
586Note again that if you specify a negative number for "C<$count>" it will
587be interpreted as a large positive number due to its internal two's
588complement binary representation.
589
590In such a case, the bit vector constructor method will obediently attempt
591to create that many bit vectors, probably resulting in an exception ("out
592of memory").
593
594=item *
595
596C<$vector = Bit::Vector-E<gt>new_Hex($bits,$string);>
597
598This method is an alternative constructor which allows you to create
599a new bit vector object (with "C<$bits>" bits) and to initialize it
600all in one go.
601
602The method internally first calls the bit vector constructor method
603"C<new()>" and then passes the given string to the method "C<from_Hex()>".
604
605However, this method is more efficient than performing these two steps
606separately: First because in this method, the memory area occupied by
607the new bit vector is not initialized to zeros (which is pointless in
608this case), and second because it saves you from the associated overhead
609of one additional method invocation.
610
611An exception will be raised if the necessary memory cannot be allocated
612(see the description of the method "C<new()>" immediately above for
613possible causes) or if the given string cannot be converted successfully
614(see the description of the method "C<from_Hex()>" further below for
615details).
616
617In the latter case, the memory occupied by the new bit vector is
618released first (i.e., "free"d) before the exception is actually
619raised.
620
621=item *
622
623C<$vector = Bit::Vector-E<gt>new_Bin($bits,$string);>
624
625This method is an alternative constructor which allows you to create
626a new bit vector object (with "C<$bits>" bits) and to initialize it
627all in one go.
628
629The method internally first calls the bit vector constructor method
630"C<new()>" and then passes the given string to the method "C<from_Bin()>".
631
632However, this method is more efficient than performing these two steps
633separately: First because in this method, the memory area occupied by
634the new bit vector is not initialized to zeros (which is pointless in
635this case), and second because it saves you from the associated overhead
636of one additional method invocation.
637
638An exception will be raised if the necessary memory cannot be allocated
639(see the description of the method "C<new()>" above for possible causes)
640or if the given string cannot be converted successfully (see the
641description of the method "C<from_Bin()>" further below for details).
642
643In the latter case, the memory occupied by the new bit vector is
644released first (i.e., "free"d) before the exception is actually
645raised.
646
647=item *
648
649C<$vector = Bit::Vector-E<gt>new_Dec($bits,$string);>
650
651This method is an alternative constructor which allows you to create
652a new bit vector object (with "C<$bits>" bits) and to initialize it
653all in one go.
654
655The method internally first calls the bit vector constructor method
656"C<new()>" and then passes the given string to the method "C<from_Dec()>".
657
658However, this method is more efficient than performing these two steps
659separately: First because in this method, "C<new()>" does not initialize
660the memory area occupied by the new bit vector with zeros (which is
661pointless in this case, because "C<from_Dec()>" will do it anyway),
662and second because it saves you from the associated overhead of one
663additional method invocation.
664
665An exception will be raised if the necessary memory cannot be allocated
666(see the description of the method "C<new()>" above for possible causes)
667or if the given string cannot be converted successfully (see the
668description of the method "C<from_Dec()>" further below for details).
669
670In the latter case, the memory occupied by the new bit vector is
671released first (i.e., "free"d) before the exception is actually
672raised.
673
674=item *
675
676C<$vector = Bit::Vector-E<gt>new_Enum($bits,$string);>
677
678This method is an alternative constructor which allows you to create
679a new bit vector object (with "C<$bits>" bits) and to initialize it
680all in one go.
681
682The method internally first calls the bit vector constructor method
683"C<new()>" and then passes the given string to the method "C<from_Enum()>".
684
685However, this method is more efficient than performing these two steps
686separately: First because in this method, "C<new()>" does not initialize
687the memory area occupied by the new bit vector with zeros (which is
688pointless in this case, because "C<from_Enum()>" will do it anyway),
689and second because it saves you from the associated overhead of one
690additional method invocation.
691
692An exception will be raised if the necessary memory cannot be allocated
693(see the description of the method "C<new()>" above for possible causes)
694or if the given string cannot be converted successfully (see the
695description of the method "C<from_Enum()>" further below for details).
696
697In the latter case, the memory occupied by the new bit vector is
698released first (i.e., "free"d) before the exception is actually
699raised.
700
701=item *
702
703C<$vector = Bit::Vector-E<gt>Concat_List(@vectors);>
704
705This method creates a new vector containing all bit vectors from the
706argument list in concatenated form.
707
708The argument list may contain any number of arguments (including
709zero); the only condition is that all arguments must be bit vectors.
710
711There is no condition concerning the length (in number of bits) of
712these arguments.
713
714The vectors from the argument list are not changed in any way.
715
716If the argument list is empty or if all arguments have length zero,
717the resulting bit vector will also have length zero.
718
719Note that the B<RIGHTMOST> bit vector from the argument list will
720become the B<LEAST> significant part of the resulting bit vector,
721and the B<LEFTMOST> bit vector from the argument list will
722become the B<MOST> significant part of the resulting bit vector.
723
724=back
725
726=head2 OBJECT METHODS
727
728=over 2
729
730=item *
731
732C<$vec2 = $vec1-E<gt>new($bits);>
733
734C<@veclist = $vec-E<gt>new($bits);>
735
736This is an alternative way of calling the bit vector constructor method.
737
738Vector "C<$vec1>" (or "C<$vec>") is not affected by this, it just serves
739as an anchor for the method invocation mechanism.
740
741In fact B<ALL> class methods in this module can be called this way,
742even though this is probably considered to be "politically incorrect"
743by OO ("object-orientation") aficionados. ;-)
744
745So even if you are too lazy to type "C<Bit::Vector-E<gt>>" instead of
746"C<$vec1-E<gt>>" (and even though laziness is - allegedly - a programmer's
747virtue C<:-)>), maybe it is better not to use this feature if you don't
748want to get booed at. ;-)
749
750=item *
751
752C<$vec2 = $vec1-E<gt>Shadow();>
753
754Creates a B<NEW> bit vector "C<$vec2>" of the B<SAME SIZE> as "C<$vec1>"
755but which is B<EMPTY>.
756
757Just like a shadow that has the same shape as the object it
758originates from, but is flat and has no volume, i.e., contains
759nothing.
760
761=item *
762
763C<$vec2 = $vec1-E<gt>Clone();>
764
765Creates a B<NEW> bit vector "C<$vec2>" of the B<SAME SIZE> as "C<$vec1>"
766which is an B<EXACT COPY> of "C<$vec1>".
767
768=item *
769
770C<$vector = $vec1-E<gt>Concat($vec2);>
771
772This method returns a new bit vector object which is the result of the
773concatenation of the contents of "C<$vec1>" and "C<$vec2>".
774
775Note that the contents of "C<$vec1>" become the B<MOST> significant part
776of the resulting bit vector, and "C<$vec2>" the B<LEAST> significant part.
777
778If both bit vector arguments have length zero, the resulting bit vector
779will also have length zero.
780
781=item *
782
783C<$vector = $vec1-E<gt>Concat_List($vec2,$vec3,...);>
784
785This is an alternative way of calling this (class) method as an
786object method.
787
788The method returns a new bit vector object which is the result of
789the concatenation of the contents of C<$vec1 . $vec2 . $vec3 . ...>
790
791See the section "class methods" above for a detailed description of
792this method.
793
794Note that the argument list may be empty and that all arguments
795must be bit vectors if it isn't.
796
797=item *
798
799C<$bits = $vector-E<gt>Size();>
800
801Returns the size (number of bits) the given vector was created with
802(or "C<Resize()>"d to).
803
804=item *
805
806C<$vector-E<gt>Resize($bits);>
807
808Changes the size of the given vector to the specified number of bits.
809
810This method allows you to change the size of an existing bit vector,
811preserving as many bits from the old vector as will fit into the
812new one (i.e., all bits with indices smaller than the minimum of the
813sizes of both vectors, old and new).
814
815If the number of machine words needed to store the new vector is smaller
816than or equal to the number of words needed to store the old vector, the
817memory allocated for the old vector is reused for the new one, and only
818the relevant book-keeping information is adjusted accordingly.
819
820This means that even if the number of bits increases, new memory is not
821necessarily being allocated (i.e., if the old and the new number of bits
822fit into the same number of machine words).
823
824If the number of machine words needed to store the new vector is greater
825than the number of words needed to store the old vector, new memory is
826allocated for the new vector, the old vector is copied to the new one,
827the remaining bits in the new vector are cleared (turned off) and the old
828vector is deleted, i.e., the memory that was allocated for it is released.
829
830(An exception will be raised if the method is unable to allocate the
831necessary memory for the new vector.)
832
833As a consequence, if you decrease the size of a given vector so that
834it will use fewer machine words, and increase it again later so that it
835will use more words than immediately before but still less than the
836original vector, new memory will be allocated anyway because the
837information about the size of the original vector is lost whenever
838you resize it.
839
840Note also that if you specify a negative number for "C<$bits>" it will
841be interpreted as a large positive number due to its internal two's
842complement binary representation.
843
844In such a case, "Resize()" will obediently attempt to create a bit
845vector of that size, probably resulting in an exception, as explained
846above.
847
848Finally, note that - in contrast to previous versions - resizing a bit
849vector to a size of zero bits (length zero) is now permitted.
850
851=item *
852
853C<$vec2-E<gt>Copy($vec1);>
854
855Copies the contents of bit vector "C<$vec1>" to bit vector "C<$vec2>".
856
857The previous contents of bit vector "C<$vec2>" get overwritten, i.e.,
858are lost.
859
860Both vectors must exist beforehand, i.e., this method does not B<CREATE>
861any new bit vector object.
862
863The two vectors may be of any size.
864
865If the source bit vector is larger than the target, this method will copy
866as much of the least significant bits of the source vector as will fit into
867the target vector, thereby discarding any extraneous most significant bits.
868
869BEWARE that this causes a brutal cutoff in the middle of your data, and it
870will also leave you with an almost unpredictable sign if subsequently the
871number in the target vector is going to be interpreted as a number! (You
872have been warned!)
873
874If the target bit vector is larger than the source, this method fills up
875the remaining most significant bits in the target bit vector with either
8760's or 1's, depending on the sign (= the most significant bit) of the
877source bit vector. This is also known as "sign extension".
878
879This makes it possible to copy numbers from a smaller bit vector into
880a larger one while preserving the number's absolute value as well as
881its sign (due to the two's complement binary representation of numbers).
882
883=item *
884
885C<$vector-E<gt>Empty();>
886
887Clears all bits in the given vector.
888
889=item *
890
891C<$vector-E<gt>Fill();>
892
893Sets all bits in the given vector.
894
895=item *
896
897C<$vector-E<gt>Flip();>
898
899Flips (i.e., complements) all bits in the given vector.
900
901=item *
902
903C<$vector-E<gt>Primes();>
904
905Clears the given bit vector and sets all bits whose
906indices are prime numbers.
907
908This method uses the algorithm known as the "Sieve of
909Erathostenes" internally.
910
911=item *
912
913C<$vec2-E<gt>Reverse($vec1);>
914
915This method copies the given vector "C<$vec1>" to
916the vector "C<$vec2>", thereby reversing the order
917of all bits.
918
919I.e., the least significant bit of "C<$vec1>" becomes the
920most significant bit of "C<$vec2>", whereas the most
921significant bit of "C<$vec1>" becomes the least
922significant bit of "C<$vec2>", and so forth
923for all bits in between.
924
925Note that in-place processing is also possible, i.e.,
926"C<$vec1>" and "C<$vec2>" may be identical.
927
928(Internally, this is the same as
929C<$vec1-E<gt>Interval_Reverse(0,$vec1-E<gt>Size()-1);>.)
930
931=item *
932
933C<$vector-E<gt>Interval_Empty($min,$max);>
934
935Clears all bits in the interval C<[$min..$max]> (including both limits)
936in the given vector.
937
938"C<$min>" and "C<$max>" may have the same value; this is the same
939as clearing a single bit with "C<Bit_Off()>" (but less efficient).
940
941Note that C<$vector-E<gt>Interval_Empty(0,$vector-E<gt>Size()-1);>
942is the same as C<$vector-E<gt>Empty();> (but less efficient).
943
944=item *
945
946C<$vector-E<gt>Interval_Fill($min,$max);>
947
948Sets all bits in the interval C<[$min..$max]> (including both limits)
949in the given vector.
950
951"C<$min>" and "C<$max>" may have the same value; this is the same
952as setting a single bit with "C<Bit_On()>" (but less efficient).
953
954Note that C<$vector-E<gt>Interval_Fill(0,$vector-E<gt>Size()-1);>
955is the same as C<$vector-E<gt>Fill();> (but less efficient).
956
957=item *
958
959C<$vector-E<gt>Interval_Flip($min,$max);>
960
961Flips (i.e., complements) all bits in the interval C<[$min..$max]>
962(including both limits) in the given vector.
963
964"C<$min>" and "C<$max>" may have the same value; this is the same
965as flipping a single bit with "C<bit_flip()>" (but less efficient).
966
967Note that C<$vector-E<gt>Interval_Flip(0,$vector-E<gt>Size()-1);>
968is the same as C<$vector-E<gt>Flip();> and
969C<$vector-E<gt>Complement($vector);>
970(but less efficient).
971
972=item *
973
974C<$vector-E<gt>Interval_Reverse($min,$max);>
975
976Reverses the order of all bits in the interval C<[$min..$max]>
977(including both limits) in the given vector.
978
979I.e., bits "C<$min>" and "C<$max>" swap places, and so forth
980for all bits in between.
981
982"C<$min>" and "C<$max>" may have the same value; this has no
983effect whatsoever, though.
984
985=item *
986
987C<if (($min,$max) = $vector-E<gt>Interval_Scan_inc($start))>
988
989Returns the minimum and maximum indices of the next contiguous block
990of set bits (i.e., bits in the "on" state).
991
992The search starts at index "C<$start>" (i.e., C<"$min" E<gt>= "$start">)
993and proceeds upwards (i.e., C<"$max" E<gt>= "$min">), thus repeatedly
994increments the search pointer "C<$start>" (internally).
995
996Note though that the contents of the variable (or scalar literal value)
997"C<$start>" is B<NOT> altered. I.e., you have to set it to the desired
998value yourself prior to each call to "C<Interval_Scan_inc()>" (see also
999the example given below).
1000
1001Actually, the bit vector is not searched bit by bit, but one machine
1002word at a time, in order to speed up execution (which means that this
1003method is quite efficient).
1004
1005An empty list is returned if no such block can be found.
1006
1007Note that a single set bit (surrounded by cleared bits) is a valid
1008block by this definition. In that case the return values for "C<$min>"
1009and "C<$max>" are the same.
1010
1011Typical use:
1012
1013 $start = 0;
1014 while (($start < $vector->Size()) &&
1015 (($min,$max) = $vector->Interval_Scan_inc($start)))
1016 {
1017 $start = $max + 2;
1018
1019 # do something with $min and $max
1020 }
1021
1022=item *
1023
1024C<if (($min,$max) = $vector-E<gt>Interval_Scan_dec($start))>
1025
1026Returns the minimum and maximum indices of the next contiguous block
1027of set bits (i.e., bits in the "on" state).
1028
1029The search starts at index "C<$start>" (i.e., C<"$max" E<lt>= "$start">)
1030and proceeds downwards (i.e., C<"$min" E<lt>= "$max">), thus repeatedly
1031decrements the search pointer "C<$start>" (internally).
1032
1033Note though that the contents of the variable (or scalar literal value)
1034"C<$start>" is B<NOT> altered. I.e., you have to set it to the desired
1035value yourself prior to each call to "C<Interval_Scan_dec()>" (see also
1036the example given below).
1037
1038Actually, the bit vector is not searched bit by bit, but one machine
1039word at a time, in order to speed up execution (which means that this
1040method is quite efficient).
1041
1042An empty list is returned if no such block can be found.
1043
1044Note that a single set bit (surrounded by cleared bits) is a valid
1045block by this definition. In that case the return values for "C<$min>"
1046and "C<$max>" are the same.
1047
1048Typical use:
1049
1050 $start = $vector->Size() - 1;
1051 while (($start >= 0) &&
1052 (($min,$max) = $vector->Interval_Scan_dec($start)))
1053 {
1054 $start = $min - 2;
1055
1056 # do something with $min and $max
1057 }
1058
1059=item *
1060
1061C<$vec2-E<gt>Interval_Copy($vec1,$offset2,$offset1,$length);>
1062
1063This method allows you to copy a stretch of contiguous bits (starting
1064at any position "C<$offset1>" you choose, with a length of "C<$length>"
1065bits) from a given "source" bit vector "C<$vec1>" to another position
1066"C<$offset2>" in a "target" bit vector "C<$vec2>".
1067
1068Note that the two bit vectors "C<$vec1>" and "C<$vec2>" do B<NOT>
1069need to have the same (matching) size!
1070
1071Consequently, any of the two terms "C<$offset1 + $length>" and
1072"C<$offset2 + $length>" (or both) may exceed the actual length
1073of its corresponding bit vector ("C<$vec1-E<gt>Size()>" and
1074"C<$vec2-E<gt>Size()>", respectively).
1075
1076In such a case, the "C<$length>" parameter is automatically reduced
1077internally so that both terms above are bounded by the number of bits
1078of their corresponding bit vector.
1079
1080This may even result in a length of zero, in which case nothing is
1081copied at all.
1082
1083(Of course the value of the "C<$length>" parameter, supplied by you
1084in the initial method call, may also be zero right from the start!)
1085
1086Note also that "C<$offset1>" and "C<$offset2>" must lie within the
1087range "C<0>" and, respectively, "C<$vec1-E<gt>Size()-1>" or
1088"C<$vec2-E<gt>Size()-1>", or a fatal "offset out of range" error
1089will occur.
1090
1091Note further that "C<$vec1>" and "C<$vec2>" may be identical, i.e.,
1092you may copy a stretch of contiguous bits from one part of a given
1093bit vector to another part.
1094
1095The source and the target interval may even overlap, in which case
1096the copying is automatically performed in ascending or descending
1097order (depending on the direction of the copy - downwards or upwards
1098in the bit vector, respectively) to handle this situation correctly,
1099i.e., so that no bits are being overwritten before they have been
1100copied themselves.
1101
1102=item *
1103
1104C<$vec2-E<gt>Interval_Substitute($vec1,$off2,$len2,$off1,$len1);>
1105
1106This method is (roughly) the same for bit vectors (i.e., arrays
1107of booleans) as what the "splice" function in Perl is for lists
1108(i.e., arrays of scalars).
1109
1110(See L<perlfunc/splice> for more details about this function.)
1111
1112The method allows you to substitute a stretch of contiguous bits
1113(defined by a position (offset) "C<$off1>" and a length of "C<$len1>"
1114bits) from a given "source" bit vector "C<$vec1>" for a different
1115stretch of contiguous bits (defined by a position (offset) "C<$off2>"
1116and a length of "C<$len2>" bits) in another, "target" bit vector
1117"C<$vec2>".
1118
1119Note that the two bit vectors "C<$vec1>" and "C<$vec2>" do B<NOT>
1120need to have the same (matching) size!
1121
1122Note further that "C<$off1>" and "C<$off2>" must lie within the
1123range "C<0>" and, respectively, "C<$vec1-E<gt>Size()>" or
1124"C<$vec2-E<gt>Size()>", or a fatal "offset out of range" error
1125will occur.
1126
1127Alert readers will have noticed that these upper limits are B<NOT>
1128"C<$vec1-E<gt>Size()-1>" and "C<$vec2-E<gt>Size()-1>", as they would
1129be for any other method in this module, but that these offsets may
1130actually point to one position B<PAST THE END> of the corresponding
1131bit vector.
1132
1133This is necessary in order to make it possible to B<APPEND> a given
1134stretch of bits to the target bit vector instead of B<REPLACING>
1135something in it.
1136
1137For reasons of symmetry and generality, the same applies to the offset
1138in the source bit vector, even though such an offset (one position past
1139the end of the bit vector) does not serve any practical purpose there
1140(but does not cause any harm either).
1141
1142(Actually this saves you from the need of testing for this special case,
1143in certain circumstances.)
1144
1145Note that whenever the term "C<$off1 + $len1>" exceeds the size
1146"C<$vec1-E<gt>Size()>" of bit vector "C<$vec1>" (or if "C<$off2 + $len2>"
1147exceeds "C<$vec2-E<gt>Size()>"), the corresponding length ("C<$len1>"
1148or "C<$len2>", respectively) is automatically reduced internally
1149so that "C<$off1 + $len1 E<lt>= $vec1-E<gt>Size()>" (and
1150"C<$off2 + $len2 E<lt>= $vec2-E<gt>Size()>") holds.
1151
1152(Note that this does B<NOT> alter the intended result, even though
1153this may seem counter-intuitive at first!)
1154
1155This may even result in a length ("C<$len1>" or "C<$len2>") of zero.
1156
1157A length of zero for the interval in the B<SOURCE> bit vector
1158("C<$len1 == 0>") means that the indicated stretch of bits in
1159the target bit vector (starting at position "C<$off2>") is to
1160be replaced by B<NOTHING>, i.e., is to be B<DELETED>.
1161
1162A length of zero for the interval in the B<TARGET> bit vector
1163("C<$len2> == 0") means that B<NOTHING> is replaced, and that the
1164stretch of bits from the source bit vector is simply B<INSERTED>
1165into the target bit vector at the indicated position ("C<$off2>").
1166
1167If both length parameters are zero, nothing is done at all.
1168
1169Note that in contrast to any other method in this module (especially
1170"C<Interval_Copy()>", "C<Insert()>" and "C<Delete()>"), this method
1171B<IMPLICITLY> and B<AUTOMATICALLY> adapts the length of the resulting
1172bit vector as needed, as given by
1173
1174 $size = $vec2->Size(); # before
1175 $size += $len1 - $len2; # after
1176
1177(The only other method in this module that changes the size of a bit
1178vector is the method "C<Resize()>".)
1179
1180In other words, replacing a given interval of bits in the target bit
1181vector with a longer or shorter stretch of bits from the source bit
1182vector, or simply inserting ("C<$len2 == 0>") a stretch of bits into
1183or deleting ("C<$len1 == 0>") an interval of bits from the target bit
1184vector will automatically increase or decrease, respectively, the size
1185of the target bit vector accordingly.
1186
1187For the sake of generality, this may even result in a bit vector with
1188a size of zero (containing no bits at all).
1189
1190This is also the reason why bit vectors of length zero are permitted
1191in this module in the first place, starting with version 5.0.
1192
1193Finally, note that "C<$vec1>" and "C<$vec2>" may be identical, i.e.,
1194in-place processing is possible.
1195
1196(If you think about that for a while or if you look at the code,
1197you will see that this is far from trivial!)
1198
1199=item *
1200
1201C<if ($vector-E<gt>is_empty())>
1202
1203Tests whether the given bit vector is empty, i.e., whether B<ALL> of
1204its bits are cleared (in the "off" state).
1205
1206In "big integer" arithmetic, this is equivalent to testing whether
1207the number stored in the bit vector is zero ("C<0>").
1208
1209Returns "true" ("C<1>") if the bit vector is empty and "false" ("C<0>")
1210otherwise.
1211
1212Note that this method also returns "true" ("C<1>") if the given bit
1213vector has a length of zero, i.e., if it contains no bits at all.
1214
1215=item *
1216
1217C<if ($vector-E<gt>is_full())>
1218
1219Tests whether the given bit vector is full, i.e., whether B<ALL> of
1220its bits are set (in the "on" state).
1221
1222In "big integer" arithmetic, this is equivalent to testing whether
1223the number stored in the bit vector is minus one ("-1").
1224
1225Returns "true" ("C<1>") if the bit vector is full and "false" ("C<0>")
1226otherwise.
1227
1228If the given bit vector has a length of zero (i.e., if it contains
1229no bits at all), this method returns "false" ("C<0>").
1230
1231=item *
1232
1233C<if ($vec1-E<gt>equal($vec2))>
1234
1235Tests the two given bit vectors for equality.
1236
1237Returns "true" ("C<1>") if the two bit vectors are exact
1238copies of one another and "false" ("C<0>") otherwise.
1239
1240=item *
1241
1242C<$cmp = $vec1-E<gt>Lexicompare($vec2);>
1243
1244Compares the two given bit vectors, which are
1245regarded as B<UNSIGNED> numbers in binary representation.
1246
1247The method returns "C<-1>" if the first bit vector is smaller
1248than the second bit vector, "C<0>" if the two bit vectors are
1249exact copies of one another and "C<1>" if the first bit vector
1250is greater than the second bit vector.
1251
1252=item *
1253
1254C<$cmp = $vec1-E<gt>Compare($vec2);>
1255
1256Compares the two given bit vectors, which are
1257regarded as B<SIGNED> numbers in binary representation.
1258
1259The method returns "C<-1>" if the first bit vector is smaller
1260than the second bit vector, "C<0>" if the two bit vectors are
1261exact copies of one another and "C<1>" if the first bit vector
1262is greater than the second bit vector.
1263
1264=item *
1265
1266C<$string = $vector-E<gt>to_Hex();>
1267
1268Returns a hexadecimal string representing the given bit vector.
1269
1270Note that this representation is quite compact, in that it only
1271needs at most twice the number of bytes needed to store the bit
1272vector itself, internally.
1273
1274Note also that since a hexadecimal digit is always worth four bits,
1275the length of the resulting string is always a multiple of four bits,
1276regardless of the true length (in bits) of the given bit vector.
1277
1278Finally, note that the B<LEAST> significant hexadecimal digit is
1279located at the B<RIGHT> end of the resulting string, and the B<MOST>
1280significant digit at the B<LEFT> end.
1281
1282=item *
1283
1284C<$vector-E<gt>from_Hex($string);>
1285
1286Allows to read in the contents of a bit vector from a hexadecimal
1287string, such as returned by the method "C<to_Hex()>" (see above).
1288
1289Remember that the least significant bits are always to the right of a
1290hexadecimal string, and the most significant bits to the left. Therefore,
1291the string is actually read in from right to left while the bit vector
1292is filled accordingly, 4 bits at a time, starting with the least significant
1293bits and going upward to the most significant bits.
1294
1295If the given string contains less hexadecimal digits than are needed
1296to completely fill the given bit vector, the remaining (most significant)
1297bits are all cleared.
1298
1299This also means that, even if the given string does not contain enough digits
1300to completely fill the given bit vector, the previous contents of the
1301bit vector are erased completely.
1302
1303If the given string is longer than it needs to fill the given bit vector,
1304the superfluous characters are simply ignored.
1305
1306(In fact they are ignored completely - they are not even checked for
1307proper syntax. See also below for more about that.)
1308
1309This behaviour is intentional so that you may read in the string
1310representing one bit vector into another bit vector of different
1311size, i.e., as much of it as will fit.
1312
1313If during the process of reading the given string any character is
1314encountered which is not a hexadecimal digit, a fatal syntax error
1315ensues ("input string syntax error").
1316
1317=item *
1318
1319C<$string = $vector-E<gt>to_Bin();>
1320
1321Returns a binary string representing the given bit vector.
1322
1323Example:
1324
1325 $vector = Bit::Vector->new(8);
1326 $vector->Primes();
1327 $string = $vector->to_Bin();
1328 print "'$string'\n";
1329
1330This prints:
1331
1332 '10101100'
1333
1334(Bits #7, #5, #3 and #2 are set.)
1335
1336Note that the B<LEAST> significant bit is located at the B<RIGHT>
1337end of the resulting string, and the B<MOST> significant bit at
1338the B<LEFT> end.
1339
1340=item *
1341
1342C<$vector-E<gt>from_Bin($string);>
1343
1344This method allows you to read in the contents of a bit vector from a
1345binary string, such as returned by the method "C<to_Bin()>" (see above).
1346
1347Note that this method assumes that the B<LEAST> significant bit is located at
1348the B<RIGHT> end of the binary string, and the B<MOST> significant bit at the
1349B<LEFT> end. Therefore, the string is actually read in from right to left
1350while the bit vector is filled accordingly, one bit at a time, starting with
1351the least significant bit and going upward to the most significant bit.
1352
1353If the given string contains less binary digits ("C<0>" and "C<1>") than are
1354needed to completely fill the given bit vector, the remaining (most significant)
1355bits are all cleared.
1356
1357This also means that, even if the given string does not contain enough digits
1358to completely fill the given bit vector, the previous contents of the
1359bit vector are erased completely.
1360
1361If the given string is longer than it needs to fill the given bit vector,
1362the superfluous characters are simply ignored.
1363
1364(In fact they are ignored completely - they are not even checked for
1365proper syntax. See also below for more about that.)
1366
1367This behaviour is intentional so that you may read in the string
1368representing one bit vector into another bit vector of different
1369size, i.e., as much of it as will fit.
1370
1371If during the process of reading the given string any character is
1372encountered which is not either "C<0>" or "C<1>", a fatal syntax error
1373ensues ("input string syntax error").
1374
1375=item *
1376
1377C<$string = $vector-E<gt>to_Dec();>
1378
1379This method returns a string representing the contents of the given bit
1380vector converted to decimal (base C<10>).
1381
1382Note that this method assumes the given bit vector to be B<SIGNED> (and
1383to contain a number in two's complement binary representation).
1384
1385Consequently, whenever the most significant bit of the given bit vector
1386is set, the number stored in it is regarded as being B<NEGATIVE>.
1387
1388The resulting string can be fed into "C<from_Dec()>" (see below) in order
1389to copy the contents of this bit vector to another one (or to restore the
1390contents of this one). This is not advisable, though, since this would be
1391very inefficient (there are much more efficient methods for storing and
1392copying bit vectors in this module).
1393
1394Note that such conversion from binary to decimal is inherently slow
1395since the bit vector has to be repeatedly divided by C<10> with remainder
1396until the quotient becomes C<0> (each remainder in turn represents a single
1397decimal digit of the resulting string).
1398
1399This is also true for the implementation of this method in this module,
1400even though a considerable effort has been made to speed it up: instead of
1401repeatedly dividing by C<10>, the bit vector is repeatedly divided by the
1402largest power of C<10> that will fit into a machine word. The remainder is
1403then repeatedly divided by C<10> using only machine word arithmetics, which
1404is much faster than dividing the whole bit vector ("divide and rule" principle).
1405
1406According to my own measurements, this resulted in an 8-fold speed increase
1407over the straightforward approach.
1408
1409Still, conversion to decimal should be used only where absolutely necessary.
1410
1411Keep the resulting string stored in some variable if you need it again,
1412instead of converting the bit vector all over again.
1413
1414Beware that if you set the configuration for overloaded operators to
1415"output=decimal", this method will be called for every bit vector
1416enclosed in double quotes!
1417
1418=item *
1419
1420C<$vector-E<gt>from_Dec($string);>
1421
1422This method allows you to convert a given decimal number, which may be
1423positive or negative, into two's complement binary representation, which
1424is then stored in the given bit vector.
1425
1426The decimal number should always be provided as a string, to avoid possible
1427truncation (due to the limited precision of integers in Perl) or formatting
1428(due to Perl's use of scientific notation for large numbers), which would
1429lead to errors.
1430
1431If the binary representation of the given decimal number is too big to fit
1432into the given bit vector (if the given bit vector does not contain enough
1433bits to hold it), a fatal "numeric overflow error" occurs.
1434
1435If the input string contains other characters than decimal digits (C<0-9>)
1436and an optional leading sign ("C<+>" or "C<->"), a fatal "input string
1437syntax error" occurs.
1438
1439Beware that large positive numbers which cause the most significant bit to
1440be set (e.g. "255" in a bit vector with 8 bits) will be printed as negative
1441numbers when converted back to decimal using the method "to_Dec()" (e.g.
1442"-1", in our example), because numbers with the most significant bit set
1443are considered to be negative in two's complement binary representation.
1444
1445Note also that while it is possible to thusly enter negative numbers as
1446large positive numbers (e.g. "255" for "-1" in a bit vector with 8 bits),
1447the contrary isn't, i.e., you cannot enter "-255" for "+1", in our example.
1448A fatal "numeric overflow error" will occur if you try to do so.
1449
1450If possible program abortion is unwanted or intolerable, use
1451"C<eval>", like this:
1452
1453 eval { $vector->from_Dec("1152921504606846976"); };
1454 if ($@)
1455 {
1456 # an error occurred
1457 }
1458
1459There are four possible error messages:
1460
1461 if ($@ =~ /item is not a string/)
1462
1463 if ($@ =~ /input string syntax error/)
1464
1465 if ($@ =~ /numeric overflow error/)
1466
1467 if ($@ =~ /unable to allocate memory/)
1468
1469Note that the conversion from decimal to binary is costly in terms of
1470processing time, since a lot of multiplications have to be carried out
1471(in principle, each decimal digit must be multiplied with the binary
1472representation of the power of C<10> corresponding to its position in
1473the decimal number, i.e., 1, 10, 100, 1000, 10000 and so on).
1474
1475This is not as time consuming as the opposite conversion, from binary
1476to decimal (where successive divisions have to be carried out, which
1477are even more expensive than multiplications), but still noticeable.
1478
1479Again (as in the case of "C<to_Dec()>"), the implementation of this
1480method in this module uses the principle of "divide and rule" in order
1481to speed up the conversion, i.e., as many decimal digits as possible
1482are first accumulated (converted) in a machine word and only then
1483stored in the given bit vector.
1484
1485Even so, use this method only where absolutely necessary if speed is
1486an important consideration in your application.
1487
1488Beware that if you set the configuration for overloaded operators to
1489"input=decimal", this method will be called for every scalar operand
1490you use!
1491
1492=item *
1493
1494C<$string = $vector-E<gt>to_Enum();>
1495
1496Converts the given bit vector or set into an enumeration of single
1497indices and ranges of indices (".newsrc" style), representing the
1498bits that are set ("C<1>") in the bit vector.
1499
1500Example:
1501
1502 $vector = Bit::Vector->new(20);
1503 $vector->Bit_On(2);
1504 $vector->Bit_On(3);
1505 $vector->Bit_On(11);
1506 $vector->Interval_Fill(5,7);
1507 $vector->Interval_Fill(13,19);
1508 print "'", $vector->to_Enum(), "'\n";
1509
1510which prints
1511
1512 '2,3,5-7,11,13-19'
1513
1514If the given bit vector is empty, the resulting string will
1515also be empty.
1516
1517Note, by the way, that the above example can also be written
1518a little handier, perhaps, as follows:
1519
1520 Bit::Vector->Configuration("out=enum");
1521 $vector = Bit::Vector->new(20);
1522 $vector->Index_List_Store(2,3,5,6,7,11,13,14,15,16,17,18,19);
1523 print "'$vector'\n";
1524
1525=item *
1526
1527C<$vector-E<gt>from_Enum($string);>
1528
1529This method first empties the given bit vector and then tries to
1530set the bits and ranges of bits specified in the given string.
1531
1532The string "C<$string>" must only contain unsigned integers
1533or ranges of integers (two unsigned integers separated by a
1534dash "-"), separated by commas (",").
1535
1536All other characters are disallowed (including white space!)
1537and will lead to a fatal "input string syntax error".
1538
1539In each range, the first integer (the lower limit of the range)
1540must always be less than or equal to the second integer (the
1541upper limit), or a fatal "minimum > maximum index" error occurs.
1542
1543All integers must lie in the permitted range for the given
1544bit vector, i.e., they must lie between "C<0>" and
1545"C<$vector-E<gt>Size()-1>".
1546
1547If this condition is not met, a fatal "index out of range"
1548error occurs.
1549
1550If possible program abortion is unwanted or intolerable, use
1551"C<eval>", like this:
1552
1553 eval { $vector->from_Enum("2,3,5-7,11,13-19"); };
1554 if ($@)
1555 {
1556 # an error occurred
1557 }
1558
1559There are four possible error messages:
1560
1561 if ($@ =~ /item is not a string/)
1562
1563 if ($@ =~ /input string syntax error/)
1564
1565 if ($@ =~ /index out of range/)
1566
1567 if ($@ =~ /minimum > maximum index/)
1568
1569Note that the order of the indices and ranges is irrelevant,
1570i.e.,
1571
1572 eval { $vector->from_Enum("11,5-7,3,13-19,2"); };
1573
1574results in the same vector as in the example above.
1575
1576Ranges and indices may also overlap.
1577
1578This is because each (single) index in the string is passed
1579to the method "C<Bit_On()>", internally, and each range to
1580the method "C<Interval_Fill()>".
1581
1582This means that the resulting bit vector is just the union
1583of all the indices and ranges specified in the given string.
1584
1585=item *
1586
1587C<$vector-E<gt>Bit_Off($index);>
1588
1589Clears the bit with index "C<$index>" in the given vector.
1590
1591=item *
1592
1593C<$vector-E<gt>Bit_On($index);>
1594
1595Sets the bit with index "C<$index>" in the given vector.
1596
1597=item *
1598
1599C<$vector-E<gt>bit_flip($index)>
1600
1601Flips (i.e., complements) the bit with index "C<$index>"
1602in the given vector.
1603
1604Moreover, this method returns the B<NEW> state of the
1605bit in question, i.e., it returns "C<0>" if the bit is
1606cleared or "C<1>" if the bit is set (B<AFTER> flipping it).
1607
1608=item *
1609
1610C<if ($vector-E<gt>bit_test($index))>
1611
1612C<if ($vector-E<gt>contains($index))>
1613
1614Returns the current state of the bit with index "C<$index>"
1615in the given vector, i.e., returns "C<0>" if it is cleared
1616(in the "off" state) or "C<1>" if it is set (in the "on" state).
1617
1618=item *
1619
1620C<$vector-E<gt>Bit_Copy($index,$bit);>
1621
1622Sets the bit with index "C<$index>" in the given vector either
1623to "C<0>" or "C<1>" depending on the boolean value "C<$bit>".
1624
1625=item *
1626
1627C<$vector-E<gt>LSB($bit);>
1628
1629Allows you to set the least significant bit in the given bit
1630vector to the value given by the boolean parameter "C<$bit>".
1631
1632This is a (faster) shortcut for "C<$vector-E<gt>Bit_Copy(0,$bit);>".
1633
1634=item *
1635
1636C<$vector-E<gt>MSB($bit);>
1637
1638Allows you to set the most significant bit in the given bit
1639vector to the value given by the boolean parameter "C<$bit>".
1640
1641This is a (faster) shortcut for
1642"C<$vector-E<gt>Bit_Copy($vector-E<gt>Size()-1,$bit);>".
1643
1644=item *
1645
1646C<$bit = $vector-E<gt>lsb();>
1647
1648Returns the least significant bit of the given bit vector.
1649
1650This is a (faster) shortcut for "C<$bit = $vector-E<gt>bit_test(0);>".
1651
1652=item *
1653
1654C<$bit = $vector-E<gt>msb();>
1655
1656Returns the most significant bit of the given bit vector.
1657
1658This is a (faster) shortcut for
1659"C<$bit = $vector-E<gt>bit_test($vector-E<gt>Size()-1);>".
1660
1661=item *
1662
1663C<$carry_out = $vector-E<gt>rotate_left();>
1664
1665 carry MSB vector: LSB
1666 out:
1667 +---+ +---+---+---+--- ---+---+---+---+
1668 | | <---+--- | | | | ... | | | | <---+
1669 +---+ | +---+---+---+--- ---+---+---+---+ |
1670 | |
1671 +------------------------------------------------+
1672
1673The least significant bit (LSB) is the bit with index "C<0>", the most
1674significant bit (MSB) is the bit with index "C<$vector-E<gt>Size()-1>".
1675
1676=item *
1677
1678C<$carry_out = $vector-E<gt>rotate_right();>
1679
1680 MSB vector: LSB carry
1681 out:
1682 +---+---+---+--- ---+---+---+---+ +---+
1683 +---> | | | | ... | | | | ---+---> | |
1684 | +---+---+---+--- ---+---+---+---+ | +---+
1685 | |
1686 +------------------------------------------------+
1687
1688The least significant bit (LSB) is the bit with index "C<0>", the most
1689significant bit (MSB) is the bit with index "C<$vector-E<gt>Size()-1>".
1690
1691=item *
1692
1693C<$carry_out = $vector-E<gt>shift_left($carry_in);>
1694
1695 carry MSB vector: LSB carry
1696 out: in:
1697 +---+ +---+---+---+--- ---+---+---+---+ +---+
1698 | | <--- | | | | ... | | | | <--- | |
1699 +---+ +---+---+---+--- ---+---+---+---+ +---+
1700
1701The least significant bit (LSB) is the bit with index "C<0>", the most
1702significant bit (MSB) is the bit with index "C<$vector-E<gt>Size()-1>".
1703
1704=item *
1705
1706C<$carry_out = $vector-E<gt>shift_right($carry_in);>
1707
1708 carry MSB vector: LSB carry
1709 in: out:
1710 +---+ +---+---+---+--- ---+---+---+---+ +---+
1711 | | ---> | | | | ... | | | | ---> | |
1712 +---+ +---+---+---+--- ---+---+---+---+ +---+
1713
1714The least significant bit (LSB) is the bit with index "C<0>", the most
1715significant bit (MSB) is the bit with index "C<$vector-E<gt>Size()-1>".
1716
1717=item *
1718
1719C<$vector-E<gt>Move_Left($bits);>
1720
1721Shifts the given bit vector left by "C<$bits>" bits, i.e., inserts "C<$bits>"
1722new bits at the lower end (least significant bit) of the bit vector, moving
1723all other bits up by "C<$bits>" places, thereby losing the "C<$bits>" most
1724significant bits.
1725
1726The inserted new bits are all cleared (set to the "off" state).
1727
1728This method does nothing if "C<$bits>" is equal to zero.
1729
1730Beware that the whole bit vector is cleared B<WITHOUT WARNING> if
1731"C<$bits>" is greater than or equal to the size of the given bit vector!
1732
1733In fact this method is equivalent to
1734
1735 for ( $i = 0; $i < $bits; $i++ ) { $vector->shift_left(0); }
1736
1737except that it is much more efficient (for "C<$bits>" greater than or
1738equal to the number of bits in a machine word on your system) than
1739this straightforward approach.
1740
1741=item *
1742
1743C<$vector-E<gt>Move_Right($bits);>
1744
1745Shifts the given bit vector right by "C<$bits>" bits, i.e., deletes the
1746"C<$bits>" least significant bits of the bit vector, moving all other bits
1747down by "C<$bits>" places, thereby creating "C<$bits>" new bits at the upper
1748end (most significant bit) of the bit vector.
1749
1750These new bits are all cleared (set to the "off" state).
1751
1752This method does nothing if "C<$bits>" is equal to zero.
1753
1754Beware that the whole bit vector is cleared B<WITHOUT WARNING> if
1755"C<$bits>" is greater than or equal to the size of the given bit vector!
1756
1757In fact this method is equivalent to
1758
1759 for ( $i = 0; $i < $bits; $i++ ) { $vector->shift_right(0); }
1760
1761except that it is much more efficient (for "C<$bits>" greater than or
1762equal to the number of bits in a machine word on your system) than
1763this straightforward approach.
1764
1765=item *
1766
1767C<$vector-E<gt>Insert($offset,$bits);>
1768
1769This method inserts "C<$bits>" fresh new bits at position "C<$offset>"
1770in the given bit vector.
1771
1772The "C<$bits>" most significant bits are lost, and all bits starting
1773with bit number "C<$offset>" up to and including bit number
1774"C<$vector-E<gt>Size()-$bits-1>" are moved up by "C<$bits>" places.
1775
1776The now vacant "C<$bits>" bits starting at bit number "C<$offset>"
1777(up to and including bit number "C<$offset+$bits-1>") are then set
1778to zero (cleared).
1779
1780Note that this method does B<NOT> increase the size of the given bit
1781vector, i.e., the bit vector is B<NOT> extended at its upper end to
1782"rescue" the "C<$bits>" uppermost (most significant) bits - instead,
1783these bits are lost forever.
1784
1785If you don't want this to happen, you have to increase the size of the
1786given bit vector B<EXPLICITLY> and B<BEFORE> you perform the "Insert"
1787operation, with a statement such as the following:
1788
1789 $vector->Resize($vector->Size() + $bits);
1790
1791Or use the method "C<Interval_Substitute()>" instead of "C<Insert()>",
1792which performs automatic growing and shrinking of its target bit vector.
1793
1794Note also that "C<$offset>" must lie in the permitted range between
1795"C<0>" and "C<$vector-E<gt>Size()-1>", or a fatal "offset out of range"
1796error will occur.
1797
1798If the term "C<$offset + $bits>" exceeds "C<$vector-E<gt>Size()-1>",
1799all the bits starting with bit number "C<$offset>" up to bit number
1800"C<$vector-E<gt>Size()-1>" are simply cleared.
1801
1802=item *
1803
1804C<$vector-E<gt>Delete($offset,$bits);>
1805
1806This method deletes, i.e., removes the bits starting at position
1807"C<$offset>" up to and including bit number "C<$offset+$bits-1>"
1808from the given bit vector.
1809
1810The remaining uppermost bits (starting at position "C<$offset+$bits>"
1811up to and including bit number "C<$vector-E<gt>Size()-1>") are moved
1812down by "C<$bits>" places.
1813
1814The now vacant uppermost (most significant) "C<$bits>" bits are then
1815set to zero (cleared).
1816
1817Note that this method does B<NOT> decrease the size of the given bit
1818vector, i.e., the bit vector is B<NOT> clipped at its upper end to
1819"get rid of" the vacant "C<$bits>" uppermost bits.
1820
1821If you don't want this, i.e., if you want the bit vector to shrink
1822accordingly, you have to do so B<EXPLICITLY> and B<AFTER> the "Delete"
1823operation, with a couple of statements such as these:
1824
1825 $size = $vector->Size();
1826 if ($bits > $size) { $bits = $size; }
1827 $vector->Resize($size - $bits);
1828
1829Or use the method "C<Interval_Substitute()>" instead of "C<Delete()>",
1830which performs automatic growing and shrinking of its target bit vector.
1831
1832Note also that "C<$offset>" must lie in the permitted range between
1833"C<0>" and "C<$vector-E<gt>Size()-1>", or a fatal "offset out of range"
1834error will occur.
1835
1836If the term "C<$offset + $bits>" exceeds "C<$vector-E<gt>Size()-1>",
1837all the bits starting with bit number "C<$offset>" up to bit number
1838"C<$vector-E<gt>Size()-1>" are simply cleared.
1839
1840=item *
1841
1842C<$carry = $vector-E<gt>increment();>
1843
1844This method increments the given bit vector.
1845
1846Note that this method regards bit vectors as being unsigned,
1847i.e., the largest possible positive number is directly
1848followed by the smallest possible (or greatest possible,
1849speaking in absolute terms) negative number:
1850
1851 before: 2 ^ (b-1) - 1 (= "0111...1111")
1852 after: 2 ^ (b-1) (= "1000...0000")
1853
1854where "C<b>" is the number of bits of the given bit vector.
1855
1856The method returns "false" ("C<0>") in all cases except when a
1857carry over occurs (in which case it returns "true", i.e., "C<1>"),
1858which happens when the number "1111...1111" is incremented,
1859which gives "0000...0000" plus a carry over to the next higher
1860(binary) digit.
1861
1862This can be used for the terminating condition of a "while" loop,
1863for instance, in order to cycle through all possible values the
1864bit vector can assume.
1865
1866=item *
1867
1868C<$carry = $vector-E<gt>decrement();>
1869
1870This method decrements the given bit vector.
1871
1872Note that this method regards bit vectors as being unsigned,
1873i.e., the smallest possible (or greatest possible, speaking
1874in absolute terms) negative number is directly followed by
1875the largest possible positive number:
1876
1877 before: 2 ^ (b-1) (= "1000...0000")
1878 after: 2 ^ (b-1) - 1 (= "0111...1111")
1879
1880where "C<b>" is the number of bits of the given bit vector.
1881
1882The method returns "false" ("C<0>") in all cases except when a
1883carry over occurs (in which case it returns "true", i.e., "C<1>"),
1884which happens when the number "0000...0000" is decremented,
1885which gives "1111...1111" minus a carry over to the next higher
1886(binary) digit.
1887
1888This can be used for the terminating condition of a "while" loop,
1889for instance, in order to cycle through all possible values the
1890bit vector can assume.
1891
1892=item *
1893
1894C<$overflow = $vec2-E<gt>inc($vec1);>
1895
1896This method copies the contents of bit vector "C<$vec1>" to bit
1897vector "C<$vec2>" and increments the copy (not the original).
1898
1899If by incrementing the number its sign becomes invalid, the return
1900value ("overflow" flag) will be true ("C<1>"), or false ("C<0>")
1901if not. (See the description of the method "add()" below for
1902a more in-depth explanation of what "overflow" means).
1903
1904Note that in-place operation is also possible, i.e., "C<$vec1>"
1905and "C<$vec2>" may be identical.
1906
1907=item *
1908
1909C<$overflow = $vec2-E<gt>dec($vec1);>
1910
1911This method copies the contents of bit vector "C<$vec1>" to bit
1912vector "C<$vec2>" and decrements the copy (not the original).
1913
1914If by decrementing the number its sign becomes invalid, the return
1915value ("overflow" flag) will be true ("C<1>"), or false ("C<0>")
1916if not. (See the description of the method "subtract()" below
1917for a more in-depth explanation of what "overflow" means).
1918
1919Note that in-place operation is also possible, i.e., "C<$vec1>"
1920and "C<$vec2>" may be identical.
1921
1922=item *
1923
1924C<$carry = $vec3-E<gt>add($vec1,$vec2,$carry);>
1925
1926C<($carry,$overflow) = $vec3-E<gt>add($vec1,$vec2,$carry);>
1927
1928This method adds the two numbers contained in bit vector "C<$vec1>"
1929and "C<$vec2>" with carry "C<$carry>" and stores the result in
1930bit vector "C<$vec3>".
1931
1932I.e.,
1933 $vec3 = $vec1 + $vec2 + $carry
1934
1935Note that the "C<$carry>" parameter is a boolean value, i.e.,
1936only its least significant bit is taken into account. (Think of
1937it as though "C<$carry &= 1;>" was always executed internally.)
1938
1939In scalar context, the method returns a boolean value which
1940indicates if a carry over (to the next higher bit position)
1941has occured. In list context, the method returns the carry
1942and the overflow flag (in this order).
1943
1944The overflow flag is true ("C<1>") if the sign (i.e., the most
1945significant bit) of the result is wrong. This can happen when
1946adding two very large positive numbers or when adding two (by
1947their absolute value) very large negative numbers. See also
1948further below.
1949
1950The carry in- and output is needed mainly for cascading, i.e.,
1951to add numbers that are fragmented into several pieces.
1952
1953Example:
1954
1955 # initialize
1956
1957 for ( $i = 0; $i < $n; $i++ )
1958 {
1959 $a[$i] = Bit::Vector->new($bits);
1960 $b[$i] = Bit::Vector->new($bits);
1961 $c[$i] = Bit::Vector->new($bits);
1962 }
1963
1964 # fill @a and @b
1965
1966 # $a[ 0 ] is low order part,
1967 # $a[$n-1] is high order part,
1968 # and same for @b
1969
1970 # add
1971
1972 $carry = 0;
1973 for ( $i = 0; $i < $n; $i++ )
1974 {
1975 $carry = $c[$i]->add($a[$i],$b[$i],$carry);
1976 }
1977
1978Note that it makes no difference to this method whether the numbers
1979in "C<$vec1>" and "C<$vec2>" are unsigned or signed (i.e., in two's
1980complement binary representation).
1981
1982Note however that the return value (carry flag) is not meaningful
1983when the numbers are B<SIGNED>.
1984
1985Moreover, when the numbers are signed, a special type of error can
1986occur which is commonly called an "overflow error".
1987
1988An overflow error occurs when the sign of the result (its most
1989significant bit) is flipped (i.e., falsified) by a carry over
1990from the next-lower bit position ("MSB-1").
1991
1992In fact matters are a bit more complicated than that: the overflow
1993flag is set to "true" whenever there is a carry over from bit
1994position MSB-1 to the most significant bit (MSB) but no carry
1995over from the MSB to the output carry flag, or vice-versa, i.e.,
1996when there is no carry over from bit position MSB-1 to the most
1997significant bit (MSB) but a carry over to the output carry flag.
1998
1999Thus the overflow flag is the result of an exclusive-or operation
2000between incoming and outgoing carry over at the most significant
2001bit position.
2002
2003=item *
2004
2005C<$carry = $vec3-E<gt>subtract($vec1,$vec2,$carry);>
2006
2007C<($carry,$overflow) = $vec3-E<gt>subtract($vec1,$vec2,$carry);>
2008
2009This method subtracts the two numbers contained in bit vector
2010"C<$vec1>" and "C<$vec2>" with carry "C<$carry>" and stores the
2011result in bit vector "C<$vec3>".
2012
2013I.e.,
2014 $vec3 = $vec1 - $vec2 - $carry
2015
2016Note that the "C<$carry>" parameter is a boolean value, i.e.,
2017only its least significant bit is taken into account. (Think of
2018it as though "C<$carry &= 1;>" was always executed internally.)
2019
2020In scalar context, the method returns a boolean value which
2021indicates if a carry over (to the next higher bit position)
2022has occured. In list context, the method returns the carry
2023and the overflow flag (in this order).
2024
2025The overflow flag is true ("C<1>") if the sign (i.e., the most
2026significant bit) of the result is wrong. This can happen when
2027subtracting a very large negative number from a very large
2028positive number or vice-versa. See also further below.
2029
2030The carry in- and output is needed mainly for cascading, i.e.,
2031to subtract numbers that are fragmented into several pieces.
2032
2033Example:
2034
2035 # initialize
2036
2037 for ( $i = 0; $i < $n; $i++ )
2038 {
2039 $a[$i] = Bit::Vector->new($bits);
2040 $b[$i] = Bit::Vector->new($bits);
2041 $c[$i] = Bit::Vector->new($bits);
2042 }
2043
2044 # fill @a and @b
2045
2046 # $a[ 0 ] is low order part,
2047 # $a[$n-1] is high order part,
2048 # and same for @b
2049
2050 # subtract
2051
2052 $carry = 0;
2053 for ( $i = 0; $i < $n; $i++ )
2054 {
2055 $carry = $c[$i]->subtract($a[$i],$b[$i],$carry);
2056 }
2057
2058Note that it makes no difference to this method whether the numbers
2059in "C<$vec1>" and "C<$vec2>" are unsigned or signed (i.e., in two's
2060complement binary representation).
2061
2062Note however that the return value (carry flag) is not meaningful
2063when the numbers are B<SIGNED>.
2064
2065Moreover, when the numbers are signed, a special type of error can
2066occur which is commonly called an "overflow error".
2067
2068An overflow error occurs when the sign of the result (its most
2069significant bit) is flipped (i.e., falsified) by a carry over
2070from the next-lower bit position ("MSB-1").
2071
2072In fact matters are a bit more complicated than that: the overflow
2073flag is set to "true" whenever there is a carry over from bit
2074position MSB-1 to the most significant bit (MSB) but no carry
2075over from the MSB to the output carry flag, or vice-versa, i.e.,
2076when there is no carry over from bit position MSB-1 to the most
2077significant bit (MSB) but a carry over to the output carry flag.
2078
2079Thus the overflow flag is the result of an exclusive-or operation
2080between incoming and outgoing carry over at the most significant
2081bit position.
2082
2083=item *
2084
2085C<$vec2-E<gt>Neg($vec1);>
2086
2087C<$vec2-E<gt>Negate($vec1);>
2088
2089This method calculates the two's complement of the number in bit
2090vector "C<$vec1>" and stores the result in bit vector "C<$vec2>".
2091
2092Calculating the two's complement of a given number in binary representation
2093consists of inverting all bits and incrementing the result by one.
2094
2095This is the same as changing the sign of the given number from "C<+>" to
2096"C<->" or vice-versa. In other words, applying this method twice on a given
2097number yields the original number again.
2098
2099Note that in-place processing is also possible, i.e., "C<$vec1>" and
2100"C<$vec2>" may be identical.
2101
2102Most importantly, beware that this method produces a counter-intuitive
2103result if the number contained in bit vector "C<$vec1>" is C<2 ^ (n-1)>
2104(i.e., "1000...0000"), where "C<n>" is the number of bits the given bit
2105vector contains: The negated value of this number is the number itself!
2106
2107=item *
2108
2109C<$vec2-E<gt>Abs($vec1);>
2110
2111C<$vec2-E<gt>Absolute($vec1);>
2112
2113Depending on the sign (i.e., the most significant bit) of the number in
2114bit vector "C<$vec1>", the contents of bit vector "C<$vec1>" are copied
2115to bit vector "C<$vec2>" either with the method "C<Copy()>" (if the number
2116in bit vector "C<$vec1>" is positive), or with "C<Negate()>" (if the number
2117in bit vector "C<$vec1>" is negative).
2118
2119In other words, this method calculates the absolute value of the number
2120in bit vector "C<$vec1>" and stores the result in bit vector "C<$vec2>".
2121
2122Note that in-place processing is also possible, i.e., "C<$vec1>" and
2123"C<$vec2>" may be identical.
2124
2125Most importantly, beware that this method produces a counter-intuitive
2126result if the number contained in bit vector "C<$vec1>" is C<2 ^ (n-1)>
2127(i.e., "1000...0000"), where "C<n>" is the number of bits the given bit
2128vector contains: The absolute value of this number is the number itself,
2129even though this number is still negative by definition (the most
2130significant bit is still set)!
2131
2132=item *
2133
2134C<$sign = $vector-E<gt>Sign();>
2135
2136This method returns "C<0>" if all bits in the given bit vector are cleared,
2137i.e., if the given bit vector contains the number "C<0>", or if the given
2138bit vector has a length of zero (contains no bits at all).
2139
2140If not all bits are cleared, this method returns "C<-1>" if the most
2141significant bit is set (i.e., if the bit vector contains a negative
2142number), or "C<1>" otherwise (i.e., if the bit vector contains a
2143positive number).
2144
2145=item *
2146
2147C<$vec3-E<gt>Multiply($vec1,$vec2);>
2148
2149This method multiplies the two numbers contained in bit vector "C<$vec1>"
2150and "C<$vec2>" and stores the result in bit vector "C<$vec3>".
2151
2152Note that this method regards its arguments as B<SIGNED>.
2153
2154If you want to make sure that a large number can never be treated as being
2155negative by mistake, make your bit vectors at least one bit longer than the
2156largest number you wish to represent, right from the start, or proceed as
2157follows:
2158
2159 $msb1 = $vec1->msb();
2160 $msb2 = $vec2->msb();
2161 $vec1->Resize($vec1->Size()+1);
2162 $vec2->Resize($vec2->Size()+1);
2163 $vec3->Resize($vec3->Size()+1);
2164 $vec1->MSB($msb1);
2165 $vec2->MSB($msb2);
2166 $vec3->Multiply($vec1,$vec2);
2167
2168Note also that all three bit vector arguments must in principle obey the
2169rule of matching sizes, but that the bit vector "C<$vec3>" may be larger
2170than the two factors "C<$vec1>" and "C<$vec2>".
2171
2172In fact multiplying two binary numbers with "C<n>" bits may yield a result
2173which is at most "C<2n>" bits long.
2174
2175Therefore, it is usually a good idea to let bit vector "C<$vec3>" have
2176twice the size of bit vector "C<$vec1>" and "C<$vec2>", unless you are
2177absolutely sure that the result will fit into a bit vector of the same
2178size as the two factors.
2179
2180If you are wrong, a fatal "numeric overflow error" will occur.
2181
2182Finally, note that in-place processing is possible, i.e., "C<$vec3>"
2183may be identical with "C<$vec1>" or "C<$vec2>", or both.
2184
2185=item *
2186
2187C<$quot-E<gt>Divide($vec1,$vec2,$rest);>
2188
2189This method divides the two numbers contained in bit vector "C<$vec1>"
2190and "C<$vec2>" and stores the quotient in bit vector "C<$quot>" and
2191the remainder in bit vector "C<$rest>".
2192
2193I.e.,
2194 $quot = $vec1 / $vec2; # div
2195 $rest = $vec1 % $vec2; # mod
2196
2197Therefore, "C<$quot>" and "C<$rest>" must be two B<DISTINCT> bit vectors,
2198or a fatal "result vector(s) must be distinct" error will occur.
2199
2200Note also that a fatal "division by zero error" will occur if "C<$vec2>"
2201is equal to zero.
2202
2203Note further that this method regards its arguments as B<SIGNED>.
2204
2205If you want to make sure that a large number can never be treated as being
2206negative by mistake, make your bit vectors at least one bit longer than the
2207largest number you wish to represent, right from the start, or proceed as
2208follows:
2209
2210 $msb1 = $vec1->msb();
2211 $msb2 = $vec2->msb();
2212 $vec1->Resize($vec1->Size()+1);
2213 $vec2->Resize($vec2->Size()+1);
2214 $quot->Resize($quot->Size()+1);
2215 $rest->Resize($rest->Size()+1);
2216 $vec1->MSB($msb1);
2217 $vec2->MSB($msb2);
2218 $quot->Divide($vec1,$vec2,$rest);
2219
2220Finally, note that in-place processing is possible, i.e., "C<$quot>"
2221may be identical with "C<$vec1>" or "C<$vec2>" or both, and "C<$rest>"
2222may also be identical with "C<$vec1>" or "C<$vec2>" or both, as long
2223as "C<$quot>" and "C<$rest>" are distinct. (!)
2224
2225=item *
2226
2227C<$vecgcd-E<gt>GCD($veca,$vecb);>
2228
2229This method calculates the "Greatest Common Divisor" of the two numbers
2230contained in bit vector "C<$veca>" and "C<$vecb>" and stores the result
2231in bit vector "C<$vecgcd>".
2232
2233The method uses Euklid's algorithm internally:
2234
2235 int GCD(int a, int b)
2236 {
2237 int t;
2238
2239 while (b != 0)
2240 {
2241 t = a % b; /* = remainder of (a div b) */
2242 a = b;
2243 b = t;
2244 }
2245 return(a);
2246 }
2247
2248Note that C<GCD(z,0) == GCD(0,z) == z>.
2249
2250=item *
2251
2252C<$vecgcd-E<gt>GCD($vecx,$vecy,$veca,$vecb);>
2253
2254This variant of the "GCD" method calculates the "Greatest Common Divisor"
2255of the two numbers contained in bit vector "C<$veca>" and "C<$vecb>" and
2256stores the result in bit vector "C<$vecgcd>".
2257
2258Moreover, it determines the two factors which are necessary in order to
2259represent the greatest common divisor as a linear combination of its two
2260arguments, i.e., the two factors C<"x"> and C<"y"> so that
2261C<GCD(a,b) == x * a + y * b>, and stores them in bit vector "C<$vecx>"
2262and "C<$vecy>", respectively.
2263
2264For example:
2265
2266 a = 2322
2267 b = 654
2268
2269 GCD( 2322, 654 ) == 6
2270
2271 x = 20
2272 y = -71
2273
2274 20 * 2322 - 71 * 654 == 6
2275
2276Please see http://www.cut-the-knot.org/blue/extension.shtml
2277for an explanation of how this extension of Euklid's algorithm works.
2278
2279=item *
2280
2281C<$vec3-E<gt>Power($vec1,$vec2);>
2282
2283This method calculates the exponentiation of base "C<$vec1>" elevated to
2284the "C<$vec2>" power, i.e., "C<$vec1 ** $vec2>", and stores the result
2285in bit vector "C<$vec3>".
2286
2287The method uses an efficient divide-and-conquer algorithm:
2288
2289Suppose the exponent is (decimal) 13, for example. The binary
2290representation of this exponent is "1101".
2291
2292This means we want to calculate
2293
2294 $vec1 * $vec1 * $vec1 * $vec1 * $vec1 * $vec1 * $vec1 * $vec1 *
2295 $vec1 * $vec1 * $vec1 * $vec1 *
2296 $vec1
2297
2298That is, "C<$vec1>" multiplied with itself 13 times. The grouping
2299into lines above is no coincidence. The first line comprises 8
2300factors, the second contains 4, and the last line just one. This
2301just happens to be the binary representation of 13. C<;-)>
2302
2303We then calculate a series of squares (of squares of squares...) of
2304the base, i.e.,
2305
2306 $power[0] = $vec1;
2307 $power[1] = $vec1 * $vec1;
2308 $power[2] = $power[1] * $power[1];
2309 $power[3] = $power[2] * $power[2];
2310 etc.
2311
2312To calculate the power of our example, we simply initialize our result
2313with 1 and consecutively multiply it with the items of the series of
2314powers we just calculated, if the corresponding bit of the binary
2315representation of the exponent is set:
2316
2317 $result = 1;
2318 $result *= $power[0] if ($vec2 & 1);
2319 $result *= $power[1] if ($vec2 & 2);
2320 $result *= $power[2] if ($vec2 & 4);
2321 $result *= $power[3] if ($vec2 & 8);
2322 etc.
2323
2324The bit vector "C<$vec3>" must be of the same size as the base
2325"C<$vec1>" or greater. "C<$vec3>" and "C<$vec1>" may be the same
2326vector (i.e., in-place calculation as in "C<$vec1 **= $vec2;>" is
2327possible), but "C<$vec3>" and "C<$vec2>" must be distinct. Finally,
2328the exponent "C<$vec2>" must be positive. A fatal error occurs if
2329any of these conditions is not met.
2330
2331=item *
2332
2333C<$vector-E<gt>Block_Store($buffer);>
2334
2335This method allows you to load the contents of a given bit vector in
2336one go.
2337
2338This is useful when you store the contents of a bit vector in a file,
2339for instance (using method "C<Block_Read()>"), and when you want to
2340restore the previously saved bit vector.
2341
2342For this, "C<$buffer>" B<MUST> be a string (B<NO> automatic conversion
2343from numeric to string is provided here as would normally in Perl!)
2344containing the bit vector in "low order byte first" order.
2345
2346If the given string is shorter than what is needed to completely fill
2347the given bit vector, the remaining (most significant) bytes of the
2348bit vector are filled with zeros, i.e., the previous contents of the
2349bit vector are always erased completely.
2350
2351If the given string is longer than what is needed to completely fill
2352the given bit vector, the superfluous bytes are simply ignored.
2353
2354See L<perlfunc/sysread> for how to read in the contents of "C<$buffer>"
2355from a file prior to passing it to this method.
2356
2357=item *
2358
2359C<$buffer = $vector-E<gt>Block_Read();>
2360
2361This method allows you to export the contents of a given bit vector in
2362one block.
2363
2364This is useful when you want to save the contents of a bit vector for
2365later, for instance in a file.
2366
2367The advantage of this method is that it allows you to do so in the
2368compactest possible format, in binary.
2369
2370The method returns a Perl string which contains an exact copy of the
2371contents of the given bit vector in "low order byte first" order.
2372
2373See L<perlfunc/syswrite> for how to write the data from this string
2374to a file.
2375
2376=item *
2377
2378C<$size = $vector-E<gt>Word_Size();>
2379
2380Each bit vector is internally organized as an array of machine words.
2381
2382The methods whose names begin with "Word_" allow you to access this
2383internal array of machine words.
2384
2385Note that because the size of a machine word may vary from system to
2386system, these methods are inherently B<MACHINE-DEPENDENT>!
2387
2388Therefore, B<DO NOT USE> these methods unless you are absolutely certain
2389that portability of your code is not an issue!
2390
2391You have been warned!
2392
2393To be machine-independent, use the methods whose names begin with "C<Chunk_>"
2394instead, with chunk sizes no greater than 32 bits.
2395
2396The method "C<Word_Size()>" returns the number of machine words that the
2397internal array of words of the given bit vector contains.
2398
2399This is similar in function to the term "C<scalar(@array)>" for a Perl array.
2400
2401=item *
2402
2403C<$vector-E<gt>Word_Store($offset,$word);>
2404
2405This method allows you to store a given value "C<$word>" at a given
2406position "C<$offset>" in the internal array of words of the given
2407bit vector.
2408
2409Note that "C<$offset>" must lie in the permitted range between "C<0>"
2410and "C<$vector-E<gt>Word_Size()-1>", or a fatal "offset out of range"
2411error will occur.
2412
2413This method is similar in function to the expression
2414"C<$array[$offset] = $word;>" for a Perl array.
2415
2416=item *
2417
2418C<$word = $vector-E<gt>Word_Read($offset);>
2419
2420This method allows you to access the value of a given machine word
2421at position "C<$offset>" in the internal array of words of the given
2422bit vector.
2423
2424Note that "C<$offset>" must lie in the permitted range between "C<0>"
2425and "C<$vector-E<gt>Word_Size()-1>", or a fatal "offset out of range"
2426error will occur.
2427
2428This method is similar in function to the expression
2429"C<$word = $array[$offset];>" for a Perl array.
2430
2431=item *
2432
2433C<$vector-E<gt>Word_List_Store(@words);>
2434
2435This method allows you to store a list of values "C<@words>" in the
2436internal array of machine words of the given bit vector.
2437
2438Thereby the B<LEFTMOST> value in the list ("C<$words[0]>") is stored
2439in the B<LEAST> significant word of the internal array of words (the
2440one with offset "C<0>"), the next value from the list ("C<$words[1]>")
2441is stored in the word with offset "C<1>", and so on, as intuitively
2442expected.
2443
2444If the list "C<@words>" contains fewer elements than the internal
2445array of words of the given bit vector contains machine words,
2446the remaining (most significant) words are filled with zeros.
2447
2448If the list "C<@words>" contains more elements than the internal
2449array of words of the given bit vector contains machine words,
2450the superfluous values are simply ignored.
2451
2452This method is comparable in function to the expression
2453"C<@array = @words;>" for a Perl array.
2454
2455=item *
2456
2457C<@words = $vector-E<gt>Word_List_Read();>
2458
2459This method allows you to retrieve the internal array of machine
2460words of the given bit vector all at once.
2461
2462Thereby the B<LEFTMOST> value in the returned list ("C<$words[0]>")
2463is the B<LEAST> significant word from the given bit vector, and the
2464B<RIGHTMOST> value in the returned list ("C<$words[$#words]>") is
2465the B<MOST> significant word of the given bit vector.
2466
2467This method is similar in function to the expression
2468"C<@words = @array;>" for a Perl array.
2469
2470=item *
2471
2472C<$vector-E<gt>Word_Insert($offset,$count);>
2473
2474This method inserts "C<$count>" empty new machine words at position
2475"C<$offset>" in the internal array of words of the given bit vector.
2476
2477The "C<$count>" most significant words are lost, and all words starting
2478with word number "C<$offset>" up to and including word number
2479"C<$vector-E<gt>Word_Size()-$count-1>" are moved up by "C<$count>" places.
2480
2481The now vacant "C<$count>" words starting at word number "C<$offset>"
2482(up to and including word number "C<$offset+$count-1>") are then set
2483to zero (cleared).
2484
2485Note that this method does B<NOT> increase the size of the given bit
2486vector, i.e., the bit vector is B<NOT> extended at its upper end to
2487"rescue" the "C<$count>" uppermost (most significant) words - instead,
2488these words are lost forever.
2489
2490If you don't want this to happen, you have to increase the size of the
2491given bit vector B<EXPLICITLY> and B<BEFORE> you perform the "Insert"
2492operation, with a statement such as the following:
2493
2494 $vector->Resize($vector->Size() + $count * Bit::Vector->Word_Bits());
2495
2496Note also that "C<$offset>" must lie in the permitted range between
2497"C<0>" and "C<$vector-E<gt>Word_Size()-1>", or a fatal "offset out
2498of range" error will occur.
2499
2500If the term "C<$offset + $count>" exceeds "C<$vector-E<gt>Word_Size()-1>",
2501all the words starting with word number "C<$offset>" up to word number
2502"C<$vector-E<gt>Word_Size()-1>" are simply cleared.
2503
2504=item *
2505
2506C<$vector-E<gt>Word_Delete($offset,$count);>
2507
2508This method deletes, i.e., removes the words starting at position
2509"C<$offset>" up to and including word number "C<$offset+$count-1>"
2510from the internal array of machine words of the given bit vector.
2511
2512The remaining uppermost words (starting at position "C<$offset+$count>"
2513up to and including word number "C<$vector-E<gt>Word_Size()-1>") are
2514moved down by "C<$count>" places.
2515
2516The now vacant uppermost (most significant) "C<$count>" words are then
2517set to zero (cleared).
2518
2519Note that this method does B<NOT> decrease the size of the given bit
2520vector, i.e., the bit vector is B<NOT> clipped at its upper end to
2521"get rid of" the vacant "C<$count>" uppermost words.
2522
2523If you don't want this, i.e., if you want the bit vector to shrink
2524accordingly, you have to do so B<EXPLICITLY> and B<AFTER> the "Delete"
2525operation, with a couple of statements such as these:
2526
2527 $bits = $vector->Size();
2528 $count *= Bit::Vector->Word_Bits();
2529 if ($count > $bits) { $count = $bits; }
2530 $vector->Resize($bits - $count);
2531
2532Note also that "C<$offset>" must lie in the permitted range between
2533"C<0>" and "C<$vector-E<gt>Word_Size()-1>", or a fatal "offset out
2534of range" error will occur.
2535
2536If the term "C<$offset + $count>" exceeds "C<$vector-E<gt>Word_Size()-1>",
2537all the words starting with word number "C<$offset>" up to word number
2538"C<$vector-E<gt>Word_Size()-1>" are simply cleared.
2539
2540=item *
2541
2542C<$vector-E<gt>Chunk_Store($chunksize,$offset,$chunk);>
2543
2544This method allows you to set more than one bit at a time with
2545different values.
2546
2547You can access chunks (i.e., ranges of contiguous bits) between
2548one and at most "C<Bit::Vector-E<gt>Long_Bits()>" bits wide.
2549
2550In order to be portable, though, you should never use chunk sizes
2551larger than 32 bits.
2552
2553If the given "C<$chunksize>" does not lie between "C<1>" and
2554"C<Bit::Vector-E<gt>Long_Bits()>", a fatal "chunk size out of range"
2555error will occur.
2556
2557The method copies the "C<$chunksize>" least significant bits
2558from the value "C<$chunk>" to the given bit vector, starting at
2559bit position "C<$offset>" and proceeding upwards until bit number
2560"C<$offset+$chunksize-1>".
2561
2562(I.e., bit number "C<0>" of "C<$chunk>" becomes bit number "C<$offset>"
2563in the given bit vector, and bit number "C<$chunksize-1>" becomes
2564bit number "C<$offset+$chunksize-1>".)
2565
2566If the term "C<$offset+$chunksize-1>" exceeds "C<$vector-E<gt>Size()-1>",
2567the corresponding superfluous (most significant) bits from "C<$chunk>"
2568are simply ignored.
2569
2570Note that "C<$offset>" itself must lie in the permitted range between
2571"C<0>" and "C<$vector-E<gt>Size()-1>", or a fatal "offset out of range"
2572error will occur.
2573
2574This method (as well as the other "C<Chunk_>" methods) is useful, for
2575example, when you are reading in data in chunks of, say, 8 bits, which
2576you need to access later, say, using 16 bits at a time (like audio CD
2577wave files, for instance).
2578
2579=item *
2580
2581C<$chunk = $vector-E<gt>Chunk_Read($chunksize,$offset);>
2582
2583This method allows you to read the values of more than one bit at
2584a time.
2585
2586You can read chunks (i.e., ranges of contiguous bits) between
2587one and at most "C<Bit::Vector-E<gt>Long_Bits()>" bits wide.
2588
2589In order to be portable, though, you should never use chunk sizes
2590larger than 32 bits.
2591
2592If the given "C<$chunksize>" does not lie between "C<1>" and
2593"C<Bit::Vector-E<gt>Long_Bits()>", a fatal "chunk size out of range"
2594error will occur.
2595
2596The method returns the "C<$chunksize>" bits from the given bit vector
2597starting at bit position "C<$offset>" and proceeding upwards until
2598bit number "C<$offset+$chunksize-1>".
2599
2600(I.e., bit number "C<$offset>" of the given bit vector becomes bit number
2601"C<0>" of the returned value, and bit number "C<$offset+$chunksize-1>"
2602becomes bit number "C<$chunksize-1>".)
2603
2604If the term "C<$offset+$chunksize-1>" exceeds "C<$vector-E<gt>Size()-1>",
2605the non-existent bits are simply not returned.
2606
2607Note that "C<$offset>" itself must lie in the permitted range between
2608"C<0>" and "C<$vector-E<gt>Size()-1>", or a fatal "offset out of range"
2609error will occur.
2610
2611=item *
2612
2613C<$vector-E<gt>Chunk_List_Store($chunksize,@chunks);>
2614
2615This method allows you to fill the given bit vector with a list of
2616data packets ("chunks") of any size ("C<$chunksize>") you like
2617(within certain limits).
2618
2619In fact the given "C<$chunksize>" must lie in the range between "C<1>"
2620and "C<Bit::Vector-E<gt>Long_Bits()>", or a fatal "chunk size out of
2621range" error will occur.
2622
2623In order to be portable, though, you should never use chunk sizes
2624larger than 32 bits.
2625
2626The given bit vector is thereby filled in ascending order: The first
2627chunk from the list (i.e., "C<$chunks[0]>") fills the "C<$chunksize>"
2628least significant bits, the next chunk from the list ("C<$chunks[1]>")
2629fills the bits number "C<$chunksize>" to number "C<2*$chunksize-1>",
2630the third chunk ("C<$chunks[2]>") fills the bits number "C<2*$chunksize>",
2631to number "C<3*$chunksize-1>", and so on.
2632
2633If there a less chunks in the list than are needed to fill the entire
2634bit vector, the remaining (most significant) bits are cleared, i.e.,
2635the previous contents of the given bit vector are always erased completely.
2636
2637If there are more chunks in the list than are needed to fill the entire
2638bit vector, and/or if a chunk extends beyond "C<$vector-E<gt>Size()-1>"
2639(which happens whenever "C<$vector-E<gt>Size()>" is not a multiple of
2640"C<$chunksize>"), the superfluous chunks and/or bits are simply ignored.
2641
2642The method is useful, for example (and among many other applications),
2643for the conversion of packet sizes in a data stream.
2644
2645This method can also be used to store an octal string in a given
2646bit vector:
2647
2648 $vector->Chunk_List_Store(3, split(//, reverse $string));
2649
2650Note however that unlike the conversion methods "C<from_Hex()>",
2651"C<from_Bin()>", "C<from_Dec()>" and "C<from_Enum()>",
2652this statement does not include any syntax checking, i.e.,
2653it may fail silently, without warning.
2654
2655To perform syntax checking, add the following statements:
2656
2657 if ($string =~ /^[0-7]+$/)
2658 {
2659 # okay, go ahead with conversion as shown above
2660 }
2661 else
2662 {
2663 # error, string contains other than octal characters
2664 }
2665
2666Another application is to store a repetitive pattern in a given
2667bit vector:
2668
2669 $pattern = 0xDEADBEEF;
2670 $length = 32; # = length of $pattern in bits
2671 $size = $vector->Size();
2672 $factor = int($size / $length);
2673 if ($size % $length) { $factor++; }
2674 $vector->Chunk_List_Store($length, ($pattern) x $factor);
2675
2676=item *
2677
2678C<@chunks = $vector-E<gt>Chunk_List_Read($chunksize);>
2679
2680This method allows you to access the contents of the given bit vector in
2681form of a list of data packets ("chunks") of a size ("C<$chunksize>")
2682of your choosing (within certain limits).
2683
2684In fact the given "C<$chunksize>" must lie in the range between "C<1>"
2685and "C<Bit::Vector-E<gt>Long_Bits()>", or a fatal "chunk size out of
2686range" error will occur.
2687
2688In order to be portable, though, you should never use chunk sizes
2689larger than 32 bits.
2690
2691The given bit vector is thereby read in ascending order: The
2692"C<$chunksize>" least significant bits (bits number "C<0>" to
2693"C<$chunksize-1>") become the first chunk in the returned list
2694(i.e., "C<$chunks[0]>"). The bits number "C<$chunksize>" to
2695"C<2*$chunksize-1>" become the next chunk in the list
2696("C<$chunks[1]>"), and so on.
2697
2698If "C<$vector-E<gt>Size()>" is not a multiple of "C<$chunksize>",
2699the last chunk in the list will contain fewer bits than "C<$chunksize>".
2700
2701B<BEWARE> that for large bit vectors and/or small values of "C<$chunksize>",
2702the number of returned list elements can be extremely large! B<BE CAREFUL!>
2703
2704You could blow up your application with lack of memory (each list element
2705is a full-grown Perl scalar, internally, with an associated memory overhead
2706for its administration!) or at least cause a noticeable, more or less
2707long-lasting "freeze" of your application!
2708
2709Possible applications:
2710
2711The method is especially useful in the conversion of packet sizes in
2712a data stream.
2713
2714This method can also be used to convert a given bit vector to a string
2715of octal numbers:
2716
2717 $string = reverse join('', $vector->Chunk_List_Read(3));
2718
2719=item *
2720
2721C<$vector-E<gt>Index_List_Remove(@indices);>
2722
2723This method allows you to specify a list of indices of bits which
2724should be turned off in the given bit vector.
2725
2726In fact this method is a shortcut for
2727
2728 foreach $index (@indices)
2729 {
2730 $vector->Bit_Off($index);
2731 }
2732
2733In contrast to all other import methods in this module, this method
2734does B<NOT> clear the given bit vector before processing its list
2735of arguments.
2736
2737Instead, this method allows you to accumulate the results of various
2738consecutive calls.
2739
2740(The same holds for the method "C<Index_List_Store()>". As a
2741consequence, you can "wipe out" what you did using the method
2742"C<Index_List_Remove()>" by passing the identical argument list
2743to the method "C<Index_List_Store()>".)
2744
2745=item *
2746
2747C<$vector-E<gt>Index_List_Store(@indices);>
2748
2749This method allows you to specify a list of indices of bits which
2750should be turned on in the given bit vector.
2751
2752In fact this method is a shortcut for
2753
2754 foreach $index (@indices)
2755 {
2756 $vector->Bit_On($index);
2757 }
2758
2759In contrast to all other import methods in this module, this method
2760does B<NOT> clear the given bit vector before processing its list
2761of arguments.
2762
2763Instead, this method allows you to accumulate the results of various
2764consecutive calls.
2765
2766(The same holds for the method "C<Index_List_Remove()>". As a
2767consequence, you can "wipe out" what you did using the method
2768"C<Index_List_Store()>" by passing the identical argument list
2769to the method "C<Index_List_Remove()>".)
2770
2771=item *
2772
2773C<@indices = $vector-E<gt>Index_List_Read();>
2774
2775This method returns a list of Perl scalars.
2776
2777The list contains one scalar for each set bit in the given
2778bit vector.
2779
2780B<BEWARE> that for large bit vectors, this can result in a literally
2781overwhelming number of list elements! B<BE CAREFUL!> You could run
2782out of memory or slow down your application considerably!
2783
2784Each scalar contains the number of the index corresponding to
2785the bit in question.
2786
2787These indices are always returned in ascending order.
2788
2789If the given bit vector is empty (contains only cleared bits)
2790or if it has a length of zero (if it contains no bits at all),
2791the method returns an empty list.
2792
2793This method can be useful, for instance, to obtain a list of
2794prime numbers:
2795
2796 $limit = 1000; # or whatever
2797 $vector = Bit::Vector->new($limit+1);
2798 $vector->Primes();
2799 @primes = $vector->Index_List_Read();
2800
2801=item *
2802
2803C<$vec3-E<gt>Or($vec1,$vec2);>
2804
2805C<$set3-E<gt>Union($set1,$set2);>
2806
2807This method calculates the union of "C<$set1>" and "C<$set2>" and stores
2808the result in "C<$set3>".
2809
2810This is usually written as "C<$set3 = $set1 u $set2>" in set theory
2811(where "u" is the "cup" operator).
2812
2813(On systems where the "cup" character is unavailable this operator
2814is often denoted by a plus sign "+".)
2815
2816In-place calculation is also possible, i.e., "C<$set3>" may be identical
2817with "C<$set1>" or "C<$set2>" or both.
2818
2819=item *
2820
2821C<$vec3-E<gt>And($vec1,$vec2);>
2822
2823C<$set3-E<gt>Intersection($set1,$set2);>
2824
2825This method calculates the intersection of "C<$set1>" and "C<$set2>" and
2826stores the result in "C<$set3>".
2827
2828This is usually written as "C<$set3 = $set1 n $set2>" in set theory
2829(where "n" is the "cap" operator).
2830
2831(On systems where the "cap" character is unavailable this operator
2832is often denoted by an asterisk "*".)
2833
2834In-place calculation is also possible, i.e., "C<$set3>" may be identical
2835with "C<$set1>" or "C<$set2>" or both.
2836
2837=item *
2838
2839C<$vec3-E<gt>AndNot($vec1,$vec2);>
2840
2841C<$set3-E<gt>Difference($set1,$set2);>
2842
2843This method calculates the difference of "C<$set1>" less "C<$set2>" and
2844stores the result in "C<$set3>".
2845
2846This is usually written as "C<$set3 = $set1 \ $set2>" in set theory
2847(where "\" is the "less" operator).
2848
2849In-place calculation is also possible, i.e., "C<$set3>" may be identical
2850with "C<$set1>" or "C<$set2>" or both.
2851
2852=item *
2853
2854C<$vec3-E<gt>Xor($vec1,$vec2);>
2855
2856C<$set3-E<gt>ExclusiveOr($set1,$set2);>
2857
2858This method calculates the symmetric difference of "C<$set1>" and "C<$set2>"
2859and stores the result in "C<$set3>".
2860
2861This can be written as "C<$set3 = ($set1 u $set2) \ ($set1 n $set2)>" in set
2862theory (the union of the two sets less their intersection).
2863
2864When sets are implemented as bit vectors then the above formula is
2865equivalent to the exclusive-or between corresponding bits of the two
2866bit vectors (hence the name of this method).
2867
2868Note that this method is also much more efficient than evaluating the
2869above formula explicitly since it uses a built-in machine language
2870instruction internally.
2871
2872In-place calculation is also possible, i.e., "C<$set3>" may be identical
2873with "C<$set1>" or "C<$set2>" or both.
2874
2875=item *
2876
2877C<$vec2-E<gt>Not($vec1);>
2878
2879C<$set2-E<gt>Complement($set1);>
2880
2881This method calculates the complement of "C<$set1>" and stores the result
2882in "C<$set2>".
2883
2884In "big integer" arithmetic, this is equivalent to calculating the one's
2885complement of the number stored in the bit vector "C<$set1>" in binary
2886representation.
2887
2888In-place calculation is also possible, i.e., "C<$set2>" may be identical
2889with "C<$set1>".
2890
2891=item *
2892
2893C<if ($set1-E<gt>subset($set2))>
2894
2895Returns "true" ("C<1>") if "C<$set1>" is a subset of "C<$set2>"
2896(i.e., completely contained in "C<$set2>") and "false" ("C<0>")
2897otherwise.
2898
2899This means that any bit which is set ("C<1>") in "C<$set1>" must
2900also be set in "C<$set2>", but "C<$set2>" may contain set bits
2901which are not set in "C<$set1>", in order for the condition
2902of subset relationship to be true between these two sets.
2903
2904Note that by definition, if two sets are identical, they are
2905also subsets (and also supersets) of each other.
2906
2907=item *
2908
2909C<$norm = $set-E<gt>Norm();>
2910
2911Returns the norm (number of bits which are set) of the given vector.
2912
2913This is equivalent to the number of elements contained in the given
2914set.
2915
2916Uses a byte lookup table for calculating the number of set bits
2917per byte, and thus needs a time for evaluation (and a number of
2918loops) linearly proportional to the length of the given bit vector
2919(in bytes).
2920
2921This should be the fastest algorithm on average.
2922
2923=item *
2924
2925C<$norm = $set-E<gt>Norm2();>
2926
2927Returns the norm (number of bits which are set) of the given vector.
2928
2929This is equivalent to the number of elements contained in the given
2930set.
2931
2932This does the same as the method "C<Norm()>" above, only with a
2933different algorithm:
2934
2935This method counts the number of set and cleared bits at the same
2936time and will stop when either of them has been exhausted, thus
2937needing at most half as many loops per machine word as the total
2938number of bits in a machine word - in fact it will need a number
2939of loops equal to the minimum of the number of set bits and the
2940number of cleared bits.
2941
2942This might be a faster algorithm than of the method "C<Norm()>"
2943above on some systems, depending on the system's architecture
2944and the compiler and optimisation used, for bit vectors with
2945sparse set bits and for bit vectors with sparse cleared bits
2946(i.e., predominantly set bits).
2947
2948=item *
2949
2950C<$norm = $set-E<gt>Norm3();>
2951
2952Returns the norm (number of bits which are set) of the given vector.
2953
2954This is equivalent to the number of elements contained in the given
2955set.
2956
2957This does the same as the two methods "C<Norm()>" and "C<Norm2()>"
2958above, however with a different algorithm.
2959
2960In fact this is the implementation of the method "C<Norm()>" used
2961in previous versions of this module.
2962
2963The method needs a number of loops per machine word equal to the
2964number of set bits in that machine word.
2965
2966Only for bit vectors with sparse set bits will this method be
2967fast; it will depend on a system's architecture and compiler
2968whether the method will be faster than any of the two methods
2969above in such cases.
2970
2971On average however, this is probably the slowest method of the
2972three.
2973
2974=item *
2975
2976C<$min = $set-E<gt>Min();>
2977
2978Returns the minimum of the given set, i.e., the minimum of all
2979indices of all set bits in the given bit vector "C<$set>".
2980
2981If the set is empty (no set bits), plus infinity (represented
2982by the constant "MAX_LONG" on your system) is returned.
2983
2984(This constant is usually S<2 ^ (n-1) - 1>, where "C<n>" is the
2985number of bits of an unsigned long on your machine.)
2986
2987=item *
2988
2989C<$max = $set-E<gt>Max();>
2990
2991Returns the maximum of the given set, i.e., the maximum of all
2992indices of all set bits in the given bit vector "C<$set>".
2993
2994If the set is empty (no set bits), minus infinity (represented
2995by the constant "MIN_LONG" on your system) is returned.
2996
2997(This constant is usually S<-(2 ^ (n-1) - 1)> or S<-(2 ^ (n-1))>,
2998where "C<n>" is the number of bits of an unsigned long on your machine.)
2999
3000=item *
3001
3002C<$m3-E<gt>Multiplication($r3,$c3,$m1,$r1,$c1,$m2,$r2,$c2);>
3003
3004This method multiplies two boolean matrices (stored as bit vectors)
3005"C<$m1>" and "C<$m2>" and stores the result in matrix "C<$m3>".
3006
3007The method uses the binary "xor" operation ("C<^>") as the boolean
3008addition operator ("C<+>").
3009
3010An exception is raised if the product of the number of rows and
3011columns of any of the three matrices differs from the actual size
3012of their underlying bit vector.
3013
3014An exception is also raised if the numbers of rows and columns
3015of the three matrices do not harmonize in the required manner:
3016
3017 rows3 == rows1
3018 cols3 == cols2
3019 cols1 == rows2
3020
3021This method is used by the module "Math::MatrixBool".
3022
3023See L<Math::MatrixBool(3)> for details.
3024
3025=item *
3026
3027C<$m3-E<gt>Product($r3,$c3,$m1,$r1,$c1,$m2,$r2,$c2);>
3028
3029This method multiplies two boolean matrices (stored as bit vectors)
3030"C<$m1>" and "C<$m2>" and stores the result in matrix "C<$m3>".
3031
3032This special method uses the binary "or" operation ("C<|>") as the
3033boolean addition operator ("C<+>").
3034
3035An exception is raised if the product of the number of rows and
3036columns of any of the three matrices differs from the actual size
3037of their underlying bit vector.
3038
3039An exception is also raised if the numbers of rows and columns
3040of the three matrices do not harmonize in the required manner:
3041
3042 rows3 == rows1
3043 cols3 == cols2
3044 cols1 == rows2
3045
3046This method is used by the module "Math::MatrixBool".
3047
3048See L<Math::MatrixBool(3)> for details.
3049
3050=item *
3051
3052C<$matrix-E<gt>Closure($rows,$cols);>
3053
3054This method calculates the reflexive transitive closure of the
3055given boolean matrix (stored as a bit vector) using Kleene's
3056algoritm.
3057
3058(See L<Math::Kleene(3)> for a brief introduction into the
3059theory behind Kleene's algorithm.)
3060
3061The reflexive transitive closure answers the question whether
3062a path exists between any two vertices of a graph whose edges
3063are given as a matrix:
3064
3065If a (directed) edge exists going from vertex "i" to vertex "j",
3066then the element in the matrix with coordinates (i,j) is set to
3067"C<1>" (otherwise it remains set to "C<0>").
3068
3069If the edges are undirected, the resulting matrix is symmetric,
3070i.e., elements (i,j) and (j,i) always contain the same value.
3071
3072The matrix representing the edges of the graph only answers the
3073question whether an B<EDGE> exists between any two vertices of
3074the graph or not, whereas the reflexive transitive closure
3075answers the question whether a B<PATH> (a series of adjacent
3076edges) exists between any two vertices of the graph!
3077
3078Note that the contents of the given matrix are modified by
3079this method, so make a copy of the initial matrix in time
3080if you are going to need it again later.
3081
3082An exception is raised if the given matrix is not quadratic,
3083i.e., if the number of rows and columns of the given matrix
3084is not identical.
3085
3086An exception is also raised if the product of the number of
3087rows and columns of the given matrix differs from the actual
3088size of its underlying bit vector.
3089
3090This method is used by the module "Math::MatrixBool".
3091
3092See L<Math::MatrixBool(3)> for details.
3093
3094=item *
3095
3096C<$matrix2-E<gt>Transpose($rows2,$cols2,$matrix1,$rows1,$cols1);>
3097
3098This method calculates the transpose of a boolean matrix "C<$matrix1>"
3099(stored as a bit vector) and stores the result in matrix "C<$matrix2>".
3100
3101The transpose of a boolean matrix, representing the edges of a graph,
3102can be used for finding the strongly connected components of that graph.
3103
3104An exception is raised if the product of the number of rows and
3105columns of any of the two matrices differs from the actual size
3106of its underlying bit vector.
3107
3108An exception is also raised if the following conditions are not
3109met:
3110
3111 rows2 == cols1
3112 cols2 == rows1
3113
3114Note that in-place processing ("C<$matrix1>" and "C<$matrix2>" are
3115identical) is only possible if the matrix is quadratic. Otherwise,
3116a fatal "matrix is not quadratic" error will occur.
3117
3118This method is used by the module "Math::MatrixBool".
3119
3120See L<Math::MatrixBool(3)> for details.
3121
3122=back
3123
3124=head1 SEE ALSO
3125
3126Bit::Vector::Overload(3),
3127Bit::Vector::String(3).
3128
3129Set::IntRange(3),
3130Math::MatrixBool(3),
3131Math::MatrixReal(3),
3132DFA::Kleene(3),
3133Math::Kleene(3),
3134Graph::Kruskal(3).
3135
3136=head1 VERSION
3137
3138This man page documents "Bit::Vector" version 6.4.
3139
3140=head1 AUTHOR
3141
3142 Steffen Beyer
3143 mailto:sb@engelschall.com
3144 http://www.engelschall.com/u/sb/download/
3145
3146=head1 COPYRIGHT
3147
3148Copyright (c) 1995 - 2004 by Steffen Beyer. All rights reserved.
3149
3150=head1 LICENSE
3151
3152This package is free software; you can redistribute it and/or
3153modify it under the same terms as Perl itself, i.e., under the
3154terms of the "Artistic License" or the "GNU General Public License".
3155
3156The C library at the core of this Perl module can additionally
3157be redistributed and/or modified under the terms of the "GNU
3158Library General Public License".
3159
3160Please refer to the files "Artistic.txt", "GNU_GPL.txt" and
3161"GNU_LGPL.txt" in this distribution for details!
3162
3163=head1 DISCLAIMER
3164
3165This package is distributed in the hope that it will be useful,
3166but WITHOUT ANY WARRANTY; without even the implied warranty of
3167MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
3168
3169See the "GNU General Public License" for more details.
3170