IntegerBufferSet.js 6 KB
Newer Older
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
/**
 * Copyright (c) 2013-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @typechecks
 */
'use strict';

var Heap = require("./Heap");

var invariant = require("./invariant"); // Data structure that allows to store values and assign positions to them
// in a way to minimize changing positions of stored values when new ones are
// added or when some values are replaced. Stored elements are alwasy assigned
// a consecutive set of positoins startin from 0 up to count of elements less 1
// Following actions can be executed
// * get position assigned to given value (null if value is not stored)
// * create new entry for new value and get assigned position back
// * replace value that is furthest from specified value range with new value
//   and get it's position back
// All operations take amortized log(n) time where n is number of elements in
// the set.


var IntegerBufferSet =
/*#__PURE__*/
function () {
  function IntegerBufferSet() {
    this._valueToPositionMap = {};
    this._size = 0;
    this._smallValues = new Heap([], // Initial data in the heap
    this._smallerComparator);
    this._largeValues = new Heap([], // Initial data in the heap
    this._greaterComparator);
    this.getNewPositionForValue = this.getNewPositionForValue.bind(this);
    this.getValuePosition = this.getValuePosition.bind(this);
    this.getSize = this.getSize.bind(this);
    this.replaceFurthestValuePosition = this.replaceFurthestValuePosition.bind(this);
  }

  var _proto = IntegerBufferSet.prototype;

  _proto.getSize = function getSize()
  /*number*/
  {
    return this._size;
  };

  _proto.getValuePosition = function getValuePosition(
  /*number*/
  value)
  /*?number*/
  {
    if (this._valueToPositionMap[value] === undefined) {
      return null;
    }

    return this._valueToPositionMap[value];
  };

  _proto.getNewPositionForValue = function getNewPositionForValue(
  /*number*/
  value)
  /*number*/
  {
    !(this._valueToPositionMap[value] === undefined) ? process.env.NODE_ENV !== "production" ? invariant(false, "Shouldn't try to find new position for value already stored in BufferSet") : invariant(false) : void 0;
    var newPosition = this._size;
    this._size++;

    this._pushToHeaps(newPosition, value);

    this._valueToPositionMap[value] = newPosition;
    return newPosition;
  };

  _proto.replaceFurthestValuePosition = function replaceFurthestValuePosition(
  /*number*/
  lowValue,
  /*number*/
  highValue,
  /*number*/
  newValue)
  /*?number*/
  {
    !(this._valueToPositionMap[newValue] === undefined) ? process.env.NODE_ENV !== "production" ? invariant(false, "Shouldn't try to replace values with value already stored value in " + "BufferSet") : invariant(false) : void 0;

    this._cleanHeaps();

    if (this._smallValues.empty() || this._largeValues.empty()) {
      // Threre are currently no values stored. We will have to create new
      // position for this value.
      return null;
    }

    var minValue = this._smallValues.peek().value;

    var maxValue = this._largeValues.peek().value;

    if (minValue >= lowValue && maxValue <= highValue) {
      // All values currently stored are necessary, we can't reuse any of them.
      return null;
    }

    var valueToReplace;

    if (lowValue - minValue > maxValue - highValue) {
      // minValue is further from provided range. We will reuse it's position.
      valueToReplace = minValue;

      this._smallValues.pop();
    } else {
      valueToReplace = maxValue;

      this._largeValues.pop();
    }

    var position = this._valueToPositionMap[valueToReplace];
    delete this._valueToPositionMap[valueToReplace];
    this._valueToPositionMap[newValue] = position;

    this._pushToHeaps(position, newValue);

    return position;
  };

  _proto._pushToHeaps = function _pushToHeaps(
  /*number*/
  position,
  /*number*/
  value) {
    var element = {
      position: position,
      value: value
    }; // We can reuse the same object in both heaps, because we don't mutate them

    this._smallValues.push(element);

    this._largeValues.push(element);
  };

  _proto._cleanHeaps = function _cleanHeaps() {
    // We not usually only remove object from one heap while moving value.
    // Here we make sure that there is no stale data on top of heaps.
    this._cleanHeap(this._smallValues);

    this._cleanHeap(this._largeValues);

    var minHeapSize = Math.min(this._smallValues.size(), this._largeValues.size());
    var maxHeapSize = Math.max(this._smallValues.size(), this._largeValues.size());

    if (maxHeapSize > 10 * minHeapSize) {
      // There are many old values in one of heaps. We nned to get rid of them
      // to not use too avoid memory leaks
      this._recreateHeaps();
    }
  };

  _proto._recreateHeaps = function _recreateHeaps() {
    var sourceHeap = this._smallValues.size() < this._largeValues.size() ? this._smallValues : this._largeValues;
    var newSmallValues = new Heap([], // Initial data in the heap
    this._smallerComparator);
    var newLargeValues = new Heap([], // Initial datat in the heap
    this._greaterComparator);

    while (!sourceHeap.empty()) {
      var element = sourceHeap.pop(); // Push all stil valid elements to new heaps

      if (this._valueToPositionMap[element.value] !== undefined) {
        newSmallValues.push(element);
        newLargeValues.push(element);
      }
    }

    this._smallValues = newSmallValues;
    this._largeValues = newLargeValues;
  };

  _proto._cleanHeap = function _cleanHeap(
  /*object*/
  heap) {
    while (!heap.empty() && this._valueToPositionMap[heap.peek().value] === undefined) {
      heap.pop();
    }
  };

  _proto._smallerComparator = function _smallerComparator(
  /*object*/
  lhs,
  /*object*/
  rhs)
  /*boolean*/
  {
    return lhs.value < rhs.value;
  };

  _proto._greaterComparator = function _greaterComparator(
  /*object*/
  lhs,
  /*object*/
  rhs)
  /*boolean*/
  {
    return lhs.value > rhs.value;
  };

  return IntegerBufferSet;
}();

module.exports = IntegerBufferSet;