LimitedOrderedSet

1.0.0

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

What's New

v1.0.0

2022-09-17T15:14:55Z

First public release.

LimitedOrderedSet

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.

Installation

LimitedOrderedSet is available through Swift Package Manager.

.package(url: "https://github.com/MrAsterisco/LimitedOrderedSet", from: "<see GitHub releases>")

Latest Release

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

Usage

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

Documentation

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

Compatibility

LimitedOrderedSet is compatible with all Apple platforms.

LimitedOrderedSet depends on:

  • swift-collections/OrderedSet version 1.

Contributions

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.

Status

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.

License

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

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

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