[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re2: [MacPerl] BigInt sluggishness.



At 5:10 PM -0500 12/5/00, Chris Nandor wrote:

>Here's another way.  vec() is kinda confusing, but fun.  :)

*WOW* cool Cris!

It amazes me, after all these years, how much Perl I don't know.

Brian, if you are still with us, vec() is DEFINITELY the way to go! 
Once I realized what it was doing, I didn't find vec() confusing at 
all, and it is the ultimate way to do bit twiddling.

Cris' example is much better than mine in many if not all ways, but I 
modified mine to use vec() nonetheless because I find mine easier to 
understand, and I thought Brian might be like me in that regard.  So, 
in the spirit of TIMTOWTDI, appended is my second cut at the script.

-David-

#! /usr/bin/perl
#
# The first line is not necessary for MacPerl, but since I move between
# ...platforms a lot, it is a habit of mine.
#
# binary_compare2.plx
# by David Steffen, 2000
# Do with this as you like.
#
# Purpose: to illustrate an idea to the MacPerl mailing list
#
# The code below is a bit sloppy, I don't use any of the recommended
# ...perl error checking, for example. Do not as I do :-)

# Input to the program will be sets of strings where each position in the
# ...string represents a time block (hour?) in which a course might be held,
# ...where time blocks used are indicated by the character '1', and those
# ...not used by the character '0'
# Strings are expected to be 71 characters long.  Error checking for strings
# ...in the wrong format should be added, but I think this code forces all
# ...input to 71 anyway.
# This is my second version of the script; rather than trying to pack things
# ...into integers, I am using the vec() function to pack things into strings.

$a = "00101010101010100001011101011010101110101000001010101010101110101110001";
$b = "11000000000000000000000000000000000000000000000000000000000000000000000";

# we pack $a & $b into strings which are bitmap translation of the originals.
# we print the original strings and the decode of the bitmap to prove that
# decoding reverses encoding
print "$a\n";
print "$b\n";
$a = &encode($a);
$b = &encode($b);
print &decode($a), "\n";
print &decode($b), "\n";

# now we compare them
if( &test(@$a, $b) ) {
     print "conflict!\n";
} else {
     print "courses fit.\n";
}

print "-----\n";

# Now merge courses $a and $b into a schedule $c
# Of course, course $a will no longer fit, already having been included.
# Print them out and then test them.
$c = &merge($a, $b);
print &decode($a), "\n";
print &decode($c), "\n";

if( &test($a, $c) ) {
     print "conflict!\n";
} else {
     print "course fits.\n";
}

# The program defines four functions:
#  * &encode($string) converts the input data into a working form.
#  * &decode(@array) does the reverse.
#  * &test(@array1, @array2) compares two datasets to see if there is 
an overlap
#  * &merge(@array1, @array2) is used to "accumulate" courses, to create a
#    master dataset indicated which time blocks have been used so far.
#
# I don't know if these are the functions needed, but I thought they 
illustrated
# ...my point.

sub encode {
     # &encode($string) takesthe input string (ignoring any other arguments)
     # and returns a packed bitmap representation.
     # For safety sake, we first fix its length at 71 characters
     my $retval = '';
 
     # force to 71 long
     $retval = ("0" x 71) . $retval; # pad the beginning with "0"
     $retval = substr($retval, -71); # trim to 71 long

     $retval= unpack("b*", $_[0]);   # convert to a bitmap
     return $retval;
}

sub decode {
     # &decode($string) converts a packed binary bitmap into a more readable
     # "01010" representation
     return pack("b*", $_[0]);
}

sub test {
     # Using the packed bitmap, determines if the
     # two timesets (a timeset either being an original course or an
     # accumulation of courses resulting from the &merge() function)
     # have any overlap.
     return ($_[0] & $_[1]);
}

sub merge {
     # Using the packed bitmap,  merges them
     # into one dataset combining the hours used in both.
     return $_[0] | $_[1];
}
__END__
David Steffen, Ph.D.
President, Biomedical Computing, Inc. <http://www.biomedcomp.com/>
Phone: (713) 610-9770 FAX: (713) 610-9769 E-mail: steffen@biomedcomp.com

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org