Hi Maximillian,<div><br>On Friday, 19 February 2016, Maximilian Hünenberger <<a>m.huenenberger@me.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div></div><div>Discussions on different index handling should belong to another thread if there isn't one already.</div></div></blockquote><div><font size="2"><span style="background-color:rgba(255,255,255,0)">I mentioned indices only to provide context :) the linked document (Swift master) discusses an approach that fixes many of the issues with a custom wrapper approaches mentioned here.</span></font></div><div><font size="2"><span style="background-color:rgba(255,255,255,0)"><br></span></font></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div><br></div><div>Your proposed Set change covers at least the custom Hashable behavior.</div><div>What about Equatable? I think it could be expressed as behaviors.</div></div></blockquote><div> </div>Custom Equatable could also be implemented as a similar closure (Element,Element)->Bool. I left it off, but I probably shouldn't have. My main goal was to minimise changes to the existing interfaces and provide similar performance.</div><div><br></div><div>I wonder if there's any cases someone would want anything other than Equatable or pointer comparison.</div><div><br></div><div>As for your behaviour suggestion: I may be misinterpreting how it works. I like the idea, but it seems like a much larger change than is necessary. It may change all the type signatures in normal usage, and obsfucate the Element type.</div><div><br></div><div><font size="2"><span style="background-color:rgba(255,255,255,0)">Ideally the sets type would only show the element type, not implementation details like a custom hash algorithm.</span></font><br></div><div><br></div><div>If you're happy to change the Set's type signature for each hash/equality algorithm then a wrapper like Nicola suggests is probably good enough.</div><div><br></div><div>Even easier if you're happy to manually wrap and unwrap:</div><div><br></div><div>class HashableReference<T: AnyObject>: Hashable {</div><div> let object: T</div><div> var hashValue: Int {</div><div> return ObjectIdentifier(object).hashValue</div><div> }</div><div>}</div><div>func ==<T: AnyObject>(lhs: <font size="2"><span style="background-color:rgba(255,255,255,0)">HashableReference<T></span></font>, rhs: <font size="2"><span style="background-color:rgba(255,255,255,0)">HashableReference<T></span></font>) -> Bool {</div><div> return <font size="2"><span style="background-color:rgba(255,255,255,0)">ObjectIdentifier(lhs) == ObjectIdentifier(rhs)</span></font></div><div>}</div><div><br></div><div>Apologies for any errors, this was done from my iPhone from memory.</div><div><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div><br></div><div>- Maximilian</div><div><br>Am 19.02.2016 um 11:58 schrieb Andrew Bennett via swift-evolution <<a>swift-evolution@swift.org</a>>:<br><br></div><blockquote type="cite"><div><div dir="ltr"><div>Good points Dimitri,</div><div><br></div><div>I only wrote this quickly, so please look for other issues too, I don't expect it to be complete by any means.</div><div><br></div><div>I think a way to get an element's hashValue should probably be exposed, similar to `<font face="monospace, monospace">collection.next(index)</font>` that I mentioned earlier. I think this is useful, but I don't think it's actually necessary. Equatable is sufficient to provide Set functionality, Hashable is just for performance</div><div><br></div><div>I'm not aware of any algorithms that can't be built on top of the existing set methods that would require the same hash value the element used for binning.<br></div><div><br></div><div>I was initially thinking that you could compare the Hashable to determine equality, but I agree that there is a potential for the closure to convert to a narrower type.</div><div><br></div><div>A much stronger solution would change the interface slightly to require Element to be `<font face="monospace, monospace">Equatable`</font>, and provide `<font face="monospace, monospace">Element -> Int</font>` as the closure. It's slightly less flexibly, but I do agree that a narrowing makes Equatability of s1 == s2 ambiguous.<br></div><div><br></div><div><b>In summary</b>:</div><div><br></div><div><div style="font-size:13px"><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: Equatable> {</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(</font></span><span style="font-family:monospace,monospace;background-color:rgb(217,234,211)">hashValue</span><span style="background-color:rgb(217,234,211)"><font face="monospace, monospace">ForElement: Element -> Int) {</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 style="font-size:13px"><span style="background-color:rgb(217,234,211)"><font face="monospace, monospace">+ public init<H: Hashable>(minimumCapacity: Int, </font></span><span style="font-family:monospace,monospace;background-color:rgb(217,234,211)">hashValue</span><span style="font-family:monospace,monospace;background-color:rgb(217,234,211)">ForElement: Element -> Int</span><span style="background-color:rgb(217,234,211)"><font face="monospace, monospace">) {</font></span></div><div style="font-size:13px"><span style="background-color:rgb(217,234,211)"><font face="monospace, monospace"> ... </font></span></div><div style="font-size:13px"><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 style="font-size:13px"><div><span style="font-family:monospace,monospace"> ... (same for other public initialisers)</span></div></div><div style="font-size:13px"><span style="font-family:monospace,monospace;background-color:rgb(217,234,211)">+ public func hashValueForElement(element: Element) -> Int { ... </span><span style="font-family:monospace,monospace;background-color:rgb(217,234,211)">}</span></div><div style="font-size:13px"><span style="font-family:monospace,monospace"> }</span><br></div><div style="font-size:13px"><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></div></div><div><br></div><div>-</div><div>Andrew Bennett</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Feb 19, 2016 at 7:14 PM, Dmitri Gribenko <span dir="ltr"><<a>gribozavr@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>On Fri, Feb 19, 2016 at 2:06 AM, Andrew Bennett via swift-evolution<br>
<<a>swift-evolution@swift.org</a>> wrote:<br>
> Wrapper Solution?<br>
><br>
> I've had a go at a wrapper solution before and it seems to either need a<br>
> unique type per sort, or a block stored per element (unstable).<br>
><br>
> Similar overheads were discussed when an index needs to store a reference to<br>
> the parent. There's some work to fix it that makes indices moveable, so<br>
> instead of going index.successor() you use collection.next(index).<br>
><br>
> <a href="https://github.com/apple/swift/blob/master/test/Prototypes/CollectionsMoveIndices.swift" rel="noreferrer" target="_blank">https://github.com/apple/swift/blob/master/test/Prototypes/CollectionsMoveIndices.swift</a><br>
><br>
> Potential solution:<br>
><br>
> The collection interfaces could change like this:<br>
><br>
> - struct Set<Element: Hashable> {<br>
> - struct Set<Element> {<br>
> ...<br>
> - public init() { ... }<br>
> + public init<H: Hashable>(elementHasher: Element -> H) {<br>
> ...<br>
> }<br>
<br>
</span>How do you compare such sets? That is, what does s1 == s2 mean, if<br>
the two sets are constructed with a hashing closure? We can't even<br>
compare closures to find that they are the same.<br>
<br>
Also, how would this affect algorithms on sets? When handling a set,<br>
you essentially wouldn't know the rules according to which it<br>
operates, unless we expose this mapping function.<br>
<div><div><br>
Dmitri<br>
<br>
--<br>
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if<br>
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <<a>gribozavr@gmail.com</a>>*/<br>
</div></div></blockquote></div><br></div>
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a>swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></div></blockquote></div>