Reactive-Extensions/rx.priorityqueue

Name: rx.priorityqueue

Owner: Reactive Extensions

Description: Standalone Priority Queue from the RxJS library

Created: 2016-01-05 22:19:09.0

Updated: 2018-04-20 14:36:38.0

Pushed: 2016-01-07 19:42:54.0

Homepage: null

Size: 12

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Build Status GitHub version NPM version Downloads

rx.priorityqueue - Standalone PriorityQueue from the RxJS library

This is a standalone version of the RxJS PriorityQueue class. This class allows you to keep items in priority order based upon standard ordering or a custom ordering function that you provide. In RxJS, this is used heavily in the schedulers to ensure that all operations are executed in the precise order.

Installation

The rx.priorityqueue library can be installed by the following:

NPM
m install rx.priorityqueue
Usage

You can use the PriorityQueue to keep items in order such as the following:

t q = new PriorityQueue();

queue(42);
queue(41);
queue(44);
queue(43);

ole.log(`queue length is ${q.length}`);
> queue length is 4

ole.log(`dequeued value is ${q.dequeue()}`);
> First value is 41

ole.log(`dequeued value is ${q.dequeue()}`);
> dequeued value is 42

ole.log(`dequeued value is ${q.dequeue()}`);
> dequeued value is 43

ole.log(`dequeued value is ${q.dequeue()}`);
> dequeued value is 44

You can also use your own custom comparer for complex objects to determine order. To do that, simply pass in a function which takes two arguments, the left hand side and right hand side. By default, the comparer function which is used looks like the following where if the left is greater than it should be positive, less than, should be negative and equal should be zero. Else, if you want to have it on the object compared directly, simply implement compareTo on the object to be compared and then it will work.

tion defaultComparer (x, y) {
 (x > y) {
return 1;
else if (y > x) {
return -1;
else {
return 0;


Knowing this, we can now apply this to our custom object:

tion createItem(dueTime, action) {
turn {
dueTime,
action



tion comparer (x, y) { return x.dueTime - y.dueTime; }

q = new PriorityQueue(comparer);

queue(createItem(new Date(42), () => console.log(42)));
queue(createItem(new Date(41), () => console.log(41)));
queue(createItem(new Date(44), () => console.log(44)));
queue(createItem(new Date(43), () => console.log(43)));

queue().action();
> 41

queue().action();
> 42

queue().action();
> 43

queue().action();
> 44
Documentation
PriorityQueue constructor
PriorityQueue Instance Methods
PriorityQueue Instance Properties
PriorityQueue Constructor
PriorityQueue(comparer)

Creates a queue organized by priority based upon the comparer to determine order.

Arguments
  1. [comparer]: Function - The comparer used to determine order. If not specified this will default to determining order using greater than, less than.
Example
tion createItem(dueTime, action) {
turn {
dueTime,
action



tion comparer (x, y) { return x.dueTime - y.dueTime; }

q = new PriorityQueue(comparer);

queue(createItem(new Date(42), () => console.log(42)));

PriorityQueue Instance Methods
PriorityQueue.prototype.dequeue()

Removes the first item from the queue and returns the value.

Returns

Any - The first item in the priority queue.

Example
t q = new PriorityQueue();

queue(42);

ole.log(q.dequeue());
> 42

ole.log(q.length);
> 0

PriorityQueue.prototype.enqueue(item)

Adds an item to the priority queue which is then reordered based upon priority.

Arguments
  1. item: Any - The item to add to the priority queue.
Example
t q = new PriorityQueue();

queue(42);

ole.log(q.length);
> 1

PriorityQueue.prototype.peek()

Returns the first item in the priority queue.

Returns

Any - The first item in the priority queue.

Example
t q = new PriorityQueue();

queue(42);

ole.log(q.peek());
> 42

ole.log(q.length);
> 1

PriorityQueue.prototype.remove(item)

Removes the specified item from the priority queue.

Arguments
  1. item: Any - The item to remove from the priority queue.
Returns

Boolean - true if removed; else false.

Example
t q = new PriorityQueue();

queue(42);

ole.log(`Item was removed is ${q.dequeue()}`);
> Item was remove is true

PriorityQueue Instance Properties
length

Gets the length of the priority queue

Example
t q = new PriorityQueue();

queue(42);

ole.log(`Priority queue length is ${q.length}`);
> Priority queue length is 1

Contributing

We appreciate any contributions by the community as long as they abide by the Code of Conduct.

Want to get started? Here are some ways you can get involved.

  1. Documentation

    • Examples
    • How Do I?
    • API Documentation
  2. Code

    • Additional disposables
    • Unit tests

LICENSE

The MIT License (MIT)

Copyright (c) 2016 Microsoft Corporation

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.