Class HTMLPurifier_Zipper

InheritanceHTMLPurifier_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.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$back HTMLPurifier_Zipper
$front HTMLPurifier_Zipper

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__construct() HTMLPurifier_Zipper
advance() Iterated hole advancement. HTMLPurifier_Zipper
delete() Delete contents of current hole, shifting hole to next element. HTMLPurifier_Zipper
done() Returns true if we are at the end of the list. HTMLPurifier_Zipper
fromArray() Creates a zipper from an array, with a hole in the 0-index position. HTMLPurifier_Zipper
insertAfter() Insert element after hole. HTMLPurifier_Zipper
insertBefore() Insert element before hole. HTMLPurifier_Zipper
next() Move hole to the next element. HTMLPurifier_Zipper
prev() Move hole to the previous element HTMLPurifier_Zipper
splice() Splice in multiple elements at hole. Functional specification in terms of array_splice: HTMLPurifier_Zipper
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.) HTMLPurifier_Zipper

Property Details

$back public property
public $back null
$front public property
public $front null

Method Details

__construct() public method

public void __construct ( $front, $back )
$front
$back
advance() public method

Iterated hole advancement.

public \Original advance ( $t, $n )
$t

Element to fill hole with

$n
return \Original

Contents of new hole, i away

delete() public method

Delete contents of current hole, shifting hole to next element.

public \Original delete ( )
return \Original

Contents of new hole.

done() public method

Returns true if we are at the end of the list.

public bool done ( )
fromArray() public static method

Creates a zipper from an array, with a hole in the 0-index position.

public static \Tuple fromArray ( $array )
$array
return \Tuple

Of zipper and element of first position.

insertAfter() public method

Insert element after hole.

public void insertAfter ( $t )
$t
insertBefore() public method

Insert element before hole.

public void insertBefore ( $t )
$t
next() public method

Move hole to the next element.

public \Original next ( $t )
$t

Element to fill hole with

return \Original

Contents of new hole.

prev() public method

Move hole to the previous element

public \Original prev ( $t )
$t

Element to fill hole with

return \Original

Contents of new hole.

splice() public method

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!

public void splice ( $t, $delete, $replacement )
$t
$delete
$replacement
toArray() public method

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 void toArray ( $t NULL )
$t