]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/lists/class_BaseList.php
Continued with hub:
[hub.git] / application / hub / main / lists / class_BaseList.php
index 3bc2933e33abb1c716d69c4f26bb0b47c12f0147..db8699b7d0568e142de50da83ef47f5a63860031 100644 (file)
@@ -2,11 +2,11 @@
 /**
  * A general list class
  *
- * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Hub Developer Team
  * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.ship-simu.org
+ * @link               http://www.shipsimu.org
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -59,21 +59,30 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
         * @return      $iteratorInstance       An instance of a Iterator class
         */
        public function getIterator () {
-               // Prepare a default iterator
-               $iteratorInstance = ObjectFactory::createObjectByConfiguredName('default_iterator_class', array($this));
+               // Get iterator from here
+               $iteratorInstance = $this->getIteratorInstance();
+
+               // Is the instance set?
+               if (is_null($iteratorInstance)) {
+                       // Prepare a default iterator
+                       $iteratorInstance = ObjectFactory::createObjectByConfiguredName('default_iterator_class', array($this));
+
+                       // Set it here
+                       $this->setIteratorInstance($iteratorInstance);
+               } // END - if
 
                // And return it
                return $iteratorInstance;
        }
 
        /**
-        * Checks wether the given group is set
+        * Checks whether the given group is set
         *
         * @param       $groupName      Group to check if found in list
-        * @return      $isset          Wether the group is valid
+        * @return      $isset          Whether the group is valid
         */
        public function isGroupSet ($groupName) {
-               //* DEBUG: */ $this->debugOutput(__METHOD__.': '.$groupName);
+               //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__.': '.$groupName);
                return isset($this->listGroups[$groupName]);
        }
 
@@ -85,7 +94,7 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
         * @throws      ListGroupAlreadyAddedException  If the given group is already added
         */
        public function addGroup ($groupName) {
-               //* DEBUG: */ $this->debugOutput(__METHOD__.': '.$groupName . ' - START');
+               //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__.': '.$groupName . ' - START');
                // Is the group already added?
                if ($this->isGroupSet($groupName)) {
                        // Throw the exception here
@@ -94,20 +103,20 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
 
                // Add the group which is a simple array
                $this->listGroups[$groupName] = ObjectFactory::createObjectByConfiguredName('list_group_class');
-               //* DEBUG: */ $this->debugOutput(__METHOD__.': '.$groupName . ' - FINISHED');
+               //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__.': '.$groupName . ' - FINISHED');
        }
 
        /**
         * Adds the given instance to list group and sub group
         *
-        * @param       $groupName      Group to add instance to
-        * @param       $subGroup       Sub group to add instance to
-        * @param       $instance       An instance of Visitable
+        * @param       $groupName                      Group to add instance to
+        * @param       $subGroup                       Sub group to add instance to
+        * @param       $visitableInstance      An instance of Visitable
         * @return      void
         * @throws      NoListGroupException    If the given group is not found
         */
-       public function addInstance ($groupName, $subGroup, Visitable $instance) {
-               //* DEBUG: */ $this->debugOutput(__METHOD__.': '.$groupName  . '/' . $subGroup . ' - START');
+       public function addInstance ($groupName, $subGroup, Visitable $visitableInstance) {
+               //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__.': '.$groupName  . '/' . $subGroup . ' - START');
                // Is the group there?
                if (!$this->isGroupSet($groupName)) {
                        // Throw the exception here
@@ -121,17 +130,17 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
                } // END - if
 
                // Generate the hash
-               $hash = $this->generateHash($groupName, $subGroup, $instance);
+               $hash = $this->generateHash($groupName, $subGroup, $visitableInstance);
 
                // Now add it to the group list and hash it
                $this->listGroups[$groupName]->addEntry($subGroup, $hash);
 
                // Add the hash to the index
-               $this->listIndex[] = $hash;
+               array_push($this->listIndex, $hash);
 
                // Add the instance itself to the list
-               $this->listEntries[$hash] = $instance;
-               //* DEBUG: */ $this->debugOutput(__METHOD__.': '.$groupName  . '/' . $subGroup . ' - FINISHED');
+               $this->listEntries[$hash] = $visitableInstance;
+               //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__.': '.$groupName  . '/' . $subGroup . ' - FINISHED');
        }
 
        /**
@@ -163,7 +172,7 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
                        // Is the list entry set?
                        if ($this->isHashValid($hash)) {
                                // Add it
-                               $array[] = $this->listEntries[$hash];
+                               array_push($array, $this->listEntries[$hash]);
                                //* DEBUG: */ print __METHOD__.": ADDED!\n";
                        } // END - if
                } // END - foreach
