4 * A zipper is a purely-functional data structure which contains
5 * a focus that can be efficiently manipulated. It is known as
6 * a "one-hole context". This mutable variant implements a zipper
7 * for a list as a pair of two arrays, laid out as follows:
9 * Base list: 1 2 3 4 [ ] 6 7 8 9
13 * User is expected to keep track of the "current element" and properly
14 * fill it back in as necessary. (ToDo: Maybe it's more user friendly
15 * to implicitly track the current element?)
17 * Nota bene: the current class gets confused if you try to store NULLs
21 class HTMLPurifier_Zipper
25 public function __construct($front, $back) {
26 $this->front = $front;
31 * Creates a zipper from an array, with a hole in the
33 * @param Array to zipper-ify.
34 * @return Tuple of zipper and element of first position.
36 static public function fromArray($array) {
37 $z = new self(array(), array_reverse($array));
38 $t = $z->delete(); // delete the "dummy hole"
43 * Convert zipper back into a normal array, optionally filling in
44 * the hole with a value. (Usually you should supply a $t, unless you
45 * are at the end of the array.)
47 public function toArray($t = NULL) {
49 if ($t !== NULL) $a[] = $t;
50 for ($i = count($this->back)-1; $i >= 0; $i--) {
51 $a[] = $this->back[$i];
57 * Move hole to the next element.
58 * @param $t Element to fill hole with
59 * @return Original contents of new hole.
61 public function next($t) {
62 if ($t !== NULL) array_push($this->front, $t);
63 return empty($this->back) ? NULL : array_pop($this->back);
67 * Iterated hole advancement.
68 * @param $t Element to fill hole with
69 * @param $i How many forward to advance hole
70 * @return Original contents of new hole, i away
72 public function advance($t, $n) {
73 for ($i = 0; $i < $n; $i++) {
80 * Move hole to the previous element
81 * @param $t Element to fill hole with
82 * @return Original contents of new hole.
84 public function prev($t) {
85 if ($t !== NULL) array_push($this->back, $t);
86 return empty($this->front) ? NULL : array_pop($this->front);
90 * Delete contents of current hole, shifting hole to
92 * @return Original contents of new hole.
94 public function delete() {
95 return empty($this->back) ? NULL : array_pop($this->back);
99 * Returns true if we are at the end of the list.
102 public function done() {
103 return empty($this->back);
107 * Insert element before hole.
108 * @param Element to insert
110 public function insertBefore($t) {
111 if ($t !== NULL) array_push($this->front, $t);
115 * Insert element after hole.
116 * @param Element to insert
118 public function insertAfter($t) {
119 if ($t !== NULL) array_push($this->back, $t);
123 * Splice in multiple elements at hole. Functional specification
124 * in terms of array_splice:
127 * $old1 = array_splice($arr1, $i, $delete, $replacement);
129 * list($z, $t) = HTMLPurifier_Zipper::fromArray($arr);
130 * $t = $z->advance($t, $i);
131 * list($old2, $t) = $z->splice($t, $delete, $replacement);
132 * $arr2 = $z->toArray($t);
134 * assert($old1 === $old2);
135 * assert($arr1 === $arr2);
137 * NB: the absolute index location after this operation is
140 * @param Current contents of hole.
142 public function splice($t, $delete, $replacement) {
146 for ($i = $delete; $i > 0; $i--) {
148 $r = $this->delete();
151 for ($i = count($replacement)-1; $i >= 0; $i--) {
152 $this->insertAfter($r);
153 $r = $replacement[$i];
155 return array($old, $r);