3 namespace CoreFramework\Lists;
5 // Import framework stuff
6 use CoreFramework\Generic\FrameworkInterface;
7 use CoreFramework\Object\BaseFrameworkSystem;
10 * A general list class
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countable {
32 // Exception constants
33 const EXCEPTION_GROUP_ALREADY_ADDED = 0xf20;
34 const EXCEPTION_GROUP_NOT_FOUND = 0xf21;
35 const EXCEPTION_INVALID_HASH = 0xf22;
40 private $listGroups = array();
45 private $listEntries = array();
50 private $listIndex = array();
53 * Protected constructor
55 * @param $className Name of the class
58 protected function __construct ($className) {
59 // Call parent constructor
60 parent::__construct($className);
64 * Getter for iterator instance from this list
66 * @return $iteratorInstance An instance of a Iterator class
68 public function getIterator () {
69 // Get iterator from here
70 $iteratorInstance = $this->getIteratorInstance();
72 // Is the instance set?
73 if (is_null($iteratorInstance)) {
74 // Prepare a default iterator
75 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('default_iterator_class', array($this));
78 $this->setIteratorInstance($iteratorInstance);
82 return $iteratorInstance;
86 * Checks whether the given group is set
88 * @param $groupName Group to check if found in list
89 * @return $isset Whether the group is valid
91 public function isGroupSet ($groupName) {
92 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName);
93 return isset($this->listGroups[$groupName]);
97 * Adds the given group or if already added issues a ListGroupAlreadyAddedException
99 * @param $groupName Group to add
101 * @throws ListGroupAlreadyAddedException If the given group is already added
103 public function addGroup ($groupName) {
104 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!');
105 // Is the group already added?
106 if ($this->isGroupSet($groupName)) {
107 // Throw the exception here
108 throw new ListGroupAlreadyAddedException(array($this, $groupName), self::EXCEPTION_GROUP_ALREADY_ADDED);
111 // Add the group which is a simple array
112 $this->listGroups[$groupName] = ObjectFactory::createObjectByConfiguredName('list_group_class');
113 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - EXIT!');
117 * Adds the given instance to list group and sub group
119 * @param $groupName Group to add instance to
120 * @param $subGroup Sub group to add instance to
121 * @param $visitableInstance An instance of Visitable
123 * @throws NoListGroupException If the given group is not found
125 public function addInstance ($groupName, $subGroup, Visitable $visitableInstance) {
127 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',subGroup=' . $subGroup . ',visitableInstance=' . $visitableInstance->__toString() . ' - CALLED!');
129 // Is the group there?
130 if (!$this->isGroupSet($groupName)) {
131 // Throw the exception here
132 throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
135 // Is the sub group there?
136 if (!$this->listGroups[$groupName]->isGroupSet($subGroup)) {
137 // Automatically add it
138 $this->listGroups[$groupName]->addGroup($subGroup);
142 $hash = $this->generateHash($groupName, $subGroup, $visitableInstance);
145 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',subGroup=' . $subGroup . ',hash=' . $hash . ' - Calling addEntry() ...');
147 // Now add it to the group list and hash it
148 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',this->listGroups[' . $groupName . ']=' . $this->listGroups[$groupName]->__toString());
149 //$this->listGroups[$groupName]->addEntry($subGroup, $hash);
151 // Add the hash to the index
152 array_push($this->listIndex, $hash);
154 // Add the instance itself to the list
155 $this->listEntries[$hash] = $visitableInstance;
158 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',subGroup=' . $subGroup . ' - EXIT!');
162 * Gets an array from given list
164 * @param $list The requested list
165 * @return $array The requested array
166 * @throws NoListGroupException If the given group is not found
168 public final function getArrayFromList ($list) {
170 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',list[' . gettype($list) . ']=' . $list . ' - CALLED!');
172 // Is the group there?
173 if ((!is_null($list)) && (!$this->isGroupSet($list))) {
174 // Throw the exception here
175 throw new NoListGroupException(array($this, $list), self::EXCEPTION_GROUP_NOT_FOUND);
181 // Is there another list?
182 if (!is_null($list)) {
183 // Then get it as well
184 $array = $this->listGroups[$list]->getArrayFromList(NULL);
187 // Walk through all entries
188 foreach ($this->listIndex as $hash) {
190 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: hash=' . $hash);
192 // Is the list entry set?
193 if ($this->isHashValid($hash)) {
195 array_push($array, $this->listEntries[$hash]);
196 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: hash=' . $hash . ',array(' . count($array) . ')=' . print_r($array, TRUE) . ' - ADDED!');
201 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',list[' . gettype($list) . ']=' . $list . ',array()=' . count($array) . ' - EXIT!');
208 * Adds the given entry to list group
210 * @param $groupName Group to add instance to
211 * @param $entry An entry of any type
213 * @throws NoListGroupException If the given group is not found
215 public function addEntry ($groupName, $entry) {
217 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!');
219 // Is the group already added?
220 if (!$this->isGroupSet($groupName)) {
221 // Throw the exception here
222 throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
226 $hash = $this->generateHash($groupName, $groupName, $entry);
229 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',entry=' . print_r($entry, TRUE) . ', hash=' . $hash);
231 // Add the hash to the index
232 array_push($this->listIndex, $hash);
235 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',listEntries()=' . count($this->listEntries));
237 // Now add the entry to the list
238 $this->listEntries[$hash] = $entry;
241 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',listEntries()=' . count($this->listEntries) . ' - EXIT!');
245 * Removes given entry from the list group
247 * @param $groupName Group where we should remove the entry from
248 * @param $entry The entry we should remove
250 * @throws NoListGroupException If the given group is not found
252 public function removeEntry ($groupName, $entry) {
254 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!');
256 // Is the group already added?
257 if (!$this->isGroupSet($groupName)) {
258 // Throw the exception here
259 throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
263 $hash = $this->generateHash($groupName, $groupName, $entry);
266 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',entry=' . $entry . ', hash=' . $hash);
268 // Remove it from the list ...
269 unset($this->listEntries[$hash]);
271 // ... and hash list as well
272 unset($this->listIndex[array_search($hash, $this->listIndex)]);
275 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - EXIT!');
279 * Generates a hash from given group, sub group and entry
281 * @param $groupName Group to add instance to
282 * @param $subGroup Sub group to add instance to
283 * @param $entry An entry of any type
284 * @return $hash The generated
286 private function generateHash ($groupName, $subGroup, $entry) {
287 // Created entry, 'null' is default
290 // Determine type of entry
291 if (is_null($entry)) {
293 } elseif ($entry instanceof FrameworkInterface) {
294 // Own instance detected
295 $entry2 = $entry->hashCode();
296 } elseif ((is_int($entry)) || (is_float($entry)) || (is_resource($entry))) {
297 // Integer/float/resource detected
298 $entry2 = gettype($entry) . ':' . $entry;
299 } elseif (is_string($entry)) {
301 $entry2 = crc32($entry) . ':' . strlen($entry);
302 } elseif ((is_array($entry)) && (isset($entry['id']))) {
303 // Supported array found
304 $entry2 = crc32($entry['id']) . ':' . count($entry);
305 } elseif ((is_array($entry)) && (isset($entry[BasePool::SOCKET_ARRAY_RESOURCE])) && (isset($entry[BasePool::SOCKET_ARRAY_CONN_TYPE]))) {
306 // Is a socket resource array
307 $entry2 = crc32($entry[BasePool::SOCKET_ARRAY_RESOURCE] . ':' . $entry[BasePool::SOCKET_ARRAY_CONN_TYPE]);
309 // Unsupported type detected
310 self::createDebugInstance(__CLASS__)->debugOutput('BASE-LIST[' . __METHOD__ . ':' . __LINE__ . ']: Entry type ' . gettype($entry) . ' is unsupported.');
312 // @TODO Extend this somehow?
313 $entry2 = gettype($entry);
316 // Construct string which we shall hash
317 $hashString = $groupName . ':' . $subGroup . ':' . $entry2;
319 // Hash it with fastest hasher
320 $hash = crc32($hashString);
327 * Clears an array of groups, all are being checked for existence
329 * @param $groupNames An array with existing list groups
332 protected function clearGroups (array $groupNames) {
333 // Walk through all groups
334 foreach ($groupNames as $groupName) {
336 $this->clearGroup($groupName);
341 * Clears a single group by resetting it to its initial state (empty array)
343 * @param $groupName Name of an existing group to clear
346 protected function clearGroup ($groupName) {
347 // Does this group exist?
348 if (!$this->isGroupSet($groupName)) {
349 // Throw the exception here
350 throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
353 // Then clear this group list
354 $this->listGroups[$groupName]->clearList();
357 $this->listIndex = array();
358 $this->listEntries = array();
362 * Counts all entries in this list
364 * @return $count All entries in this list
366 public final function count () {
367 return count($this->listIndex);
371 * Checks whether the given hash is valid
373 * @param $hash The hash we should validate
374 * @return $isValid Whether the given hash is valid
376 public final function isHashValid ($hash) {
378 $isValid = ((in_array($hash, $this->listIndex)) && (isset($this->listEntries[$hash])));
385 * Getter for hash from given hash index
387 * @param $hashIndex Index holding the hash
388 * @return $hash The hash
390 public final function getHash ($hashIndex) {
392 $hash = $this->listIndex[$hashIndex];
399 * Gets an entry from given hash index
401 * @param $hashIndex The hash index to resolve the mapped entry
402 * @return $entry Solved entry from list
403 * @throws InvalidListHashException If the solved hash index is invalid
405 public function getEntry ($hashIndex) {
406 // Get the hash value
407 $hash = $this->getHash($hashIndex);
409 // Is the hash valid?
410 if (!$this->isHashValid($hash)) {
411 // Throw an exception here
412 throw new InvalidListHashException(array($this, $hash, $hashIndex), self::EXCEPTION_INVALID_HASH);
415 // Now copy the entry
416 $entry = $this->listEntries[$hash];
423 * Gets a full array from given group name
425 * @param $groupName The group name to get a list for
426 * @return $entries The array with all entries
427 * @throws NoListGroupException If the specified group is invalid
429 public function getArrayFromProtocolInstance ($groupName) {
430 // Is the group valid?
431 if (!$this->isGroupSet($groupName)) {
432 // Throw the exception here
433 throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
436 // Init the entries' array
440 $iteratorInstance = $this->listGroups[$groupName]->getIterator();
442 // Rewind the iterator for this round
443 $iteratorInstance->rewind();
445 // Go through all entries
446 while ($iteratorInstance->valid()) {
448 $entryIndex = $iteratorInstance->key();
450 // ... and the final entry which is the stored instance
451 $entry = $this->getEntry($entryIndex);
454 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('LIST: Adding entry ' . $entry . ' ...');
456 // Add it to the list
457 $entries[$iteratorInstance->current()] = $entry;
460 $iteratorInstance->next();
468 * Updates the given entry by hash with given array
470 * @param $hash Hash for this entry
471 * @param $entryArray Array with entry we should update
473 * @throws InvalidListHashException If the solved hash index is invalid
475 public function updateCurrentEntryByHash ($hash, array $entryArray) {
476 // Is the hash valid?
477 if (!$this->isHashValid($hash)) {
478 // Throw an exception here, hashIndex is unknown at this point
479 throw new InvalidListHashException(array($this, $hash, -999), self::EXCEPTION_INVALID_HASH);
483 $this->listEntries[$hash] = $entryArray;