https://github.com/tonix-tuft/linked-hash-map
How I would implement a linked hash map in PHP if PHP wouldn't have associative arrays.
https://github.com/tonix-tuft/linked-hash-map
data-structure data-structures hash-map hash-table linked linked-hash-map map
Last synced: 2 months ago
JSON representation
How I would implement a linked hash map in PHP if PHP wouldn't have associative arrays.
- Host: GitHub
- URL: https://github.com/tonix-tuft/linked-hash-map
- Owner: tonix-tuft
- License: mit
- Created: 2021-07-13T21:31:09.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-07-17T09:35:31.000Z (over 4 years ago)
- Last Synced: 2024-12-16T19:04:25.143Z (over 1 year ago)
- Topics: data-structure, data-structures, hash-map, hash-table, linked, linked-hash-map, map
- Language: PHP
- Homepage:
- Size: 46.9 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# linked-hash-map
How I would implement a linked hash map in PHP if PHP wouldn't have associative arrays.
## Installation
Using [Composer](https://getcomposer.org/):
```
composer require tonix-tuft/linked-hash-map
```
## Usage
This map implements the [ArrayAccess](https://www.php.net/manual/en/class.arrayaccess.php) interface as well as the [Iterator](https://www.php.net/manual/en/class.iterator.php) and [Countable](https://www.php.net/manual/en/class.countable.php) interfaces and therefore can be used as a built-in PHP array:
```php
$value) {
var_dump($key, $value);
}
```
### Using any PHP data type for the key
This map allows using any PHP type for the key (i.e. even an array or an object can be used for the key):
```php
"A value"];
var_dump($arr[1.5]); // "A value"
var_dump($arr[1]); // "A value"
```
2. This map allows prepending instead of appending when setting the `LinkedHashMap::INSERT_MODE_PREPEND` flag (using the `setInsertMode` method):
```php
setInsertMode(LinkedHashMap::INSERT_MODE_PREPEND); // Defaults to `LinkedHashMap::INSERT_MODE_APPEND`
$map['a'] = 1;
$map['b'] = 2;
foreach ($map as $key => $value) {
var_dump($key, $value);
}
// 'b', 2
// 'a', 1
```
3. This map also allows setting the loop order (iteration) order (using the `setLoopOrder` method), whether normal (`LinkedHashMap::LOOP_ORDER_NORMAL`, the default) or reversed (`LinkedHashMap::LOOP_ORDER_REVERSE`):
```php
setLoopOrder(LinkedHashMap::LOOP_ORDER_REVERSE); // Defaults to `LinkedHashMap::LOOP_ORDER_NORMAL`
$map['a'] = 1;
$map['b'] = 2;
foreach ($map as $key => $value) {
var_dump($key, $value);
}
// 'b', 2
// 'a', 1
// Example 2:
$map = new LinkedHashMap();
$map->setInsertMode(LinkedHashMap::INSERT_MODE_PREPEND); // Defaults to `LinkedHashMap::INSERT_MODE_APPEND`
$map->setLoopOrder(LinkedHashMap::LOOP_ORDER_REVERSE); // Defaults to `LinkedHashMap::LOOP_ORDER_NORMAL`
$map['a'] = 1;
$map['b'] = 2;
foreach ($map as $key => $value) {
var_dump($key, $value);
}
// 'a', 1
// 'b', 2
```
4. Appending/prepending to the map works in the same way as with built-in PHP arrays (a positional index (an `int or integer string >= 0`) is created or the highest positional index used so far is incremented internally).
Accessing an unknown index does not trigger/emit a notice (just returns `NULL`):
```php
propertyA = rand(0, 100000);
$this->propertyB = rand(0, 100000);
}
// ...
/**
* {@inheritdoc}
*/
public function hashCode() {
// Compute the hash code somehow...
$prime = 31;
$hash = 1;
$hash = $prime * $hash + $this->propertyA;
$hash = $prime * $hash + $this->propertyB;
return $hash;
}
}
$map = new LinkedHashMap();
$obj1 = new ClassWithCustomHashCode();
$obj2 = new ClassWithCustomHashCode();
$map[$obj1] = "A value";
$map[$obj2] = "Another value";
var_dump($map[$obj1]); // "A value"
var_dump($map[$obj2]); // "Another value"
```
## License
MIT © [Anton Bagdatyev (Tonix)](https://github.com/tonix-tuft)