note 82336 added to ref.array

From: Date: Sun, 06 Apr 2008 14:38:40 +0000
Subject: note 82336 added to ref.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-137983@lists.php.net to get a copy of this message
Java Like Array Implementation! <?php /* * Author : Pragash Jeyaratnam * Vector Class * PHP Version 5.2.5 * Contact : pragash_jey@yahoo.com. */ class Vector { /* * The number of valid components in this Vector object. */ protected $elementCount; /* * The array buffer into which the components of the vector are stored. */ protected $elementData; /**Default vector Constructor*/ function __construct() { $this->elementData = array(); $this->elementCount = count($this->elementData); } /**Append the parameter element to the vector*/ public function add($object) { if(!empty($object)) { $this->elementData[] = $object; $this->elementCount = count($this->elementData); return true; } else return false; } /* * Returns true of Vector is empty false otherwise */ public function isEmpty() { return($this->elementCount==0); } /* * Returns the current size of this Vector */ public function size() { return $this->elementCount; } /* * Searches for the first occurence of the given argument **/ public function indexOf($object) { if(($index =array_search($object,$this->elementData)) !==false) return $index; else return -1; } /* * This function will retain TRUE if $object is contained * within the vector else FALSE */ public function contains($object) { return ($this->indexOf($object)>=0); } /* * Returns Vector Object at index $index * Error : Null is returned */ public function get($index) { if($this->checkBound($index)) { return ($this->elementData[$index]); } return NULL; } /* * Sets the object at $index to be $object **/ public function set($index,$object) { if($this->checkBound($index)) { $this->elementData[$index] =$object ; return true; } return false; } /* * Removes element at $index **/ public function removeAt($index) { if ($this->checkBound($index)) { for ($i = $index; $i != $this->elementCount - 1; $i++) { $this->elementData[$i] = $this->elementData[$i +1]; } array_pop($this->elementData); $this->elementCount = count($this->elementData); return true; } else { return false; } } /** * Removes all elements from the Vector. Note that this does not * resize the internal data array. */ public function removeAll() { if($this->elementCount ==0) return; for($i=0;$i<$this->size();$i++) { $this->set($i,NULL); } } /* * This function checks whether index * is within the array bound **/ private function checkBound($index) { if($index > $this->elementCount-1 || $index < 0) { throw new Exception('Array Index Out Of Bound Exception'); return false; } return true; } /* *Removes the first element from the Vector */ public function removeFirstElement() { if($this->elementCount==0) { throw new Exception('No Such Element'); } $test =array_shift($this->elementData); if(!empty($test)) $this->elementCount = count($this->elementData); } } ?> ---- Server IP: 61.195.146.164 Probable Submitter: 219.165.234.211 ---- Manual Page -- http://www.php.net/manual/en/ref.array.php Edit -- https://master.php.net/note/edit/82336 Del: integrated -- https://master.php.net/note/delete/82336/integrated Del: useless -- https://master.php.net/note/delete/82336/useless Del: bad code -- https://master.php.net/note/delete/82336/bad+code Del: spam -- https://master.php.net/note/delete/82336/spam Del: non-english -- https://master.php.net/note/delete/82336/non-english Del: in docs -- https://master.php.net/note/delete/82336/in+docs Del: other reasons-- https://master.php.net/note/delete/82336 Reject -- https://master.php.net/note/reject/82336 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#137983) next »