Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OrderedSet append(contentsOf:) with insertion check result/results #329

Open
LePips opened this issue Nov 19, 2023 · 0 comments
Open

OrderedSet append(contentsOf:) with insertion check result/results #329

LePips opened this issue Nov 19, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@LePips
Copy link

LePips commented Nov 19, 2023

Motivation

I am essentially using OrderedSet as the data source for a UICollectionView wrapper. In said wrapper, I allow passing in an OrderedSet, but I would also like to allow passing in any Sequence (where Element: Hashable) where I would instead create the necessary OrderedSet. Additionally, I would like to log a warning if the given sequence contained duplicates and will result in unexpected behavior, similar to SwiftUI's warnings with ForEach/etc.

Problem

Currently, append(contentsOf:) doesn't return any insertion results like append does. I think that generally this would be helpful.

Design

The basic idea can be done with the new function append(checkingContentsOf:):

@inlinable
@discardableResult
mutating func append(checkingContentsOf elements: some Sequence<Element>) -> [(inserted: Bool, index: Int)] {
    var results: [(Bool, Int)] = []

    for item in elements {
        results.append(append(item))
    }

    return results
}

However that can be a bit heavy for unexpected long sequences and I would have to iterate over the results for duplication check. So instead we can just check instead if the given sequence contained any duplicates:

@inlinable
@discardableResult
mutating func append(checkingContentsOf elements: some Sequence<Element>) -> Bool {
    var didContainDuplicate = false

    for item in elements {
        if append(item).inserted {
            didContainDuplicate = true
        }
    }

    return didContainDuplicate
}

I propose that one of these, or something similar, be implemented on OrderedSet or that the current append(contentsOf:) gains a discardable return result.

@LePips LePips added the enhancement New feature or request label Nov 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant