ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
HashTable.php
Go to the documentation of this file.
1 <?php
37 {
43  public $_items = array();
44 
50  public $_keyMap = array();
51 
58  public function __construct($pSource = null)
59  {
60  if (!is_null($pSource)) {
61  // Create HashTable
62  $this->addFromSource($pSource);
63  }
64  }
65 
72  public function addFromSource($pSource = null) {
73  // Check if an array was passed
74  if ($pSource == null) {
75  return;
76  } else if (!is_array($pSource)) {
77  throw new Exception('Invalid array parameter passed.');
78  }
79 
80  foreach ($pSource as $item) {
81  $this->add($item);
82  }
83  }
84 
91  public function add(PHPExcel_IComparable $pSource = null) {
92  $hash = $pSource->getHashCode();
93  if (!isset($this->_items[$hash])) {
94  $this->_items[$hash] = $pSource;
95  $this->_keyMap[count($this->_items) - 1] = $hash;
96  }
97  }
98 
105  public function remove(PHPExcel_IComparable $pSource = null) {
106  $hash = $pSource->getHashCode();
107  if (isset($this->_items[$hash])) {
108  unset($this->_items[$hash]);
109 
110  $deleteKey = -1;
111  foreach ($this->_keyMap as $key => $value) {
112  if ($deleteKey >= 0) {
113  $this->_keyMap[$key - 1] = $value;
114  }
115 
116  if ($value == $hash) {
117  $deleteKey = $key;
118  }
119  }
120  unset($this->_keyMap[count($this->_keyMap) - 1]);
121  }
122  }
123 
128  public function clear() {
129  $this->_items = array();
130  $this->_keyMap = array();
131  }
132 
138  public function count() {
139  return count($this->_items);
140  }
141 
148  public function getIndexForHashCode($pHashCode = '') {
149  return array_search($pHashCode, $this->_keyMap);
150  }
151 
159  public function getByIndex($pIndex = 0) {
160  if (isset($this->_keyMap[$pIndex])) {
161  return $this->getByHashCode( $this->_keyMap[$pIndex] );
162  }
163 
164  return null;
165  }
166 
174  public function getByHashCode($pHashCode = '') {
175  if (isset($this->_items[$pHashCode])) {
176  return $this->_items[$pHashCode];
177  }
178 
179  return null;
180  }
181 
187  public function toArray() {
188  return $this->_items;
189  }
190 
194  public function __clone() {
195  $vars = get_object_vars($this);
196  foreach ($vars as $key => $value) {
197  if (is_object($value)) {
198  $this->$key = clone $value;
199  }
200  }
201  }
202 }