{"id":26698545,"url":"https://github.com/frazer-rbsn/orderedset","last_synced_at":"2025-04-13T05:39:34.260Z","repository":{"id":191928798,"uuid":"351561629","full_name":"frazer-rbsn/orderedset","owner":"frazer-rbsn","description":"A simple, static Ordered Set collection type for Swift. Doesn't depend on Foundation.","archived":false,"fork":false,"pushed_at":"2024-12-22T05:55:45.000Z","size":61,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-26T22:17:07.521Z","etag":null,"topics":["arrayset","collection","immutable","nsorderedset","ordered","ordered-set","orderedset","set","sortedset","static","swift"],"latest_commit_sha":null,"homepage":"","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/frazer-rbsn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2021-03-25T20:05:43.000Z","updated_at":"2024-12-22T05:51:53.000Z","dependencies_parsed_at":null,"dependency_job_id":"9f69cef5-1dbd-48bd-9778-1681b0ba9a6c","html_url":"https://github.com/frazer-rbsn/orderedset","commit_stats":null,"previous_names":["frazer-rbsn/orderedset"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frazer-rbsn%2Forderedset","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frazer-rbsn%2Forderedset/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frazer-rbsn%2Forderedset/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frazer-rbsn%2Forderedset/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/frazer-rbsn","download_url":"https://codeload.github.com/frazer-rbsn/orderedset/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248670506,"owners_count":21142897,"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":["arrayset","collection","immutable","nsorderedset","ordered","ordered-set","orderedset","set","sortedset","static","swift"],"created_at":"2025-03-26T22:17:12.702Z","updated_at":"2025-04-13T05:39:33.985Z","avatar_url":"https://github.com/frazer-rbsn.png","language":"Swift","readme":"# OrderedSet\nA static, ordered collection of unique objects.\n\n[![macOS](https://github.com/frazer-rbsn/OrderedSet/actions/workflows/macos.yml/badge.svg)](https://github.com/frazer-rbsn/OrderedSet/actions/workflows/macos.yml)\n[![Ubuntu](https://github.com/frazer-rbsn/OrderedSet/actions/workflows/ubuntu.yml/badge.svg)](https://github.com/frazer-rbsn/OrderedSet/actions/workflows/ubuntu.yml)\n\n\n\n## About\n\nIn short, an `OrderedSet` is an immutable hybrid of an `Array` and a `Set`. Like an `Array`, it's elements have\na defined order, but it enforces uniqueness on it's members like a `Set`. \n\nYou can use `OrderedSet` as a drop-in replacement for an `Array` if:\n * the element type conforms to `Hashable` (e.g. `Int`, `String`, `Double` etc., or your own type),\n * you do not need to perform in-place modification of the array,\n * and all elements should occur only once.\n\nIt can give you significant performance boosts when working with large collections, over using an `Array`.\n\nYou can use `OrderedSet` as a drop-in replacement for a `Set` if:\n * you do not need to perform in-place modification of the set,\n * and you require the members of the set to have a defined order.\n\n`OrderedSet` is implemented in pure Swift with no Objective-C constructs.  \n`OrderedSet` is a *static set*, i.e. once initialised, it cannot be mutated. It is similar to `NSOrderedSet` from Objective-C Foundation.  \n`OrderedSet` has no dependencies. It does not depend on `Foundation`.\n\n## Installation\n\nSwift Package Manager:\n\n```swift\ndependencies: [\n  .package(url: \"https://github.com/frazer-rbsn/OrderedSet.git\", exact: \"2.0.0\"),\n],\n```\n\n\n## Usage\n\nFor example, if we have a large array of strings:\n\n```swift\nlet largeCollection: [String]\n```\n\nIf it's important to retain the order of these strings, and we want to ensure that each string is unqiue, we can easily change this to an `OrderedSet` like this:\n\n```swift\nlet largeCollection: OrderedSet\u003cString\u003e\n```\n\nThe benefits of doing this can be seen when performing operations such as `.contains(:_)` on large collections, \nwhich on an `OrderedSet`, is an *O(1)* operation, as it's backed by a standard `Set`. \nOn an array, this operation would be *O(n)*, as the time required to compute it is affected by the number of items in the array.\n\n\n### Initialisation\n\nAn `OrderedSet` can be initialised in the following ways:\n\n```swift\n// With an array\nlet s = OrderedSet([\"John\", \"Sally\", \"Bob\", \"Alice\"])\n\n// With a range\nlet s = OrderedSet(0..\u003c5)\n// ---\u003e OrderedSet([0,1,2,3,4])\n\n// With an array literal\nlet t : OrderedSet\u003cString\u003e = [\"A\", \"B\", \"C\"]\n\n// With a set, sorted by a closure\nlet s = OrderedSet(someSet, sortedBy: { $0.name \u003c $1.name })\n\n// With a set, where the elements conform to Comparable\nlet comparableSet = Set([3,4,1,2])\nlet s = OrderedSet(comparableSet)\n// ---\u003e OrderedSet([1,2,3,4])\n```\n\nWhen initialising an `OrderedSet` with an array, by default the `OrderedSet` will retain \nthe first instance of any duplicate elements:\n\n```swift\nlet arr = [\"Chris\", \"Bob\", \"Chris\", \"Alice\"]\nlet s = OrderedSet(arr)\n// ---\u003e OrderedSet([\"Chris\", \"Bob\", \"Alice\"])\n```\n\nIf you wish to retain the *last* occurrence of an element, use the `retainLastOccurrences` parameter and set it to `true`:\n\n```swift\nlet arr = [\"Chris\", \"Bob\", \"Chris\", \"Alice\"]\nlet s = OrderedSet(arr, retainLastOccurrences: true)\n// ---\u003e OrderedSet([\"Bob\", \"Chris\", \"Alice\"])\n```\n\n\n### Properties\n\nWhen working with an `OrderedSet`, you may need to cast it back to an `Array` or `Set` when working with an API.\n`OrderedSet` provides access to it's internal storage via these properties:\n\n* `.array`\n* `.contiguousArray`\n* `.unorderedSet`\n\n### Functions\n\n`OrderedSet` implements many of the common collection functions, such as `compactMap(:_)`, `filter(:_)` \nand `shuffled(:_)`. These will return a new `OrderedSet` instead of an array where possible. \n\n```swift\nlet s = OrderedSet([\"john\", \"sally\", \"bob\", \"alice\"])\nlet t = s.map { $0.capitalized }\n// ---\u003e OrderedSet([\"John\", \"Sally\", \"Bob\", \"Alice\"])\n```\n\nBecause `OrderedSet` is static -- it has no operations that can modify the collection --  \nit provides a few non-standard functions, such as `removingAll(:_)` \nwhich gives the inverse of an equivalent call to `filter(:_)`, and `appending(:_)`, \nwhich returns a new `OrderedSet` with the given element appended to the end.\n\nStandard set algebra functions are available, and can be used with both other `OrderedSet` instances \nand standard `Set` collections.\n\n```swift\nlet a = Set(1,2,4)\nlet b = OrderedSet([1,2,3,4,5])\nlet isSuperSet = b.isSuperset(of: a)\n// ---\u003e true\n```\n\nUnlike with an `Array`, checking the index of an element in `OrderedSet` is also an *O(1)* operation.\n\n```swift\nlet s = OrderedSet([\"Carol\", \"Bob\", \"Joan\"])\nlet i = s.index(of: \"Bob\")\n// ---\u003e 1\n```\n\n\n### Conformances\n\n* `Sendable`\n* `ExpressibleByArrayLiteral`\n* `RandomAccessCollection`\n* `Hashable`\n* `Equatable`\n* `Codable` (if Element conforms to `Codable`)\n* `CustomStringConvertible`\n\n`OrderedSet` will encode to and decode from a JSON array.\n\n```swift\nstruct House: Codable {\n  let members: OrderedSet\u003cString\u003e\n}\n\nlet json = \"\"\"\n    {\n      \"members\": [\n        \"Jim\",\n        \"Carol\",\n        \"Joan\",\n        \"Felix\"\n      ]\n    }\n    \"\"\"\nlet jsonData = json.data(using: .utf8)!\nlet decoder = JSONDecoder()\nlet house = try decoder.decode(House.self, from: jsonData)\n// ---\u003e house.members -\u003e OrderedSet([\"Jim\",\"Carol\",\"Joan\",\"Felix\"])\n```\n\nEquality is determined in the same way as an array.\n\n```swift\nlet array = [1,2,3,4,5]\nlet array2 = [1,3,2,4,5]\nlet s1 = OrderedSet(array)\nlet s2 = OrderedSet(array2)\nlet isEqual = s1 == s2\n// ---\u003e false\n```\n\n## License\n\nMIT. See `LICENSE`.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrazer-rbsn%2Forderedset","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffrazer-rbsn%2Forderedset","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrazer-rbsn%2Forderedset/lists"}