Skip to content

Commit

Permalink
Public accessors, introduce invalidargumentexceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel O'Connor committed Nov 27, 2011
1 parent 2277bd6 commit 30ca562
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
30 changes: 16 additions & 14 deletions Math/CompactedTuple.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@

require_once "PEAR.php";

class Math_CompactedTuple {
class Math_CompactedTuple
{

var $data;

function Math_CompactedTuple ($arg)
/** @todo Avoid work in constructor? */
public function __construct($arg)
{
if (is_array($arg)) {
$this->data = $this->_genCompactedArray($arg);
Expand All @@ -35,46 +37,46 @@ function Math_CompactedTuple ($arg)
$msg = "Incorrect parameter for Math_CompactedTuple constructor. ".
"Expecting an unidimensional array or a Math_Tuple object,".
" got '$arg'\n";
PEAR::raiseError($msg);
throw new InvalidArgumentException($msg);
}
return true;
}

function getSize() {
public function getSize() {
return count($this->_genUnCompactedArray($this->data));
}

function getCompactedSize() {
public function getCompactedSize() {
return count($this->data);
}

function getCompactedData() {
public function getCompactedData() {
return $this->data;
}

function getData() {
public function getData() {
return $this->_genUnCompactedArray($this->data);
}

function addElement($value) {
public function addElement($value) {
$this->data[$value]++;
}

function delElement($value) {
public function delElement($value) {
if (in_array($value, array_keys($this->data))) {
$this->data[$value]--;
if ($this->data[$value] == 0)
if ($this->data[$value] == 0) {
unset ($this->data[$value]);
}
return true;
}
return PEAR::raiseError("value does not exist in compacted tuple");
}

function hasElement($value) {
public function hasElement($value) {
return in_array($value, array_keys($this->data));
}

function _genCompactedArray($arr) {
public function _genCompactedArray($arr) {
if (function_exists("array_count_values")) {
return array_count_values($arr);
} else {
Expand All @@ -86,7 +88,7 @@ function _genCompactedArray($arr) {
}
}

function _genUnCompactedArray($arr) {
public function _genUnCompactedArray($arr) {
$out = array();
foreach ($arr as $val=>$count) {
for($i=0; $i < $count; $i++) {
Expand Down
1 change: 1 addition & 0 deletions Math/Vector.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
//

require_once "PEAR.php";
require_once "Math/Vector/Exception.php";
require_once "Math/Tuple.php";
require_once "Math/VectorOp.php";

Expand Down

0 comments on commit 30ca562

Please sign in to comment.