Buckets

Swift Collection Data Structures Library


Keywords
bimap, bitarray, bloom-filter, carthage, circular-buffer, cocoapods, deque, graph, matrix, multimap, multiset, priority-queue, queue, stack, swift, swift-3, swift-package-manager, trie
License
MIT
Install
pod try Buckets

Documentation

Buckets

Build Status CocoaPods Compatible Carthage Compatible Platform

Swift Collection Data Structures Library

Buckets is a complete, tested and documented collections library for swift.

Requirements

  • iOS 8.0+/OS X 10.10+/watchOS/tvOS
  • Xcode 7.3+

Included collections

Setup

CocoaPods

CocoaPods is a dependency manager for Swift and Objective-C projects. The latest version adds support for embedded frameworks. You can install it with the following command:

$ gem install cocoapods

To integrate Buckets into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

pod 'Buckets', '~> 1.2.0'

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your application.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate Buckets into your Xcode project using Carthage, specify it in your Cartfile:

github "mauriciosantos/Buckets-Swift" >= 1.2.0

Manually

You can also integrate Buckets into your project manually:

  • Download the latest release and unzip it in your project's folder.
  • Open the Buckets folder, and drag Buckets.xcodeproj into the file navigator of your app project. This means inside your project, not at the top level.
  • Ensure that the deployment target of Buckets.framework matches that of the application target.
  • Open your project's "Build Phases" panel. Expand the "Target Dependencies" group, and add Buckets.framework.
  • Click on the + button at the top left of the panel and select "New Copy Files Phase". Set the "Destination" to "Frameworks", and add Buckets.framework. There are 2 versions, one for iOS and one for OS X. Select the right one.

Usage

All collection types are implemented as structures. This means they are copied when they are assigned to a new constant or variable, or when they are passed to a function or method.

You shouldn't worry about copying structs:

The behavior you see in your code will always be as if a copy took place. However, Swift only performs an actual copy behind the scenes when it is absolutely necessary to do so. Swift manages all value copying to ensure optimal performance, and you should not avoid assignment to try to preempt this optimization.

Buckets collection types are optimized in the same way.

Walkthrough

import Buckets

var queue = Queue<String>()
queue.enqueue("first")
queue.enqueue("last")
queue.dequeue() // "first"

var deque = Deque<String>()
deque.enqueueLast("last")
deque.enqueueFirst("first")
deque.dequeueFirst() // "first"

var stack = Stack<String>()
stack.push("first")
stack.push("last")
stack.pop() // "last"

var pQueue = PriorityQueue<Int>(<)
pQueue.enqueue(3)
pQueue.enqueue(1)
pQueue.enqueue(2)
pQueue.dequeue() // 1

var multiset = Multiset<String>()
multiset.insert("a")
multiset.insert("b")
multiset.insert("a")
multiset.distinctCount // 2
multiset.count("a") // 2

var multimap = Multimap<String, Int>()
multimap.insertValue(1, forKey: "a")
multimap.insertValue(5, forKey: "a")
multimap["a"] // [1, 5]

var bimap = Bimap<String, Int>()
bimap[key: "a"] = 1
bimap[value: 3] = "b"
bimap[value: 1] // "a"
bimap[key: "b"] // 3

var graph = Graph<String, Int>()
graph["Boston", "NY"] = 5
graph["NY", "Miami"] = 5
graph.pathFrom("Boston", to: "Miami") // ["Boston", "NY", "Miami"]

var trie = Trie()
trie.insert("Apple")
trie.insert("App Store")
trie.findPrefix("App") // ["App Store", "Apple"]

var matrix: Matrix<Double> = [[1,2,3], [4,5,6]]
matrix[1, 0] = 5
matrix - [[1,0,1], [1,0,1]] // [[0,2,2],[4,5,5]]

var bitArray: BitArray = [true, false]
bitArray.append(true)
bitArray.cardinality // 2

var circArray = CircularArray<Int>()
circArray.append(1)
circArray.prepend(2)
circArray.first // 2

var bFilter = BloomFilter<String>(expectedCount: 100)
bFilter.insert("a")
bFilter.contains("a") // true

Read the documentation.

Contact

Mauricio Santos, mauriciosantoss@gmail.com