# all files / ol/structs/ priorityqueue.js

97.39% Statements 112/115
100% Branches 12/12
93.75% Functions 15/16
97.39% Lines 112/115

    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
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    3021×

    1×
    1×

    1×

    138×

    138×

    138×

    138×

    138×

    1×

    1×
    37×
    37×
    37×
    37×
    37×
    37×
    298×
    298×

    298×

    1×

    1×
    158×
    158×

    157×
    157×
    157×
    8×
    8×

    149×
    149×
    149×

    157×
    157×

    157×
    157×

    1×
    217×

    216×
    216×
    215×
    215×
    215×
    215×
    215×

    1×

    1×
    52×

    1×
    1272×

    1×
    1272×

    1×
    679×

    1×
    6×
    6×
    150×

    1×
    194×

    1×
    18×

    1×
    16×

    1×
    299×
    299×
    299×
    299×
    299×
    299×

    299×
    676×
    676×

    676×

    676×
    676×
    676×

    299×
    299×
    299×

    1×
    514×
    514×
    514×
    514×

    514×
    679×
    679×
    256×
    256×
    256×

    423×

    514×
    514×

    1×
    4×
    4×
    4×
    4×
    4×
    4×
    4×
    166×
    166×
    166×
    66×

    100×
    100×

    4×
    4×
    4×

     goog.provide('ol.structs.PriorityQueue');

    goog.require('goog.asserts');
    goog.require('goog.object');

    /**
     * Priority queue.
     *
     * The implementation is inspired from the Closure Library's Heap class and
     * Python's heapq module.
     *
     * @see http://closure-library.googlecode.com/svn/docs/closure_goog_structs_heap.js.source.html
     * @see http://hg.python.org/cpython/file/2.7/Lib/heapq.py
     *
     * @constructor
     * @param {function(T): number} priorityFunction Priority function.
     * @param {function(T): string} keyFunction Key function.
     * @struct
     * @template T
     */
    ol.structs.PriorityQueue = function(priorityFunction, keyFunction) {

      /**
       * @type {function(T): number}
       * @private
       */
      this.priorityFunction_ = priorityFunction;

      /**
       * @type {function(T): string}
       * @private
       */
      this.keyFunction_ = keyFunction;

      /**
       * @type {Array.<T>}
       * @private
       */
      this.elements_ = [];

      /**
       * @type {Array.<number>}
       * @private
       */
      this.priorities_ = [];

      /**
       * @type {Object.<string, boolean>}
       * @private
       */
      this.queuedElements_ = {};

    };

    /**
     * @const
     * @type {number}
     */
    ol.structs.PriorityQueue.DROP = Infinity;

    /**
     * FIXME empty description for jsdoc
     */
    ol.structs.PriorityQueue.prototype.assertValid = function() {
      var elements = this.elements_;
      var priorities = this.priorities_;
      var n = elements.length;
      goog.asserts.assert(priorities.length == n);
      var i, priority;
      for (i = 0; i < (n >> 1) - 1; ++i) {
        priority = priorities[i];
        goog.asserts.assert(priority <= priorities[this.getLeftChildIndex_(i)],
            'priority smaller than or equal to priority of left child (%s <= %s)',
            priority, priorities[this.getLeftChildIndex_(i)]);
        goog.asserts.assert(priority <= priorities[this.getRightChildIndex_(i)],
            'priority smaller than or equal to priority of right child (%s <= %s)',
            priority, priorities[this.getRightChildIndex_(i)]);
      }
    };

    /**
     * FIXME empty description for jsdoc
     */
    ol.structs.PriorityQueue.prototype.clear = function() {
      this.elements_.length = 0;
      this.priorities_.length = 0;
      goog.object.clear(this.queuedElements_);
    };

    /**
     * Remove and return the highest-priority element. O(log N).
     * @return {T} Element.
     */
    ol.structs.PriorityQueue.prototype.dequeue = function() {
      var elements = this.elements_;
      goog.asserts.assert(elements.length > 0,
          'must have elements in order to be able to dequeue');
      var priorities = this.priorities_;
      var element = elements[0];
      if (elements.length == 1) {
        elements.length = 0;
        priorities.length = 0;
      } else {
        elements[0] = elements.pop();
        priorities[0] = priorities.pop();
        this.siftUp_(0);
      }
      var elementKey = this.keyFunction_(element);
      goog.asserts.assert(elementKey in this.queuedElements_,
          'key %s is not listed as queued', elementKey);
      delete this.queuedElements_[elementKey];
      return element;
    };

    /**
     * Enqueue an element. O(log N).
     * @param {T} element Element.
     * @return {boolean} The element was added to the queue.
     */
    ol.structs.PriorityQueue.prototype.enqueue = function(element) {
      goog.asserts.assert(!(this.keyFunction_(element) in this.queuedElements_),
          'key %s is already listed as queued', this.keyFunction_(element));
      var priority = this.priorityFunction_(element);
      if (priority != ol.structs.PriorityQueue.DROP) {
        this.elements_.push(element);
        this.priorities_.push(priority);
        this.queuedElements_[this.keyFunction_(element)] = true;
        this.siftDown_(0, this.elements_.length - 1);
        return true;
      }
      return false;
    };

    /**
     * @return {number} Count.
     */
    ol.structs.PriorityQueue.prototype.getCount = function() {
      return this.elements_.length;
    };

    /**
     * Gets the index of the left child of the node at the given index.
     * @param {number} index The index of the node to get the left child for.
     * @return {number} The index of the left child.
     * @private
     */
    ol.structs.PriorityQueue.prototype.getLeftChildIndex_ = function(index) {
      return index * 2 + 1;
    };

    /**
     * Gets the index of the right child of the node at the given index.
     * @param {number} index The index of the node to get the right child for.
     * @return {number} The index of the right child.
     * @private
     */
    ol.structs.PriorityQueue.prototype.getRightChildIndex_ = function(index) {
      return index * 2 + 2;
    };

    /**
     * Gets the index of the parent of the node at the given index.
     * @param {number} index The index of the node to get the parent for.
     * @return {number} The index of the parent.
     * @private
     */
    ol.structs.PriorityQueue.prototype.getParentIndex_ = function(index) {
      return (index - 1) >> 1;
    };

    /**
     * Make this a heap. O(N).
     * @private
     */
    ol.structs.PriorityQueue.prototype.heapify_ = function() {
      var i;
      for (i = (this.elements_.length >> 1) - 1; i >= 0; i--) {
        this.siftUp_(i);
      }
    };

    /**
     * @return {boolean} Is empty.
     */
    ol.structs.PriorityQueue.prototype.isEmpty = function() {
      return this.elements_.length === 0;
    };

    /**
     * @param {string} key Key.
     * @return {boolean} Is key queued.
     */
    ol.structs.PriorityQueue.prototype.isKeyQueued = function(key) {
      return key in this.queuedElements_;
    };

    /**
     * @param {T} element Element.
     * @return {boolean} Is queued.
     */
    ol.structs.PriorityQueue.prototype.isQueued = function(element) {
      return this.isKeyQueued(this.keyFunction_(element));
    };

    /**
     * @param {number} index The index of the node to move down.
     * @private
     */
    ol.structs.PriorityQueue.prototype.siftUp_ = function(index) {
      var elements = this.elements_;
      var priorities = this.priorities_;
      var count = elements.length;
      var element = elements[index];
      var priority = priorities[index];
      var startIndex = index;

      while (index < (count >> 1)) {
        var lIndex = this.getLeftChildIndex_(index);
        var rIndex = this.getRightChildIndex_(index);

        var smallerChildIndex = rIndex < count &&
            priorities[rIndex] < priorities[lIndex] ?
            rIndex : lIndex;

        elements[index] = elements[smallerChildIndex];
        priorities[index] = priorities[smallerChildIndex];
        index = smallerChildIndex;
      }

      elements[index] = element;
      priorities[index] = priority;
      this.siftDown_(startIndex, index);
    };

    /**
     * @param {number} startIndex The index of the root.
     * @param {number} index The index of the node to move up.
     * @private
     */
    ol.structs.PriorityQueue.prototype.siftDown_ = function(startIndex, index) {
      var elements = this.elements_;
      var priorities = this.priorities_;
      var element = elements[index];
      var priority = priorities[index];

      while (index > startIndex) {
        var parentIndex = this.getParentIndex_(index);
        if (priorities[parentIndex] > priority) {
          elements[index] = elements[parentIndex];
          priorities[index] = priorities[parentIndex];
          index = parentIndex;
        } else {
          break;
        }
      }
      elements[index] = element;
      priorities[index] = priority;
    };

    /**
     * FIXME empty description for jsdoc
     */
    ol.structs.PriorityQueue.prototype.reprioritize = function() {
      var priorityFunction = this.priorityFunction_;
      var elements = this.elements_;
      var priorities = this.priorities_;
      var index = 0;
      var n = elements.length;
      var element, i, priority;
      for (i = 0; i < n; ++i) {
        element = elements[i];
        priority = priorityFunction(element);
        if (priority == ol.structs.PriorityQueue.DROP) {
          delete this.queuedElements_[this.keyFunction_(element)];
        } else {
          priorities[index] = priority;
          elements[index++] = element;
        }
      }
      elements.length = index;
      priorities.length = index;
      this.heapify_();
    };