ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
HashTable.php
Go to the documentation of this file.
1 <?php
30 if (!defined('PHPEXCEL_ROOT')) {
34  define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../');
35 }
36 
38 require_once PHPEXCEL_ROOT . 'PHPExcel/IComparable.php';
39 
40 
49 {
55  public $_items = array();
56 
62  public $_keyMap = array();
63 
70  public function __construct($pSource = null)
71  {
72  if (!is_null($pSource)) {
73  // Create HashTable
74  $this->addFromSource($pSource);
75  }
76  }
77 
84  public function addFromSource($pSource = null) {
85  // Check if an array was passed
86  if ($pSource == null) {
87  return;
88  } else if (!is_array($pSource)) {
89  throw new Exception('Invalid array parameter passed.');
90  }
91 
92  foreach ($pSource as $item) {
93  $this->add($item);
94  }
95  }
96 
103  public function add(PHPExcel_IComparable $pSource = null) {
104  // Determine hashcode
105  $hashCode = null;
106  $hashIndex = $pSource->getHashIndex();
107  if ( is_null ( $hashIndex ) ) {
108  $hashCode = $pSource->getHashCode();
109  } else if ( isset ( $this->_keyMap[$hashIndex] ) ) {
110  $hashCode = $this->_keyMap[$hashIndex];
111  } else {
112  $hashCode = $pSource->getHashCode();
113  }
114 
115  // Add value
116  if (!isset($this->_items[ $hashCode ])) {
117  $this->_items[ $hashCode ] = $pSource;
118  $index = count($this->_items) - 1;
119  $this->_keyMap[ $index ] = $hashCode;
120  $pSource->setHashIndex( $index );
121  } else {
122  $pSource->setHashIndex( $this->_items[ $hashCode ]->getHashIndex() );
123  }
124  }
125 
132  public function remove(PHPExcel_IComparable $pSource = null) {
133  if (isset($this->_items[ $pSource->getHashCode() ])) {
134  unset($this->_items[ $pSource->getHashCode() ]);
135 
136  $deleteKey = -1;
137  foreach ($this->_keyMap as $key => $value) {
138  if ($deleteKey >= 0) {
139  $this->_keyMap[$key - 1] = $value;
140  }
141 
142  if ($value == $pSource->getHashCode()) {
143  $deleteKey = $key;
144  }
145  }
146  unset($this->_keyMap[ count($this->_keyMap) - 1 ]);
147  }
148  }
149 
154  public function clear() {
155  $this->_items = array();
156  $this->_keyMap = array();
157  }
158 
164  public function count() {
165  return count($this->_items);
166  }
167 
174  public function getIndexForHashCode($pHashCode = '') {
175  return array_search($pHashCode, $this->_keyMap);
176  }
177 
185  public function getByIndex($pIndex = 0) {
186  if (isset($this->_keyMap[$pIndex])) {
187  return $this->getByHashCode( $this->_keyMap[$pIndex] );
188  }
189 
190  return null;
191  }
192 
200  public function getByHashCode($pHashCode = '') {
201  if (isset($this->_items[$pHashCode])) {
202  return $this->_items[$pHashCode];
203  }
204 
205  return null;
206  }
207 
213  public function toArray() {
214  return $this->_items;
215  }
216 
220  public function __clone() {
221  $vars = get_object_vars($this);
222  foreach ($vars as $key => $value) {
223  if (is_object($value)) {
224  $this->$key = clone $value;
225  }
226  }
227  }
228 }