-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassList.php
executable file
·345 lines (319 loc) · 11.3 KB
/
ClassList.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
/*
* Copyright 2010 Bion Oren
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* You know, the whole reason this class exists is because there are classes that, from
* the API's perspective, are the same. But, from the user's perspective, they're
* completely different. You might be able to encapsulate the differences in arrays
* inside the classes themselves (kind of like reference counters). If this becomes a
* performance bottleneck...
*/
require_once($path."Object.php");
/**
* Defines an associative array of classes that may contain duplicates.
*
* @author Bion Oren
*/
class ClassList extends Object implements ArrayAccess, Countable, Iterator {
/** ARRAY Internal class structure of the form $classes[KEY] = array(item, item, ...). */
protected $classes = array();
/** INTEGER Number of elements in the array. */
protected $count = 0;
/** INTEGER index into the current element of the current element in $classes. */
protected $innerCount = 0;
/**
* Constructs a new object.
*
* @param ARRAY $vals Simple array of items to prepopulate this list with.
*/
public function __construct(array $vals=array()) {
foreach($vals as $val) {
$this[$val->ID] = $val;
}
}
/**
* Returns the total number of elements in the list.
*
* @return INTEGER Number of elements.
* @see count
*/
public function count() {
return $this->count;
}
/**
* Returns the current element in the array.
*
* @return MIXED Current item in the list.
*/
public function current() {
$arr = current($this->classes);
return $arr[$this->innerCount];
}
/**
* Returns a new ClassList with all the elements in $list1 XOR $list2.
*
* @param ClassList $list1 One list.
* @param ClassList $list2 Another list.
* @return ClassList $list1 xor $list2.
*/
public static function diff(ClassList $list1, ClassList $list2) {
$ret = new ClassList();
return $ret->diffHelper($list1, $list2);
}
/**
* Returns this ClassList with all the elements in $list1 XOR $list2.
*
* @param ClassList $list1 One list.
* @param ClassList $list2 Another list.
* @return ClassList $list1 xor $list2.
*/
protected function diffHelper(ClassList $list1, ClassList $list2) {
foreach($list1->classes as $id=>$arr) {
foreach($arr as $class) {
if(!isset($list2->classes[$id]) || !in_array($class, $list2->classes[$id], true)) {
$this[$id] = $class;
}
}
}
foreach($list2->classes as $id=>$arr) {
foreach($arr as $class) {
if(!isset($list1->classes[$id]) || !in_array($class, $list1->classes[$id], true)) {
$this[$id] = $class;
}
}
}
return $this;
}
/**
* Debug function to print the internal contents of this list.
*
* @param STRING $name Name to use to identify the output.
* @return VOID
*/
public function dump($name) {
dump($name, $this->classes);
}
/**
* Filters the class list based on a user defined function.
*
* @param FUNCTION $function Returns true if the element should be returned.
* @return ClassList New filtered ClassList instance.
*/
public function filter($function) {
$ret = new ClassList();
return $ret->filterHelper($this, $function);
}
/**
* Performs the actual filter operation.
*
* @param ClassList $classes List of classes to filter.
* @param FUNCTION $function Returns true if the element should be returned.
* @return ClassList Filtered ClassList.
* @see filter
*/
protected function filterHelper(ClassList $classes, $function) {
foreach($classes as $class) {
if($function($class)) {
$this[$class->ID] = $class;
}
}
return $this;
}
/**
* Returns a new ClassList with all the elements in $list1 AND $list2.
*
* @param ClassList $list1 One list.
* @param ClassList $list2 Another list.
* @return ClassList $list1 AND $list2.
*/
public static function intersect(ClassList $list1, ClassList $list2) {
$ret = new ClassList();
return $ret->intersectHelper($list1, $list2);
}
/**
* Returns this ClassList with all the elements in $list1 AND $list2.
*
* @param ClassList $list1 One list.
* @param ClassList $list2 Another list.
* @return ClassList $list1 AND $list2.
*/
protected function intersectHelper(ClassList $list1, ClassList $list2) {
$this->classes = $list1->classes;
$this->count = $list1->count();
foreach($this->classes as $id=>$arr) {
if(!$list2->offsetExists($id)) {
$this->count -= count($arr);
unset($this->classes[$id]);
continue;
}
foreach($arr as $key=>$class) {
foreach($list2->classes[$id] as $class2) {
if($class->equals($class2)) {
break 2;
}
}
unset($arr[$key]);
$this->count--;
}
$this->classes[$id] = array_values($arr);
}
return $this;
}
/**
* Returns the key for the current element in the array.
* @return STRING Item key.
*/
public function key() {
return $this->current()->ID;
}
/**
* Returns a new ClassList with all the elements in $list1 OR $list2.
*
* @param ClassList $list1 One list.
* @param ClassList $list2 Another list.
* @return ClassList $list1 OR $list2.
*/
public static function merge(ClassList $list1, ClassList $list2) {
$ret = new ClassList();
return $ret->mergeHelper($list1, $list2);
}
/**
* Returns this ClassList with all the elements in $list1 OR $list2.
*
* @param ClassList $list1 One list.
* @param ClassList $list2 Another list.
* @return ClassList $list1 OR $list2.
*/
protected function mergeHelper(ClassList $list1, ClassList $list2) {
$this->classes = $list1->classes;
$this->count = $list1->count();
foreach($list2->classes as $id=>$arr) {
if(empty($this->classes[$id])) {
$this->classes[$id] = $arr;
$this->count += count($arr);
continue;
}
foreach($arr as $class) {
foreach($this->classes[$id] as $class2) {
if($class->equals($class2)) {
break 2;
}
}
$this[$id] = $class;
$this->count++;
}
}
return $this;
}
/**
* Moves to the next element in the list.
*
* @return VOID
*/
public function next() {
if(++$this->innerCount == count(current($this->classes))) {
$this->innerCount = 0;
next($this->classes);
}
}
/**
* Checks to see if $index is a valid key.
*
* @param STRING $index Key to test.
* @return BOOLEAN True if the index $key exists.
*/
public function offsetExists($index) {
return !empty($this->classes[$index]);
}
/**
* Returns the first element at the given offset.
*
* @param STRING $index Key to use.
* @return MIXED The first item at the given key, or null if the index doesn't exist.
*/
public function offsetGet($index) {
if(isset($this->classes[$index])) {
return $this->classes[$index][0];
} else {
return null;
}
}
/**
* Adds a value to the list.
*
* @param STRING $index Key to insert with.
* @param MIXED $value Item to insert at $index.
* @return VOID
*/
public function offsetSet($index, $value) {
$this->count++;
$this->classes[$index][] = $value;
}
/**
* Removes an element at the specified index.
*
* @param STRING $index Index to remove an element from.
* @return VOID
*/
public function offsetUnset($index) {
if(isset($this->classes[$index])) {
$this->count--;
if(count($this->classes[$index]) <= 1) {
unset($this->classes[$index]);
} else {
array_shift($this->classes[$index]);
}
}
}
/**
* Resets the internal array pointers to the beginning of the list.
*
* @return VOID
*/
public function rewind() {
$this->innerCount = 0;
reset($this->classes);
}
/**
* Sorts the internal class list.
*
* @return ARRAY Sorted internal class list.
*/
public function sort() {
uasort($this->classes, function(array $first, array $second) { return strcmp($first[0], $second[0]); });
}
/**
* Returns true if we haven't hit the end of the list yet.
*
* @return BOOLEAN True if there are more elements.
*/
public function valid() {
return current($this->classes) !== false;
}
public function __clone() {
$temp = $this->classes;
$this->classes = array();
$this->count = 0;
$this->innerCount = 0;
foreach($temp as $key=>$arr) {
foreach($arr as $class) {
$this[$key] = clone $class;
}
}
}
public function __toString() {
return "ClassList(".$this->count().")";
}
}
?>