Coder Social home page Coder Social logo

swift-priorityqueue's Introduction

Swift-PriorityQueue

When included as a framework, import it as any other framework:

import PriorityQueue

The priority queue is defined as a generic and initialized with a comparison callback, like PriorityQueue<T>((T, T) -> Bool). For example, it can operate on characters like this:

var characters = PriorityQueue<Character>(<)
characters.push("C")
characters.push("B")
characters.push("A")

println("Characters:")
for p in characters {
    println(" * \(p)")
}
println()

This would print:

 * A
 * B
 * C

A more real-world use-case would operate on structs or classes, like this:

struct Node {
    let priority: Int
}

var nodes = PriorityQueue<Node>({ $0.priority < $1.priority })
nodes.push(Node(priority: 4))
nodes.push(Node(priority: 5))
nodes.push(Node(priority: 3))
nodes.push(Node(priority: 1))

println("Nodes:")
for node in nodes {
    println(" * Node(priority: \(node.priority))")
}
println()

This would print:

Nodes:
* Node(priority: 1)
* Node(priority: 3)
* Node(priority: 4)
* Node(priority: 5)

Removing items

var ints = PriorityQueue<Int>(<)
ints.push(3)
ints.remove(3)  // Returns 3
ints.remove(3)  // Returns nil

Inspecting the heap

ints.push(5)
ints.push(4)
ints.heap  // Returns [4, 5]

Alternatives

Development

Playground

Rebuild this playground with the following command:

rm -rf README.playground/
playground README.md --platform ios

Credits

This library was written by Bouke Haarsma.

swift-priorityqueue's People

Contributors

bouke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

swift-priorityqueue's Issues

Generator should not mutate the PriorityQueue

extension PriorityQueue: GeneratorType { public typealias Element = T public func next() -> Element? { return pop() } }

Just iterating the elements causes the PriorityQueue to be cleared out.
If you use any of the swift functions that iterate over SequenceType, functions like contains() the queue also gets cleared.

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.