GithubHelp home page GithubHelp logo

jacks205 / swift-priorityqueue Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bouke/swift-priorityqueue

0.0 1.0 0.0 22 KB

Binary heap implementation in Swift

License: MIT License

Swift 100.00%

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

  • 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

swift-priorityqueue's People

Contributors

bouke avatar

Watchers

 avatar

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.