Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Create a data structure yourselves. Store the ordering in an array that is internal to the structure. Store the objects mapped by a key in a regular object. Let's call it <code>OrderedMap</code> which will have a map, an array, and four basic methods.</p> <pre><code>OrderedMap map _array set(key, value) get(key) remove(key) forEach(fn) function OrderedMap() { this.map = {}; this._array = []; } </code></pre> <p>When inserting an element, add it to the array at the desired position as well as to the object. Insertion by index or at the end is in O(1).</p> <pre><code>OrderedMap.prototype.set = function(key, value) { // key already exists, replace value if(key in this.map) { this.map[key] = value; } // insert new key and value else { this._array.push(key); this.map[key] = value; } }; </code></pre> <p>When deleting an object, remove it from the array and the object. If deleting by a key or a value, complexity is O(n) since you will need to traverse the internal array that maintains ordering. When deleting by index, complexity is O(1) since you have direct access to the value in both the array and the object.</p> <pre><code>OrderedMap.prototype.remove = function(key) { var index = this._array.indexOf(key); if(index == -1) { throw new Error('key does not exist'); } this._array.splice(index, 1); delete this.map[key]; }; </code></pre> <p>Lookups will be in O(1). Retrieve the value by key from the associative array (object).</p> <pre><code>OrderedMap.prototype.get = function(key) { return this.map[key]; }; </code></pre> <p>Traversal will be ordered and can use either of the approaches. When ordered traversal is required, create an array with the objects (values only) and return it. Being an array, it would not support keyed access. The other option is to ask the client to provide a callback function that should be applied to each object in the array.</p> <pre><code>OrderedMap.prototype.forEach = function(f) { var key, value; for(var i = 0; i &lt; this._array.length; i++) { key = this._array[i]; value = this.map[key]; f(key, value); } }; </code></pre> <p>See Google's implementation of a <a href="http://closure-library.googlecode.com/svn/docs/class_goog_structs_LinkedMap.html" rel="noreferrer">LinkedMap</a> from the Closure Library for documentation and source for such a class.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload