Algorithm is a library of tools that is used to create intelligent applications.

Overview

Algorithm

Welcome to Algorithm

Algorithm is a library of tools that is used to create intelligent applications.

Features

  • Probability Tools
  • Expected Value
  • Programmable Probability Blocks
  • Array Extensions
  • Set Extensions

Data Structures

  • DoublyLinkedList
  • Stack
  • Queue
  • Deque
  • RedBlackTree
  • SortedSet
  • SortedMultiSet
  • SortedDictionary
  • SortedMultiDictionary

Requirements

  • iOS 8.0+ / Mac OS X 10.9+
  • Xcode 8.0+

Communication

  • If you need help, use Stack Overflow. (Tag 'cosmicmind')
  • If you'd like to ask a general question, use Stack Overflow.
  • If you found a bug, and can provide steps to reliably reproduce it, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

Installation

Embedded frameworks require a minimum deployment target of iOS 8.

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate Algorithm's core features 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 'Algorithm', '~> 3.1.0'

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

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

github "CosmicMind/Algorithm"

Run carthage update to build the framework and drag the built Algorithm.framework into your Xcode project.

Changelog

Algorithm is a growing project and will encounter changes throughout its development. It is recommended that the Changelog be reviewed prior to updating versions.

Samples

The following are samples to see how Algorithm may be used within your applications.

  • Visit the Samples repo to see example projects using Algorithm.

Probability

Each data structure within Algorithm is equipped with probability tools.

Basic Probability

For example, determining the probability of rolling a 3 using a die of 6 numbers.

let die = [Int](arrayLiteral: 1, 2, 3, 4, 5, 6)

if 0.1 < die.probability(of: 3) 
		// Do something ...
}

Conditional Probability

For conditional probabilities that require a more complex calculation, use block statements.

let die = [Int](arrayLiteral: 1, 2, 3, 4, 5, 6)

let pOfX = die.probability { (number) in
	return 5 < number || 0 == number % 3
}

if 0.33 < pOfX {
	// Do something ...
}

Expected Value

The expected value of rolling a 3 or 6 with 100 trials using a die of 6 numbers.

let die = [Int](arrayLiteral: 1, 2, 3, 4, 5, 6)

if 20 < die.expectedValue(trials: 100, for: 3, 6) {
		// Do something ...
}

DoublyLinkedList

The DoublyLinkedList data structure is excellent for large growing collections of data. Below is an example of its usage.

var listA = DoublyLinkedList<Int>()
        
listA.insert(atFront: 3)
listA.insert(atFront: 2)
listA.insert(atFront: 1)

var listB = DoublyLinkedList<Int>()

listB.insert(atBack: 4)
listB.insert(atBack: 5)
listB.insert(atBack: 6)

var listC = listA + listB

listC.cursorToFront()

var value = listC.cursor

while nil != value {
    // Do something ...
    
    value = listC.next()
}

Stack

The Stack data structure is a container of objects that are inserted and removed according to the last-in-first-out (LIFO) principle. Below is an example of its usage.

var stack = Stack<Int>()

stack.push(1)
stack.push(2)
stack.push(3)

while !stack.isEmpty {
	let value = stack.pop()
	
	// Do something ...
}

Queue

The Queue data structure is a container of objects that are inserted and removed according to the first-in-first-out (FIFO) principle. Below is an example of its usage.

var queue = Queue<Int>()

queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)

while !queue.isEmpty {
    let value = queue.dequeue()

    // Do something ...
}

Deque

The Deque data structure is a container of objects that are inserted and removed according to the first-in-first-out (FIFO) and last-in-first-out (LIFO) principle. Essentially, a Deque is a Stack and Queue combined. Below are examples of its usage.

var dequeA = Deque<Int>()
dequeA.insert(atBack: 1)
dequeA.insert(atBack: 2)
dequeA.insert(atBack: 3)

while !dequeA.isEmpty {
	let value = dequeA.removeAtFront()
	
	// Do something ...
}

var dequeB = Deque<Int>()
dequeB.insert(atBack: 4)
dequeB.insert(atBack: 5)
dequeB.insert(atBack: 6)

while !dequeB.isEmpty {
	let value = dequeB.removeAtFront()
	
	// Do something ...
}

RedBlackTree

A RedBlackTree is a Balanced Binary Search Tree that maintains insert, remove, update, and search operations in a complexity of O(logn). The following implementation of a RedBlackTree also includes an order-statistic, which allows the data structure to be accessed using subscripts like an array or dictionary. RedBlackTrees may store unique keys or non-unique key values. Below is an example of its usage.

var ages = RedBlackTree<String, Int>(uniqueKeys: true)

ages.insert(value: 16, for: "Sarah")
ages.insert(value: 12, for: "Peter")
ages.insert(value: 23, for: "Alex")

let node = ages[1]

if "Peter" == node.key {
    // Do something ...
}

SortedSet

SortedSets are a powerful data structure for algorithm and analysis design. Elements within a SortedSet are unique and insert, remove, and search operations have a complexity of O(logn). The following implementation of a SortedSet also includes an order-statistic, which allows the data structure to be accessed using an index subscript like an array. Below are examples of its usage.

