Math_Vector
[ class tree: Math_Vector ] [ index: Math_Vector ] [ all elements ]

Source for file CompactedTuple.php

Documentation is available at CompactedTuple.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net>                |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: CompactedTuple.php 304045 2010-10-05 00:16:53Z clockwerx $
  20. //
  21.             
  22. require_once "PEAR.php";
  23.  
  24. class Math_CompactedTuple {
  25.  
  26.     var $data;
  27.  
  28.     function Math_CompactedTuple ($arg
  29.     {
  30.         if (is_array($arg)) {
  31.             $this->data $this->_genCompactedArray($arg);
  32.         elseif (is_object($arg&& get_class($arg== "math_tuple"{
  33.             $this->data $this->_genCompacterArray($arg->getData());
  34.         else {
  35.             $msg "Incorrect parameter for Math_CompactedTuple constructor. ".
  36.                     "Expecting an unidimensional array or a Math_Tuple object,"
  37.                     " got '$arg'\n";
  38.             PEAR::raiseError($msg);
  39.         }
  40.         return true;
  41.     }
  42.  
  43.     function getSize({
  44.         return count($this->_genUnCompactedArray($this->data));
  45.     }
  46.  
  47.     function getCompactedSize({
  48.         return count($this->data);
  49.     }
  50.  
  51.     function getCompactedData({
  52.         return $this->data;
  53.     }
  54.  
  55.     function getData({
  56.         return $this->_genUnCompactedArray($this->data);
  57.     }
  58.  
  59.     function addElement($value{
  60.         $this->data[$value]++;
  61.     }
  62.     
  63.     function delElement($value{
  64.         if (in_array($valuearray_keys($this->data))) {
  65.             $this->data[$value]--;
  66.             if ($this->data[$value== 0)
  67.                 unset ($this->data[$value]);
  68.             return true;
  69.         }
  70.         return PEAR::raiseError("value does not exist in compacted tuple");
  71.     }
  72.  
  73.     function hasElement($value{
  74.         return in_array($valuearray_keys($this->data));
  75.     }
  76.  
  77.     function _genCompactedArray($arr{
  78.         if (function_exists("array_count_values")) {
  79.             return array_count_values($arr);
  80.         else {
  81.             $out = array();
  82.             foreach ($arr as $val)
  83.                 $out[$val]++;
  84.             return $out;
  85.         }
  86.     }
  87.  
  88.     function _genUnCompactedArray($arr{
  89.         $out = array();
  90.         foreach ($arr as $val=>$count)
  91.             for($i=0; $i $count$i++)
  92.                 $out[$val;
  93.         return $out;
  94.     }
  95. }
  96.  
  97. ?>

Documentation generated on Mon, 11 Mar 2019 15:39:22 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.