Zipper.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * A zipper is a purely-functional data structure which contains
  4. * a focus that can be efficiently manipulated. It is known as
  5. * a "one-hole context". This mutable variant implements a zipper
  6. * for a list as a pair of two arrays, laid out as follows:
  7. *
  8. * Base list: 1 2 3 4 [ ] 6 7 8 9
  9. * Front list: 1 2 3 4
  10. * Back list: 9 8 7 6
  11. *
  12. * User is expected to keep track of the "current element" and properly
  13. * fill it back in as necessary. (ToDo: Maybe it's more user friendly
  14. * to implicitly track the current element?)
  15. *
  16. * Nota bene: the current class gets confused if you try to store NULLs
  17. * in the list.
  18. */
  19. class HTMLPurifier_Zipper
  20. {
  21. public $front, $back;
  22. public function __construct($front, $back) {
  23. $this->front = $front;
  24. $this->back = $back;
  25. }
  26. /**
  27. * Creates a zipper from an array, with a hole in the
  28. * 0-index position.
  29. * @param Array to zipper-ify.
  30. * @return Tuple of zipper and element of first position.
  31. */
  32. static public function fromArray($array) {
  33. $z = new self(array(), array_reverse($array));
  34. $t = $z->delete(); // delete the "dummy hole"
  35. return array($z, $t);
  36. }
  37. /**
  38. * Convert zipper back into a normal array, optionally filling in
  39. * the hole with a value. (Usually you should supply a $t, unless you
  40. * are at the end of the array.)
  41. */
  42. public function toArray($t = NULL) {
  43. $a = $this->front;
  44. if ($t !== NULL) $a[] = $t;
  45. for ($i = count($this->back)-1; $i >= 0; $i--) {
  46. $a[] = $this->back[$i];
  47. }
  48. return $a;
  49. }
  50. /**
  51. * Move hole to the next element.
  52. * @param $t Element to fill hole with
  53. * @return Original contents of new hole.
  54. */
  55. public function next($t) {
  56. if ($t !== NULL) array_push($this->front, $t);
  57. return empty($this->back) ? NULL : array_pop($this->back);
  58. }
  59. /**
  60. * Iterated hole advancement.
  61. * @param $t Element to fill hole with
  62. * @param $i How many forward to advance hole
  63. * @return Original contents of new hole, i away
  64. */
  65. public function advance($t, $n) {
  66. for ($i = 0; $i < $n; $i++) {
  67. $t = $this->next($t);
  68. }
  69. return $t;
  70. }
  71. /**
  72. * Move hole to the previous element
  73. * @param $t Element to fill hole with
  74. * @return Original contents of new hole.
  75. */
  76. public function prev($t) {
  77. if ($t !== NULL) array_push($this->back, $t);
  78. return empty($this->front) ? NULL : array_pop($this->front);
  79. }
  80. /**
  81. * Delete contents of current hole, shifting hole to
  82. * next element.
  83. * @return Original contents of new hole.
  84. */
  85. public function delete() {
  86. return empty($this->back) ? NULL : array_pop($this->back);
  87. }
  88. /**
  89. * Returns true if we are at the end of the list.
  90. * @return bool
  91. */
  92. public function done() {
  93. return empty($this->back);
  94. }
  95. /**
  96. * Insert element before hole.
  97. * @param Element to insert
  98. */
  99. public function insertBefore($t) {
  100. if ($t !== NULL) array_push($this->front, $t);
  101. }
  102. /**
  103. * Insert element after hole.
  104. * @param Element to insert
  105. */
  106. public function insertAfter($t) {
  107. if ($t !== NULL) array_push($this->back, $t);
  108. }
  109. /**
  110. * Splice in multiple elements at hole. Functional specification
  111. * in terms of array_splice:
  112. *
  113. * $arr1 = $arr;
  114. * $old1 = array_splice($arr1, $i, $delete, $replacement);
  115. *
  116. * list($z, $t) = HTMLPurifier_Zipper::fromArray($arr);
  117. * $t = $z->advance($t, $i);
  118. * list($old2, $t) = $z->splice($t, $delete, $replacement);
  119. * $arr2 = $z->toArray($t);
  120. *
  121. * assert($old1 === $old2);
  122. * assert($arr1 === $arr2);
  123. *
  124. * NB: the absolute index location after this operation is
  125. * *unchanged!*
  126. *
  127. * @param Current contents of hole.
  128. */
  129. public function splice($t, $delete, $replacement) {
  130. // delete
  131. $old = array();
  132. $r = $t;
  133. for ($i = $delete; $i > 0; $i--) {
  134. $old[] = $r;
  135. $r = $this->delete();
  136. }
  137. // insert
  138. for ($i = count($replacement)-1; $i >= 0; $i--) {
  139. $this->insertAfter($r);
  140. $r = $replacement[$i];
  141. }
  142. return array($old, $r);
  143. }
  144. }