let setA = SortedSet<Int>(elements: 1, 2, 3)
let setB = SortedSet<Int>(elements: 4, 3, 6)
let setC = SortedSet<Int>(elements: 7, 1, 2)
let setD = SortedSet<Int>(elements: 1, 7)
let setE = SortedSet<Int>(elements: 1, 6, 7)

// Union.
setA + setB
setA.union(setB)

// Intersection.
setC.intersection(setD)

// Subset.
setD < setC
setD.isSubset(of: setC)

// Superset.
setD > setC
setD.isSuperset(of: setC)

// Contains.
setE.contains(setA.first!)

// Probability.
setE.probability(of: setA.first!, setA.last!)

SortedMultiSet

A SortedMultiSet is identical to a SortedSet, except that a SortedMultiSet allows non-unique elements. Look at SortedSet for examples of its usage.

SortedDictionary

A SortedDictionary is a powerful data structure that maintains a sorted set of keys with value pairs. Keys within a SortedDictionary are unique and insert, remove, update, and search operations have a complexity of O(logn).

SortedMultiDictionary

A SortedMultiDictionary is identical to a SortedDictionary, except that a SortedMultiDictionary allows non-unique keys. Below is an example of its usage.

struct Student {
    var name: String
}

let sarah = Student(name: "Sarah")
let peter = Student(name: "Peter")
let alex = Student(name: "Alex")

var students = SortedMultiDictionary<String, Student>()

students.insert(value: sarah, for: sarah.name)
students.insert(value: peter, for: peter.name)
students.insert(value: alex, for: alex.name)

for student in students {
    // Do something ...
}

License

The MIT License (MIT)

Copyright (C) 2019, CosmicMind, Inc. http://cosmicmind.com. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Comments
  • Xcode 9 build failures

    Xcode 9 build failures

    There are some build failures when trying to build with Xcode 9 Beta 2, for example:

    • 'Collection' requires the types 'Key' and '(key: Key, value: Value?)' be equivalent
    • 'Sequence' requires the types 'Key' and '(key: Key, value: Value?)' be equivalent

    The project is still set to Swift 3 (3.2).

    help wanted 
    opened by plu 8
  • Defining sorting criteria in SortedDictionary

    Defining sorting criteria in SortedDictionary

    Is it possible defining a sorting criteria in SortedDictionary? I'm using this data structure but i need to sort elements in order descending directly.

    help wanted 
    opened by andreadelfante 4
  • Update code documentation comments to the newer style

    Update code documentation comments to the newer style

    This style comments:

     /**
      :name: removeValueForKey
      :description: Removes a single instance of a value for a key. This is
      important when using non-unique keys.
      - returns: Value?
     */
    public func removeInstanceValueForKey(_ key: Key) -> Value? {}
    

    Should be updated to the newer style:

     /**
      Removes a single instance of a value for a key. This is
      important when using non-unique keys.
      - Parameter _ key: A Key.
      - Returns: Removed Value for given key, nil on fail.
     */
    public func removeInstanceValueForKey(_ key: Key) -> Value? {}
    
    enhancement algorithm 
    opened by OrkhanAlikhanov 1
  • Add a Gitter chat badge to README.md

    Add a Gitter chat badge to README.md

    CosmicMind/Algorithm now has a Chat Room on Gitter

    @danieldahan has just created a chat room. You can visit it here: https://gitter.im/CosmicMind/Algorithm.

    This pull-request adds this badge to your README.md:

    Gitter

    If my aim is a little off, please let me know.

    Happy chatting.

    PS: Click here if you would prefer not to receive automatic pull-requests from Gitter in future.

    opened by gitter-badger 0
  • Add a Gitter chat badge to README.md

    Add a Gitter chat badge to README.md

    CosmicMind/Algorithm now has a Chat Room on Gitter

    @danieldahan has just created a chat room. You can visit it here: https://gitter.im/CosmicMind/Algorithm.

    This pull-request adds this badge to your README.md:

    Gitter

    If my aim is a little off, please let me know.

    Happy chatting.

    PS: Click here if you would prefer not to receive automatic pull-requests from Gitter in future.

    opened by gitter-badger 0
  • Discussion about dropping some collections

    Discussion about dropping some collections

    Let's leverage instead of re-write Apple collections: https://github.com/apple/swift-collections.git

    I want to add these collections by adding swift-collections as a dependency.

    Deque<Element>, a double-ended queue backed by a ring buffer. Deques are range-replaceable, mutable, random-access collections.
    
    OrderedSet<Element>, a variant of the standard Set where the order of items is well-defined and items can be arbitrarily reordered. Uses a ContiguousArray as its backing store, augmented by a separate hash table of bit packed offsets into it.
    
    OrderedDictionary<Key, Value>, an ordered variant of the standard Dictionary, providing similar benefits.
    

    In lieu of:

    • Deque
    • SortedDictionary
    enhancement 
    opened by adamdahan 2
