home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Editores / Perl5 / perl / lib / site / Bit / Vector.pm < prev   
Encoding:
Perl POD Document  |  1997-08-10  |  63.7 KB  |  2,367 lines

  1.  
  2. #  Copyright (c) 1995, 1996, 1997 by Steffen Beyer. All rights reserved.
  3. #  This package is free software; you can redistribute it and/or modify
  4. #  it under the same terms as Perl itself.
  5.  
  6. package Bit::Vector;
  7.  
  8. use strict;
  9. use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION);
  10.  
  11. require Exporter;
  12. require DynaLoader;
  13.  
  14. @ISA = qw(Exporter DynaLoader);
  15.  
  16. @EXPORT = qw();
  17.  
  18. @EXPORT_OK = qw();
  19.  
  20. $VERSION = '4.2';
  21.  
  22. bootstrap Bit::Vector $VERSION;
  23.  
  24. use Carp;
  25.  
  26. use overload
  27.       '""' => '_string',
  28.      'neg' => '_complement',
  29.        '~' => '_complement',
  30.     'bool' => '_boolean',
  31.        '!' => '_not_boolean',
  32.      'abs' => '_norm',
  33.       '++' => '_increment',
  34.       '--' => '_decrement',
  35.        '+' => '_union',
  36.        '|' => '_union',                # alternative for '+'
  37.        '-' => '_difference',
  38.        '*' => '_intersection',
  39.        '&' => '_intersection',         # alternative for '*'
  40.        '^' => '_exclusive_or',
  41.       '<<' => '_move_left',
  42.       '>>' => '_move_right',
  43.       '+=' => '_assign_union',
  44.       '|=' => '_assign_union',         # alternative for '+='
  45.       '-=' => '_assign_difference',
  46.       '*=' => '_assign_intersection',
  47.       '&=' => '_assign_intersection',  # alternative for '*='
  48.       '^=' => '_assign_exclusive_or',
  49.      '<<=' => '_assign_move_left',
  50.      '>>=' => '_assign_move_right',
  51.       '==' => '_equal',
  52.       '!=' => '_not_equal',
  53.        '<' => '_true_sub_set',
  54.       '<=' => '_sub_set',
  55.        '>' => '_true_super_set',
  56.       '>=' => '_super_set',
  57.      'cmp' => '_compare',              # also enables lt, le, gt, ge, eq, ne
  58.        '=' => '_clone',
  59. 'fallback' =>   undef;
  60.  
  61. sub Shadow
  62. {
  63.     croak "Usage: \$other_vector = \$some_vector->Shadow();"
  64.       if (@_ != 1);
  65.  
  66.     my($object) = @_;
  67.     my($result);
  68.  
  69.     $result = $object->new($object->Size());
  70.     return($result);
  71. }
  72.  
  73. sub Clone
  74. {
  75.     croak "Usage: \$twin_vector = \$some_vector->Clone();"
  76.       if (@_ != 1);
  77.  
  78.     my($object) = @_;
  79.     my($result);
  80.  
  81.     $result = $object->new($object->Size());
  82.     $result->Copy($object);
  83.     return($result);
  84. }
  85.  
  86. sub to_ASCII
  87. {
  88.     croak "Usage: \$string = \$vector->to_ASCII();"
  89.       if (@_ != 1);
  90.  
  91.     my($object) = @_;
  92.     my($start,$string);
  93.     my($min,$max);
  94.  
  95.     $start = 0;
  96.     $string = '';
  97.     while (($start < $object->Size()) &&
  98.         (($min,$max) = $object->Interval_Scan_inc($start)))
  99.     {
  100.         $start = $max + 2;
  101.         if    ($min == $max)   { $string .= "${min},"; }
  102.         elsif ($min == $max-1) { $string .= "${min},${max},"; }
  103.         else                   { $string .= "${min}-${max},"; }
  104.     }
  105.     $string =~ s/,$//;
  106.     return($string);
  107. }
  108.  
  109. sub from_ASCII
  110. {
  111.     croak "Usage: \$vector->from_ASCII(\$string);"
  112.       if (@_ != 2);
  113.  
  114.     my($object,$string) = @_;
  115.     my(@intervals,$interval);
  116.     my($min,$max);
  117.  
  118.     croak "Bit::Vector::from_ASCII(): syntax error in input string"
  119.       unless ($string =~ /^ (?: \d+ (?: - \d+ )? )
  120.                       (?: , (?: \d+ (?: - \d+ )? ) )* $/x);
  121.  
  122.     $object->Empty();
  123.  
  124.     @intervals = split(/,/, $string);
  125.  
  126.     foreach $interval (@intervals)
  127.     {
  128.         if ($interval =~ /-/)
  129.         {
  130.             ($min,$max) = split(/-/, $interval);
  131.  
  132.             croak "Bit::Vector::from_ASCII(): minimum index out of range"
  133.               if (($min < 0) || ($min >= $object->Size()));
  134.  
  135.             croak "Bit::Vector::from_ASCII(): maximum index out of range"
  136.               if (($max < 0) || ($max >= $object->Size()));
  137.  
  138.             croak "Bit::Vector::from_ASCII(): minimum > maximum index"
  139.               if ($min > $max);
  140.  
  141.             $object->Interval_Fill($min,$max);
  142.         }
  143.         else
  144.         {
  145.             croak "Bit::Vector::from_ASCII(): index out of range"
  146.               if (($interval < 0) || ($interval >= $object->Size()));
  147.  
  148.             $object->Bit_On($interval);
  149.         }
  150.     }
  151. }
  152.  
  153. sub new_from_String
  154. {
  155.     croak "Usage: \$vector = Bit::Vector->new_from_String(\$string);"
  156.       if (@_ != 2);
  157.  
  158.     my $proto  = shift;
  159.     my $class  = ref($proto) || $proto || 'Bit::Vector';
  160.     my $string = shift;
  161.     my($length);
  162.     my($result);
  163.  
  164.     $length = length($string) * 4;
  165.  
  166.     if ($length > 0)
  167.     {
  168.         $result = Bit::Vector::new( $class, $length );
  169.  
  170.         if (defined($result) && ref($result) && (${$result} != 0))
  171.         {
  172.             if ($result->from_string($string))
  173.             {
  174.                 return($result);
  175.             }
  176.             else
  177.             {
  178.                 croak
  179. "Bit::Vector::new_from_String(): syntax error in input string";
  180.             }
  181.         }
  182.         else
  183.         {
  184.             croak
  185. "Bit::Vector::new_from_String(): unable to create new 'Bit::Vector' object";
  186.         }
  187.     }
  188.     else
  189.     {
  190.         croak
  191. "Bit::Vector::new_from_String(): zero length 'Bit::Vector' object not permitted";
  192.     }
  193. }
  194.  
  195. sub _string
  196. {
  197.     my($object,$argument,$flag) = @_;
  198. #   my($name) = '""'; #&_trace($name,$object,$argument,$flag);
  199.  
  200.     return( $object->to_ASCII() );
  201. }
  202.  
  203. sub _complement
  204. {
  205.     my($object,$argument,$flag) = @_;
  206. #   my($name) = "'~'"; #&_trace($name,$object,$argument,$flag);
  207.     my($result);
  208.  
  209.     $result = $object->new($object->Size());
  210.     $result->Complement($object);
  211.     return($result);
  212. }
  213.  
  214. sub _boolean
  215. {
  216.     my($object,$argument,$flag) = @_;
  217. #   my($name) = "bool"; #&_trace($name,$object,$argument,$flag);
  218.  
  219.     return( ! $object->is_empty() );
  220. }
  221.  
  222. sub _not_boolean
  223. {
  224.     my($object,$argument,$flag) = @_;
  225. #   my($name) = "'!'"; #&_trace($name,$object,$argument,$flag);
  226.  
  227.     return( $object->is_empty() );
  228. }
  229.  
  230. sub _norm
  231. {
  232.     my($object,$argument,$flag) = @_;
  233. #   my($name) = "abs"; #&_trace($name,$object,$argument,$flag);
  234.  
  235.     return( $object->Norm() );
  236. }
  237.  
  238. sub _increment
  239. {
  240.     my($object,$argument,$flag) = @_;
  241. #   my($name) = "'++'"; #&_trace($name,$object,$argument,$flag);
  242.  
  243.     return( $object->increment() );
  244. }
  245.  
  246. sub _decrement
  247. {
  248.     my($object,$argument,$flag) = @_;
  249. #   my($name) = "'--'"; #&_trace($name,$object,$argument,$flag);
  250.  
  251.     return( $object->decrement() );
  252. }
  253.  
  254. sub _union
  255. {
  256.     my($object,$argument,$flag) = @_;
  257.     my($name) = "'+'"; #&_trace($name,$object,$argument,$flag);
  258.     my($result);
  259.  
  260.     if ((defined $argument) && ref($argument) &&
  261.         (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
  262.     {
  263.         if (defined $flag)
  264.         {
  265.             $result = $object->new($object->Size());
  266.             $result->Union($object,$argument);
  267.             return($result);
  268.         }
  269.         else
  270.         {
  271.             $object->Union($object,$argument);
  272.             return($object);
  273.         }
  274.     }
  275.     elsif ((defined $argument) && !(ref($argument)))
  276.     {
  277.         if (defined $flag)
  278.         {
  279.             $result = $object->new($object->Size());
  280.             $result->Copy($object);
  281.             $result->Bit_On($argument);
  282.             return($result);
  283.         }
  284.         else
  285.         {
  286.             $object->Bit_On($argument);
  287.             return($object);
  288.         }
  289.     }
  290.     else
  291.     {
  292.         croak "Bit::Vector $name: wrong argument type";
  293.     }
  294. }
  295.  
  296. sub _difference
  297. {
  298.     my($object,$argument,$flag) = @_;
  299.     my($name) = "'-'"; #&_trace($name,$object,$argument,$flag);
  300.     my($result);
  301.  
  302.     if ((defined $argument) && ref($argument) &&
  303.         (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
  304.     {
  305.         if (defined $flag)
  306.         {
  307.             $result = $object->new($object->Size());
  308.             if ($flag) { $result->Difference($argument,$object); }
  309.             else       { $result->Difference($object,$argument); }
  310.             return($result);
  311.         }
  312.         else
  313.         {
  314.             $object->Difference($object,$argument);
  315.             return($object);
  316.         }
  317.     }
  318.     elsif ((defined $argument) && !(ref($argument)))
  319.     {
  320.         if (defined $flag)
  321.         {
  322.             $result = $object->new($object->Size());
  323.             if ($flag)
  324.             {
  325.                 unless ($object->bit_test($argument))
  326.                 { $result->Bit_On($argument); }
  327.             }
  328.             else
  329.             {
  330.                 $result->Copy($object);
  331.                 $result->Bit_Off($argument);
  332.             }
  333.             return($result);
  334.         }
  335.         else
  336.         {
  337.             $object->Bit_Off($argument);
  338.             return($object);
  339.         }
  340.     }
  341.     else
  342.     {
  343.         croak "Bit::Vector $name: wrong argument type";
  344.     }
  345. }
  346.  
  347. sub _intersection
  348. {
  349.     my($object,$argument,$flag) = @_;
  350.     my($name) = "'*'"; #&_trace($name,$object,$argument,$flag);
  351.     my($result);
  352.  
  353.     if ((defined $argument) && ref($argument) &&
  354.         (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
  355.     {
  356.         if (defined $flag)
  357.         {
  358.             $result = $object->new($object->Size());
  359.             $result->Intersection($object,$argument);
  360.             return($result);
  361.         }
  362.         else
  363.         {
  364.             $object->Intersection($object,$argument);
  365.             return($object);
  366.         }
  367.     }
  368.     elsif ((defined $argument) && !(ref($argument)))
  369.     {
  370.         if (defined $flag)
  371.         {
  372.             $result = $object->new($object->Size());
  373.             if ($object->bit_test($argument))
  374.             { $result->Bit_On($argument); }
  375.             return($result);
  376.         }
  377.         else
  378.         {
  379.             $flag = $object->bit_test($argument);
  380.             $object->Empty();
  381.             if ($flag) { $object->Bit_On($argument); }
  382.             return($object);
  383.         }
  384.     }
  385.     else
  386.     {
  387.         croak "Bit::Vector $name: wrong argument type";
  388.     }
  389. }
  390.  
  391. sub _exclusive_or
  392. {
  393.     my($object,$argument,$flag) = @_;
  394.     my($name) = "'^'"; #&_trace($name,$object,$argument,$flag);
  395.     my($result);
  396.  
  397.     if ((defined $argument) && ref($argument) &&
  398.         (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
  399.     {
  400.         if (defined $flag)
  401.         {
  402.             $result = $object->new($object->Size());
  403.             $result->ExclusiveOr($object,$argument);
  404.             return($result);
  405.         }
  406.         else
  407.         {
  408.             $object->ExclusiveOr($object,$argument);
  409.             return($object);
  410.         }
  411.     }
  412.     elsif ((defined $argument) && !(ref($argument)))
  413.     {
  414.         if (defined $flag)
  415.         {
  416.             $result = $object->new($object->Size());
  417.             $result->Copy($object);
  418.             $result->bit_flip($argument);
  419.             return($result);
  420.         }
  421.         else
  422.         {
  423.             $object->bit_flip($argument);
  424.             return($object);
  425.         }
  426.     }
  427.     else
  428.     {
  429.         croak "Bit::Vector $name: wrong argument type";
  430.     }
  431. }
  432.  
  433. sub _move_left
  434. {
  435.     my($object,$argument,$flag) = @_;
  436.     my($name) = "'<<'"; #&_trace($name,$object,$argument,$flag);
  437.     my($result);
  438.  
  439.     if ((defined $argument) && !(ref($argument)))
  440.     {
  441.         if (defined $flag)
  442.         {
  443.             unless ($flag)
  444.             {
  445.                 $result = $object->new($object->Size());
  446.                 $result->Copy($object);
  447.                 $result->Move_Left($argument);
  448.                 return($result);
  449.             }
  450.             else
  451.             {
  452.                 croak "Bit::Vector $name: wrong argument type";
  453.             }
  454.         }
  455.         else
  456.         {
  457.             $object->Move_Left($argument);
  458.             return($object);
  459.         }
  460.     }
  461.     else
  462.     {
  463.         croak "Bit::Vector $name: wrong argument type";
  464.     }
  465. }
  466.  
  467. sub _move_right
  468. {
  469.     my($object,$argument,$flag) = @_;
  470.     my($name) = "'>>'"; #&_trace($name,$object,$argument,$flag);
  471.     my($result);
  472.  
  473.     if ((defined $argument) && !(ref($argument)))
  474.     {
  475.         if (defined $flag)
  476.         {
  477.             unless ($flag)
  478.             {
  479.                 $result = $object->new($object->Size());
  480.                 $result->Copy($object);
  481.                 $result->Move_Right($argument);
  482.                 return($result);
  483.             }
  484.             else
  485.             {
  486.                 croak "Bit::Vector $name: wrong argument type";
  487.             }
  488.         }
  489.         else
  490.         {
  491.             $object->Move_Right($argument);
  492.             return($object);
  493.         }
  494.     }
  495.     else
  496.     {
  497.         croak "Bit::Vector $name: wrong argument type";
  498.     }
  499. }
  500.  
  501. sub _assign_union
  502. {
  503.     my($object,$argument,$flag) = @_;
  504. #   my($name) = "'+='"; #&_trace($name,$object,$argument,$flag);
  505.  
  506.     return( &_union($object,$argument,undef) );
  507. }
  508.  
  509. sub _assign_difference
  510. {
  511.     my($object,$argument,$flag) = @_;
  512. #   my($name) = "'-='"; #&_trace($name,$object,$argument,$flag);
  513.  
  514.     return( &_difference($object,$argument,undef) );
  515. }
  516.  
  517. sub _assign_intersection
  518. {
  519.     my($object,$argument,$flag) = @_;
  520. #   my($name) = "'*='"; #&_trace($name,$object,$argument,$flag);
  521.  
  522.     return( &_intersection($object,$argument,undef) );
  523. }
  524.  
  525. sub _assign_exclusive_or
  526. {
  527.     my($object,$argument,$flag) = @_;
  528. #   my($name) = "'^='"; #&_trace($name,$object,$argument,$flag);
  529.  
  530.     return( &_exclusive_or($object,$argument,undef) );
  531. }
  532.  
  533. sub _assign_move_left
  534. {
  535.     my($object,$argument,$flag) = @_;
  536. #   my($name) = "'<<='"; #&_trace($name,$object,$argument,$flag);
  537.  
  538.     return( &_move_left($object,$argument,undef) );
  539. }
  540.  
  541. sub _assign_move_right
  542. {
  543.     my($object,$argument,$flag) = @_;
  544. #   my($name) = "'>>='"; #&_trace($name,$object,$argument,$flag);
  545.  
  546.     return( &_move_right($object,$argument,undef) );
  547. }
  548.  
  549. sub _equal
  550. {
  551.     my($object,$argument,$flag) = @_;
  552.     my($name) = "'=='"; #&_trace($name,$object,$argument,$flag);
  553.     my($result);
  554.  
  555.     if ((defined $argument) && ref($argument) &&
  556.         (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
  557.     {
  558.         $result = $argument;
  559.     }
  560.     elsif ((defined $argument) && !(ref($argument)))
  561.     {
  562.         $result = $object->new($object->Size());
  563.         $result->Bit_On($argument);
  564.     }
  565.     else
  566.     {
  567.         croak "Bit::Vector $name: wrong argument type";
  568.     }
  569.     return( $object->equal($result) );
  570. }
  571.  
  572. sub _not_equal
  573. {
  574.     my($object,$argument,$flag) = @_;
  575.     my($name) = "'!='"; #&_trace($name,$object,$argument,$flag);
  576.     my($result);
  577.  
  578.     if ((defined $argument) && ref($argument) &&
  579.         (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
  580.     {
  581.         $result = $argument;
  582.     }
  583.     elsif ((defined $argument) && !(ref($argument)))
  584.     {
  585.         $result = $object->new($object->Size());
  586.         $result->Bit_On($argument);
  587.     }
  588.     else
  589.     {
  590.         croak "Bit::Vector $name: wrong argument type";
  591.     }
  592.     return( !($object->equal($result)) );
  593. }
  594.  
  595. sub _true_sub_set
  596. {
  597.     my($object,$argument,$flag) = @_;
  598.     my($name) = "'<'"; #&_trace($name,$object,$argument,$flag);
  599.     my($result);
  600.  
  601.     if ((defined $argument) && ref($argument) &&
  602.         (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
  603.     {
  604.         $result = $argument;
  605.     }
  606.     elsif ((defined $argument) && !(ref($argument)))
  607.     {
  608.         $result = $object->new($object->Size());
  609.         $result->Bit_On($argument);
  610.     }
  611.     else
  612.     {
  613.         croak "Bit::Vector $name: wrong argument type";
  614.     }
  615.     if ((defined $flag) && $flag)
  616.     {
  617.         return( !($result->equal($object)) &&
  618.                  ($result->subset($object)) );
  619.     }
  620.     else
  621.     {
  622.         return( !($object->equal($result)) &&
  623.                  ($object->subset($result)) );
  624.     }
  625. }
  626.  
  627. sub _sub_set
  628. {
  629.     my($object,$argument,$flag) = @_;
  630.     my($name) = "'<='"; #&_trace($name,$object,$argument,$flag);
  631.     my($result);
  632.  
  633.     if ((defined $argument) && ref($argument) &&
  634.         (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
  635.     {
  636.         $result = $argument;
  637.     }
  638.     elsif ((defined $argument) && !(ref($argument)))
  639.     {
  640.         $result = $object->new($object->Size());
  641.         $result->Bit_On($argument);
  642.     }
  643.     else
  644.     {
  645.         croak "Bit::Vector $name: wrong argument type";
  646.     }
  647.     if ((defined $flag) && $flag)
  648.     {
  649.         return( $result->subset($object) );
  650.     }
  651.     else
  652.     {
  653.         return( $object->subset($result) );
  654.     }
  655. }
  656.  
  657. sub _true_super_set
  658. {
  659.     my($object,$argument,$flag) = @_;
  660.     my($name) = "'>'"; #&_trace($name,$object,$argument,$flag);
  661.     my($result);
  662.  
  663.     if ((defined $argument) && ref($argument) &&
  664.         (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
  665.     {
  666.         $result = $argument;
  667.     }
  668.     elsif ((defined $argument) && !(ref($argument)))
  669.     {
  670.         $result = $object->new($object->Size());
  671.         $result->Bit_On($argument);
  672.     }
  673.     else
  674.     {
  675.         croak "Bit::Vector $name: wrong argument type";
  676.     }
  677.     if ((defined $flag) && $flag)
  678.     {
  679.         return( !($object->equal($result)) &&
  680.                  ($object->subset($result)) );
  681.     }
  682.     else
  683.     {
  684.         return( !($result->equal($object)) &&
  685.                  ($result->subset($object)) );
  686.     }
  687. }
  688.  
  689. sub _super_set
  690. {
  691.     my($object,$argument,$flag) = @_;
  692.     my($name) = "'>='"; #&_trace($name,$object,$argument,$flag);
  693.     my($result);
  694.  
  695.     if ((defined $argument) && ref($argument) &&
  696.         (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
  697.     {
  698.         $result = $argument;
  699.     }
  700.     elsif ((defined $argument) && !(ref($argument)))
  701.     {
  702.         $result = $object->new($object->Size());
  703.         $result->Bit_On($argument);
  704.     }
  705.     else
  706.     {
  707.         croak "Bit::Vector $name: wrong argument type";
  708.     }
  709.     if ((defined $flag) && $flag)
  710.     {
  711.         return( $object->subset($result) );
  712.     }
  713.     else
  714.     {
  715.         return( $result->subset($object) );
  716.     }
  717. }
  718.  
  719. sub _compare
  720. {
  721.     my($object,$argument,$flag) = @_;
  722.     my($name) = "cmp"; #&_trace($name,$object,$argument,$flag);
  723.     my($result);
  724.  
  725.     if ((defined $argument) && ref($argument) &&
  726.         (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/))
  727.     {
  728.         $result = $argument;
  729.     }
  730.     elsif ((defined $argument) && !(ref($argument)))
  731.     {
  732.         $result = $object->new($object->Size());
  733.         $result->Bit_On($argument);
  734.     }
  735.     else
  736.     {
  737.         croak "Bit::Vector $name: wrong argument type";
  738.     }
  739.     if ((defined $flag) && $flag)
  740.     {
  741.         return( $result->Compare($object) );
  742.     }
  743.     else
  744.     {
  745.         return( $object->Compare($result) );
  746.     }
  747. }
  748.  
  749. sub _clone
  750. {
  751.     my($object,$argument,$flag) = @_;
  752. #   my($name) = "'='"; #&_trace($name,$object,$argument,$flag);
  753.     my($result);
  754.  
  755.     $result = $object->new($object->Size());
  756.     $result->Copy($object);
  757.     return($result);
  758. }
  759.  
  760. sub _trace
  761. {
  762.     my($text,$object,$argument,$flag) = @_;
  763.  
  764.     unless (defined $object)   { $object   = 'undef'; };
  765.     unless (defined $argument) { $argument = 'undef'; };
  766.     unless (defined $flag)     { $flag     = 'undef'; };
  767.     if (ref($object))   { $object   = ref($object);   }
  768.     if (ref($argument)) { $argument = ref($argument); }
  769.     print "$text: \$obj='$object' \$arg='$argument' \$flag='$flag'\n";
  770. }
  771.  
  772. 1;
  773.  
  774. __END__
  775.  
  776. =head1 NAME
  777.  
  778. Bit::Vector - bit vectors of arbitrary length (base class)
  779.  
  780. Versatile implementation of bit vectors of arbitrary length
  781. with efficient and easy-to-use methods for various applications,
  782. especially sets.
  783.  
  784. Base class for all applications and classes using bit vectors as
  785. their underlying data type.
  786.  
  787. Provides overloaded arithmetic and relational operators for maximum
  788. comfort.
  789.  
  790. For an analysis of the time complexity of the methods implemented in
  791. this module, please refer to the file "COMPLEXITY" in the "Bit::Vector"
  792. distribution directory!
  793.  
  794. =head1 SYNOPSIS
  795.  
  796. =head2 METHODS IMPLEMENTED IN C (fastest)
  797.  
  798.   Version
  799.       $version = Bit::Vector::Version(); # version of "Vector.xs"
  800.  
  801.   new
  802.       $vector = new Bit::Vector($elements);
  803.       $vector = Bit::Vector->new($elements);
  804.       $vector = $any_vector->new($elements);
  805.  
  806.   Resize
  807.       $vector->Resize($elements);
  808.  
  809.   Size
  810.       $elements = $vector->Size();
  811.  
  812.   Empty
  813.       $vector->Empty();
  814.  
  815.   Fill
  816.       $vector->Fill();
  817.  
  818.   Flip
  819.       $vector->Flip();
  820.  
  821.   Interval_Empty
  822.       $vector->Interval_Empty($min,$max);
  823.  
  824.   Interval_Fill
  825.       $vector->Interval_Fill($min,$max);
  826.  
  827.   Interval_Flip
  828.       $vector->Interval_Flip($min,$max);
  829.  
  830.   Interval_Scan_inc
  831.       while (($min,$max) = $vector->Interval_Scan_inc($start))
  832.  
  833.   Interval_Scan_dec
  834.       while (($min,$max) = $vector->Interval_Scan_dec($start))
  835.  
  836.   Bit_Off
  837.       $vector->Bit_Off($index);
  838.       $vector->Delete($index);           # (deprecated)
  839.  
  840.   Bit_On
  841.       $vector->Bit_On($index);
  842.       $vector->Insert($index);           # (deprecated)
  843.  
  844.   bit_flip
  845.       $bit = $vector->bit_flip($index);
  846.       if ($vector->bit_flip($index))
  847.       $bit = $vector->flip($index);      # (deprecated)
  848.       if ($vector->flip($index))         # (deprecated)
  849.  
  850.   bit_test
  851.       $bit = $vector->bit_test($index);
  852.       if ($vector->bit_test($index))
  853.       $bit = $vector->contains($index);
  854.       if ($vector->contains($index))
  855.       $bit = $vector->in($index);        # (deprecated)
  856.       if ($vector->in($index))           # (deprecated)
  857.  
  858.   is_empty
  859.       if ($vector->is_empty())
  860.  
  861.   is_full
  862.       if ($vector->is_full())
  863.  
  864.   equal
  865.       if ($vector1->equal($vector2))
  866.  
  867.   lexorder
  868.       if ($vector1->lexorder($vector2))
  869.  
  870.   Compare
  871.       $cmp = $vector1->Compare($vector2);
  872.  
  873.   Copy
  874.       $vector1->Copy($vector2);
  875.  
  876.   rotate_left
  877.       $carry_out = $vector->rotate_left();
  878.  
  879.   rotate_right
  880.       $carry_out = $vector->rotate_right();
  881.  
  882.   shift_left
  883.       $carry_out = $vector->shift_left($carry_in);
  884.  
  885.   shift_right
  886.       $carry_out = $vector->shift_right($carry_in);
  887.  
  888.   Move_Left
  889.       $vector->Move_Left($bits);
  890.  
  891.   Move_Right
  892.       $vector->Move_Right($bits);
  893.  
  894.   increment
  895.       $carry = $vector->increment();
  896.  
  897.   decrement
  898.       $carry = $vector->decrement();
  899.  
  900.   to_String
  901.       $string = $vector->to_String();    # e.g., "A08A28AC"
  902.  
  903.   from_string
  904.       $ok = $vector->from_string($string);
  905.  
  906.   Union
  907.       $set1->Union($set2,$set3);         # in-place is possible!
  908.  
  909.   Intersection
  910.       $set1->Intersection($set2,$set3);  # in-place is possible!
  911.  
  912.   Difference
  913.       $set1->Difference($set2,$set3);    # in-place is possible!
  914.  
  915.   ExclusiveOr
  916.       $set1->ExclusiveOr($set2,$set3);   # in-place is possible!
  917.  
  918.   Complement
  919.       $set1->Complement($set2);          # in-place is possible!
  920.  
  921.   subset
  922.       if ($set1->subset($set2))
  923.       if ($set1->inclusion($set2))       # (deprecated)
  924.  
  925.   Norm
  926.       $norm = $set->Norm();
  927.  
  928.   Min
  929.       $min = $set->Min();
  930.  
  931.   Max
  932.       $max = $set->Max();
  933.  
  934.   Multiplication
  935.       $matrix1->Multiplication($rows1,$cols1,
  936.       $matrix2,$rows2,$cols2,
  937.       $matrix3,$rows3,$cols3);
  938.  
  939.   Closure
  940.       $matrix->Closure($rows,$cols);
  941.  
  942. =head2 METHODS IMPLEMENTED IN PERL
  943.  
  944.   Version
  945.       $version = $Bit::Vector::VERSION;  # version of "Vector.pm"
  946.  
  947.   Shadow
  948.       $other_vector = $some_vector->Shadow();
  949.  
  950.   Clone
  951.       $twin_vector = $some_vector->Clone();
  952.  
  953.   new_from_String
  954.       eval { $vector = Bit::Vector->new_from_String($string); };
  955.  
  956.   to_ASCII
  957.       $string = $vector->to_ASCII();     # e.g., "2,3,5-7,11,13-19"
  958.  
  959.   from_ASCII
  960.       eval { $vector->from_ASCII($string); };
  961.  
  962. =head2 OVERLOADED OPERATORS (slowest)
  963.  
  964.       # "$index" is a number or a Perl scalar variable containing a
  965.       # number which represents the set containing only that element:
  966.  
  967.   Emptyness
  968.       if ($vector) # if not empty
  969.       if (! $vector) # if empty
  970.       unless ($vector) # if empty
  971.  
  972.   Equality
  973.       if ($vector1 == $vector2)
  974.       if ($vector1 != $vector2)
  975.       if ($vector == $index)
  976.       if ($vector != $index)
  977.  
  978.   Lexical Comparison
  979.       $cmp = $vector1 cmp $vector2;
  980.       if ($vector1 lt $vector2)
  981.       if ($vector1 le $vector2)
  982.       if ($vector1 gt $vector2)
  983.       if ($vector1 ge $vector2)
  984.       if ($vector1 eq $vector2)
  985.       if ($vector1 ne $vector2)
  986.       $cmp = $vector cmp $index;
  987.       if ($vector lt $index)
  988.       if ($vector le $index)
  989.       if ($vector gt $index)
  990.       if ($vector ge $index)
  991.       if ($vector eq $index)
  992.       if ($vector ne $index)
  993.  
  994.   Move Left
  995.       $vector1 = $vector2 << $bits;
  996.       $vector <<= $bits;
  997.  
  998.   Move Right
  999.       $vector1 = $vector2 >> $bits;
  1000.       $vector >>= $bits;
  1001.  
  1002.   Increment
  1003.       ++$vector;
  1004.       $vector++;
  1005.  
  1006.   Decrement
  1007.       --$vector;
  1008.       $vector--;
  1009.  
  1010.   String Conversion
  1011.       $string = "$vector";
  1012.       print "\$vector = '$vector'\n";
  1013.  
  1014.   Union
  1015.       $set1 = $set2 + $set3;
  1016.       $set1 += $set2;
  1017.       $set1 = $set2 | $set3;
  1018.       $set1 |= $set2;
  1019.       $vector1 = $vector2 + $index;
  1020.       $vector += $index;
  1021.       $vector1 = $vector2 | $index;
  1022.       $vector |= $index;
  1023.  
  1024.   Intersection
  1025.       $set1 = $set2 * $set3;
  1026.       $set1 *= $set2;
  1027.       $set1 = $set2 & $set3;
  1028.       $set1 &= $set2;
  1029.       $vector1 = $vector2 * $index;
  1030.       $vector *= $index;
  1031.       $vector1 = $vector2 & $index;
  1032.       $vector &= $index;
  1033.  
  1034.   Difference
  1035.       $set1 = $set2 - $set3;
  1036.       $set1 -= $set2;
  1037.       $set1 = $set2 - $set1;
  1038.       $vector1 = $vector2 - $index;
  1039.       $vector1 = $index - $vector2;
  1040.       $vector -= $index;
  1041.  
  1042.   ExclusiveOr
  1043.       $set1 = $set2 ^ $set3;
  1044.       $set1 ^= $set2;
  1045.       $vector1 = $vector2 ^ $index;
  1046.       $vector ^= $index;
  1047.  
  1048.   Complement
  1049.       $set1 = -$set2;
  1050.       $set1 = ~$set2;
  1051.       $set = -$set;
  1052.       $set = ~$set;
  1053.  
  1054.   Subset Relationship
  1055.       if ($set1 <= $set2)
  1056.  
  1057.   True Subset Relationship
  1058.       if ($set1 < $set2)
  1059.  
  1060.   Superset Relationship
  1061.       if ($set1 >= $set2)
  1062.  
  1063.   True Superset Relationship
  1064.       if ($set1 > $set2)
  1065.  
  1066.   Norm
  1067.       $norm = abs($set);
  1068.  
  1069. =head1 IMPORTANT NOTES
  1070.  
  1071. =head2 GENERAL HINTS
  1072.  
  1073. =over 2
  1074.  
  1075. =item *
  1076.  
  1077. Method naming convention
  1078.  
  1079. Method names completely in lower case indicate a boolean return value!
  1080.  
  1081. (Except for method "C<new()>", of course.)
  1082.  
  1083. =item *
  1084.  
  1085. Boolean return values
  1086.  
  1087. Boolean return values in this class are always a numerical (!)
  1088. zero ("0") for "false" and a numerical (!) one ("1") for "true".
  1089.  
  1090. This means that you can use the methods of this class with boolean
  1091. return values as the conditional expression in "if", "unless" and
  1092. "while" statements.
  1093.  
  1094. =item *
  1095.  
  1096. Version
  1097.  
  1098. The function "C<Bit::Vector::Version()>" (the version of the "Vector.xs"
  1099. file) should always return the same version number as contained in the
  1100. variable "C<$Bit::Vector::VERSION>" (the version of the "Vector.pm" file).
  1101.  
  1102. =back
  1103.  
  1104. =head2 METHODS IMPLEMENTED IN C
  1105.  
  1106. =over 2
  1107.  
  1108. =item *
  1109.  
  1110. C<$vector = Bit::Vector-E<gt>new($elements);>
  1111.  
  1112. This is the bit vector constructor method.
  1113.  
  1114. Call this method to create a new bit vector containing
  1115. "$elements" bits (with indices from C<0> to C<$elements - 1>).
  1116.  
  1117. The method returns a reference to the new bit vector.
  1118.  
  1119. A new bit vector is always initialized so that all bits are cleared
  1120. (turned off).
  1121.  
  1122. An exception will be raised if the method is unable to allocate the
  1123. necessary memory.
  1124.  
  1125. An exception will also be raised if you try to create a bit vector
  1126. with zero elements (i.e., with length zero).
  1127.  
  1128. Note that if you specify a negative number for "$elements" it
  1129. will be interpreted as a large positive number due to its internal
  1130. 2's complement binary representation!
  1131.  
  1132. In such a case, the bit vector constructor method will obediently attempt
  1133. to create a bit vector of that size, probably resulting in an exception,
  1134. as explained above.
  1135.  
  1136. =item *
  1137.  
  1138. C<$vector-E<gt>Resize($elements);>
  1139.  
  1140. Changes the size of the given vector to the specified number of bits.
  1141.  
  1142. This method allows you to change the size of an existing bit vector or
  1143. set, preserving as many bits from the old vector as will fit into the
  1144. new one (i.e., all bits with indices smaller than the minimum of the
  1145. sizes of the two vectors, old and new).
  1146.  
  1147. If the number of machine words needed to store the new vector is smaller
  1148. than or equal to the number of words needed to store the old vector, the
  1149. memory allocated for the old vector is reused for the new one, and only
  1150. the relevant book-keeping information is adjusted accordingly.
  1151.  
  1152. This means that even if the number of bits increases, new memory is not
  1153. necessarily being allocated (i.e., if the old and the new number of bits
  1154. fit into the same number of machine words)!
  1155.  
  1156. If the number of machine words needed to store the new vector is greater
  1157. than the number of words needed to store the old vector, new memory is
  1158. allocated for the new vector, the old vector is copied to the new one,
  1159. the remaining bits in the new vector are cleared (turned off) and the old
  1160. vector is deleted, i.e., the memory that was allocated for it is released.
  1161.  
  1162. This also means that if you decrease the size of a given vector so that
  1163. it will use fewer machine words, and increase it again later so that it
  1164. will use more words than before but still less than the original vector,
  1165. new memory will be allocated anyway because the information about the
  1166. size of the original vector is lost when you downsize it.
  1167.  
  1168. Note also that if you specify a negative number for "$elements" it
  1169. will be interpreted as a large positive number due to its internal
  1170. 2's complement binary representation!
  1171.  
  1172. In such a case, "Resize()" will obediently attempt to create a bit
  1173. vector of that size, probably resulting in an exception, as explained
  1174. above (see method "new()").
  1175.  
  1176. Finally, note that resizing a bit vector to a size of zero elements (length
  1177. zero) is disallowed; an exception will be raised if you try to do so.
  1178.  
  1179. =item *
  1180.  
  1181. C<$elements = $vector-E<gt>Size();>
  1182.  
  1183. Returns the size (number of bits) the given vector was created with
  1184. (or "C<Resize()>"d to).
  1185.  
  1186. =item *
  1187.  
  1188. C<$vector-E<gt>Empty();>
  1189.  
  1190. Clears all bits in the given vector.
  1191.  
  1192. =item *
  1193.  
  1194. C<$vector-E<gt>Fill();>
  1195.  
  1196. Sets all bits in the given vector.
  1197.  
  1198. =item *
  1199.  
  1200. C<$vector-E<gt>Flip();>
  1201.  
  1202. Flips (i.e., complements) all bits in the given vector.
  1203.  
  1204. =item *
  1205.  
  1206. C<$vector-E<gt>Interval_Empty($min,$max);>
  1207.  
  1208. Clears all bits in the interval C<[$min..$max]> (including both limits)
  1209. in the given vector.
  1210.  
  1211. "C<$min>" and "C<$max>" may have the same value; this is the same
  1212. as clearing a single bit with "C<Bit_Off()>" (but less efficient).
  1213.  
  1214. Note that C<$vector-E<gt>Interval_Empty(0,$vector-E<gt>Size()-1);>
  1215. is the same as C<$vector-E<gt>Empty();> (but less efficient).
  1216.  
  1217. =item *
  1218.  
  1219. C<$vector-E<gt>Interval_Fill($min,$max);>
  1220.  
  1221. Sets all bits in the interval C<[$min..$max]> (including both limits)
  1222. in the given vector.
  1223.  
  1224. "C<$min>" and "C<$max>" may have the same value; this is the same
  1225. as setting a single bit with "C<Bit_On()>" (but less efficient).
  1226.  
  1227. Note that C<$vector-E<gt>Interval_Fill(0,$vector-E<gt>Size()-1);>
  1228. is the same as C<$vector-E<gt>Fill();> (but less efficient).
  1229.  
  1230. =item *
  1231.  
  1232. C<$vector-E<gt>Interval_Flip($min,$max);>
  1233.  
  1234. Flips (i.e., complements) all bits in the interval C<[$min..$max]>
  1235. (including both limits) in the given vector.
  1236.  
  1237. "C<$min>" and "C<$max>" may have the same value; this is the same
  1238. as flipping a single bit with "C<bit_flip()>" (but less efficient).
  1239.  
  1240. Note that C<$vector-E<gt>Interval_Flip(0,$vector-E<gt>Size()-1);>
  1241. is the same as C<$vector-E<gt>Flip();> and
  1242. C<$vector-E<gt>Complement($vector);>
  1243. (but less efficient).
  1244.  
  1245. =item *
  1246.  
  1247. C<($min,$max) = $vector-E<gt>Interval_Scan_inc($start)>
  1248.  
  1249. Returns the minimum and maximum indices of the next contiguous block
  1250. of set bits (i.e., bits in the "on" state).
  1251.  
  1252. The search starts at index "$start" (i.e., C<"$min" E<gt>= "$start">)
  1253. and proceeds upwards (i.e., C<"$max" E<gt>= "$min">), thus repeatedly
  1254. increments the search pointer "$start" (internally).
  1255.  
  1256. Note though that the contents of the variable (or scalar literal value)
  1257. "$start" is not altered! I.e., you have to set it to the desired value
  1258. yourself prior to each call to "Interval_Scan_inc()"! (See also the
  1259. example given below!)
  1260.  
  1261. Actually, the bit vector is not searched bit by bit, but one machine
  1262. word at a time, in order to speed up execution (this means that this
  1263. method is quite efficient!).
  1264.  
  1265. An empty list is returned if no such block can be found.
  1266.  
  1267. Note that a single set bit (surrounded by cleared bits) is a valid
  1268. block by this definition. In that case the return values for "$min"
  1269. and "$max" are the same.
  1270.  
  1271. Typical use:
  1272.  
  1273.     $start = 0;
  1274.     while (($start < $vector->Size()) &&
  1275.         (($min,$max) = $vector->Interval_Scan_inc($start)))
  1276.     {
  1277.         $start = $max + 2;
  1278.  
  1279.         # do something with $min and $max
  1280.     }
  1281.  
  1282. =item *
  1283.  
  1284. C<($min,$max) = $vector-E<gt>Interval_Scan_dec($start)>
  1285.  
  1286. Returns the minimum and maximum indices of the next contiguous block
  1287. of set bits (i.e., bits in the "on" state).
  1288.  
  1289. The search starts at index "$start" (i.e., C<"$max" E<lt>= "$start">)
  1290. and proceeds downwards (i.e., C<"$min" E<lt>= "$max">), thus repeatedly
  1291. decrements the search pointer "$start" (internally).
  1292.  
  1293. Note though that the contents of the variable (or scalar literal value)
  1294. "$start" is not altered! I.e., you have to set it to the desired value
  1295. yourself prior to each call to "Interval_Scan_dec()"! (See also the
  1296. example given below!)
  1297.  
  1298. Actually, the bit vector is not searched bit by bit, but one machine
  1299. word at a time, in order to speed up execution (this means that this
  1300. method is quite efficient!).
  1301.  
  1302. An empty list is returned if no such block can be found.
  1303.  
  1304. Note that a single set bit (surrounded by cleared bits) is a valid
  1305. block by this definition. In that case the return values for "$min"
  1306. and "$max" are the same.
  1307.  
  1308. Typical use:
  1309.  
  1310.     $start = $vector->Size() - 1;
  1311.     while (($start >= 0) &&
  1312.         (($min,$max) = $vector->Interval_Scan_dec($start)))
  1313.     {
  1314.         $start = $min - 2;
  1315.  
  1316.         # do something with $min and $max
  1317.     }
  1318.  
  1319. =item *
  1320.  
  1321. C<$vector-E<gt>Bit_Off($index);>
  1322.  
  1323. Clears the bit with index "$index" in the given vector.
  1324.  
  1325. This is equivalent to removing the element "$index"
  1326. from the given set.
  1327.  
  1328. Note that if you specify a negative number for "$index"
  1329. it will be interpreted as a large positive number due
  1330. to its internal 2's complement binary representation!
  1331.  
  1332. An exception is raised if "$index" lies outside the
  1333. permitted range from "C<0>" to "C<$vector-E<gt>Size()-1>".
  1334.  
  1335. =item *
  1336.  
  1337. C<$vector-E<gt>Bit_On($index);>
  1338.  
  1339. Sets the bit with index "$index" in the given vector.
  1340.  
  1341. This is equivalent to adding the element "$index"
  1342. to the given set.
  1343.  
  1344. Note that if you specify a negative number for "$index"
  1345. it will be interpreted as a large positive number due
  1346. to its internal 2's complement binary representation!
  1347.  
  1348. An exception is raised if "$index" lies outside the
  1349. permitted range from "C<0>" to "C<$vector-E<gt>Size()-1>".
  1350.  
  1351. =item *
  1352.  
  1353. C<$vector-E<gt>bit_flip($index)>
  1354.  
  1355. Flips (i.e., complements) the bit with index "$index"
  1356. in the given vector.
  1357.  
  1358. This is equivalent to adding the element "$index"
  1359. to the given set if it is NOT contained yet and
  1360. removing it if it IS contained.
  1361.  
  1362. Also returns the NEW state of the specified bit, i.e.,
  1363. returns "0" if it is cleared (in the "off" state) or
  1364. "1" if it is set (in the "on" state).
  1365.  
  1366. In other words it returns "true" if the specified
  1367. element is contained in the given set and "false"
  1368. otherwise.
  1369.  
  1370. Note that if you specify a negative number for "$index"
  1371. it will be interpreted as a large positive number due
  1372. to its internal 2's complement binary representation!
  1373.  
  1374. An exception is raised if "$index" lies outside the
  1375. permitted range from "C<0>" to "C<$vector-E<gt>Size()-1>".
  1376.  
  1377. =item *
  1378.  
  1379. C<$vector-E<gt>bit_test($index)>
  1380.  
  1381. Returns the current state of the bit with index "$index"
  1382. in the given vector, i.e., returns "0" if it is cleared
  1383. (in the "off" state) or "1" if it is set (in the "on" state).
  1384.  
  1385. In other words it returns "true" if the specified
  1386. element is contained in the given set and "false"
  1387. otherwise.
  1388.  
  1389. Note that if you specify a negative number for "$index"
  1390. it will be interpreted as a large positive number due
  1391. to its internal 2's complement binary representation!
  1392.  
  1393. An exception is raised if "$index" lies outside the
  1394. permitted range from "C<0>" to "C<$vector-E<gt>Size()-1>".
  1395.  
  1396. =item *
  1397.  
  1398. C<$vector-E<gt>is_empty()>
  1399.  
  1400. Tests wether the given bit vector is empty, i.e., wether ALL of
  1401. its bits are cleared (in the "off" state).
  1402.  
  1403. Returns "true" ("1") if the bit vector is empty and "false" ("0")
  1404. otherwise.
  1405.  
  1406. =item *
  1407.  
  1408. C<$vector-E<gt>is_full()>
  1409.  
  1410. Tests wether the given bit vector is full, i.e., wether ALL of
  1411. its bits are set (in the "on" state).
  1412.  
  1413. Returns "true" ("1") if the bit vector is full and "false" ("0")
  1414. otherwise.
  1415.  
  1416. =item *
  1417.  
  1418. C<$vector1-E<gt>equal($vector2)>
  1419.  
  1420. Tests the two given bit vectors for equality.
  1421.  
  1422. Returns "true" ("1") if the two bit vectors are exact
  1423. copies of one another and "false" ("0") otherwise.
  1424.  
  1425. An exception is raised if the two bit vectors have
  1426. different sizes, i.e., if C<$vector1-E<gt>Size()>
  1427. C<!=> C<$vector2-E<gt>Size()>.
  1428.  
  1429. =item *
  1430.  
  1431. C<$vector1-E<gt>lexorder($vector2)>
  1432.  
  1433. Tests the lexical order of the two given bit vectors.
  1434.  
  1435. I.e., the two bit vectors are regarded as two large
  1436. (positive) numbers in binary representation which
  1437. are compared.
  1438.  
  1439. The least significant bit (LSB) of this binary number
  1440. is the bit with index "C<0>", the most significant bit
  1441. (MSB) is the bit with index "C<$vector-E<gt>Size()-1>".
  1442.  
  1443. Returns "true" ("1") if the first bit vector is less
  1444. than or equal to the second bit vector and "false"
  1445. ("0") otherwise.
  1446.  
  1447. An exception is raised if the two bit vectors have
  1448. different sizes, i.e., if C<$vector1-E<gt>Size()>
  1449. C<!=> C<$vector2-E<gt>Size()>.
  1450.  
  1451. =item *
  1452.  
  1453. C<$vector1-E<gt>Compare($vector2)>
  1454.  
  1455. Compares the two given bit vectors.
  1456.  
  1457. I.e., the two bit vectors are regarded as two large
  1458. (positive) numbers in binary representation which
  1459. are compared.
  1460.  
  1461. The least significant bit (LSB) of this binary number
  1462. is the bit with index "C<0>", the most significant bit
  1463. (MSB) is the bit with index "C<$vector-E<gt>Size()-1>".
  1464.  
  1465. Returns "-1" if the first bit vector is less than the
  1466. second bit vector, "0" if the two bit vectors are exact
  1467. copies of one another and "1" if the first bit vector
  1468. is greater than the second bit vector.
  1469.  
  1470. An exception is raised if the two bit vectors have
  1471. different sizes, i.e., if C<$vector1-E<gt>Size()>
  1472. C<!=> C<$vector2-E<gt>Size()>.
  1473.  
  1474. =item *
  1475.  
  1476. C<$vector1-E<gt>Copy($vector2);>
  1477.  
  1478. Copies the contents of bit vector "$vector2" to
  1479. bit vector "$vector1".
  1480.  
  1481. The previous contents of bit vector "$vector1"
  1482. get overwritten, i.e., are lost.
  1483.  
  1484. Both vectors must exist beforehand, i.e., this method
  1485. does not CREATE any new bit vector object!
  1486.  
  1487. An exception is raised if the two bit vectors have
  1488. different sizes, i.e., if C<$vector1-E<gt>Size()>
  1489. C<!=> C<$vector2-E<gt>Size()>.
  1490.  
  1491. =item *
  1492.  
  1493. C<$carry_out = $vector-E<gt>rotate_left();>
  1494.  
  1495.   carry             MSB           vector:           LSB
  1496.    out:
  1497.   +---+            +---+---+---+---     ---+---+---+---+
  1498.   |   |  <---+---  |   |   |   |    ...    |   |   |   |  <---+
  1499.   +---+      |     +---+---+---+---     ---+---+---+---+      |
  1500.              |                                                |
  1501.              +------------------------------------------------+
  1502.  
  1503. The least significant bit (LSB) is the bit with index "C<0>", the most
  1504. significant bit (MSB) is the bit with index "C<$vector-E<gt>Size()-1>".
  1505.  
  1506. =item *
  1507.  
  1508. C<$carry_out = $vector-E<gt>rotate_right();>
  1509.  
  1510.           MSB           vector:           LSB            carry
  1511.                                                           out:
  1512.          +---+---+---+---     ---+---+---+---+           +---+
  1513.   +--->  |   |   |   |    ...    |   |   |   |  ---+---> |   |
  1514.   |      +---+---+---+---     ---+---+---+---+     |     +---+
  1515.   |                                                |
  1516.   +------------------------------------------------+
  1517.  
  1518. The least significant bit (LSB) is the bit with index "C<0>", the most
  1519. significant bit (MSB) is the bit with index "C<$vector-E<gt>Size()-1>".
  1520.  
  1521. =item *
  1522.  
  1523. C<$carry_out = $vector-E<gt>shift_left($carry_in);>
  1524.  
  1525.   carry         MSB           vector:           LSB         carry
  1526.    out:                                                      in:
  1527.   +---+        +---+---+---+---     ---+---+---+---+        +---+
  1528.   |   |  <---  |   |   |   |    ...    |   |   |   |  <---  |   |
  1529.   +---+        +---+---+---+---     ---+---+---+---+        +---+
  1530.  
  1531. The least significant bit (LSB) is the bit with index "C<0>", the most
  1532. significant bit (MSB) is the bit with index "C<$vector-E<gt>Size()-1>".
  1533.  
  1534. =item *
  1535.  
  1536. C<$carry_out = $vector-E<gt>shift_right($carry_in);>
  1537.  
  1538.   carry         MSB           vector:           LSB         carry
  1539.    in:                                                       out:
  1540.   +---+        +---+---+---+---     ---+---+---+---+        +---+
  1541.   |   |  --->  |   |   |   |    ...    |   |   |   |  --->  |   |
  1542.   +---+        +---+---+---+---     ---+---+---+---+        +---+
  1543.  
  1544. The least significant bit (LSB) is the bit with index "C<0>", the most
  1545. significant bit (MSB) is the bit with index "C<$vector-E<gt>Size()-1>".
  1546.  
  1547. =item *
  1548.  
  1549. C<$vector-E<gt>Move_Left($bits);>
  1550.  
  1551. Shifts the given bit vector left by "$bits" bits, i.e., inserts "$bits"
  1552. new bits at the lower end (least significant bit) of the bit vector,
  1553. moving all other bits up by "$bits" places, thereby losing the "$bits"
  1554. most significant bits.
  1555.  
  1556. The inserted new bits are all cleared (set to the "off" state).
  1557.  
  1558. This method does nothing if "$bits" is equal to zero.
  1559.  
  1560. Beware that the whole bit vector is cleared WITHOUT WARNING if "$bits"
  1561. is greater than or equal to the size of the given bit vector!
  1562.  
  1563. Beware also that if you specify a negative number for "$bits", it will be
  1564. interpreted as a large positive number due to its internal 2's complement
  1565. binary representation, which will probably empty your bit vector!
  1566.  
  1567. In fact this method is equivalent to
  1568.  
  1569.   for ( $i = 0; $i < $bits; $i++ ) { $vector->shift_left(0); }
  1570.  
  1571. except that it is much more efficient (for "$bits" greater than or
  1572. equal to the number of bits in a machine word on your system) than
  1573. this straightforward approach.
  1574.  
  1575. =item *
  1576.  
  1577. C<$vector-E<gt>Move_Right($bits);>
  1578.  
  1579. Shifts the given bit vector right by "$bits" bits, i.e., deletes the
  1580. "$bits" least significant bits of the bit vector, moving all other bits
  1581. down by "$bits" places, thereby creating "$bits" new bits at the upper
  1582. end (most significant bit) of the bit vector.
  1583.  
  1584. These new bits are all cleared (set to the "off" state).
  1585.  
  1586. This method does nothing if "$bits" is equal to zero.
  1587.  
  1588. Beware that the whole bit vector is cleared WITHOUT WARNING if "$bits"
  1589. is greater than or equal to the size of the given bit vector!
  1590.  
  1591. Beware also that if you specify a negative number for "$bits", it will be
  1592. interpreted as a large positive number due to its internal 2's complement
  1593. binary representation, which will probably empty your bit vector!
  1594.  
  1595. In fact this method is equivalent to
  1596.  
  1597.   for ( $i = 0; $i < $bits; $i++ ) { $vector->shift_right(0); }
  1598.  
  1599. except that it is much more efficient (for "$bits" greater than or
  1600. equal to the number of bits in a machine word on your system) than
  1601. this straightforward approach.
  1602.  
  1603. =item *
  1604.  
  1605. C<$carry = $vector-E<gt>increment();>
  1606.  
  1607. This method is crucial for generating all possible patterns of set
  1608. and cleared bits for a given bit vector in an ordered fashion, a
  1609. feature needed in many applications to cycle through all possible
  1610. values a bit vector of the given length can assume.
  1611.  
  1612. The method increments the given bit vector as though it was a large
  1613. (positive) number in binary representation.
  1614.  
  1615. The least significant bit (LSB) of this binary number
  1616. is the bit with index "C<0>", the most significant bit
  1617. (MSB) is the bit with index "C<$vector-E<gt>Size()-1>".
  1618.  
  1619. This method returns "true" ("1") if a carry-over occurred, i.e.,
  1620. if the bit vector was completely filled with set bits before this
  1621. operation took place (the bit vector will contain only cleared bits
  1622. after this operation in that case) and "false" ("0") otherwise.
  1623.  
  1624. This can be used as the terminating condition in a "while" loop,
  1625. for instance.
  1626.  
  1627. =item *
  1628.  
  1629. C<$carry = $vector-E<gt>decrement();>
  1630.  
  1631. This method is crucial for generating all possible patterns of set
  1632. and cleared bits for a given bit vector in an ordered fashion, a
  1633. feature needed in many applications to cycle through all possible
  1634. values a bit vector of the given length can assume.
  1635.  
  1636. The method decrements the given bit vector as though it was a large
  1637. (positive) number in binary representation.
  1638.  
  1639. The least significant bit (LSB) of this binary number
  1640. is the bit with index "C<0>", the most significant bit
  1641. (MSB) is the bit with index "C<$vector-E<gt>Size()-1>".
  1642.  
  1643. This method returns "true" ("1") if a carry-over occurred, i.e.,
  1644. if the bit vector was completely filled with cleared bits before
  1645. this operation took place (the bit vector will contain only set bits
  1646. after this operation in that case) and "false" ("0") otherwise.
  1647.  
  1648. This can be used as the terminating condition in a "while" loop,
  1649. for instance.
  1650.  
  1651. =item *
  1652.  
  1653. C<$string = $vector-E<gt>to_String();>
  1654.  
  1655. Returns a hexadecimal string representing the given bit vector.
  1656.  
  1657. Note that this representation is quite compact, in that it only
  1658. needs twice the number of bytes needed to store the bit vector
  1659. itself, internally!
  1660.  
  1661. The rightmost hexadecimal digit in this string represents the
  1662. four least significant bits of the given bit vector (i.e., the
  1663. bits with indices "3", "2", "1" and "0").
  1664.  
  1665. The leftmost hexadecimal digit(s) in the string represent(s) the
  1666. most significant and/or unused bits - this is due to the fact
  1667. that this class uses machine words as its basic storage unit (to
  1668. increase efficiency).
  1669.  
  1670. Since a hexadecimal digit is always worth four bits, the length
  1671. of the string always corresponds to a multiple of four bits anyway.
  1672.  
  1673. To spare extra overhead, the most significant machine word is always
  1674. completely converted into hexadecimal characters - which may produce
  1675. some (innocuous) leading hexadecimal zeros at the left end of the string
  1676. representing the unused bits of that bit vector.
  1677.  
  1678. =item *
  1679.  
  1680. C<$vector-E<gt>from_string($string)>
  1681.  
  1682. Allows to read in the contents of a bit vector from a hexadecimal
  1683. string, such as returned by the method "to_String()" (described
  1684. immediately above), for instance.
  1685.  
  1686. The string is read from right to left (!), and the bits corresponding
  1687. to each hexadecimal digit are assigned to the bits in the given bit
  1688. vector in ascending order of their indices, i.e., the rightmost
  1689. hexadecimal digit is assigned to the bits with indices "0", "1", "2"
  1690. and "3", the second rightmost hexadecimal digit is assigned to the
  1691. bits with indices "4", "5", "6" and "7", and so on.
  1692.  
  1693. If the given string contains less hexadecimal digits than are needed
  1694. to completely fill the given bit vector, the remaining bits are all
  1695. cleared.
  1696.  
  1697. In other words, even if the given string does not contain enough digits
  1698. to completely fill the given bit vector, the previous contents of the
  1699. bit vector are erased completely.
  1700.  
  1701. If the given string is longer than it needs to fill the given bit vector,
  1702. the superfluous characters are simply ignored.
  1703.  
  1704. (In fact they are ignored completely - they are not even checked for
  1705. proper syntax! See also immediately below.)
  1706.  
  1707. This behaviour is intentional so that you may read in the string
  1708. representing one bit vector into another bit vector of different
  1709. size, i.e., as much of it as will fit!
  1710.  
  1711. If during the process of reading the given string any character is
  1712. encountered which is not a hexadecimal digit, an error ensues.
  1713.  
  1714. In such a case the bit vector is filled up with zeros starting at
  1715. the point of the error and the method returns "false" ("0").
  1716.  
  1717. If all goes well the method returns "true" ("1").
  1718.  
  1719. =item *
  1720.  
  1721. C<$set1-E<gt>Union($set2,$set3);>
  1722.  
  1723. This method calculates the union of "$set2" and "$set3" and stores
  1724. the result in "$set1".
  1725.  
  1726. This is usually written as "C<$set1 = $set2 u $set3>" in set theory
  1727. (where "u" is the "cup" operator).
  1728.  
  1729. (On systems where the "cup" character is unavailable this operator
  1730. is often denoted by a plus sign "+".)
  1731.  
  1732. In-place calculation is also possible, i.e., "$set1" may be identical
  1733. with "$set2" or "$set3" or both.
  1734.  
  1735. An exception is raised if the sizes of the three sets do not match.
  1736.  
  1737. =item *
  1738.  
  1739. C<$set1-E<gt>Intersection($set2,$set3);>
  1740.  
  1741. This method calculates the intersection of "$set2" and "$set3" and
  1742. stores the result in "$set1".
  1743.  
  1744. This is usually written as "C<$set1 = $set2 n $set3>" in set theory
  1745. (where "n" is the "cap" operator).
  1746.  
  1747. (On systems where the "cap" character is unavailable this operator
  1748. is often denoted by an asterisk "*".)
  1749.  
  1750. In-place calculation is also possible, i.e., "$set1" may be identical
  1751. with "$set2" or "$set3" or both.
  1752.  
  1753. An exception is raised if the sizes of the three sets do not match.
  1754.  
  1755. =item *
  1756.  
  1757. C<$set1-E<gt>Difference($set2,$set3);>
  1758.  
  1759. This method calculates the difference of "$set2" less "$set3" and
  1760. stores the result in "$set1".
  1761.  
  1762. This is usually written as "C<$set1 = $set2 \ $set3>" in set theory
  1763. (where "\" is the "less" operator).
  1764.  
  1765. In-place calculation is also possible, i.e., "$set1" may be identical
  1766. with "$set2" or "$set3" or both.
  1767.  
  1768. An exception is raised if the sizes of the three sets do not match.
  1769.  
  1770. =item *
  1771.  
  1772. C<$set1-E<gt>ExclusiveOr($set2,$set3);>
  1773.  
  1774. This method calculates the symmetric difference of "$set2" and "$set3"
  1775. and stores the result in "$set1".
  1776.  
  1777. This can be written as "C<($vec2 u $vec3) \ ($vec2 n $vec3)>" in set
  1778. theory (the union of the two sets less their intersection).
  1779.  
  1780. When sets are implemented as bit vectors then the above formula is
  1781. equivalent to the exclusive-or between corresponding bits of the two
  1782. bit vectors (hence the name of this method).
  1783.  
  1784. Note that this method is also much more efficient than evaluating the
  1785. above formula explicitly since it uses a built-in machine language
  1786. instruction internally.
  1787.  
  1788. In-place calculation is also possible, i.e., "$set1" may be identical
  1789. with "$set2" or "$set3" or both.
  1790.  
  1791. An exception is raised if the sizes of the three sets do not match.
  1792.  
  1793. =item *
  1794.  
  1795. C<$set1-E<gt>Complement($set2);>
  1796.  
  1797. This method calculates the complement of "$set2" and stores the result
  1798. in "$set1".
  1799.  
  1800. In-place calculation is also possible, i.e., "$set1" may be identical
  1801. with "$set2".
  1802.  
  1803. An exception is raised if the sizes of the two sets do not match.
  1804.  
  1805. =item *
  1806.  
  1807. C<$set1-E<gt>subset($set2)>
  1808.  
  1809. Returns "true" ("1") if "$set1" is a subset of "$set2"
  1810. (i.e., completely contained in "$set2") and "false" ("0")
  1811. otherwise.
  1812.  
  1813. Note that by definition, if two sets are identical they are
  1814. also subsets (and also supersets) of each other.
  1815.  
  1816. An exception is raised if the sizes of the two sets do not match.
  1817.  
  1818. =item *
  1819.  
  1820. C<$norm = $set-E<gt>Norm();>
  1821.  
  1822. Returns the norm (number of bits which are set) of the given vector.
  1823.  
  1824. This is equivalent to the number of elements contained in the given
  1825. set.
  1826.  
  1827. =item *
  1828.  
  1829. C<$min = $set-E<gt>Min();>
  1830.  
  1831. Returns the minimum of the given set.
  1832.  
  1833. If the set is empty, plus infinity (represented by the constant
  1834. "MAX_LONG" on your system) is returned.
  1835.  
  1836. =item *
  1837.  
  1838. C<$max = $set-E<gt>Max();>
  1839.  
  1840. Returns the maximum of the given set.
  1841.  
  1842. If the set is empty, minus infinity (represented by the constant
  1843. "MIN_LONG" on your system) is returned.
  1844.  
  1845. =item *
  1846.  
  1847. C<$m1-E<gt>Multiplication($r1,$c1,$m2,$r2,$c2,$m3,$r3,$c3,);>
  1848.  
  1849. This method multiplies two boolean matrices (stored as bit vectors)
  1850. "$m2" and "$m3" and stores the result in matrix "$m1".
  1851.  
  1852. An exception is raised if the product of the number of rows and
  1853. columns of any of the three matrices differs from the size of
  1854. the corresponding bit vector.
  1855.  
  1856. An exception is also raised if the numbers of rows and columns
  1857. of the three matrices do not harmonize in the required manner:
  1858.  
  1859.   rows1 == rows2
  1860.   cols1 == cols3
  1861.   cols2 == rows3
  1862.  
  1863. This method is used by the "Math::MatrixBool" application class
  1864. (see also L<Math::MatrixBool(3)>).
  1865.  
  1866. =item *
  1867.  
  1868. C<$matrix-E<gt>Closure($rows,$cols);>
  1869.  
  1870. This method calculates the reflexive transitive closure of the
  1871. given boolean matrix (stored as a bit vector) using Kleene's
  1872. algoritm.
  1873.  
  1874. (See L<Math::Kleene(3)> for a brief introduction into the
  1875. theory behind Kleene's algorithm.)
  1876.  
  1877. The reflexive transitive closure answers the question wether
  1878. a path exists between any two vortices of a graph whose edges
  1879. are given as a matrix:
  1880.  
  1881. If a (directed) edge exists going from vortex "i" to vortex "j",
  1882. then the element in the matrix with coordinates (i,j) is set to
  1883. "1" (otherwise it remains set to "0").
  1884.  
  1885. If the edges are undirected the resulting matrix is symmetric,
  1886. i.e., elements (i,j) and (j,i) always contain the same value.
  1887.  
  1888. The matrix representing the edges of the graph only answers the
  1889. question wether an EDGE (!) exists between any two vortices of
  1890. the graph or not, whereas the reflexive transitive closure
  1891. answers the question wether a PATH (a series of adjacent edges)
  1892. exists between any two vortices of the graph!
  1893.  
  1894. Note that the contents of the given matrix are modified by
  1895. this method, so make a copy of the initial matrix in time
  1896. if you are going to need it again later!
  1897.  
  1898. An exception is raised if the given matrix is not quadratic,
  1899. i.e., if the number of rows and columns of the given matrix
  1900. is not identical.
  1901.  
  1902. An exception is also raised if the product of the number of
  1903. rows and columns of the given matrix differs from the size
  1904. of its underlying bit vector.
  1905.  
  1906. This method is used by the "Math::MatrixBool" application class
  1907. (see also L<Math::MatrixBool(3)>).
  1908.  
  1909. =back
  1910.  
  1911. =head2 METHODS IMPLEMENTED IN PERL
  1912.  
  1913. =over 2
  1914.  
  1915. =item *
  1916.  
  1917. C<$other_vector = $some_vector-E<gt>Shadow();>
  1918.  
  1919. Creates a NEW bit vector of the SAME SIZE as "$some_vector"
  1920. which is EMPTY.
  1921.  
  1922. Just like a shadow that has the same shape as the object it
  1923. originates from, but is flat and has no volume, i.e., contains
  1924. nothing.
  1925.  
  1926. =item *
  1927.  
  1928. C<$twin_vector = $some_vector-E<gt>Clone();>
  1929.  
  1930. Creates a NEW bit vector of the SAME SIZE as "$some_vector"
  1931. which is an EXACT COPY of "$some_vector".
  1932.  
  1933. =item *
  1934.  
  1935. C<$vector = Bit::Vector-E<gt>new_from_String($string);>
  1936.  
  1937. Creates a new bit vector of the size C<4 * length($string)>
  1938. and tries to fill it with the contents of "$string" which
  1939. must consist entirely of hexadecimal characters.
  1940.  
  1941. Example:
  1942.  
  1943.   $vector = Bit::Vector->new_from_String("20208828828208A20A08A28AC");
  1944.  
  1945. (Fills "$vector" with all prime numbers below 100.)
  1946.  
  1947. Hexadecimal characters "A" through "F" may be in lower or upper case
  1948. indiscriminately.
  1949.  
  1950. An exception is raised if the string contains other than hexadecimal
  1951. characters.
  1952.  
  1953. An exception is also raised if the string is empty because bit vectors
  1954. of zero elements (length zero) are not permitted in this class.
  1955.  
  1956. Finally, an exception is also raised if the necessary memory for the
  1957. bit vector cannot be allocated.
  1958.  
  1959. =item *
  1960.  
  1961. C<$string = $vector-E<gt>to_ASCII();>
  1962.  
  1963. Converts the given bit vector or set into an enumeration of single
  1964. indices and ranges of indices ("newsrc" style), representing the
  1965. bits that are set (i.e., in the "on" state) in the bit vector.
  1966.  
  1967. Example:
  1968.  
  1969.   $vector = Bit::Vector->new(20);
  1970.   $vector->Bit_On(2);
  1971.   $vector->Bit_On(3);
  1972.   $vector->Bit_On(11);
  1973.   $vector->Interval_Fill(5,7);
  1974.   $vector->Interval_Fill(13,19);
  1975.   print $vector->to_ASCII(), "\n";
  1976.  
  1977. which prints "2,3,5-7,11,13-19".
  1978.  
  1979. If the given bit vector is empty the resulting string will
  1980. also be empty.
  1981.  
  1982. =item *
  1983.  
  1984. C<$vector-E<gt>from_ASCII($string);>
  1985.  
  1986. First empties the given vector and then tries to set the
  1987. bits and ranges of bits specified in the given string.
  1988.  
  1989. The string "$string" must contain positive integers or
  1990. ranges (two positive integers separated by a dash "-")
  1991. separated by commas.
  1992.  
  1993. All other characters are disallowed (including white space).
  1994.  
  1995. An exception will be raised if the string does not obey
  1996. this syntax.
  1997.  
  1998. In each range the first integer must always be less than
  1999. or equal to the second one, otherwise an exception is raised.
  2000.  
  2001. An exception is also raised if any of the integers exceeds
  2002. the range of permitted indices in the given string, i.e., if
  2003. any integer is greater than or equal to C<$vector-E<gt>Size()>.
  2004.  
  2005. Example:
  2006.  
  2007.   eval { $vector->from_ASCII("2,3,5-7,11,13-19"); };
  2008.  
  2009. Note that the order of the indices and ranges is irrelevant,
  2010. i.e.,
  2011.  
  2012.   eval { $vector->from_ASCII("11,5-7,3,13-19,2"); };
  2013.  
  2014. results in the same vector as in the example above.
  2015.  
  2016. Ranges and indices may also overlap.
  2017.  
  2018. This is because each (single) index in the string is passed
  2019. to the method "Bit_On()" and each range to the method
  2020. "Interval_Fill()" internally.
  2021.  
  2022. So the resulting vector (or set) is just the union of all
  2023. the specified elements and ranges.
  2024.  
  2025. =back
  2026.  
  2027. =head2 OVERLOADED OPERATORS
  2028.  
  2029. =over 2
  2030.  
  2031. =item *
  2032.  
  2033. Emptyness
  2034.  
  2035. Note that the method for determining emptyness is quite efficient:
  2036.  
  2037. The method stops searching the given bit vector as soon as it finds
  2038. the first non-zero machine word.
  2039.  
  2040. =item *
  2041.  
  2042. Equality
  2043.  
  2044. The method for determining equality is also quite efficient:
  2045.  
  2046. It stops at the first differing machine word it finds.
  2047.  
  2048. =item *
  2049.  
  2050. Lexical Comparison
  2051.  
  2052. Using the overloaded operator "cmp" to compare two bit vectors as in
  2053. "C<$vector1 cmp $vector2>" is essentially the same as comparing the
  2054. two corresponding hexadecimal strings returned by the "to_String()"
  2055. method, i.e., "C<$vector1-E<gt>to_String() cmp $vector2-E<gt>to_String()>".
  2056.  
  2057. The result is exactly the same (provided that both bit vectors have
  2058. the same size!), but using the overloaded operator "cmp" is much more
  2059. efficient since the additional overhead of converting both bit vectors
  2060. into strings is avoided.
  2061.  
  2062. Moreover, with the overloaded operator "cmp", the two bit vectors
  2063. are compared one machine word (usually 32 or 64 bits) at a time,
  2064. which is much faster than comparing one hexadecimal character
  2065. (4 bits worth!) at a time in a string comparison.
  2066.  
  2067. This comparison ends as soon as two differing words are found, i.e.,
  2068. in many cases the operator doesn't even need to look at the entire
  2069. bit vector!
  2070.  
  2071. Again, since the operator looks at more bits at a time, the search
  2072. ends much earlier than in a string comparison.
  2073.  
  2074. =item *
  2075.  
  2076. Move Left
  2077.  
  2078. In its first form, C<$vector1 = $vector2 E<lt>E<lt> $bits;>, creates
  2079. a new vector of the same size as "$vector2", copies the contents of
  2080. "$vector2" to it, then shifts this new vector left by the indicated
  2081. number of bits and finally returns a reference to this new vector.
  2082.  
  2083. Note that an exception will be raised if you swap the two arguments
  2084. of this operator.
  2085.  
  2086. In its second form, C<$vector E<lt>E<lt>= $bits;>, shifts the given
  2087. vector "$vector" left by the indicated number of bits.
  2088.  
  2089. =item *
  2090.  
  2091. Move Right
  2092.  
  2093. In its first form, C<$vector1 = $vector2 E<gt>E<gt> $bits;>, creates
  2094. a new vector of the same size as "$vector2", copies the contents of
  2095. "$vector2" to it, then shifts this new vector right by the indicated
  2096. number of bits and finally returns a reference to this new vector.
  2097.  
  2098. Note that an exception will be raised if you swap the two arguments
  2099. of this operator.
  2100.  
  2101. In its second form, C<$vector E<gt>E<gt>= $bits;>, shifts the given
  2102. vector "$vector" right by the indicated number of bits.
  2103.  
  2104. =item *
  2105.  
  2106. String Conversion
  2107.  
  2108. Currently, converting a bit vector into a string using the overloaded
  2109. operator C<""> is performed using the method "to_ASCII()" internally,
  2110. which is probably the preferred behaviour.
  2111.  
  2112. If you think that this operator should rather convert any given bit
  2113. vector into a hexadecimal string using the method "to_String()", then
  2114. you should edit the file "Vector.pm" in this distribution as follows:
  2115.  
  2116. Locate the method "sub _string" and change the line
  2117.  
  2118.   return( $object->to_ASCII() );
  2119.  
  2120. to
  2121.  
  2122.   return( $object->to_String() );
  2123.  
  2124. =item *
  2125.  
  2126. Union
  2127.  
  2128. Since there is no "cup" character in the ASCII alphabet, the plus sign "+"
  2129. is used here to denote the union operator from set theory.
  2130.  
  2131. The pipe symbol (or "vertical bar") "|" may be used as an alias for the
  2132. plus sign "+".
  2133.  
  2134. =item *
  2135.  
  2136. Intersection
  2137.  
  2138. Since there is no "cap" character in the ASCII alphabet, the asterisk "*"
  2139. is used here to denote the intersection operator from set theory.
  2140.  
  2141. The ampersand "&" may be used as an alias for the asterisk "*".
  2142.  
  2143. =item *
  2144.  
  2145. Difference
  2146.  
  2147. Since the backslash "\" is not an (overloadable) operator in Perl
  2148. (and a very special character anyway) the minus sign "-" is used
  2149. here to denote the "less" operator from set theory.
  2150.  
  2151. =item *
  2152.  
  2153. ExclusiveOr
  2154.  
  2155. Since there is no widely accepted symbol to denote the symmetric
  2156. difference in set theory (at least not to my knowledge - unless it
  2157. is the dotted minus sign, which alas is also a character unavailable
  2158. in the ASCII alphabet), the caret "^" (which is the "exclusive-or"
  2159. operator anyway) is simply used here to express the symmetric
  2160. difference of two sets.
  2161.  
  2162. =item *
  2163.  
  2164. Complement
  2165.  
  2166. The tilde "~" as well as the unary minus "-" are used here
  2167. (interchangeably!) to denote the complement of a set.
  2168.  
  2169. =item *
  2170.  
  2171. Subset Relationship
  2172.  
  2173. Since there is no "contained in or equal" sign in the ASCII alphabet,
  2174. the usual operator "<=" is used instead to denote subset relationship.
  2175.  
  2176. =item *
  2177.  
  2178. True Subset Relationship
  2179.  
  2180. Since there is no "contained in" sign in the ASCII alphabet, the usual
  2181. operator "<" is used instead to denote (true) subset relationship.
  2182.  
  2183. =item *
  2184.  
  2185. Superset Relationship
  2186.  
  2187. Since there is no "superset of or equal" sign in the ASCII alphabet,
  2188. the usual operator ">=" is used instead to denote superset relationship.
  2189.  
  2190. =item *
  2191.  
  2192. True Superset Relationship
  2193.  
  2194. Since there is no "superset of" sign in the ASCII alphabet, the usual
  2195. operator ">" is used instead to denote (true) superset relationship.
  2196.  
  2197. =item *
  2198.  
  2199. Norm
  2200.  
  2201. The function "abs()" can be used to return the number of elements in
  2202. a given set.
  2203.  
  2204. =back
  2205.  
  2206. =head1 DESCRIPTION
  2207.  
  2208. This class allows you to create bit vectors and sets of arbitrary size
  2209. (only limited by the size of a machine word and available memory on your
  2210. system) with indices (= elements) in the range from zero to some positive
  2211. integer, to dynamically change the size of such bit vectors or sets and to
  2212. perform a broad range of basic operations on them, like
  2213.  
  2214. =over 4
  2215.  
  2216. =item -
  2217.  
  2218. adding or removing elements (setting and clearing single bits),
  2219.  
  2220. =item -
  2221.  
  2222. testing the presence of a certain element (testing a single bit),
  2223.  
  2224. =item -
  2225.  
  2226. setting or clearing contiguous ranges of bits,
  2227.  
  2228. =item -
  2229.  
  2230. detecting contiguous ranges of set bits,
  2231.  
  2232. =item -
  2233.  
  2234. copying bit vectors,
  2235.  
  2236. =item -
  2237.  
  2238. converting a bit vector into either a compact (hexadecimal) or a
  2239. human-readable string representation (allowing you to store bit
  2240. vectors in a file, for instance),
  2241.  
  2242. =item -
  2243.  
  2244. reading in the contents of a bit vector from a string,
  2245.  
  2246. =item -
  2247.  
  2248. comparing two bit vectors for equality and lexical order,
  2249.  
  2250. =item -
  2251.  
  2252. performing bitwise shift and rotation operations,
  2253.  
  2254. =item -
  2255.  
  2256. computing the union, intersection, difference, symmetric difference
  2257. or complement of sets,
  2258.  
  2259. =item -
  2260.  
  2261. testing two sets for equality or inclusion (subset relationship),
  2262.  
  2263. =item -
  2264.  
  2265. computing the minimum, the maximum and the norm (number of elements)
  2266. of a set,
  2267.  
  2268. =back
  2269.  
  2270. and more.
  2271.  
  2272. Note also that it is very easy to implement sets of arbitrary intervals of
  2273. integers using this module (negative indices are no obstacle), despite the
  2274. fact that only intervals of positive integers (from zero to some positive
  2275. integer) are supported directly.
  2276.  
  2277. Please refer to the "Set::IntegerRange" module (also contained in this
  2278. distribution) and L<Set::IntegerRange(3)> to see how this can be done!
  2279.  
  2280. The "Bit::Vector" module is mainly intended for mathematical or algorithmical
  2281. computations. There are also a number of efficient algorithms that rely on
  2282. sets (and bit vectors).
  2283.  
  2284. An example of such an efficient algorithm (which uses a different
  2285. representation for sets, however, not bit vectors) is Kruskal's
  2286. algorithm for minimal spanning trees in graphs.
  2287.  
  2288. (See the module "Graph::Kruskal" and L<Graph::Kruskal(3)> in this
  2289. distribution.)
  2290.  
  2291. Another famous algorithm using bit vectors is the "Sieve of Erathostenes"
  2292. for calculating prime numbers, which is included here as a demo program
  2293. (see the file "primes.pl" in this distribution).
  2294.  
  2295. An important field of application is the computation of "first", "follow"
  2296. and "look-ahead" character sets for the construction of LL, SLR, LR and LALR
  2297. parsers for compilers (or a compiler-compiler, like "yacc", for instance).
  2298.  
  2299. (That's what the C library in this package was initially written for.)
  2300.  
  2301. (See Aho, Hopcroft, Ullman, "The Design and Analysis of Computer Algorithms"
  2302. for an excellent book on efficient algorithms and the famous "Dragon Book"
  2303. on how to build compilers by Aho, Sethi, Ullman.)
  2304.  
  2305. Therefore, this module is primarily designed for efficiency, which is the
  2306. reason why most of its methods are implemented in C.
  2307.  
  2308. To increase execution speed, the module doesn't use bytes as its basic storage
  2309. unit, it rather uses machine words, assuming that a machine word is the most
  2310. efficiently handled size of all scalar types on any machine (that's what the
  2311. ANSI C standard proposes and assumes anyway).
  2312.  
  2313. In order to achieve this, it automatically determines the number of bits
  2314. in a machine word on your system and then adjusts its internal configuration
  2315. constants accordingly.
  2316.  
  2317. The greater the size of this basic storage unit, the better the complexity
  2318. (= execution speed) of the methods in this module (but also the greater the
  2319. average waste of unused bits in the last word).
  2320.  
  2321. Note that the C library of this package ("BitVector.c") is designed in such
  2322. a way that it can be used independently from Perl and this Perl extension
  2323. module. (!)
  2324.  
  2325. For this, you can use the file "BitVector.o" exactly as it is produced when
  2326. building this module! It contains no references to Perl, and it doesn't need
  2327. any Perl header files in order to compile. (It only needs "Definitions.h" and
  2328. some system header files.)
  2329.  
  2330. Note however that this C library does not perform any bounds checking
  2331. whatsoever! (This is your application's duty!)
  2332.  
  2333. (See the respective explanation in the file "BitVector.c" for more details
  2334. and the file "Vector.xs" for an example of how this can be done!)
  2335.  
  2336. In this module, all bounds and type checking (which should be absolutely
  2337. fool-proof, BTW!) is done in the XSUB routines (in C).
  2338.  
  2339. For more details about the modules in this distribution, please refer to
  2340. their respective man pages!
  2341.  
  2342. =head1 SEE ALSO
  2343.  
  2344. Set::IntegerFast(3), Set::IntegerRange(3), Math::MatrixBool(3),
  2345. Math::MatrixReal(3), DFA::Kleene(3), Math::Kleene(3), Graph::Kruskal(3).
  2346.  
  2347. perl(1), perlsub(1), perlmod(1), perlref(1), perlobj(1), perlbot(1),
  2348. perltoot(1), perlxs(1), perlxstut(1), perlguts(1), overload(3).
  2349.  
  2350. =head1 VERSION
  2351.  
  2352. This man page documents "Bit::Vector" version 4.2.
  2353.  
  2354. =head1 AUTHOR
  2355.  
  2356. Steffen Beyer <sb@sdm.de>.
  2357.  
  2358. =head1 COPYRIGHT
  2359.  
  2360. Copyright (c) 1995, 1996, 1997 by Steffen Beyer. All rights reserved.
  2361.  
  2362. =head1 LICENSE
  2363.  
  2364. This package is free software; you can redistribute it and/or modify
  2365. it under the same terms as Perl itself.
  2366.  
  2367.