How to use tinyqueue - 9 common examples

To help you get started, we’ve selected a few tinyqueue examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github openplannerteam / planner.js / src / pathfinding / dijkstra-tree / DijkstraTreeInstance.ts View on Github external
public async continue(maxCost: number): Promise<IPathTree> {
        const queue = new TinyQueue(this.nextQueue, (a, b) => a.duration - b.duration);
        this.nextQueue = [];

        while (queue.length) {
            const { position, distance, duration, cost } = queue.pop();

            if (duration > this.getCost(position)) {
                // we have already found a better way
                continue;
            }

            if (this.graph.getBreakPoint(position)) {
                await this.graph.getBreakPoint(position)(this.graph.getLabel(position));
            }

            if (cost > maxCost) {
                // remember this state for subsequent calls
github awesome-schedule / plannable / src / algorithm / Coloring.ts View on Github external
export function intervalScheduling(blocks: ScheduleBlock[], assignment: Int16Array) {
    if (blocks.length === 0) return 0;

    blocks.sort((b1, b2) => b1.startMin - b2.startMin); // sort by start time
    // min heap, the top element is the room whose end time is minimal
    // a room is represented as a pair: [end time, room index]
    const queue = new PriorityQueue<readonly [number, number]>(
        [[blocks[0].endMin, 0]],
        (r1, r2) => r1[0] - r2[0]
    );
    let numRooms = 0;
    assignment[0] = 0;
    for (let i = 1; i < blocks.length; i++) {
        const { startMin, endMin } = blocks[i];
        const [earliestEnd, roomIdx] = queue.peek()!;
        if (earliestEnd > startMin) {
            // conflict, need to add a new room
            numRooms += 1;
            queue.push([endMin, numRooms]);
            assignment[i] = numRooms;
        } else {
            queue.pop(); // update the room end time
            queue.push([endMin, roomIdx]);
github rowanwins / sweepline-intersections / src / main.js View on Github external
export default function bentleyOttmann (geojson) {
    const intersectionPoints = []
    const eventQueue = new TinyQueue([], checkWhichEventIsLeft);

    fillEventQueue(geojson, eventQueue)

    const outQueue = new TinyQueue([], checkWhichSegmentHasRightEndpointFirst);

    while (eventQueue.length) {
        const event = eventQueue.pop();
        if (event.isLeftEndpoint) {
            // debugEventAndSegments(event.p, outQueue.data)
            const segment = new Segment(event)
            for (let i = 0; i < outQueue.data.length; i++) {
                const intersection = testSegmentIntersect(segment, outQueue.data[i])
                if (intersection !== false) intersectionPoints.push(intersection)
            }
            outQueue.push(segment)
        } else if (event.isLeftEndpoint === false) {
            const seg = outQueue.pop()
            // debugRemovingSegment(event.p, seg)
        }
    }
github captbaritone / webamp / js / loadQueue.js View on Github external
constructor({ threads }) {
    // TODO: Consider not running items with zero priority
    // Priority is a function so that items can change their priority between
    // when their priority is evaluated.
    // For example, we might add a track to the playlist and then scroll to/away
    // from it before it gets processed.
    this._queue = new TinyQueue([], (a, b) => a.priority() - b.priority());
    this._availableThreads = threads;
  }
github moroshko / rxviz / lib / data-parser.js View on Github external
export const getModel = ({
  data,
  renderer,
  inheritMainColor = true,
  mergeThreshold
}) => {
  let result = {
    observables: [],
    connectors: []
  };

  if (!isObservable(data)) {
    return result;
  }

  let queue = new TinyQueue(
    [
      {
        observable: data,
        startTime: 0,
        mainColor: defaultMainColor
      }
    ],
    (obs1, obs2) => obs1.startTime - obs2.startTime
  );

  while (queue.length > 0) {
    const { observable, startTime, mainColor, fromIndex } = queue.pop();
    const observableIndex = result.observables.length;

    if (!isUndefined(fromIndex)) {
      result.connectors.push({
github openplannerteam / planner.js / src / pathfinding / dijkstra / DijkstraInstance.ts View on Github external
public async queryPath(from: string, to: string, maxDistance = Infinity) {
        let queue: TinyQueue<Float32Array>;
        if (this.useWeightedCost) {
            queue = new TinyQueue([], (a, b) => a[COST] - b[COST]);
        } else {
            queue = new TinyQueue([], (a, b) => a[DURATION] - b[DURATION]);
        }

        this.costs = this.costs.fill(Infinity);
        this.previousNodes = this.previousNodes.fill(undefined);

        const fromIndex = this.graph.getNodeIndex(from);
        this.setCost(fromIndex, 0);
        const state = new Float32Array(4);
        state[COST] = 0;
        state[POSITION] = fromIndex;
        state[DISTANCE] = 0;
        state[DURATION] = 0;
        queue.push(state);
github openplannerteam / planner.js / src / planner / public-transport / CSA / data-structure / MultiConnectionQueue.ts View on Github external
constructor(asyncIterator: AsyncIterator<IConnection>) {
        this.closed = false;
        this.asyncIterator = asyncIterator;
        this.queue = new TinyQueue([], (a, b) => {
            return a.departureTime - b.departureTime;
        });
    }
github openplannerteam / planner.js / src / pathfinding / bidirdijkstra / BidirDijkstraInstance.ts View on Github external
public async queryPath(from: string, to: string, maxDistance = Infinity) {
        let forwardQueue: TinyQueue<IState>;
        let backwardQueue: TinyQueue<IState>;
        if (this.useWeightedCost) {
            forwardQueue = new TinyQueue([], (a, b) => a.cost - b.cost);
            backwardQueue = new TinyQueue([], (a, b) => a.cost - b.cost);
        } else {
            forwardQueue = new TinyQueue([], (a, b) => a.duration - b.duration);
            backwardQueue = new TinyQueue([], (a, b) => a.duration - b.duration);
        }

        this.forwardCosts = this.forwardCosts.fill(Infinity);
        this.forwardParents = this.forwardParents.fill(undefined);
        this.backwardCosts = this.backwardCosts.fill(Infinity);
        this.backwardParents = this.backwardParents.fill(undefined);

        const fromIndex = this.graph.getNodeIndex(from);
        const toIndex = this.graph.getNodeIndex(to);
        this.setForwardCost(fromIndex, 0);
        this.setBackwardCost(toIndex, 0);
        forwardQueue.push({ distance: 0, duration: 0, cost: 0, position: fromIndex });
        backwardQueue.push({ distance: 0, duration: 0, cost: 0, position: toIndex });

        while (forwardQueue.length && backwardQueue.length) {
            const [forwardResult, backwardResult] = await Promise.all([
github openplannerteam / planner.js / src / pathfinding / bidirdijkstra / BidirDijkstraInstance.ts View on Github external
public async queryPath(from: string, to: string, maxDistance = Infinity) {
        let forwardQueue: TinyQueue<IState>;
        let backwardQueue: TinyQueue<IState>;
        if (this.useWeightedCost) {
            forwardQueue = new TinyQueue([], (a, b) => a.cost - b.cost);
            backwardQueue = new TinyQueue([], (a, b) => a.cost - b.cost);
        } else {
            forwardQueue = new TinyQueue([], (a, b) => a.duration - b.duration);
            backwardQueue = new TinyQueue([], (a, b) => a.duration - b.duration);
        }

        this.forwardCosts = this.forwardCosts.fill(Infinity);
        this.forwardParents = this.forwardParents.fill(undefined);
        this.backwardCosts = this.backwardCosts.fill(Infinity);
        this.backwardParents = this.backwardParents.fill(undefined);

        const fromIndex = this.graph.getNodeIndex(from);
        const toIndex = this.graph.getNodeIndex(to);
        this.setForwardCost(fromIndex, 0);
        this.setBackwardCost(toIndex, 0);
        forwardQueue.push({ distance: 0, duration: 0, cost: 0, position: fromIndex });

tinyqueue

The smallest and simplest JavaScript priority queue

ISC
Latest version published 3 years ago

Package Health Score

65 / 100
Full package analysis

Popular tinyqueue functions