Code Monkey home page Code Monkey logo

lpriorityqueue's Introduction

Build Status license

Lua priority queue

Lua implementation of priority queue data structure using indirect binary heap( which are basically binary heaps with efficient search). Indirect heaps offer efficient removal and updating priority operations. This implementation is based on binaryheap libary with some changed design.

PriorityQueue.new( [array/ordering] )

Create new priority queue. You can pass array to initialize queue with O(n) complexity (implemented with batchenq, see below). First argument also could be an ordering function defining higher priority or you can simply pass "min" for min-heap( default behavior ) or "max" for max-heap (also array can contain higherpriority field).

Min heap:

local q = PriorityQueue.new()
local q = PriorityQueue.new('min') 

Max heap using function notation:

local q = PriorityQueue( 'max' )

Array initialization:

local security = PriorityQueue{ 
	'high', 1, 
	'low', 10, 
	'moderate', 5, 
	'moderate-', 7, 
	'moderate+', 3,
}

Array initialization with max-heap ordering:

local security = PriorityQueue{
	higherpriority = 'max',
	'high', 10, 
	'low', 1, 
	'moderate', 5, 
	'moderate-', 3, 
	'moderate+', 7,
}

Custom comparator:

local security = PriorityQueue{
	higherpriority = function(a,b)
		return a.level > b.level
	end,
	'high', {level = 10}, 
	'low', {level = 1}, 
	'moderate', {level = 5}, 
	'moderate-', {level = 3}, 
	'moderate+', {level = 7},
}

enqueue( item, priority )

Enqueue the item with the priority to the heap. The priority must be comparable if you use builtin comparators, i.e. it must be either number or string or a table with metatable with __lt metamethod defined. Otherwise you have to define custom comparator. Time complexity is O(logn).

dequeue()

Dequeue from the heap. Returns item and associated priority. If the heap is empty then an error will raise. Returns an item with highest priority. Time complexity is O(logn).

peek()

Returns the item with minimal priority and priority itself for BinaryMinHeap(maximal for BinaryMaxHeap) or nil if the heap is empty.

len()

Returns items count. Also you can use # operator for the same effect.

empty()

Returns true if the heap has no items and false otherwise.

batchenq( iparray )

Efficiently enqueues list of item-priority pairs into the heap. Note that this is efficient only when the amount of inserting elements greater or equal than the current length. Time complexity of this operation is O(n)(sequential approach is O(nlogn)).

contains( item )

Checking that heap contains the item. This operation is O(1).

remove( item )

Removes the item from the heap. Returns true if item was in the heap and false otherwise. This operation is O(logn).

update( item, priority )

Changes item priority. Returns true if item was in the queue (even if priority not changed) and false otherwise. This operation is O(logn), internally it's just remove followed by enqueue.

lpriorityqueue's People

Contributors

iskolbin avatar

Stargazers

Josip avatar Jack O'Sullivan avatar holderman avatar logisiti avatar YangJun avatar  avatar sora avatar

Watchers

James Cloos avatar  avatar

lpriorityqueue's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.