-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircularArray.php
145 lines (142 loc) · 3.68 KB
/
circularArray.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace alesinicio;
/**
* This class enables the creation and management of circular arrays.
* Sometimes we need arrays that wrap around, and this should provide enough methods
* to ease the development process.
*
*
* @author alexandre.sinicio
* @license https://opensource.org/licenses/GPL-3.0 GNU Public License, version 3
* @package alesinicio\
* @version 1.0
* @link https://www.facebook.com/alesinicio
*/
class circularArray {
private $array;
private $curIndex = 0;
private $totalItems = 0;
private $indexes = [];
/**
* Creates a circular array.
* The $array parameter may be either an associative or an indexed array.
* @param unknown $array
*/
public function __construct($array) {
$this->array = $array;
$this->totalItems = count($array);
$this->mapKeys();
}
/**
* Returns the value of the current position of the circular array.
* @return mixed
*/
public function getCurrentValue() {
return $this->array[$this->indexes[$this->curIndex]];
}
/**
* Returns the value of the current position of the circular array
* and advances for the next position.
* @return unknown
*/
public function getCurrentValueAndAdvance() {
$val = $this->array[$this->indexes[$this->curIndex]];
$this->next();
return $val;
}
/**
* Returns the value of the current position of the circular array
* and advances for the next position.
* @return unknown
*/
public function getCurrentValueAndRewind() {
$val = $this->array[$this->indexes[$this->curIndex]];
$this->previous();
return $val;
}
/**
* Returns the current index of the circular array.
* @return mixed
*/
public function getCurrentIndex() {
return $this->indexes[$this->curIndex];
}
/**
* Advances for the next position of the circular array and returns
* the new value.
* @return mixed
*/
public function next() {
$this->curIndex = $this->nextIndex();
return $this->getCurrentValue();
}
/**
* Rewind for the previous position of the circular array and returns
* the new value.
* @return mixed
*/
public function previous() {
$this->curIndex = $this->previousIndex();
return $this->getCurrentValue();
}
/**
* Resets the array to the first position and returns the corresponding value.
* @return mixed
*/
public function reset() {
$this->curIndex = 0;
return $this->getCurrentValue();
}
/**
* Advances $intPositions on the circular array and returns the corresponding value.
* @param int $intPositions
* @return mixed
*/
public function advancePosition($intPositions) {
for ($i=0; $i<$intPositions; $i++) {
$this->next();
}
return $this->getCurrentValue();
}
/**
* Rewinds $intPositions on the circular array and returns the corresponding value.
* @param int $intPositions
* @return mixed
*/
public function rewindsPosition($intPositions) {
for ($i=0; $i<$intPositions; $i++) {
$this->previous();
}
return $this->getCurrentValue();
}
/**
* Maps the keys of the array and returns an array containing the values found.
* $return array;
*/
private function mapKeys() {
$this->indexes = array_keys($this->array);
return $this->indexes;
}
/**
* Returns the next position of the circular array;
* @return number
*/
private function nextIndex() {
if ($this->curIndex+1 >= $this->totalItems) {
return 0;
} else {
return $this->curIndex+1;
}
}
/**
* Returns the previous position of the circular array;
* @return number
*/
private function previousIndex() {
if ($this->curIndex <= 0) {
return $this->totalItems-1;
} else {
return $this->curIndex-1;
}
}
}