HTMLPurifier_Zipper
in package
A zipper is a purely-functional data structure which contains a focus that can be efficiently manipulated. It is known as a "one-hole context". This mutable variant implements a zipper for a list as a pair of two arrays, laid out as follows:
Base list: 1 2 3 4 [ ] 6 7 8 9 Front list: 1 2 3 4 Back list: 9 8 7 6
User is expected to keep track of the "current element" and properly fill it back in as necessary. (ToDo: Maybe it's more user friendly to implicitly track the current element?)
Nota bene: the current class gets confused if you try to store NULLs in the list.
Table of Contents
Properties
Methods
- __construct() : mixed
- advance() : Original
- Iterated hole advancement.
- delete() : Original
- Delete contents of current hole, shifting hole to next element.
- done() : bool
- Returns true if we are at the end of the list.
- fromArray() : Tuple
- Creates a zipper from an array, with a hole in the 0-index position.
- insertAfter() : mixed
- Insert element after hole.
- insertBefore() : mixed
- Insert element before hole.
- next() : Original
- Move hole to the next element.
- prev() : Original
- Move hole to the previous element
- splice() : mixed
- Splice in multiple elements at hole. Functional specification in terms of array_splice:
- toArray() : mixed
- Convert zipper back into a normal array, optionally filling in the hole with a value. (Usually you should supply a $t, unless you are at the end of the array.)
Properties
$back
public
mixed
$back
$front
public
mixed
$front
Methods
__construct()
public
__construct(mixed $front, mixed $back) : mixed
Parameters
- $front : mixed
- $back : mixed
advance()
Iterated hole advancement.
public
advance(mixed $t, mixed $n) : Original
Parameters
- $t : mixed
-
Element to fill hole with
- $n : mixed
Return values
Original —contents of new hole, i away
delete()
Delete contents of current hole, shifting hole to next element.
public
delete() : Original
Return values
Original —contents of new hole.
done()
Returns true if we are at the end of the list.
public
done() : bool
Return values
boolfromArray()
Creates a zipper from an array, with a hole in the 0-index position.
public
static fromArray(mixed $array) : Tuple
Parameters
- $array : mixed
Return values
Tuple —of zipper and element of first position.
insertAfter()
Insert element after hole.
public
insertAfter(mixed $t) : mixed
Parameters
- $t : mixed
insertBefore()
Insert element before hole.
public
insertBefore(mixed $t) : mixed
Parameters
- $t : mixed
next()
Move hole to the next element.
public
next(mixed $t) : Original
Parameters
- $t : mixed
-
Element to fill hole with
Return values
Original —contents of new hole.
prev()
Move hole to the previous element
public
prev(mixed $t) : Original
Parameters
- $t : mixed
-
Element to fill hole with
Return values
Original —contents of new hole.
splice()
Splice in multiple elements at hole. Functional specification in terms of array_splice:
public
splice(mixed $t, mixed $delete, mixed $replacement) : mixed
$arr1 = $arr; $old1 = array_splice($arr1, $i, $delete, $replacement);
list($z, $t) = HTMLPurifier_Zipper::fromArray($arr);
$t = $z->advance($t, $i);
list($old2, $t) = $z->splice($t, $delete, $replacement);
$arr2 = $z->toArray($t);
assert($old1 === $old2);
assert($arr1 === $arr2);
NB: the absolute index location after this operation is unchanged!
Parameters
- $t : mixed
- $delete : mixed
- $replacement : mixed
toArray()
Convert zipper back into a normal array, optionally filling in the hole with a value. (Usually you should supply a $t, unless you are at the end of the array.)
public
toArray([mixed $t = NULL ]) : mixed
Parameters
- $t : mixed = NULL