Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Represents a new HashMap.
- *
- * @author Vendetta <admin@vendetta-media.nl>
- * @copyright (c) 2011 Vendetta Media
- */
- class HashMap {
- /**
- * Associative array that contains the data of this hashmap.
- *
- * @var Array
- */
- protected $data;
- /**
- * The current index in the array.
- *
- * @var int
- */
- protected $offset = 0;
- /**
- * Constructs a new HashMap.
- */
- public function __construct() {
- }
- /**
- * Adds a new value to the HashMap.
- *
- * @param Mixed $key
- * @param Mixed $value
- */
- public function put($key, $value) {
- if (is_numeric($key)) {
- $this->data[] = $value;
- } else {
- $this->data[$key] = $value;
- }
- $this->offset++;
- }
- /**
- * Adds a complete array to the HashMap.
- *
- * @param Array $data
- */
- public function putAll(array $data) {
- foreach($data as $key => $value) {
- $this->put($key, $value);
- }
- }
- /**
- * Gets a value from the HashMap.
- * If $key is NULL it will return the item,
- * at the current offset.
- *
- * @param Mixed $key
- * @return Mixed $value
- */
- public function get($key = NULL) {
- return $this->data[(is_null($key) ? $this->offset : $key)];
- }
- /**
- * Removes an element from the array.
- * If the $key is NULL, the element at the current,
- * offset will be deleted.
- *
- * @param Mixed $key
- */
- public function remove($key = NULL) {
- if (is_null($offset)) {
- unset($this->data[$this->offset]);
- } else {
- if (array_key_exists($key, $this->data)) {
- unset($this->data[$key]);
- }
- }
- }
- /**
- * Returns the total length of the HashMap.
- *
- * @return int
- */
- public function size() {
- return (int) count($this->data);
- }
- /**
- * Clears all data from the HashMap,
- * and resets the offset.
- */
- public function clear() {
- $this->data = array();
- $this->offset = 0;
- }
- /**
- * Sets the offset to the next element.
- */
- public function next() {
- $this->offset++;
- }
- /**
- * Sets the offset to the previous element.
- */
- public function previous() {
- $this->offset--;
- }
- /**
- * Reset the offset back to 0.
- */
- public function rewind() {
- $this->offset = 0;
- }
- /**
- * If the param is null, the function will return the,
- * current offset else it will set the current offset.
- *
- * @param Mixed $offset
- * @return int $offset
- * @throws InvalidArgumentException
- */
- public function offset($offset = NULL) {
- if (is_null($offset)) {
- return (int) $this->offset;
- }
- if (!is_numeric($offset)) {
- throw new InvalidArgumentException("$offset must be of type Integer.");
- }
- $this->offset = $offset;
- }
- /**
- * Returns the HashMap as array.
- *
- * @return Array $data
- */
- public function asArray() {
- return (array) $this->data;
- }
- /**
- * Checks if a $key exists or not.
- *
- * @param Mixed $key
- * @return Boolean
- */
- public function containsKey($key) {
- return (boolean) array_key_exists($key);
- }
- /**
- * Checks if a $value exists in the array or not.
- *
- * @param Mixed $value
- * @return Boolean
- */
- public function containsKey($value) {
- return (boolean) in_array($value);
- }
- /**
- * Returns true if the HashMap is empty.
- *
- * @return Boolean
- */
- public function isEmpty() {
- return (boolean) (count($this->data) == 0 ? true : false);
- }
- }
- ?>
Add Comment
Please, Sign In to add comment