topical media & game development

talk show tell print

lib-as-de-polygonal-ds-HashMap.ax

lib-as-de-polygonal-ds-HashMap.ax (swf ) [ flash ] flex


  package de.polygonal.ds
  {
          import flash.utils.Dictionary;
          import de.polygonal.ds.Collection;
          
          public class @ax-lib-as-de-polygonal-ds-HashMap implements Collection
          {
                  private var _keyMap:Dictionary;
                  private var _objMap:Dictionary;
                  private var _size:int;
                  
                  
Initializes a hash map, which maps keys to values. It cannot contain duplicate keys so each key can map to at most one value.

  
                  public function @ax-lib-as-de-polygonal-ds-HashMap()
                  {
                          _keyMap = new Dictionary(true);
                          _objMap = new Dictionary(true);
                          _size = 0;
                  }
                  
                  
Inserts a key/data couple into the table.
parameter: key The key.
parameter: obj The data associated with the key.

  
                  public function insert(key:*, obj:*):Boolean
                  {
                          if (_keyMap[key]) return false;
                          ++_size;
                          _objMap[obj] = key;
                          _keyMap[key] = obj;
                          return true;
                  }
                  
                  
Finds the entry that is associated with the given key.
parameter: key The key to search for.
returns: The data associated with the key or null if no matching entry was found.

  
                  public function find(key:*):*
                  {
                          return _keyMap[key] || null;
                  }
                  
                  
Finds the key that maps the given value.
parameter: val The value which maps the sought-after key.
returns: The key mapping the given value or null if no matching key was found.

  
                  public function findKey(val:*):*
                  {
                          return _objMap[val] || null;
                  }
                  
                  
Removes an entry based on a given key.
parameter: key The entry's key.
returns: The data associated with the key or null if no matching entry was found.

  
                  public function remove(key:*):*
                  {
                          var obj:* = _keyMap[key];
                          
                          if (obj)
                          {
                                  --_size;
                                  delete _keyMap[key];
                                  delete _objMap[obj];
                                  return obj;
                          }
                          return null;
                  }
                  
                  
Checks if a key maps the given value.
returns: True if value exists, otherwise false.

  
                  public function contains(obj:*):Boolean
                  {
                          return _objMap[obj] ? true : false;
                  }
                  
                  
Checks if a mapping exists for the given key.
returns: True if key exists, otherwise false.

  
                  public function containsKey(key:*):Boolean
                  {
                          return _keyMap[key] ? true : false;
                  }
                  
                  
Creates an interator for traversing the values.

  
                  public function getIterator():Iterator
                  {
                          return new @fileValueIterator(this);
                  }
                  
                  
Creates an interator for traversing the keys.

  
                  public function getKeyIterator():Iterator
                  {
                          return new @fileKeyIterator(this);
                  }
                  
                  
Clears all elements.

  
                  public function clear():void
                  {
                          _keyMap = new Dictionary(true);
                          _objMap = new Dictionary(true);
                          _size = 0;
                  }
                  
                  
The total number of items.

  
                  public function get size():int
                  {
                          return _size;
                  }
                  
                  
Checks if the map is empty.
returns: True if empty, otherwise false.

  
                  public function isEmpty():Boolean
                  {
                          return _size == 0;
                  }
                  
                  
Writes all values into an array.
returns: An array.

  
                  public function toArray():Array
                  {
                          var a:Array = new Array(_size), j:int = 0;
                          for each (var i:* in _keyMap)
                          {
                                  a[j++] = i;
                          }
                          return a;
                  }
                  
                  
Writes all keys into an array.
returns: An array.

  
                  public function getKeySet():Array
                  {
                          var a:Array = new Array(_size), j:int = 0;
                          for each (var i:* in _objMap)
                          {
                                  a[j++] = i;
                          }
                          return a;
                  }
                  
                  
Returns a string representing the current object.

  
                  public function toString():String
                  {
                          return "[@ax-lib-as-de-polygonal-ds-HashMap, size=" + size + "]";
                  }
                  
                  
Prints all elements (for debug/demo purposes only).

  
                  public function dump():String
                  {
                          var s:String = "@ax-lib-as-de-polygonal-ds-HashMap:\n";
                          for each (var i:* in _objMap)
                                  s += "[key: " + i + " val:" + _keyMap[i] + "]\n";
                          return s;
                  }
          }
  }
  
  import de.polygonal.ds.Iterator;
  import de.polygonal.ds.@ax-lib-as-de-polygonal-ds-HashMap;
  
  internal class @fileValueIterator implements Iterator
  {
          private var _h:@ax-lib-as-de-polygonal-ds-HashMap;
          private var _values:Array;
          private var _cursor:int;
          private var _size:int;
          
          public function @fileValueIterator(h:@ax-lib-as-de-polygonal-ds-HashMap)
          {
                  _h = h;
                  _values = h.toArray();
                  _cursor = 0;
                  _size = _h.size; 
          }
          
          public function get data():*
          {
                  return _values [cursor];
          }
          
          public function set data(obj:*):void
          {
                  var key:* = _h.findKey(_values [cursor]);
                  _h.remove(key);
                  _h.insert(key, obj);
          }
          
          public function start():void
          {
                  _cursor = 0;
          }
          
          public function hasNext():Boolean
          {
                  return _cursor < _size;
          }
          
          public function next():*
          {
                  return _values[_cursor++];
          }
  }
  
  internal class @fileKeyIterator implements Iterator
  {
          private var _h:@ax-lib-as-de-polygonal-ds-HashMap;
          private var _keys:Array;
          private var _cursor:int;
          private var _size:int;
          
          public function @fileKeyIterator(h:@ax-lib-as-de-polygonal-ds-HashMap)
          {
                  _h = h;
                  _keys = h.getKeySet();
                  _cursor = 0;
                  _size = _h.size; 
          }
          
          public function get data():*
          {
                  return _keys [cursor];
          }
          
          public function set data(obj:*):void
          {
                  var key:* = _keys [cursor];
                  var val:* = _h.find(key);
                  _h.remove(key);
                  _h.insert(obj, val);
          }
          
          public function start():void
          {
                  _cursor = 0;
          }
          
          public function hasNext():Boolean
          {
                  return _cursor < _size;
          }
          
          public function next():*
          {
                  return _keys[_cursor++];
          }
  }


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.