<div dir="ltr">I have used the node of a linked list as the list's index! The node wasn't Hashable, Equatable, or Comparable; hash and equate would be easy to add but Comparable would be expensive. It is probably not a big deal since linked lists aren't that great a data structure anyway. Code below:<div><br></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">class</span> Node<Element> {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">var</span> value: <span style="color:rgb(79,129,135)">Element</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">private(set)</span> <span style="color:rgb(187,44,162)">var</span> next: <span style="color:rgb(79,129,135)">Node</span><<span style="color:rgb(79,129,135)">Element</span>>? = <span style="color:rgb(187,44,162)">nil</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">init</span>(<span style="color:rgb(187,44,162)">_</span> value: <span style="color:rgb(79,129,135)">Element</span>) { <span style="color:rgb(187,44,162)">self</span>.<span style="color:rgb(79,129,135)">value</span> = value }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">init</span>(value: <span style="color:rgb(79,129,135)">Element</span>, successor: <span style="color:rgb(79,129,135)">Node</span><<span style="color:rgb(79,129,135)">Element</span>>) {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">self</span>.<span style="color:rgb(79,129,135)">value</span> = value</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">self</span>.<span style="color:rgb(79,129,135)">next</span> = successor</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">class</span> LinkedListGenerator<Element>: <span style="color:rgb(112,61,170)">GeneratorType</span> {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">private</span> <span style="color:rgb(187,44,162)">var</span> index: <span style="color:rgb(79,129,135)">Node</span><<span style="color:rgb(79,129,135)">Element</span>>?</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">init</span>(<span style="color:rgb(187,44,162)">_</span> startIndex: <span style="color:rgb(79,129,135)">Node</span><<span style="color:rgb(79,129,135)">Element</span>>) { <span style="color:rgb(79,129,135)">index</span> = startIndex }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">func</span> next() -> <span style="color:rgb(79,129,135)">Element</span>? {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">guard</span> <span style="color:rgb(187,44,162)">let</span> i = <span style="color:rgb(79,129,135)">index</span> <span style="color:rgb(187,44,162)">else</span> { <span style="color:rgb(187,44,162)">return</span> <span style="color:rgb(187,44,162)">nil</span> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">let</span> element = i.<span style="color:rgb(79,129,135)">value</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(79,129,135)">index</span> = i.<span style="color:rgb(79,129,135)">next</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">return</span> element</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(187,44,162)">class</span><span style="color:rgb(0,0,0)"> LinkedList<Element>: </span><span style="color:rgb(112,61,170)">SequenceType</span><span style="color:rgb(0,0,0)"> { </span>// Has same interface as Indexable, but without the ForwardIndexType constraint</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">private(set)</span> <span style="color:rgb(187,44,162)">var</span> startIndex: <span style="color:rgb(79,129,135)">Node</span><<span style="color:rgb(79,129,135)">Element</span>></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">private(set)</span> <span style="color:rgb(187,44,162)">var</span> endIndex: <span style="color:rgb(79,129,135)">Node</span><<span style="color:rgb(79,129,135)">Element</span>></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">convenience</span> <span style="color:rgb(187,44,162)">init</span><S: <span style="color:rgb(112,61,170)">SequenceType</span> <span style="color:rgb(187,44,162)">where</span> <span style="color:rgb(79,129,135)">S</span>.Generator.Element == <span style="color:rgb(79,129,135)">Element</span>>(elements: <span style="color:rgb(79,129,135)">S</span>) {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">var</span> gen = elements.<span style="color:rgb(61,29,129)">generate</span>()</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">var</span> element = gen.<span style="color:rgb(61,29,129)">next</span>()</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">guard</span> <span style="color:rgb(187,44,162)">let</span> first = element <span style="color:rgb(187,44,162)">else</span> { <span style="color:rgb(61,29,129)">fatalError</span>(<span style="color:rgb(209,47,27)">"Empty list"</span>) }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">self</span>.<span style="color:rgb(187,44,162)">init</span>(first)</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> element = gen.<span style="color:rgb(61,29,129)">next</span>()</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">while</span> element != <span style="color:rgb(187,44,162)">nil</span> {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(49,89,93)">append</span>(element!)</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> element = gen.<span style="color:rgb(61,29,129)">next</span>()</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">init</span>(<span style="color:rgb(187,44,162)">_</span> element: <span style="color:rgb(79,129,135)">Element</span>) {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(79,129,135)">startIndex</span> = <span style="color:rgb(79,129,135)">Node</span>(element)</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(0,0,0)"> </span>endIndex<span style="color:rgb(0,0,0)"> = </span>startIndex</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">subscript</span>(index: <span style="color:rgb(79,129,135)">Node</span><<span style="color:rgb(79,129,135)">Element</span>>) -> <span style="color:rgb(79,129,135)">Element</span> {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">get</span> { <span style="color:rgb(187,44,162)">return</span> index.<span style="color:rgb(79,129,135)">value</span> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">set</span> { index.<span style="color:rgb(79,129,135)">value</span> = newValue }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">func</span><span style="color:rgb(0,0,0)"> generate() -> </span>LinkedListGenerator<span style="color:rgb(0,0,0)"><</span>Element<span style="color:rgb(0,0,0)">> { </span><span style="color:rgb(187,44,162)">return</span><span style="color:rgb(0,0,0)"> </span>LinkedListGenerator<span style="color:rgb(0,0,0)">(</span>startIndex<span style="color:rgb(0,0,0)">) }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">func</span> prepend(element: <span style="color:rgb(79,129,135)">Element</span>) { <span style="color:rgb(79,129,135)">startIndex</span> = <span style="color:rgb(79,129,135)">Node</span>(value: element, successor: <span style="color:rgb(79,129,135)">startIndex</span>) }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">func</span> prepend(elements elements: <span style="color:rgb(79,129,135)">LinkedList</span><<span style="color:rgb(79,129,135)">Element</span>>) {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">let</span> old = <span style="color:rgb(79,129,135)">startIndex</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(79,129,135)">startIndex</span> = elements.<span style="color:rgb(79,129,135)">startIndex</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> elements.<span style="color:rgb(79,129,135)">endIndex</span>.<span style="color:rgb(79,129,135)">next</span> = old</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">func</span> append(element: <span style="color:rgb(79,129,135)">Element</span>) {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(79,129,135)">endIndex</span>.<span style="color:rgb(79,129,135)">next</span> = <span style="color:rgb(79,129,135)">Node</span>(element)</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(0,0,0)"> </span>endIndex<span style="color:rgb(0,0,0)"> = </span>endIndex<span style="color:rgb(0,0,0)">.</span>next<span style="color:rgb(0,0,0)">!</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">func</span> append(elements elements: <span style="color:rgb(79,129,135)">LinkedList</span><<span style="color:rgb(79,129,135)">Element</span>>) {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(0,0,0)"> </span>endIndex<span style="color:rgb(0,0,0)">.</span>next<span style="color:rgb(0,0,0)"> = elements.</span>startIndex</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(79,129,135)">endIndex</span> = elements.<span style="color:rgb(79,129,135)">endIndex</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p></div><div><br></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"> -- Howard.<br></div></div>
<br><div class="gmail_quote">On 8 March 2016 at 12:25, Dmitri Gribenko via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
What does everyone think about requiring indices to conform to<br>
Hashable, in addition to the existing requirements for Equatable and<br>
Comparable?<br>
<br>
I don't think this should limit any viable collection designs, and yet<br>
might be useful, for example, to store indices in a set.<br>
<span class="HOEnZb"><font color="#888888"><br>
Dmitri<br>
</font></span><div class="HOEnZb"><div class="h5"><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 href="mailto:gribozavr@gmail.com">gribozavr@gmail.com</a>>*/<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</div></div></blockquote></div><br></div>