Guest User

Untitled

a guest
Apr 23rd, 2018
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.36 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Represents a new HashMap.
  4.  *
  5.  * @author Vendetta <admin@vendetta-media.nl>
  6.  * @copyright (c) 2011 Vendetta Media
  7.  */
  8. class HashMap {
  9.    
  10.     /**
  11.      * Associative array that contains the data of this hashmap.
  12.      *
  13.      * @var Array
  14.      */
  15.     protected $data;
  16.    
  17.     /**
  18.      * The current index in the array.
  19.      *
  20.      * @var int
  21.      */
  22.     protected $offset = 0;
  23.    
  24.     /**
  25.      * Constructs a new HashMap.
  26.      */
  27.     public function __construct() {
  28.        
  29.     }
  30.    
  31.     /**
  32.      * Adds a new value to the HashMap.
  33.      *
  34.      * @param Mixed $key
  35.      * @param Mixed $value
  36.      */
  37.     public function put($key, $value) {
  38.         if (is_numeric($key)) {
  39.             $this->data[] = $value;
  40.         } else {
  41.             $this->data[$key] = $value;
  42.         }
  43.         $this->offset++;
  44.     }
  45.    
  46.     /**
  47.      * Adds a complete array to the HashMap.
  48.      *
  49.      * @param Array $data
  50.      */
  51.     public function putAll(array $data) {
  52.         foreach($data as $key => $value) {
  53.             $this->put($key, $value);
  54.         }
  55.     }
  56.    
  57.     /**
  58.      * Gets a value from the HashMap.
  59.      * If $key is NULL it will return the item,
  60.      * at the current offset.
  61.      *
  62.      * @param Mixed $key
  63.      * @return Mixed $value
  64.      */
  65.     public function get($key = NULL) {
  66.         return $this->data[(is_null($key) ? $this->offset : $key)];
  67.     }
  68.    
  69.     /**
  70.      * Removes an element from the array.
  71.      * If the $key is NULL, the element at the current,
  72.      * offset will be deleted.
  73.      *
  74.      * @param Mixed $key
  75.      */
  76.     public function remove($key = NULL) {
  77.         if (is_null($offset)) {
  78.             unset($this->data[$this->offset]);
  79.         } else {
  80.             if (array_key_exists($key, $this->data)) {
  81.                 unset($this->data[$key]);
  82.             }
  83.         }
  84.     }
  85.    
  86.     /**
  87.      * Returns the total length of the HashMap.
  88.      *
  89.      * @return int
  90.      */
  91.     public function size() {
  92.         return (int) count($this->data);
  93.     }
  94.    
  95.     /**
  96.      * Clears all data from the HashMap,
  97.      * and resets the offset.
  98.      */
  99.     public function clear() {
  100.         $this->data = array();
  101.         $this->offset = 0;
  102.     }
  103.    
  104.     /**
  105.      * Sets the offset to the next element.
  106.      */
  107.     public function next() {
  108.         $this->offset++;
  109.     }
  110.    
  111.     /**
  112.      * Sets the offset to the previous element.
  113.      */
  114.     public function previous() {
  115.         $this->offset--;
  116.     }
  117.    
  118.     /**
  119.      * Reset the offset back to 0.
  120.      */
  121.     public function rewind() {
  122.         $this->offset = 0;
  123.     }
  124.    
  125.     /**
  126.      * If the param is null, the function will return the,
  127.      * current offset else it will set the current offset.
  128.      *
  129.      * @param Mixed $offset
  130.      * @return int $offset
  131.      * @throws InvalidArgumentException
  132.      */
  133.     public function offset($offset = NULL) {
  134.         if (is_null($offset)) {
  135.             return (int) $this->offset;
  136.         }
  137.         if (!is_numeric($offset)) {
  138.             throw new InvalidArgumentException("$offset must be of type Integer.");
  139.         }
  140.         $this->offset = $offset;
  141.     }
  142.    
  143.     /**
  144.      * Returns the HashMap as array.
  145.      *
  146.      * @return Array $data
  147.      */
  148.     public function asArray() {
  149.         return (array) $this->data;
  150.     }
  151.    
  152.     /**
  153.      * Checks if a $key exists or not.
  154.      *
  155.      * @param Mixed $key
  156.      * @return Boolean
  157.      */
  158.     public function containsKey($key) {
  159.         return (boolean) array_key_exists($key);
  160.     }
  161.    
  162.     /**
  163.      * Checks if a $value exists in the array or not.
  164.      *
  165.      * @param Mixed $value
  166.      * @return Boolean
  167.      */
  168.     public function containsKey($value) {
  169.         return (boolean) in_array($value);
  170.     }
  171.    
  172.     /**
  173.      * Returns true if the HashMap is empty.
  174.      *
  175.      * @return Boolean
  176.      */
  177.     public function isEmpty() {
  178.         return (boolean) (count($this->data) == 0 ? true : false);
  179.     }
  180. }
  181. ?>
Add Comment
Please, Sign In to add comment