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