Skip to content
Snippets Groups Projects
Select Git revision
  • issue-703
  • master default
  • issue-752
  • develop
  • issue-677
  • issue-677-original-with-migrate
  • issue-716
  • issue-654
  • issue-732
  • issue-737
  • issue-735
  • issue-707
  • issue-706
  • issue-705
  • issue-696
  • issue-690
  • issue-675
  • issue-670
  • issue-635
  • issue-404
  • 7.19
  • 2012-04-18
  • 2012-04-03
  • 2012-04-02
  • 2012-03-01
  • 2012-02-07
  • 20120207
  • 2012-01-13
  • 2012-01-12
  • 2011-12-16
  • 2011-12-05
  • 2011-11-17
  • 2011-11-14
  • 2011-11-08.2
  • 2011-11-08
  • 2011-11-01
  • 2011-10-27
  • 2011-10-06
  • 2011-10-03
  • 2011-09-19
40 results

registry.inc

Blame
  • Forked from UNL Information Services / UNL-CMS
    Source project has a limited visibility.
    collections.js 1.32 KiB
    export class PriorityQueue {
      constructor() {
        this._vertices = [];
      }
    
      get size() {
        return this._vertices.length;
      }
    
      insert(element, measure) {
        let index = this._vertices.length;
        while (index > 0) {
          const parentIndex = Math.floor((index - 1) / 2);
          const parent = this._vertices[parentIndex];
          if (parent.measure < measure) {
            break;
          }
          this._vertices[index] = parent;
          index = parentIndex;
        }
        this._vertices[index] = {
          element,
          measure,
        };
      }
    
      remove() {
        console.assert(this._vertices.length > 0, 'Cannot remove an element from an empty priority queue');
        const result = this._vertices[0].element;
        const vertex = this._vertices[this._vertices.length - 1];
        for (let index = 0; ;) {
          this._vertices[index] = vertex;
          let swapIndex = index;
          for (const candidateIndex of [2 * index + 1, 2 * index + 2]) {
            if (candidateIndex < this._vertices.length - 1 &&
                this._vertices[candidateIndex].measure < this._vertices[swapIndex].measure) {
              swapIndex = candidateIndex;
            }
          }
          if (swapIndex === index) {
            this._vertices[index] = vertex;
            this._vertices.pop();
            return result;
          }
          this._vertices[index] = this._vertices[swapIndex];
          index = swapIndex;
        }
      }
    }