Latest 0.3
Homepage https://github.com/eisber/Swift-PriorityQueue
License MIT
Platforms ios 8.0, osx 10.9, requires ARC
Authors , ,

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

  • CFBinaryHeap (once CFunctionPointer becomes available)
  • Swift-DataStructures/MinPQ

Development

Playground

Rebuild this playground with the following command:

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

Latest podspec

{
    "name": "PriorityQueue",
    "version": "0.3",
    "license": "MIT",
    "summary": "Priority Queue in Swift",
    "homepage": "https://github.com/eisber/Swift-PriorityQueue",
    "source": {
        "git": "https://github.com/eisber/Swift-PriorityQueue.git",
        "tag": "0.3"
    },
    "authors": {
        "Bouke Haarsma": "",
        "Julien Dauphant": "",
        "Markus Cozowicz": "[email protected]"
    },
    "platforms": {
        "ios": "8.0",
        "osx": "10.9"
    },
    "source_files": "PriorityQueue/*.swift",
    "requires_arc": true
}

Pin It on Pinterest

Share This