1 |
dpavlin |
1.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 |
dpavlin |
1.2 |
|
33 |
|
|
function fetch_rev() { |
34 |
|
|
$out = $this->arr[$this->pos]; |
35 |
|
|
$this->prev(); |
36 |
|
|
return $out; |
37 |
|
|
} |
38 |
dpavlin |
1.1 |
|
39 |
|
|
// move to next |
40 |
|
|
function next() { |
41 |
|
|
if ($this->pos == $this->size) $this->did_loop = 1; |
42 |
|
|
$this->pos = ($this->pos + 1) % $this->size; |
43 |
|
|
return $this->did_loop; |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
// move to previous |
47 |
|
|
function prev() { |
48 |
|
|
if ($this->pos == 0) $this->did_loop = 1; |
49 |
|
|
$this->pos = ($this->pos + $this->size - 1) % $this->size; |
50 |
|
|
return $this->did_loop; |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
// tell size of array |
54 |
|
|
function tell_size() { |
55 |
|
|
return $this->size; |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
// tell current position in array |
59 |
|
|
function tell_pos() { |
60 |
|
|
return $this->pos; |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
function did_loop() { |
64 |
|
|
return $this->did_loop; |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
?> |