1 |
<?php |
2 |
|
3 |
// implementation of Cyclic array |
4 |
// 2001-11-03 Dobrica Pavlinusic <dpavlin@rot13.org> |
5 |
|
6 |
// generally functions return boolean if array looped |
7 |
|
8 |
class Cyclic_Arr { |
9 |
var $size; // number of elements before cycle |
10 |
var $arr = Array(); // actual data |
11 |
var $pos = 0; // current position |
12 |
var $did_loop = 0; // did last move made loop? |
13 |
|
14 |
// init size of array |
15 |
function set_size($size) { |
16 |
$this->size=$size; |
17 |
} |
18 |
|
19 |
// put element into array, move to next |
20 |
function push($val) { |
21 |
$this->arr[$this->pos] = $val; |
22 |
$this->next(); |
23 |
//return $this->did_loop; |
24 |
} |
25 |
|
26 |
// get element from array, move to next |
27 |
function fetch() { |
28 |
$out = $this->arr[$this->pos]; |
29 |
$this->next(); |
30 |
return $out; |
31 |
} |
32 |
|
33 |
// move to next |
34 |
function next() { |
35 |
if ($this->pos == $this->size) $this->did_loop = 1; |
36 |
$this->pos = ($this->pos + 1) % $this->size; |
37 |
return $this->did_loop; |
38 |
} |
39 |
|
40 |
// move to previous |
41 |
function prev() { |
42 |
if ($this->pos == 0) $this->did_loop = 1; |
43 |
$this->pos = ($this->pos + $this->size - 1) % $this->size; |
44 |
return $this->did_loop; |
45 |
} |
46 |
|
47 |
// tell size of array |
48 |
function tell_size() { |
49 |
return $this->size; |
50 |
} |
51 |
|
52 |
// tell current position in array |
53 |
function tell_pos() { |
54 |
return $this->pos; |
55 |
} |
56 |
|
57 |
function did_loop() { |
58 |
return $this->did_loop; |
59 |
} |
60 |
|
61 |
} |
62 |
|
63 |
?> |