Source for file OrderedList.class.php

Documentation is available at OrderedList.class.php

  1. <?php
  2.  
  3. //require_once('OrderedList.interface.php');
  4. /**
  5. * A generic queue of objects. It provides iterator functions next() and hasNext().
  6. *
  7. * @package harmoni.utilities
  8. *
  9. * @copyright Copyright &copy; 2005, Middlebury College
  10. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  11. *
  12. * @version $Id: OrderedList.class.php,v 1.4 2007/09/04 20:25:54 adamfranco Exp $
  13. */
  14. class OrderedList {
  15.  
  16. /* @var the array, that represents the ordered list itself. */
  17. var $_list;
  18.  
  19. /**
  20. * Constructor. Create a new ordered list.
  21. * @access public
  22. */
  23. function OrderedList(){
  24. $this->_list = array();
  25. }
  26.  
  27. /**
  28. * Add a referenced object to the end of the list and reset the internal counter.
  29. * @param object $object The object to add to the queue.
  30. * @param string $key the key by which the object is referenced in the list. By default the key will be the integer position in the list.
  31. * @access public
  32. */
  33. function add($object,$key="") {
  34. if($key=="")
  35. $key = count($this->_array);
  36.  
  37. $this->_list[$key] =$object;
  38. reset($this->_list);
  39. }
  40.  
  41. /**
  42. * Retrieve an element by reference.
  43. * @param string $key The primary key of the object to retrieve.
  44. * @return object The reference to the object with a given key. False if no object with such key exists.
  45. * @access public
  46. */
  47. function retrieve($key) {
  48. if(!isset($this->_list[$key]))
  49. return false;
  50.  
  51. else return ($this->_list[$key]);
  52. }
  53.  
  54. /**
  55. * Create a new instance of the object in the list with a given reference.
  56. * @param string $key The primary key of the object to get a copy of.
  57. * @return object A copy of an object with a given key.
  58. * @access public
  59. */
  60. function copy($key) {
  61. if(!isset($this->_list[$key]))
  62. return false;
  63.  
  64. else return($this->_list[$key]);
  65. }
  66.  
  67.  
  68. /**
  69. * Delete an element by reference.
  70. * @param string $key The primary key of the object to delete.
  71. * @return boolean True on success, false on failure (no such key exists).
  72. * @access public
  73. */
  74. function delete($key) {
  75. $check = false;
  76. if(isset($this->_list[$key]))
  77. $check = true;
  78.  
  79. unset($this->_list[$key]);
  80. return $check;
  81. }
  82.  
  83. /**
  84. * Tell whether an element with a given reference exists in the list.
  85. * @param string $key The primary key of the object to check.
  86. * @return boolean True if the reference exists, false otherwise.
  87. * @access public
  88. */
  89. function exists($key) {
  90. if(isset($this->_list[$key]))
  91. return true;
  92. else
  93. return false;
  94. }
  95.  
  96. /**
  97. * Swap two elements in the list.
  98. * @param string $key1 The primary key of first object to swap the position of.
  99. * @param string $key2 The primary key of second object to swap the position of.
  100. * @return boolean True on sucess, false on failure (either of the keys is non-existent in the list).
  101. * @access public
  102. */
  103.  
  104. function swap($key1,$key2) {
  105. if((!isset($this->_list[$key1])) || (!isset($this->_list[$key2])))
  106. return false;
  107.  
  108. $tempArray = array();
  109. foreach ($this->_list as $key=>$value) {
  110. if (($key!=$key1) && ($key!=$key2))
  111. $tempArray[$key] =$this->_list[$key];
  112. elseif ($key==$key1)
  113. $tempArray[$key2] =$this->_list[$key2];
  114. else
  115. $tempArray[$key1] =$this->_list[$key1];
  116. }
  117. $this->_list = $tempArray;
  118. reset($this->_list);
  119. return true;
  120. }
  121.  
  122. /**
  123. * Move an element up (towards the beginning) the list.
  124. * @param string $key The primary key of the object to move.
  125. * @return boolean True on success, false on failure (either the element is already at the top or it doesn't exist in the list).
  126. * @access public
  127. */
  128.  
  129. function moveUp($key) {
  130. $key1 = $key;
  131. foreach ($this->_list as $key=>$value) {
  132. if ($key1 == $key){
  133. if (isset($keyOld)) {
  134. $this->swap($key,$keyOld);
  135. return true;
  136. }
  137. else
  138. return false;
  139. }
  140. $keyOld = $key;
  141. }
  142. return false;
  143. }
  144.  
  145. /**
  146. * Move an element down (towards the end) the list.
  147. * @param string $key The primary key of the object to move.
  148. * @return boolean True on success, false on failure (either the element is already at the bottom or it doesn't exist in the list).
  149. * @access public
  150. */
  151.  
  152. function moveDown($key) {
  153. $key1 = $key;
  154. foreach ($this->_list as $key=>$value) {
  155. if ($keyOld == $key1){
  156. $this->swap($key,$keyOld);
  157. return true;
  158. }
  159. $keyOld = $key;
  160. }
  161. reset($this->_list);
  162. return false;
  163. }
  164.  
  165.  
  166. /**
  167. * Move an element to the position that preceeds that of another element or the end of the list.
  168. * @param string $source The primary key of object to move.
  169. * @param string $destination The primary key of object in front of which the source object is to be put. If $destination==="list_end" the source will be put at the end of the list, in the hope that there is no element whose key is "list_end,"
  170. * @return boolean True on sucess false on falure (either the source or the destination keys do not exist).
  171. * @access public
  172. */
  173.  
  174. function putBefore($source,$destination) {
  175. /* See if both source and destintion are set or source is set and destination==list_end */
  176. if ((!isset($this->_list[$source])) || (($destination!=="list_end") && (!isset($this->_list[$destination])))){
  177. return false;
  178. }
  179.  
  180. $tempArray = array();
  181. foreach ($this->_list as $key=>$value) {
  182. if ($key==$destination)
  183. $tempArray[$source] =$this->_list[$source];
  184. if ($key!=$source)
  185. $tempArray[$key] =$this->_list[$key];
  186. }
  187. if ($destination==="list_end")
  188. $tempArray[$source] =$this->_list[$source];
  189.  
  190. $this->_list = $tempArray;
  191. reset($this->_list);
  192. return true;
  193. }
  194. /**
  195. * Get the current element and increase the position by one.
  196. * @return object The reference to the object at the current position in the queue.
  197. * @access public
  198. */
  199. function next() {
  200. $key = key($this->_list);
  201. next($this->_list);
  202. return $this->_list[$key];
  203. }
  204.  
  205. /**
  206. * Tell whether there exists an object in the queue at the current position.
  207. * @return boolean Whether there exists an object in the queue at the current position.
  208. * @access public
  209. */
  210. function hasNext() {
  211. $key = key($this->_list);
  212. if (isset($this->_list[$key]))
  213. return true;
  214. else
  215. return false;
  216. }
  217.  
  218. /**
  219. * Get the size of the list.
  220. * @return integer The size of the list.
  221. * @access public
  222. */
  223. function getSize() {
  224. return count($this->_list);
  225. }
  226.  
  227. /**
  228. * Clear the list.
  229. *
  230. * @access public
  231. */
  232. function clear() {
  233. $this->_list = array();
  234. }
  235. }
  236.  
  237.  
  238. ?>

Documentation generated on Wed, 19 Sep 2007 10:25:22 -0400 by phpDocumentor 1.3.0RC3