<div dir="ltr"><div><b>Wrapper Solution?</b></div><div><br></div>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).<div><br></div><div>Similar overheads were discussed when an index needs to store a reference to the parent. <font size="2"><span style="background-color:rgba(255,255,255,0)">There's some work to fix it that makes indices moveable, so instead of going index.successor() you use collection.next(index). </span></font></div><div><br></div><div><a href="https://github.com/apple/swift/blob/master/test/Prototypes/CollectionsMoveIndices.swift" style="background-color:rgba(255,255,255,0)" target="_blank"><font color="#000000" size="2">https://github.com/apple/swift/blob/master/test/Prototypes/CollectionsMoveIndices.swift</font></a><br><div><br></div><div><b>Potential solution:</b></div><div><b><br></b></div><div>The collection interfaces could change like this:<br></div><div><br></div><div><span style="font-family:monospace,monospace;background-color:rgb(244,204,204)">- struct Set<Element: Hashable> {</span><br></div><div><span style="font-family:monospace,monospace;background-color:rgb(217,234,211)">- struct Set<Element> {</span></div><div><span style="font-family:monospace,monospace"> ...</span><br></div><font face="monospace, monospace" style="background-color:rgb(244,204,204)"><div><div style="font-family:arial,sans-serif;background-color:rgb(255,255,255)"><div><font face="monospace, monospace" style="background-color:rgb(244,204,204)">- public init() { ... }</font></div><div><span style="background-color:rgb(217,234,211)"><font face="monospace, monospace">+ public init<H: Hashable>(elementHasher: Element -> H) {</font></span></div><div><span style="background-color:rgb(217,234,211)"><font face="monospace, monospace"> ... </font></span></div><div><span style="background-color:rgb(217,234,211)"><font face="monospace, monospace"> }</font></span></div></div><font face="monospace, monospace" style="background-color:rgb(255,255,255)"></font></div>- public init(minimumCapacity: Int) { ... }</font></div><div><span style="background-color:rgb(217,234,211)"><font face="monospace, monospace">+ public init<H: Hashable>(minimumCapacity: Int, elementHasher: Element -> H) {</font></span></div><div><span style="background-color:rgb(217,234,211)"><font face="monospace, monospace"> ... </font></span></div><div><span style="background-color:rgb(217,234,211)"><font face="monospace, monospace"> }</font></span><font face="monospace, monospace" style="background-color:rgb(244,204,204)"><br></font></div><div><div><span style="font-family:monospace,monospace"> ... (same for other public initialisers)</span><br></div></div><div><font face="monospace, monospace"> }</font><div><br></div><div>Then the Hashable specific initialisers added back like this:</div><div><br></div><div><div><span style="font-family:monospace,monospace;background-color:rgb(217,234,211)">+ extension Set where Element: Hashable {</span><span style="font-family:monospace,monospace;background-color:rgb(217,234,211)"><br></span></div><div><span style="font-family:monospace,monospace;background-color:rgb(217,234,211)">+ public init() { ... }</span><span style="font-family:monospace,monospace;background-color:rgb(217,234,211)"><br></span></div><div><span style="font-family:monospace,monospace;background-color:rgb(217,234,211)">+ public init(minimumCapacity: Int) { ... }</span><br></div><div><span style="font-family:monospace,monospace;background-color:rgb(217,234,211)">+ }</span></div><div><span style="font-family:monospace,monospace;background-color:rgb(244,204,204)"><br></span></div><b>Potential Implementation</b><div><span style="font-family:monospace,monospace;background-color:rgb(244,204,204)"><br></span></div><div><div>I've had a quick look at `<font face="monospace, monospace">HashedCollections.swift.gyb</font>`, there's currently two underlying implementations ObjC and Swift Native, it may be possible to specialise (gyb) this Native implementation into two implementations (HashableNative, ClosureNative).</div><div><br></div><div>The specialisation is probably just a one liner in `_bucket()` and `hashValue` to use a different method to get the hash.</div></div><div><span style="font-family:monospace,monospace;background-color:rgb(244,204,204)"><br></span></div><div>I don't think this would break any code, except new initialisers that may need `where Hashable` added. I think it will provide the desired functionality.</div><div><br></div><div>It may even be a fairly straightforward implementation (famous last words), although I haven't got time to do much more at the moment.</div><div><br>On Friday, 19 February 2016, Dave Abrahams via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><br>
on Thu Feb 18 2016, Jacob Bandes-Storch <<a>swift-evolution@swift.org</a>> wrote:<br>
<br>
> Would it make sense for the standard library Set to provide variants (or<br>
> parallel versions of the same data structure) that take custom hashValue/==<br>
> implementations at init time (functions taking in Elements), rather than<br>
> relying on Hashable/Comparable protocols?<br>
><br>
> Use case: I want a set of objects that are compared for equality using ===<br>
> rather than ==. This doesn't seem possible today, using Set, without<br>
> creating some sort of wrapper object.<br>
><br>
> This particular case would be analogous to using NSHashTable with<br>
> NSPointerFunctionsObjectPointerPersonality. (Maybe all I'm asking for is a<br>
> Swiftier API for NSHashTable — including ArrayLiteralConvertible, using<br>
> generics instead of UnsafePointer<Void>, etc.)<br>
><br>
> Similarly, C++'s unordered_map<br>
> <<a href="http://en.cppreference.com/w/cpp/container/unordered_map" target="_blank">http://en.cppreference.com/w/cpp/container/unordered_map</a>> and friends have<br>
> template parameters specifying the hash function and equality comparator,<br>
> which use std::hash and == by default.<br>
<br>
It might make sense. How bad is the wrapper solution for user code?<br>
<br>
--<br>
-Dave<br>
<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a>swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div></div>
</div></div>