{"id":15037715,"url":"https://github.com/cosmicmind/algorithm","last_synced_at":"2025-04-04T08:09:08.099Z","repository":{"id":56901808,"uuid":"50301940","full_name":"CosmicMind/Algorithm","owner":"CosmicMind","description":"Algorithm is a library of tools that is used to create intelligent applications.","archived":false,"fork":false,"pushed_at":"2020-01-17T08:46:20.000Z","size":271,"stargazers_count":832,"open_issues_count":5,"forks_count":84,"subscribers_count":29,"default_branch":"development","last_synced_at":"2025-04-04T08:09:03.828Z","etag":null,"topics":["algorithm","algorithms","conditional-probability","cosmicmind","data-structure","data-structures","data-structures-algorithms","dictionary","doublylinkedlist","multidictionary","multiset","probabilistic-programming","probability","redblacktree","redblacktrees","set","sorted-sets","stack","swift","swift-3"],"latest_commit_sha":null,"homepage":"http://cosmicmind.com","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CosmicMind.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-01-24T18:42:08.000Z","updated_at":"2025-03-23T16:41:58.000Z","dependencies_parsed_at":"2022-08-20T18:10:10.082Z","dependency_job_id":null,"html_url":"https://github.com/CosmicMind/Algorithm","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CosmicMind%2FAlgorithm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CosmicMind%2FAlgorithm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CosmicMind%2FAlgorithm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CosmicMind%2FAlgorithm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CosmicMind","download_url":"https://codeload.github.com/CosmicMind/Algorithm/tar.gz/refs/heads/development","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247142074,"owners_count":20890653,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","algorithms","conditional-probability","cosmicmind","data-structure","data-structures","data-structures-algorithms","dictionary","doublylinkedlist","multidictionary","multiset","probabilistic-programming","probability","redblacktree","redblacktrees","set","sorted-sets","stack","swift","swift-3"],"created_at":"2024-09-24T20:35:24.596Z","updated_at":"2025-04-04T08:09:08.078Z","avatar_url":"https://github.com/CosmicMind.png","language":"Swift","readme":"![Algorithm](http://www.cosmicmind.com/algorithm/github/algorithm-logo.png)\n\n## Welcome to Algorithm\n\nAlgorithm is a library of tools that is used to create intelligent applications.\n\n## Features\n\n- [x] Probability Tools \n- [x] Expected Value\n- [x] Programmable Probability Blocks\n- [x] Array Extensions\n- [x] Set Extensions\n\n## Data Structures\n\n- [x] DoublyLinkedList\n- [x] Stack\n- [x] Queue\n- [x] Deque\n- [x] RedBlackTree\n- [x] SortedSet\n- [x] SortedMultiSet\n- [x] SortedDictionary\n- [x] SortedMultiDictionary\n\n## Requirements\n\n* iOS 8.0+ / Mac OS X 10.9+\n* Xcode 8.0+\n\n## Communication\n\n- If you **need help**, use [Stack Overflow](http://stackoverflow.com/questions/tagged/cosmicmind). (Tag 'cosmicmind')\n- If you'd like to **ask a general question**, use [Stack Overflow](http://stackoverflow.com/questions/tagged/cosmicmind).\n- If you **found a bug**, _and can provide steps to reliably reproduce it_, open an issue.\n- If you **have a feature request**, open an issue.\n- If you **want to contribute**, submit a pull request.\n\n## Installation\n\n\u003e **Embedded frameworks require a minimum deployment target of iOS 8.**\n\u003e - [Download Algorithm](https://github.com/CosmicMind/Algorithm/archive/master.zip)\n\n## CocoaPods\n\n[CocoaPods](http://cocoapods.org) is a dependency manager for Cocoa projects. You can install it with the following command:\n\n```bash\n$ gem install cocoapods\n```\n\nTo integrate Algorithm's core features into your Xcode project using CocoaPods, specify it in your `Podfile`:\n\n```ruby\nsource 'https://github.com/CocoaPods/Specs.git'\nplatform :ios, '8.0'\nuse_frameworks!\n\npod 'Algorithm', '~\u003e 3.1.0'\n```\n\nThen, run the following command:\n\n```bash\n$ pod install\n```\n\n## Carthage\n\nCarthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.\n\nYou can install Carthage with Homebrew using the following command:\n\n```bash\n$ brew update\n$ brew install carthage\n```\nTo integrate Algorithm into your Xcode project using Carthage, specify it in your Cartfile:\n\n```bash\ngithub \"CosmicMind/Algorithm\"\n```\n\nRun `carthage update` to build the framework and drag the built `Algorithm.framework` into your Xcode project.\n\n## Changelog\n\nAlgorithm is a growing project and will encounter changes throughout its development. It is recommended that the [Changelog](https://github.com/CosmicMind/Algorithm/wiki/Changelog) be reviewed prior to updating versions.\n\n# Samples\n\nThe following are samples to see how Algorithm may be used within your applications.\n\n* Visit the [Samples](https://github.com/CosmicMind/Samples) repo to see example projects using Algorithm.\n\n- [Samples](#samples)\n  - [Probability](#probability)\n      - [Basic Probability](#basic-probability)\n      - [Conditional Probability](#conditional-probability)\n  - [Expected Value](#expected-value)\n  - [DoublyLinkedList](#doublylinkedlist)\n  - [Stack](#stack)\n  - [Queue](#queue)\n  - [Deque](#deque)\n  - [RedBlackTree](#redblacktree)\n  - [SortedSet](#sortedset)\n  - [SortedMultiSet](#sortedmultiset)\n  - [SortedDictionary](#sorteddictionary)\n  - [SortedMultiDictionary](#sortedmultidictionary)\n  - [License](#license)\n\n\u003ca name=\"probability\"\u003e\u003c/a\u003e\n## Probability\n\nEach data structure within Algorithm is equipped with probability tools.\n\n#### Basic Probability\n\nFor example, determining the probability of rolling a 3 using a die of 6 numbers.\n\n```swift\nlet die = [Int](arrayLiteral: 1, 2, 3, 4, 5, 6)\n\nif 0.1 \u003c die.probability(of: 3) \n\t\t// Do something ...\n}\n```\n\n#### Conditional Probability\n\nFor conditional probabilities that require a more complex calculation, use block statements.\n\n```swift\nlet die = [Int](arrayLiteral: 1, 2, 3, 4, 5, 6)\n\nlet pOfX = die.probability { (number) in\n\treturn 5 \u003c number || 0 == number % 3\n}\n\nif 0.33 \u003c pOfX {\n\t// Do something ...\n}\n```\n\n\u003ca name=\"expectedvalue\"\u003e\u003c/a\u003e\n## Expected Value\n\nThe expected value of rolling a 3 or 6 with 100 trials using a die of 6 numbers.\n\n```swift\nlet die = [Int](arrayLiteral: 1, 2, 3, 4, 5, 6)\n\nif 20 \u003c die.expectedValue(trials: 100, for: 3, 6) {\n\t\t// Do something ...\n}\n```\n\n\u003ca name=\"doublylinkedlist\"\u003e\u003c/a\u003e\n## DoublyLinkedList\n\nThe DoublyLinkedList data structure is excellent for large growing collections of data. Below is an example of its usage.\n\n```swift\nvar listA = DoublyLinkedList\u003cInt\u003e()\n        \nlistA.insert(atFront: 3)\nlistA.insert(atFront: 2)\nlistA.insert(atFront: 1)\n\nvar listB = DoublyLinkedList\u003cInt\u003e()\n\nlistB.insert(atBack: 4)\nlistB.insert(atBack: 5)\nlistB.insert(atBack: 6)\n\nvar listC = listA + listB\n\nlistC.cursorToFront()\n\nvar value = listC.cursor\n\nwhile nil != value {\n    // Do something ...\n    \n    value = listC.next()\n}\n```\n\n\u003ca name=\"stack\"\u003e\u003c/a\u003e\n## Stack\n\nThe 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.\n\n```swift\nvar stack = Stack\u003cInt\u003e()\n\nstack.push(1)\nstack.push(2)\nstack.push(3)\n\nwhile !stack.isEmpty {\n\tlet value = stack.pop()\n\t\n\t// Do something ...\n}\n```\n\n\u003ca name=\"queue\"\u003e\u003c/a\u003e\n## Queue\n\nThe 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.\n\n```swift\nvar queue = Queue\u003cInt\u003e()\n\nqueue.enqueue(1)\nqueue.enqueue(2)\nqueue.enqueue(3)\n\nwhile !queue.isEmpty {\n    let value = queue.dequeue()\n\n    // Do something ...\n}\n```\n\n\u003ca name=\"deque\"\u003e\u003c/a\u003e\n## Deque\n\nThe 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.\n\n```swift\nvar dequeA = Deque\u003cInt\u003e()\ndequeA.insert(atBack: 1)\ndequeA.insert(atBack: 2)\ndequeA.insert(atBack: 3)\n\nwhile !dequeA.isEmpty {\n\tlet value = dequeA.removeAtFront()\n\t\n\t// Do something ...\n}\n\nvar dequeB = Deque\u003cInt\u003e()\ndequeB.insert(atBack: 4)\ndequeB.insert(atBack: 5)\ndequeB.insert(atBack: 6)\n\nwhile !dequeB.isEmpty {\n\tlet value = dequeB.removeAtFront()\n\t\n\t// Do something ...\n}\n```\n\n\u003ca name=\"redblacktree\"\u003e\u003c/a\u003e\n## RedBlackTree\n\nA 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.\n\n```swift\nvar ages = RedBlackTree\u003cString, Int\u003e(uniqueKeys: true)\n\nages.insert(value: 16, for: \"Sarah\")\nages.insert(value: 12, for: \"Peter\")\nages.insert(value: 23, for: \"Alex\")\n\nlet node = ages[1]\n\nif \"Peter\" == node.key {\n    // Do something ...\n}\n```\n\n\u003ca name=\"sortedset\"\u003e\u003c/a\u003e\n## SortedSet\n\nSortedSets 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.\n\n```swift\nlet setA = SortedSet\u003cInt\u003e(elements: 1, 2, 3)\nlet setB = SortedSet\u003cInt\u003e(elements: 4, 3, 6)\nlet setC = SortedSet\u003cInt\u003e(elements: 7, 1, 2)\nlet setD = SortedSet\u003cInt\u003e(elements: 1, 7)\nlet setE = SortedSet\u003cInt\u003e(elements: 1, 6, 7)\n\n// Union.\nsetA + setB\nsetA.union(setB)\n\n// Intersection.\nsetC.intersection(setD)\n\n// Subset.\nsetD \u003c setC\nsetD.isSubset(of: setC)\n\n// Superset.\nsetD \u003e setC\nsetD.isSuperset(of: setC)\n\n// Contains.\nsetE.contains(setA.first!)\n\n// Probability.\nsetE.probability(of: setA.first!, setA.last!)\n```\n\n\u003ca name=\"sortedmultiset\"\u003e\u003c/a\u003e\n## SortedMultiSet\n\nA SortedMultiSet is identical to a SortedSet, except that a SortedMultiSet allows non-unique elements. Look at [SortedSet](#sortedset) for examples of its usage.\n\n\u003ca name=\"sorteddictionary\"\u003e\u003c/a\u003e\n## SortedDictionary\n\nA 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).\n\n\u003ca name=\"sortedmultidictionary\"\u003e\u003c/a\u003e\n## SortedMultiDictionary\n\nA SortedMultiDictionary is identical to a SortedDictionary, except that a SortedMultiDictionary allows non-unique keys. Below is an example of its usage.\n\n```swift\nstruct Student {\n    var name: String\n}\n\nlet sarah = Student(name: \"Sarah\")\nlet peter = Student(name: \"Peter\")\nlet alex = Student(name: \"Alex\")\n\nvar students = SortedMultiDictionary\u003cString, Student\u003e()\n\nstudents.insert(value: sarah, for: sarah.name)\nstudents.insert(value: peter, for: peter.name)\nstudents.insert(value: alex, for: alex.name)\n\nfor student in students {\n    // Do something ...\n}\n```\n\n## License\n\nThe MIT License (MIT)\n\nCopyright (C) 2019, CosmicMind, Inc. \u003chttp://cosmicmind.com\u003e.\nAll rights reserved.\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcosmicmind%2Falgorithm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcosmicmind%2Falgorithm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcosmicmind%2Falgorithm/lists"}