All files / tick-map tick-map.js

100% Statements 99/99
100% Branches 37/37
100% Functions 22/22
100% Lines 99/99
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 2351x   1x   1x 1x 1x   1x     31x 11x     20x             1x   11x     5x     3014x       1x                   1x 2x 2x   16x       16x 14x 14x 14x 14x 8x 8x     16x             1x 1070x 1070x     1070x   1070x 1060x     10x 10x     1070x 1070x 1070x 1070x   882x 881x     1x 1x   882x 882x 882x     188x 179x     9x 9x               1x 4x 4x 4x 3x 3x 2x 1x 1x 1x     1x   2x 2x     2x             3x 3x                   1x 12x                 1x 13x 13x 13x 13x 13x 4x   13x           1x 1007x 1007x     1x 5x               1x 2010x 2010x 2008x 2008x 2007x                   1x 1004x 1004x 1001x 1357x     3x         3014x 3014x       2008x       18x       4113x       1070x 1070x    
module.exports = TickMap;
 
var Map = require('es6-map');
 
var Find = require('lodash.find');
var FindIndex = require('lodash.findIndex');
var SortedIndexBy = require('lodash.sortedindexby');
 
var EMPTY = []; // Avoid creating man empty arrays for very sparse tick maps
 
function TickMap() {
  if (!(this instanceof TickMap)) {
    return new TickMap();
  }
 
  this.internals = {
    tickSeq: [],  // Ordered array of populated ticks in the map. Duplicates are allowed
    bucketKeys: [],  // Ordered array of bucket keys
    bucketMap: new Map()
  };
}
 
Object.defineProperties(TickMap.prototype, {
  length: { enumerable: true, get: function() {
    return this.internals.tickSeq.length;
  }},
  bucketCount: { enumerable: true, get: function() {
    return this.internals.bucketKeys.length;
  }},
  _getBucket: { enumerable: false, get: function() {
    return getBucket;
  }}
});
 
var hasSymbol = typeof Symbol === 'function';
/* istanbul ignore next */ // Depends on ES6 support level
if (hasSymbol) {
  Object.defineProperty(TickMap.prototype, Symbol.iterator, {
    get: function() {
      return this.iterator;
    }
  });
}
 
TickMap.prototype.iterator = function() {
  var tm = this;
  return {
    next: function() {
      var result = {
        done: this.$bucket === tm.internals.bucketKeys.length
      };
 
      if (!result.done) {
        var bucket = tm.internals.bucketMap.get(tm.internals.bucketKeys[this.$bucket]);
        result.value = bucket[this.$bucketIdx];
        ++this.$bucketIdx;
        if (this.$bucketIdx === bucket.length) {
          this.$bucketIdx = 0;
          ++this.$bucket;
        }
      }
      return result;
    },
    $bucket: 0,
    $bucketIdx: 0
  }
}
 
TickMap.prototype.add = function(tick, value) {
  var append = false;
  var tickSeq = this.internals.tickSeq;
 
  // Optimising for append by order is worthwhile
  var append = tickSeq.length === 0 || tickSeq[tickSeq.length - 1] < tick;
 
  if (append) {
    tickSeq.push(tick);
  }
  else {
    var targetIndex = SortedIndexBy(tickSeq, tick);
    tickSeq.splice(targetIndex, 0, tick);
  }
 
  var bucketKey = makeBucketKey(tick);
  var entry = new Entry(tick, value);
  var bucket = this.internals.bucketMap.get(bucketKey);
  if (!bucket) {
    // Add the key to the bucketKeys array, used for lookup of buckets by zero-based index
    if (append) {
      this.internals.bucketKeys.push(bucketKey);
    }
    else {
      var bucketKeyIndex = SortedIndexBy(this.internals.bucketKeys, bucketKey);
      this.internals.bucketKeys.splice(bucketKeyIndex, 0, bucketKey);
    }
    bucket = [ entry ];
    this.internals.bucketMap.set(bucketKey, bucket);
    return;
  }
 
  if (append) {
    bucket.push(entry);
  }
  else {
    var bucketIndex = SortedIndexBy(bucket, entry, entryTickMap);
    bucket.splice(bucketIndex, 0, entry);
  }
}
 
