[swift-evolution] Custom equality/hash for Sets

Dmitri Gribenko gribozavr at gmail.com
Fri Feb 19 04:14:21 CST 2016

On Fri, Feb 19, 2016 at 2:06 AM, Andrew Bennett via swift-evolution
<swift-evolution at swift.org> wrote:
> Wrapper Solution?
> I've had a go at a wrapper solution before and it seems to either need a
> unique type per sort, or a block stored per element (unstable).
> Similar overheads were discussed when an index needs to store a reference to
> the parent. There's some work to fix it that makes indices moveable, so
> instead of going index.successor() you use collection.next(index).
> https://github.com/apple/swift/blob/master/test/Prototypes/CollectionsMoveIndices.swift
> Potential solution:
> The collection interfaces could change like this:
> - struct Set<Element: Hashable> {
> - struct Set<Element> {
>     ...
> -   public init() { ... }
> +   public init<H: Hashable>(elementHasher: Element -> H) {
>       ...
>     }

How do you compare such sets?  That is, what does s1 == s2 mean, if
the two sets are constructed with a hashing closure?  We can't even
compare closures to find that they are the same.

Also, how would this affect algorithms on sets?  When handling a set,
you essentially wouldn't know the rules according to which it
operates, unless we expose this mapping function.


(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/

More information about the swift-evolution mailing list