Source for file OrderedSet.class.php

Documentation is available at OrderedSet.class.php

  1. <?php
  2.  
  3. /**
  4. * @package harmoni.sets
  5. *
  6. * @copyright Copyright &copy; 2005, Middlebury College
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  8. *
  9. * @version $Id: OrderedSet.class.php,v 1.20 2007/09/04 20:25:49 adamfranco Exp $
  10. */
  11.  
  12. require_once(HARMONI."Primitives/Objects/SObject.class.php");
  13.  
  14. /**
  15. * The OrderedSet provides an easy way to manage a group of Ids.
  16. * This set is not persistant. Please see the {@link TempOrderedSet} and the
  17. * {@link PersistentOrderedSet} for persisting sets.
  18. *
  19. * @package harmoni.sets
  20. *
  21. * @copyright Copyright &copy; 2005, Middlebury College
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  23. *
  24. * @version $Id: OrderedSet.class.php,v 1.20 2007/09/04 20:25:49 adamfranco Exp $
  25. * @author Adam Franco
  26. */
  27. class OrderedSet
  28. extends SObject
  29. {
  30. /**
  31. * @var int $_i The current posititon.
  32. * @access private
  33. */
  34. var $_i = -1;
  35. /**
  36. * @var object Id $_setId The Id of this set.
  37. * @access private
  38. */
  39. var $_setId;
  40. /**
  41. * @var array $_items The items in this set.
  42. * @access private
  43. */
  44. var $_items;
  45. /**
  46. * Constructor.
  47. * @param object Id $setId The Id of this set.
  48. */
  49. function OrderedSet ( $setId ) {
  50. ArgumentValidator::validate($setId, ExtendsValidatorRule::getRule("Id"), true);
  51. // Create our internal array
  52. $this->_items = array();
  53. $this->_setId =$setId;
  54. $this->_i = -1;
  55. }
  56. /**
  57. * Answer the id of this set
  58. *
  59. * @return object Id
  60. * @access public
  61. * @since 8/5/05
  62. */
  63. function getId () {
  64. return $this->_setId;
  65. }
  66. /**
  67. * Return TRUE if there are additional Ids; FALSE otherwise.
  68. * @access public
  69. * @return boolean
  70. */
  71. function hasNext () {
  72. return ($this->_i < count($this->_items)-1);
  73. }
  74. /**
  75. * Return TRUE if the given Id is in the set
  76. * @param object Id $id The Id to check.
  77. * @access public
  78. * @return boolean
  79. */
  80. function isInSet ( $id ) {
  81. return in_array($id->getIdString(), $this->_items);
  82. }
  83. /**
  84. * Return the next id
  85. * @access public
  86. * @return object id
  87. */
  88. function next () {
  89. $idManager = Services::getService("Id");
  90. if ($this->hasNext()) {
  91. $this->_i++;
  92. return $idManager->getId($this->_items[$this->_i]);
  93. } else {
  94. throwError(new Error(NO_MORE_ITERATOR_ELEMENTS, "Set", 1));
  95. }
  96. }
  97. /**
  98. * Reset the internal pointer to the begining of the set.
  99. * @access public
  100. * @return void
  101. */
  102. function reset () {
  103. $this->_i = -1;
  104. }
  105. /**
  106. * Add a new Id to the set.
  107. * @param object Id $id The Id to add.
  108. * @access public
  109. * @return void
  110. */
  111. function addItem ( $id ) {
  112. ArgumentValidator::validate($id, ExtendsValidatorRule::getRule("Id"), true);
  113. // Add the item to our internal list
  114. $position = $this->count();
  115. $this->_items[$position] = $id->getIdString();
  116. }
  117. /**
  118. * Remove an Id from the set.
  119. * @param object Id $id The Id to remove.
  120. * @access public
  121. * @return void
  122. */
  123. function removeItem ( $id ) {
  124. ArgumentValidator::validate($id, ExtendsValidatorRule::getRule("Id"), true);
  125. if (!$this->isInSet($id))
  126. throwError(new Error("Item specified, ".$id->getIdString().", does not exist", "Set", 1));
  127. // Move it to the end to update the order keys
  128. $this->moveToPosition($id, $this->count()-1);
  129. // Remove the item from our internal list
  130. unset ($this->_items[$this->count()-1]);
  131. }
  132. /**
  133. * Remove all Items from the set.
  134. * @access public
  135. * @return void
  136. */
  137. function removeAllItems () {
  138. // Create our internal array
  139. $this->_items = array();
  140. $this->_i = -1;
  141. }
  142. /**
  143. * Return the current position of the id in the set.
  144. * @param object Id $id The Id of the item to move.
  145. * @access public
  146. * @return integer
  147. */
  148. function getPosition ( $id ) {
  149. ArgumentValidator::validate($id, ExtendsValidatorRule::getRule("Id"), true);
  150. return array_search($id->getIdString(), $this->_items);
  151. }
  152. /**
  153. * Answer the id at the position specified, false if position is out of bounds.
  154. *
  155. * @param integer $position
  156. * @return object Id
  157. * @access public
  158. * @since 1/30/06
  159. */
  160. function atPosition ( $position ) {
  161. $idManager = Services::getService("Id");
  162. if (isset($this->_items[$position]))
  163. return $idManager->getId($this->_items[$position]);
  164. else {
  165. $false = false;
  166. return $false;
  167. }
  168. }
  169. /**
  170. * Return the number of ids in the set.
  171. * @access public
  172. * @return integer
  173. */
  174. function count () {
  175. return count($this->_items);
  176. }
  177. /**
  178. * Move the specified id to the specified position in the set.
  179. * @param object Id $id The Id of the item to move.
  180. * @param integer $position The new position of the specified id.
  181. * @access public
  182. * @return void
  183. */
  184. function moveToPosition ( $id, $position ) {
  185. ArgumentValidator::validate($id, ExtendsValidatorRule::getRule("Id"), true);
  186. // printpre("Moving ".$id->getIdString()." from ".$this->getPosition($id)." to ".$position);
  187. if ($position != $this->getPosition($id)
  188. && $position >= 0
  189. && $position < $this->count()) {
  190. $previousPosition = $this->getPosition($id);
  191. if ($position < $previousPosition) {
  192. for ($i = $previousPosition; $i > $position; $i--) {
  193. $this->_items[$i] = $this->_items[$i-1];
  194. }
  195. } else {
  196. for ($i = $previousPosition; $i < $position; $i++) {
  197. $this->_items[$i] = $this->_items[$i+1];
  198. }
  199. }
  200. $this->_items[$position] = $id->getIdString();
  201. }
  202. if ($position < 0 || $position >= $this->count()) {
  203. throwError(new Error("Position specified, '".$position."', is out of bounds.", "Set", TRUE));
  204. }
  205. }
  206. /**
  207. * Move the specified id toward the begining of the set.
  208. * @param object Id $id The Id of the item to move.
  209. * @access public
  210. * @return void
  211. */
  212. function moveUp ( $id ) {
  213. ArgumentValidator::validate($id, ExtendsValidatorRule::getRule("Id"), true);
  214. if (($position = $this->getPosition($id)) > 0) {
  215. $itemBefore = $this->_items[$position-1];
  216. $this->_items[$position-1] = $id->getIdString();
  217. $this->_items[$position] = $itemBefore;
  218. }
  219. }
  220. /**
  221. * Move the specified id toward the end of the set.
  222. * @param object Id $id The Id of the item to move.
  223. * @access public
  224. * @return void
  225. */
  226. function moveDown ( $id ) {
  227. ArgumentValidator::validate($id, ExtendsValidatorRule::getRule("Id"), true);
  228. if (($position = $this->getPosition($id)) < ($this->count() - 1)) {
  229. $itemAfter = $this->_items[$position + 1];
  230. $this->_items[$position + 1] = $id->getIdString();
  231. $this->_items[$position] = $itemAfter;
  232. }
  233. }
  234. /**
  235. * Move the specifiedId to the beginning of the set
  236. *
  237. * @param object Id $id The Id of the item to move.
  238. * @return void
  239. * @access public
  240. * @since 1/30/06
  241. */
  242. function moveToBeginning ( $id ) {
  243. $this->moveToPosition($id, 0);
  244. }
  245. /**
  246. * Move the specifiedId to the end of the set
  247. *
  248. * @param object Id $id The Id of the item to move.
  249. * @return void
  250. * @access public
  251. * @since 1/30/06
  252. */
  253. function moveToEnd ( $id ) {
  254. $this->moveToPosition($id, $this->count() - 1);
  255. }
  256. /**
  257. * Initialize from a data-string
  258. *
  259. * @param string $data
  260. * @return void
  261. * @access public
  262. * @since 1/30/06
  263. */
  264. function initializeWithData ( $data ) {
  265. // Create our internal array
  266. $this->_i = -1;
  267. $items = explode("\t", $data);
  268. foreach ($items as $item) {
  269. if (preg_match('/^[^\s\t\n]+$/', $item) && !in_array($item, $this->_items))
  270. $this->_items[] = $item;
  271. }
  272. }
  273. /**
  274. * Answer a data string for storage
  275. *
  276. * @return string
  277. * @access public
  278. * @since 1/30/06
  279. */
  280. function toDataString () {
  281. return implode("\t", $this->_items);
  282. }
  283. }

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