LinkedList.class.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <?php
  2. /**
  3. * A Linked List data structure.
  4. * The structure allows iteration and array access. Note: when unsetting an array key,
  5. * the list is compressed and all subsequent keys are reset.
  6. *
  7. * @author Ironpilot
  8. * @copyright Copywrite (c) 2011, STAPLE CODE
  9. *
  10. * This file is part of the STAPLE Framework.
  11. *
  12. * The STAPLE Framework is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Lesser General Public License as published by the
  14. * Free Software Foundation, either version 3 of the License, or (at your option)
  15. * any later version.
  16. *
  17. * The STAPLE Framework is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  19. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  20. * more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public License
  23. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. class Staple_Data_LinkedList implements Iterator, Countable, ArrayAccess
  26. {
  27. /**
  28. * Pointer to the starting list node
  29. * @var Staple_Data_LinkedListNode
  30. */
  31. protected $first;
  32. /**
  33. * Pointer to the current list node
  34. * @var Staple_Data_LinkedListNode
  35. */
  36. protected $current;
  37. /**
  38. * Pointer to the ending list node
  39. * @var Staple_Data_LinkedListNode
  40. */
  41. protected $last;
  42. /**
  43. * Size of the list
  44. * @var int
  45. */
  46. protected $size;
  47. /**
  48. * Name of the list. (optional)
  49. * @var string
  50. */
  51. protected $name;
  52. /**
  53. * Constructs the list and allows an optional list name to be set.
  54. * @param unknown_type $name
  55. */
  56. public function __construct($name = NULL)
  57. {
  58. //Sets the first and last pointers to null making a blank list.
  59. $this->first = $this->last = $this->current = null;
  60. $this->size = 0;
  61. //Set the Name
  62. if(isset($name))
  63. {
  64. $this->setName($name);
  65. }
  66. }
  67. /**
  68. * Calls the getListString function on string conversion.
  69. */
  70. public function __toString()
  71. {
  72. return $this->getListString();
  73. }
  74. /**
  75. * Returns the list size.
  76. */
  77. public function count()
  78. {
  79. return $this->getSize();
  80. }
  81. /**
  82. * Iteration. Retrieves the data for the current item.
  83. */
  84. public function current()
  85. {
  86. return $this->current->getData();
  87. }
  88. /**
  89. * Iteration. Gets the key number for the current item in the list.
  90. */
  91. public function key()
  92. {
  93. return $this->findKey($this->current);
  94. }
  95. /**
  96. * Iteration. Forwards the internal pointer to the next item on the list.
  97. */
  98. public function next()
  99. {
  100. $this->current = $this->current->getNext();
  101. }
  102. /**
  103. * Iteration. Rewinds the internal pointer to the first item in the list.
  104. */
  105. public function rewind()
  106. {
  107. $this->current = $this->first;
  108. }
  109. /**
  110. * Iteration. Checks that the current item pointed to is valid.
  111. */
  112. public function valid()
  113. {
  114. return !is_null($this->current);
  115. }
  116. /**
  117. * Array Access. Checks for a valid offset.
  118. */
  119. public function offsetExists($offset)
  120. {
  121. return !is_null($this->findItemByKey($offset));
  122. }
  123. /**
  124. * Array Access. Returns the data for a specified offset.
  125. */
  126. public function offsetGet($offset)
  127. {
  128. return $this->findItemByKey($offset)->getData();
  129. }
  130. /**
  131. * Array Access. Set the data for a specified offset.
  132. */
  133. public function offsetSet($offset, $value)
  134. {
  135. $item = $this->findItemByKey($offset);
  136. if($item instanceof Staple_Data_LinkedListNode)
  137. {
  138. $item->setData($value);
  139. }
  140. }
  141. /**
  142. * Array Access. Removes the node for a specified offset
  143. */
  144. public function offsetUnset($offset)
  145. {
  146. $prev = $this->findItemByKey($offset-1);
  147. $item = $this->findItemByKey($offset);
  148. $prev->setNext($item->getNext());
  149. }
  150. /**
  151. * Alias of addBack()
  152. * @param mixed $data
  153. */
  154. public function add($data)
  155. {
  156. return $this->addBack($data);
  157. }
  158. /**
  159. * Add a node to the beginning of the list.
  160. * @param mixed $data
  161. */
  162. public function addFront($data)
  163. {
  164. if($this->is_empty())
  165. {
  166. $this->first = $this->last = new Staple_Data_LinkedListNode($data);
  167. }
  168. else
  169. {
  170. $new = new Staple_Data_LinkedListNode($data,$this->first);
  171. $this->first = $new;
  172. }
  173. //Increase List Size
  174. $this->size++;
  175. //Set the Current Node to the beginning of the list.
  176. $this->current = $this->first;
  177. return $this;
  178. }
  179. /**
  180. * Add a node to the end of the list
  181. * @param mixed $data
  182. */
  183. public function addBack($data)
  184. {
  185. //Adds a node to the end of the list.
  186. if($this->is_empty())
  187. {
  188. $this->first = $this->last = new Staple_Data_LinkedListNode($data);
  189. }
  190. else
  191. {
  192. $new = new Staple_Data_LinkedListNode($data);
  193. $this->last->next = $new;
  194. $this->last = $new;
  195. }
  196. //Increase List Size
  197. $this->size++;
  198. //Set the Current Node to the end of the list.
  199. $this->current = $this->last;
  200. return $this;
  201. }
  202. /**
  203. * Alias of removeBack()
  204. * @return Staple_Data_LinkedListNode | false
  205. */
  206. public function remove()
  207. {
  208. return $this->removeBack();
  209. }
  210. /**
  211. * Removes a node from the front of the list.
  212. * Returns the node on success or false on an empty list.
  213. * @return mixed
  214. */
  215. public function removeFront()
  216. {
  217. if($this->is_empty())
  218. {
  219. $this->size = 0;
  220. return false;
  221. }
  222. else
  223. {
  224. $node = $this->first;
  225. $this->first = $this->first->next;
  226. $this->size--;
  227. //Set the Current Node to the beginning of the list.
  228. $this->current = $this->first;
  229. return $node->getData();
  230. }
  231. }
  232. /**
  233. * Remove a node from the end of the list.
  234. * Returns the node on removal or false if the list is empty.
  235. * @return mixed
  236. */
  237. public function removeBack()
  238. {
  239. if($this->is_empty())
  240. {
  241. $this->size = 0;
  242. return false;
  243. }
  244. elseif($this->first == $this->last)
  245. {
  246. $node = $this->first;
  247. $this->first = $this->last = $this->current = null;
  248. $this->size = 0;
  249. return $node->getData();
  250. }
  251. else
  252. {
  253. $current = $this->first;
  254. while($current->next != $this->last)
  255. {
  256. $current = $current->next;
  257. }
  258. $this->last = $current;
  259. $node = $current->next;
  260. $this->last->next = null;
  261. //Decrease List Size
  262. $this->size--;
  263. //Set the Current Node to the beginning of the list.
  264. $this->current = $this->last;
  265. return $node->getData();
  266. }
  267. }
  268. /**
  269. * Look at the value stored in the first item without removing from the list.
  270. */
  271. public function peakFront()
  272. {
  273. if($this->is_empty())
  274. return NULL;
  275. else
  276. return $this->first->getData();
  277. }
  278. /**
  279. * Look at the value stored in the last item without removing from the list.
  280. */
  281. public function peakBack()
  282. {
  283. if($this->is_empty())
  284. return NULL;
  285. else
  286. return $this->last->getData();
  287. }
  288. /**
  289. * Sets the optional Name of the list
  290. * @param string $name
  291. */
  292. public function setName($name)
  293. {
  294. $this->name = $name;
  295. return $this;
  296. }
  297. /**
  298. * Gets the list name
  299. * @return string
  300. */
  301. public function getName()
  302. {
  303. return $this->name;
  304. }
  305. /**
  306. * @return int $size
  307. */
  308. public function getSize()
  309. {
  310. return (int)$this->size;
  311. }
  312. /**
  313. * Alias of getSize()
  314. */
  315. public function length()
  316. {
  317. return $this->getSize();
  318. }
  319. /**
  320. * Returns the list as a string. It converts arrays into a comma separated string.
  321. * Throws an exception if objects are encountered.
  322. * The Verbose option will also list the name of the list and its size.
  323. * @todo convert this to Iterator
  324. * @param bool $verbose
  325. * @throws Exception
  326. */
  327. public function getListString($verbose = false)
  328. {
  329. if($verbose)
  330. $lstring = "Name: {$this->name} \nSize: {$this->size} \n";
  331. else
  332. $lstring = "";
  333. $current = $this->first;
  334. while($current != null)
  335. {
  336. if(is_array($current->data))
  337. $lstring .= implode(",",$current->data)." \n";
  338. elseif(is_object($current->data))
  339. throw new Exception("Cannot interpret object as a string"); //Replace with Flat_Exception when created.
  340. else
  341. $lstring .= $current->data." \n";
  342. $current = $current->next;
  343. }
  344. return $lstring;
  345. }
  346. /**
  347. * Returns the list as an array
  348. * The Verbose option will also list the name of the list and its size.
  349. * @todo convert this to Iterator
  350. * @param bool $verbose
  351. */
  352. public function getListArray($verbose = false)
  353. {
  354. $larray = array();
  355. if($verbose)
  356. {
  357. $larray[-2] = $this->name;
  358. $larray[-1] = $this->size;
  359. }
  360. $current = $this->first;
  361. while($current != null)
  362. {
  363. $larray[] = $current->data;
  364. $current = $current->next;
  365. }
  366. return $larray;
  367. }
  368. /**
  369. * Checks the list to see if it is empty.
  370. */
  371. public function is_empty()
  372. {
  373. if($this->first == null && $this->last == null)
  374. {
  375. return true;
  376. }
  377. else
  378. {
  379. return false;
  380. }
  381. }
  382. /**
  383. * An internal troubleshooting test to see if the list is sizing correctly
  384. * @todo remove this function
  385. * @deprecated
  386. */
  387. private function check_size()
  388. {
  389. echo "List thinks it is {$this->size} item(s) long.<br>";
  390. $newSize = 0;
  391. $current = $this->first;
  392. while($current != null)
  393. {
  394. $newSize++;
  395. $current = $current->next;
  396. }
  397. echo "List currently contains $newSize node(s)";
  398. }
  399. /**
  400. * Returns the key for a specified node
  401. * @param Staple_Data_LinkedListNode $item
  402. */
  403. private function findKey(Staple_Data_LinkedListNode $item)
  404. {
  405. $counter = 0;
  406. $current = $this->first;
  407. while($item !== $current && $current instanceof Staple_Data_LinkedListNode)
  408. {
  409. $current = $current->getNext();
  410. $counter++;
  411. }
  412. return $counter;
  413. }
  414. /**
  415. * Returns the node for a specific key
  416. * @param int $key
  417. * @return Staple_Data_LinkedListNode
  418. */
  419. private function findItemByKey($key)
  420. {
  421. $current = $this->first;
  422. while($current != NULL && $key > 0)
  423. {
  424. $current = $current->getNext();
  425. $key--;
  426. }
  427. return $current;
  428. }
  429. }