/**
 * Delete the entry specified by exact tick and value
 * @return `true` if found and deleted, `false` otherwise
 */
TickMap.prototype.remove = function(tick, value) {
  var bucketKey = makeBucketKey(tick);
  var bucket = this.internals.bucketMap.get(bucketKey);
  if (bucket) {
    const bucketIndex = FindIndex(bucket, bucketFindPredicate(tick, value));
    if (bucketIndex > -1) {
      if (bucket.length === 1) { // Would be empty so delete the bucket
        this.internals.bucketMap.delete(bucketKey);
        var bucketKeyIndex = SortedIndexBy(this.internals.bucketKeys, bucketKey);
        this.internals.bucketKeys.splice(bucketKeyIndex, 1);
      }
      else {
        bucket.splice(bucketIndex, 1);
      }
      this.internals.tickSeq.splice(this.internals.tickSeq.indexOf(tick), 1);
      return true;
    }
  }
  return false;
}
 
/**
 * Predicate for finding in buckets using tick and value identity
 */
function bucketFindPredicate(tick, value) {
  return function(item) {
    return item.tick === tick && item.value === value;
  }
}
 
/**
 * Get the zero-based index of the populated bucket based on the
 * correct position for the specified tick/key whether it is present or not
 *
 * @return integer index into the populated bucket
 */
TickMap.prototype.bucketIndexFor = function(tick) {
  return SortedIndexBy(this.internals.bucketKeys, makeBucketKey(tick));
}
 
/**
 * Get the zero-based index of the populated bucket that represents
 * the nearest tick equal to or less than the specified tick.
 *
 * @return integer index into the populated bucket or 0 if no buckets exist
 */
TickMap.prototype.lastBucketIndexFor = function(tick) {
  var bucketKey = makeBucketKey(tick);
  var lastIndex = SortedIndexBy(this.internals.bucketKeys, bucketKey);
  var lastIndexKey = this.internals.bucketKeys[lastIndex];
  var bucket = lastIndexKey && this.internals.bucketMap.get(lastIndexKey);
  if (lastIndex !== 0 && (!bucket || bucket[0].tick > tick)) {
    --lastIndex; // Point to the previous populated bucket
  }
  return lastIndex;
}
 
/**
 * Retrieve an item by zero-based index
 */
TickMap.prototype.item = function(index) {
  var tick = this.internals.tickSeq[index];
  return this.get(tick);
}
 
TickMap.prototype.bucketAt = function(bucketIndex) {
  return this.internals.bucketKeys[bucketIndex];
}
 
/**
 * Retrieve an item by exact tick.
 *
 * @return The item or `undefined`
 */
TickMap.prototype.get = function(tick) {
  var bucket = this._getBucket(tick);
  if (bucket) {
    var entry = getInBucket(bucket, tick);
    if (entry) {
      return entry.value;
    }
  }
}
 
/**
 * Retrieves all items in the same bucket as the specified tick
 *
 * @return An array of items. If no items exist or no bucket exists, an empty array is returned.
 */
TickMap.prototype.getBucketItems = function(tick) {
  var entries = this._getBucket(tick);
  if (entries) {
    return entries.map(function(entry) {
      return entry.value;
    });
  }
  return EMPTY;
}
 
 
function getBucket(tick) {
  var bucketKey = makeBucketKey(tick);
  return this.internals.bucketMap.get(bucketKey);
}
 
function getInBucket(bucket, tick) {
  return Find(bucket, {tick: tick});
}
 
function entryTickMap(entry) {
  return entry.tick;
}
 
function makeBucketKey(tick) {
  return Math.floor(tick);
}
 
function Entry(tick, value) {
  this.tick = tick;
  this.value = value;
}