All files / src/kdbush tinyqueue.ts

90.9% Statements 40/44
50% Branches 7/14
85.71% Functions 6/7
93.02% Lines 40/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                    2x                     44x 44x 44x   44x               120x 120x       44x 2x     42x 42x   42x   42x 42x 42x     42x       42x       120x 120x   120x 83x 83x 83x 46x   37x 37x     120x       42x 42x 42x   42x 27x 27x   27x 1x     27x 13x     14x 14x     42x      
// Adapted from https://github.com/mourner/kdbush
 
// ISC License
 
// Copyright (c) 2017, Vladimir Agafonkin
 
// Permission to use, copy, modify, and/or distribute this software for any purpose
// with or without fee is hereby granted, provided that the above copyright notice
// and this permission notice appear in all copies.
 
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
// OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
// THIS SOFTWARE.
 
export default class TinyQueue<T> {
 
    private data: T[];
    public length: number;
    private compare: (a: T, b: T) => number;
 
    constructor(
        data: T[] = [],
        compare: (a: T, b: T) => number = (a, b) =>
            a < b ? -1 : a > b ? 1 : 0
    ) {
        this.data = data;
        this.length = this.data.length;
        this.compare = compare;
 
        Iif (this.length > 0) {
            for (let i = (this.length >> 1) - 1; i >= 0; i--) {
                this._down(i);
            }
        }
    }
 
    push(item: T): void {
        this.data.push(item);
        this._up(this.length++);
    }
 
    pop(): T | undefined {
        if (this.length === 0) {
            return undefined;
        }
 
        const top = this.data[0];
        const bottom = this.data.pop() as T;
 
        this.length--;
 
        if (this.length > 0) {
            this.data[0] = bottom;
            this._down(0);
        }
 
        return top;
    }
 
    peek(): T | undefined {
        return this.data[0];
    }
 
    private _up(pos: number): void {
        const { data, compare } = this;
        const item = data[pos];
 
        while (pos > 0) {
            const parent = (pos - 1) >> 1;
            const current = data[parent];
            if (compare(item, current) >= 0) {
                break;
            }
            data[pos] = current;
            pos = parent;
        }
 
        data[pos] = item;
    }
 
    private _down(pos: number): void {
        const { data, compare } = this;
        const halfLength = this.length >> 1;
        const item = data[pos];
 
        while (pos < halfLength) {
            let bestChild = (pos << 1) + 1;
            const right = bestChild + 1;
 
            if (right < this.length && compare(data[right], data[bestChild]) < 0) {
                bestChild = right;
            }
 
            if (compare(data[bestChild], item) >= 0) {
                break;
            }
 
            data[pos] = data[bestChild];
            pos = bestChild;
        }
 
        data[pos] = item;
    }
}