HashCompactorLite()

Fast keyword-value pairs algorithm

by Kevin Crosby

HTML

<h3>
HashCompactorLite() <code>keyword-value pair manager</code>
</h3>

<p>
<code>HashCompactorLite(optional HashCompactorLite object OR Array of Strings)</code> creates (a copy of) a HashCompactorLite object, or if an array is provided, <code>.add()</code> each item as a keyword.
<ul>
<li><code>.add(keyword,optional datum)</code> returns an <code>array</code> of data. Multiple calls to the same <code>keyword</code> pushes the <code>datum</code> onto the array. Undefined datum works well for calculating word counts or simple comparisons against a list. The <code>keyword</code> can be either a <code>String</code>, a <code>Number</code> converted to a <code>String</code> (to take advantage of 1 being the most common number), or an array of <code>String</code> objects (such as names of modules).</li>
<li><code>.count(optional show data count)</code> return number of keywords or total data items if parameter is <code>true</code>, features <code>object[keyword]</code> doesn't offer.</li>
<li><code>.item(keyword)</code> returns the keyword's data array, otherwise <code>null</code>.</li>
<li><code>.set(keyword,data,optional combine data)</code> sets the keyword's data array, either overriding what may have existed, or combining to any existing data if <code>true</code>.</li>
<li><code>.forEach(callback,optional thisp)</code> iterates through each keyword calling the callback function with the parameters <code>(keyword,data)</code> in the <code>this</code> context provided.</li>
<li><code>.sort(comparison function,optional preprocessor function,optional partial keyword)</code> returns an <code>array</code> of keywords sorted after preprocessing HashCompactor data into an array for comparisons using the <code>preprocessor</code> parameteters <code>(keyword,data,arrayToFill)</code>. If the <code>partial keyword</code> is 'a', for example, only the keywords beginning with 'a' will be returned.</li>
<li><code>.copy(HashCompactorLite object OR Array of keywords)</code>...

CSS

li { font-size:.8em }
p { font-size: .8em }

JavaScript

/**
* HashCompactorLite - by Kevin J. Crosby (SkewsMe.com)
*   Copyright (c) 1992 (concept), 2006 (implementation), 2015 (rewrite)
*   Open Source since 2006: https://sourceforge.net/projects/hashcompactor/
*   Last updated: 12 May 2017 - skewsme.com/hashcompactor_trans.js
*                 17 May 2017
*   May 27, 2017 - It seems living in a cave has made me ignorant of advancements like Map,
*			so now I need to figure out how to use it.
*
* Time trials: https://jsfiddle.net/Skews_Me/3ogxez2L/
*/

function HashCompactorLite(/* hashCompactorToCopyOrArray */) {
  let arg = arguments[0];
  if (typeof arg !== "undefined") {
    this.copy(arg);
  }
}

HashCompactorLite.prototype = {
  constructor: HashCompactorLite,
  _referenceCount: 0,
  
  _findChild: function(node,character) {
    let map = node._map;
    if (map) {
      return map.get(character);
    }
    return void 0;
  },
  
  count: function(/* countData */) {
    if (arguments[0] === true) {
      let datacount = 0;
      this.forEach(function(keyword,data) {
          datacount += data.length;
      });
      return datacount;
    }
    return this._referenceCount;
  },

  item: function(keyword) {
    let node = this._item(keyword);
    return node ? node._data : null;
  },
  _item: function(partial_keyword) {
    let node = this,
        keyOp = new HashCompactorKeywordIterator(partial_keyword);
    while (keyOp.inside()) {
      node = this._findChild(node,keyOp.character());
      if (! node) {
        return null;
      }
      keyOp.next();
    }
    return node;
  },

	// Accepts a string, a number that it converts to a string, or an array of strings
  add: function(keyword /* ,datum */) { // ASSERT(keyword !== "")
    // Create nodes for each character in key if they don't already exist. Then
    // if there's already data, push this datum onto the end, otherwise create the
    // data array, and double back to increment reference counts for the new keyword.
    let child = this,
        keyOp =...