Releases(3.1.1)
Owner
Cosmicmind
Build Dreams Together
Cosmicmind
The Binary Search Algorithm in Swift 🤯 👨🏻‍💻

Binary Search in Swift Binary search is a simple algorithm that lets you search an array for a specific value. It’s trivial to implement in Swift, whi

Sergey Lukaschuk 1 Nov 16, 2021
Commonly used data structures for Swift

Swift Collections is an open-source package of data structure implementations for the Swift programming language.

Apple 2.7k Jan 5, 2023
Examples of commonly used data structures and algorithms in Swift.

Swift Structures This project provides a framework for commonly used data structures and algorithms written in a new iOS development language called S

Wayne Bishop 2.1k Dec 28, 2022
RandMyMod base on your own struct or class create one or a set of instance, which the variable's value in the instance is automatic randomized.

RandMyMod is an IOS Native Framework helps you generate one or a set of variable base on your own model. No matter your model is Class / Struct. Insta

郭介騵 17 Sep 24, 2022
Simple diff library in pure Swift

Diff Simple diffing library in pure Swift. Installing You can use Carthage or Swift Package Manager to install Diff. Usage Start by importing the pack

Sam Soffes 120 Sep 9, 2022
A fast Swift diffing library.

HeckelDiff Pure Swift implementation of Paul Heckel's A Technique for Isolating Differences Between Files Features This is a simple diff algorithm tha

Matias Cudich 166 Oct 6, 2022
Swift library to generate differences and patches between collections.

Differ Differ generates the differences between Collection instances (this includes Strings!). It uses a fast algorithm (O((N+M)*D)) to do this. Featu

Tony Arnold 628 Dec 29, 2022
A Swift probability and statistics library

Probably Probably is a set of Swift structures for computing the probability and cumulative distributions of different probablistic functions. Right n

Harlan Haskins 270 Dec 2, 2022
KeyPathKit is a library that provides the standard functions to manipulate data along with a call-syntax that relies on typed keypaths to make the call sites as short and clean as possible.

KeyPathKit Context Swift 4 has introduced a new type called KeyPath, with allows to access the properties of an object with a very nice syntax. For in

Vincent Pradeilles 406 Dec 25, 2022
Algorithm is a library of tools that is used to create intelligent applications.

Welcome to Algorithm Algorithm is a library of tools that is used to create intelligent applications. Features Probability Tools Expected Value Progra

Cosmicmind 820 Dec 9, 2022
Swiftline is a set of tools to help you create command line applications

Swiftline is a set of tools to help you create command line applications. Swiftline is inspired by highline Swiftline contains the following: Colorize

Omar Abdelhafith 1.2k Dec 29, 2022
A general purpose embedded hierarchical lock manager used to build highly concurrent applications of all types. Same type of locker used in many of the large and small DBMSs in existence today.

StickyLocking StickyLocking is a general purpose embedded lock manager which allows for locking any resource hierarchy. Installable Lock modes allow f

Sticky Tools 2 Jun 15, 2021
Graph is a semantic database that is used to create data-driven applications.

Welcome to Graph Graph is a semantic database that is used to create data-driven applications. Download the latest sample. Features iCloud Support Mul

Cosmicmind 875 Oct 5, 2022
adb-tools-mac is a macOS menu bar app written in SwiftUI for common adb tools.

adb-tools-mac is a macOS menu bar app written in SwiftUI for common adb tools.

Naman Dwivedi 930 Jan 2, 2023
 A curated list of awesome applications, softwares, tools and shiny things for macOS.

A curated list of awesome applications, software, tools and shiny things for macOS. Items marked with are open-source software and link to the source

Chaitanya Gupta 13.8k Jan 9, 2023
A guide on setting up Xcode with all the essential Applications, Tools, and Frameworks to make your development experience with Xcode great!

A guide on setting up Xcode with all the essential Applications, Tools, and Frameworks to make your development experience with Xcode great!

Michael Royal 24 Jan 4, 2023
A collection of common tools and commands used throughout the development process, customized for Kipple projects.

KippleTools A collection of common tools and commands used throughout the development process, customized for Kipple projects. ⚠️ The code in this lib

Kipple 10 Sep 2, 2022
An iOS library for SwiftUI to create draggable sheet experiences similar to iOS applications like Maps and Stocks.

An iOS library for SwiftUI to create draggable sheet experiences similar to iOS applications like Maps and Stocks.

Wouter 63 Jan 5, 2023
An Objective-C animation library used to create floating image views.

JRMFloatingAnimation [![CI Status](http://img.shields.io/travis/Caroline Harrison/JRMFloatingAnimation.svg?style=flat)](https://travis-ci.org/Caroline

Caroline Harrison 233 Dec 28, 2022
MediaType is a library that can be used to create Media Types in a type-safe manner.

This is a general purpose Swift library for a concept of typed treatment for Media Types. We use this library on clients and servers to speak the same dialect and to enjoy all the comfort strong types provide over raw strings.

21Gram Consulting 79 Jul 19, 2022