A Swift ordered set implementation that only accepts a limited amount of items.

What's New



First public release.


An implementation of a limited ordered set in Swift, based on the OrderedSet.

You can use this data structure to enforce a limit on the amount of non-equal items stored in a collection.


LimitedOrderedSet is available through Swift Package Manager.

.package(url: "", from: "<see GitHub releases>")

Latest Release

To find out the latest version, look at the Releases tab of this repository.


A LimitedOrderedSet works exactly like an OrderedSet with the addition of a limit.

You can create a LimitedOrderedSet from any Sequence:

let set = LimitedOrderedSet(["A", "B", "C"], limit: 3)

The limit you specify is used to determine the maximum capacity of the OrderedSet. You will be able to append or insert items into the set until you hit the specified limit.

let set = LimitedOrderedSet(limit: 2)
set.append("A") // ✅ Added
set.append("B") // ✅ Added

set.isFull // <-- ✅ true

set.append("B") // ❌ Not Added

If you specify an index, you can also control the behavior of the set when attempting to add new items while it's full.

let set = LimitedOrderedSet(["A", "B"], limit: 2)

// 1️⃣ Drop Last Item
set.insert("C", at: 1, ifFull: .dropLast)
print(set) // <-- ["A", "C"]

// 2️⃣ Drop First Item
set.insert("C", at: 1, ifFull: .dropFirst)
print(set) // <-- ["B", "C"]

// 3️⃣ Reject
set.insert("C", at: 1, ifFull: .reject) // ❌ Not Added
print(set) // <-- ["A", "B"], unchanged


The class definition is document, but most of the methods are not (yet). Documentation will be improved with future releases.


LimitedOrderedSet is compatible with all Apple platforms.

LimitedOrderedSet depends on:

  • swift-collections/OrderedSet version 1.


All contributions to expand the library are welcome. Fork the repo, make the changes you want, and open a Pull Request.

If you make changes to the codebase, I am not enforcing a coding style, but I may ask you to make changes based on how the rest of the library is made.


This library is under active development. It is used in one app in Production.

Even if most of the APIs come from the Swift Standard Library, they may change in the future; but you don't have to worry about that, because releases will follow Semantic Versioning 2.0.0.


LimitedOrderedSet is distributed under the MIT license. See LICENSE for details.


  • Swift Tools 5.7.0
View More Packages from this Author


Last updated: Fri Mar 15 2024 03:43:58 GMT-0900 (Hawaii-Aleutian Daylight Time)