@@ -181,7 +190,7 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
         * @throws      NoListGroupException    If the given group is not found
         */
        public function addEntry ($groupName, $entry) {
-               //* DEBUG: */ $this->debugOutput(__METHOD__.'('.$this->__toString().'): '.$groupName . ' - START');
+               //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__.'('.$this->__toString().'): '.$groupName . ' - START');
                // Is the group already added?
                if (!$this->isGroupSet($groupName)) {
                        // Throw the exception here
@@ -190,16 +199,16 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
 
                // Generate hash
                $hash = $this->generateHash($groupName, $groupName, $entry);
-               //* DEBUG: */ $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']:  groupName=' . $groupName . ', entry=' . $entry . ', hash=' . $hash);
+               //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-LIST[' . __METHOD__ . ':' . __LINE__ . ']:  groupName=' . $groupName . ', entry=' . $entry . ', hash=' . $hash);
 
                // Add the hash to the index
-               $this->listIndex[] = $hash;
-               //* DEBUG: */ print $groupName.'/'.count($this->listIndex).chr(10);
+               array_push($this->listIndex, $hash);
+               //* DEBUG: */ print $groupName.'/'.$this->count().chr(10);
 
                // Now add the entry to the list
                $this->listEntries[$hash] = $entry;
                //* DEBUG: */ print $groupName.'/'.count($this->listEntries).chr(10);
-               //* DEBUG: */ $this->debugOutput(__METHOD__.'('.$this->__toString().'): '.$groupName . ' - FINISHED');
+               //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__.'('.$this->__toString().'): '.$groupName . ' - FINISHED');
        }
 
        /**
@@ -211,7 +220,7 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
         * @throws      NoListGroupException    If the given group is not found
         */
        public function removeEntry ($groupName, $entry) {
-               //* DEBUG: */ $this->debugOutput(__METHOD__.'('.$this->__toString().'): '.$groupName . ' - START');
+               //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__.'('.$this->__toString().'): '.$groupName . ' - START');
                // Is the group already added?
                if (!$this->isGroupSet($groupName)) {
                        // Throw the exception here
@@ -220,14 +229,14 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
 
                // Generate hash
                $hash = $this->generateHash($groupName, $groupName, $entry);
-               //* DEBUG: */ $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']:  groupName=' . $groupName . ', entry=' . $entry . ', hash=' . $hash);
+               //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BASE-LIST[' . __METHOD__ . ':' . __LINE__ . ']:  groupName=' . $groupName . ', entry=' . $entry . ', hash=' . $hash);
 
                // Remove it from the list ...
                unset($this->listEntries[$hash]);
 
                // ... and hash list as well
                unset($this->listIndex[array_search($hash, $this->listIndex)]);
-               //* DEBUG: */ $this->debugOutput(__METHOD__.'('.$this->__toString().'): '.$groupName . ' - FINISHED');
+               //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(__METHOD__.'('.$this->__toString().'): '.$groupName . ' - FINISHED');
        }
 
        /**
@@ -257,9 +266,12 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
                } elseif ((is_array($entry)) && (isset($entry['id']))) {
                        // Supported array found
                        $entry2 = crc32($entry['id']) . ':' . count($entry);
+               } elseif ((is_array($entry)) && (isset($entry[BasePool::SOCKET_ARRAY_RESOURCE])) && (isset($entry[BasePool::SOCKET_ARRAY_CONN_TYPE]))) {
+                       // Is a socket resource array
+                       $entry2 = crc32($entry[BasePool::SOCKET_ARRAY_RESOURCE] . ':' . $entry[BasePool::SOCKET_ARRAY_CONN_TYPE]);
                } else {
                        // Unsupported type detected
-                       $this->debugOutut('[' . __METHOD__ . ':' . __LINE__ . ']:  entry type ' . gettype($entry) . ' is unsupported.');
+                       self::createDebugInstance(__CLASS__)->debugOutput('BASE-LIST[' . __METHOD__ . ':' . __LINE__ . ']: Entry type ' . gettype($entry) . ' is unsupported.');
 
                        // @TODO Extend this somehow?
                        $entry2 = gettype($entry);
@@ -320,10 +332,10 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
        }
 
        /**
-        * Checks wether the given hash is valid
+        * Checks whether the given hash is valid
         *
         * @param       $hash           The hash we should validate
-        * @return      $isValid        Wether the given hash is valid
+        * @return      $isValid        Whether the given hash is valid
         */
        public final function isHashValid ($hash) {
                // Check it
@@ -391,6 +403,9 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
                // Get an iterator
                $iteratorInstance = $this->listGroups[$groupName]->getIterator();
 
+               // Rewind the iterator for this round
+               $iteratorInstance->rewind();
+
                // Go through all entries
                while ($iteratorInstance->valid()) {
                        // Get key
@@ -399,6 +414,9 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
                        // ... and the final entry which is the stored instance
                        $entry = $this->getEntry($entryIndex);
 
+                       // Debug message
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('LIST: Adding entry ' . $entry . ' ...');
+
                        // Add it to the list
                        $entries[$iteratorInstance->current()] = $entry;