-
Notifications
You must be signed in to change notification settings - Fork 22
/
PackedArray.inc
54 lines (44 loc) · 1.22 KB
/
PackedArray.inc
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
<?php
/**
* @file
*
* Provides a 'packed' array for occasions when PHP array consumes to much memory. Not that this class
* is about twice as slow as PHP arrays, but around three times as compressed. It should only be used when dealing with large arrays where
* processing speed isn't as much of a concern as memory usage.
*
*/
class PackedArray implements ArrayAccess, Serializable {
protected $array = array();
public function __construct() {
$args = func_get_args();
foreach ($args as $k => $v) {
$this->offsetSet($k, $v);
}
}
public function offsetExists($offset) {
return isset($this->array[$offset]);
}
public function offsetGet($offset) {
if ($this->offsetExists($offset)) {
return unserialize($this->array[$offset]);
}
return NULL;
}
public function offsetSet($offset, $val) {
if (isset($offset)) {
$this->array[$offset] = serialize($val);
}
else {
$this->array[] = serialize($val);
}
}
public function offsetUnset($offset) {
unset($this->array[$offset]);
}
public function serialize() {
return serialize($this->array);
}
public function unserialize($serialized) {
$this->array = unserialize($serialized);
}
}