blob: 56690f499b3303e864d13c4f797ee4696c9c6446 (
plain)
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
|
<?php
/** @file splobjectstorage.inc
* @ingroup SPL
* @brief class SplObjectStorage
* @author Marcus Boerger
* @date 2003 - 2005
*
* SPL - Standard PHP Library
*/
/**
* @brief Object storage
* @author Marcus Boerger
* @version 1.0
* @since PHP 6.0
*
* This container allows to store objects uniquly without the need to compare
* them one by one. This is only possible internally. The code represenation
* here therefore has a complexity of O(n) while the actual implementation has
* complexity O(1).
*/
class SplObjectStorage implements Iterator, Countable
{
private $storage = array();
private $index = 0;
/** Rewind to top iterator as set in constructor
*/
function rewind()
{
rewind($this->storage);
}
/** @return whether iterator is valid
*/
function valid()
{
return key($this->storage) !== false;
}
/** @return current key
*/
function key()
{
return $this->index;
}
/** @return current object
*/
function current()
{
return current($this->storage);
}
/** Forward to next element
*/
function next()
{
next($this->storage);
$this->index++;
}
/** @return number of objects in storage
*/
function count()
{
return count($this->storage);
}
/** @param obj object to look for
* @return whether $obj is contained in storage
*/
function contains($obj)
{
if (is_object($obj))
{
foreach($this->storage as $object)
{
if ($object === $obj)
{
return true;
}
}
}
return false;
}
/** @param $obj new object to attach to storage if not yet contained
*/
function attach($obj)
{
if (is_object($obj) && !$this->contains($obj))
{
$this->storage[] = $obj;
}
}
/** @param $obj object to remove from storage
*/
function detach($obj)
{
if (is_object($obj))
{
foreach($this->storage as $idx => $object)
{
if ($object === $obj)
{
unset($this->storage[$idx]);
$this->rewind();
return;
}
}
}
}
}
?>
|