ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Zipper.php
Go to the documentation of this file.
1 <?php
2 
22 {
23  public $front, $back;
24 
25  public function __construct($front, $back) {
26  $this->front = $front;
27  $this->back = $back;
28  }
29 
36  static public function fromArray($array) {
37  $z = new self(array(), array_reverse($array));
38  $t = $z->delete(); // delete the "dummy hole"
39  return array($z, $t);
40  }
41 
47  public function toArray($t = NULL) {
48  $a = $this->front;
49  if ($t !== NULL) $a[] = $t;
50  for ($i = count($this->back)-1; $i >= 0; $i--) {
51  $a[] = $this->back[$i];
52  }
53  return $a;
54  }
55 
61  public function next($t) {
62  if ($t !== NULL) array_push($this->front, $t);
63  return empty($this->back) ? NULL : array_pop($this->back);
64  }
65 
72  public function advance($t, $n) {
73  for ($i = 0; $i < $n; $i++) {
74  $t = $this->next($t);
75  }
76  return $t;
77  }
78 
84  public function prev($t) {
85  if ($t !== NULL) array_push($this->back, $t);
86  return empty($this->front) ? NULL : array_pop($this->front);
87  }
88 
94  public function delete() {
95  return empty($this->back) ? NULL : array_pop($this->back);
96  }
97 
102  public function done() {
103  return empty($this->back);
104  }
105 
110  public function insertBefore($t) {
111  if ($t !== NULL) array_push($this->front, $t);
112  }
113 
118  public function insertAfter($t) {
119  if ($t !== NULL) array_push($this->back, $t);
120  }
121 
142  public function splice($t, $delete, $replacement) {
143  // delete
144  $old = array();
145  $r = $t;
146  for ($i = $delete; $i > 0; $i--) {
147  $old[] = $r;
148  $r = $this->delete();
149  }
150  // insert
151  for ($i = count($replacement)-1; $i >= 0; $i--) {
152  $this->insertAfter($r);
153  $r = $replacement[$i];
154  }
155  return array($old, $r);
156  }
157 }
done()
Returns true if we are at the end of the list.
Definition: Zipper.php:102
splice($t, $delete, $replacement)
Splice in multiple elements at hole.
Definition: Zipper.php:142
advance($t, $n)
Iterated hole advancement.
Definition: Zipper.php:72
insertAfter($t)
Insert element after hole.
Definition: Zipper.php:118
back()
Definition: back.php:2
insertBefore($t)
Insert element before hole.
Definition: Zipper.php:110
toArray($t=NULL)
Convert zipper back into a normal array, optionally filling in the hole with a value.
Definition: Zipper.php:47
$r
Definition: example_031.php:79
__construct($front, $back)
Definition: Zipper.php:25
A zipper is a purely-functional data structure which contains a focus that can be efficiently manipul...
Definition: Zipper.php:21
$n
Definition: RandomTest.php:85
$old
Create styles array
The data for the language used.
prev($t)
Move hole to the previous element.
Definition: Zipper.php:84
next($t)
Move hole to the next element.
Definition: Zipper.php:61
$i
Definition: disco.tpl.php:19
static fromArray($array)
Creates a zipper from an array, with a hole in the 0-index position.
Definition: Zipper.php:36