<? // -*-Mode: c++;-*-
/*
PhpGa - PHP Genetic Algorithm Class Library
Copyright (C) 2000 Bill C. White
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
BitString.inc - Bill White - 7/20/00
This class is used to perform operations on a binary string.
Its original intent is to support the PhpGa genetic algorithm class.
*/
class BitString
{
// class data members
// set this flag to "1" for debugging output
var $debug = 0;
// string parameters
var $myLength;
var $myString;
// class methods
/*
BitString - constructor
$initializer = string literal initializer
*/
function BitString($length, $initializer="")
{
// bounds check specified string length
if($length < 1 || $length > 128) {
echo "BitString constructor: specified length: $length, ";
echo "is outside the bounds 1 < length <= 128.<br>\n";
exit;
}
// if no initializer, make random string
if($initializer == "") {
$this->myString = "";
// assume random number generator has been appropriately seeded
for($i=0; $i < $length; $i++) {
$this->myString .= rand(0,1);
}
}
else {
// check that specified length and initializer string length are the same
if($length == strlen($initializer)) {
$this->myString = $initializer;
}
else {
echo "BitString constructor: specified length: $length, ";
echo "does not match length of initializer: $initializer.<br>\n";
exit;
}
}
// length is okay here; checked above
$this->myLength = $length;
if($this->debug) { echo "In BitString constructor:<br>string: $this->myString, length: $this->myLength<br>\n"; }
}
// return the number of zeroes and ones in the string
function ZeroesAndOnesCount()
{
$onesCount = 0;
$zeroesCount = 0;
for($i=0; $i < $this->myLength; $i++) {
$thisChar = substr($this->myString, $i, 1);
if($thisChar == "0") {
$zeroesCount++;
}
else {
$onesCount++;
}
}
return array($zeroesCount, $onesCount);
}
function EmitAsHTML()
{
echo "$this->myString";
}
} // end of class definition
?>