Class HTMLPurifier_Zipper
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.
Methods summary
public
|
|
public static
Tuple
|
#
fromArray( Array $array )
Creates a zipper from an array, with a hole in the 0-index position.
Creates a zipper from an array, with a hole in the 0-index position.
Parameters
- $array
Array to zipper-ify.
Returns
Tuple of zipper and element of first position.
|
public
|
#
toArray( mixed $t = NULL )
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.)
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
Original
|
#
next( mixed $t )
Move hole to the next element.
Move hole to the next element.
Parameters
- $t
mixed $t Element to fill hole with
Returns
Original contents of new hole.
|
public
Original
|
#
advance( mixed $t, mixed $n )
Iterated hole advancement.
Iterated hole advancement.
Parameters
- $t
mixed $t Element to fill hole with
- $n
mixed $i How many forward to advance hole
Returns
Original contents of new hole, i away
|
public
Original
|
#
prev( mixed $t )
Move hole to the previous element
Move hole to the previous element
Parameters
- $t
mixed $t Element to fill hole with
Returns
Original contents of new hole.
|
public
Original
|
#
delete( )
Delete contents of current hole, shifting hole to next element.
Delete contents of current hole, shifting hole to next element.
Returns
Original contents of new hole.
|
public
boolean
|
#
done( )
Returns true if we are at the end of the list.
Returns true if we are at the end of the list.
Returns
boolean
|
public
|
#
insertBefore( Element $t )
Insert element before hole.
Insert element before hole.
Parameters
|
public
|
#
insertAfter( Element $t )
Insert element after hole.
Insert element after hole.
Parameters
|
public
|
#
splice( Current $t, mixed $delete, mixed $replacement )
Splice in multiple elements at hole. Functional specification in terms of
array_splice:
Splice in multiple elements at hole. Functional specification in terms of
array_splice:
$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
Current contents of hole.
- $delete
- $replacement
|
Properties summary
public
mixed
|
$front
|
|
|
public
mixed
|
$back
|
|
|