<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><blockquote type="cite" class="">On 16 Feb 2016, at 17:49, Jeff Hajewski &lt;<a href="mailto:jeff.hajewski@gmail.com" class="">jeff.hajewski@gmail.com</a>&gt; wrote:<br class=""></blockquote><div><blockquote type="cite" class=""><br class="Apple-interchange-newline"><div class=""><span style="font-family: Helvetica; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">I wonder if we can loosen the restriction of conforming to RandomAccessIndexType.</span></div></blockquote><div><br class=""></div><div>Yay, it turned out simpler than I thought!</div><div><br class=""></div><div>I added a performance test to make sure there's no regression on&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">RandomAccessIndexType</span>'s performance, then simply downgraded the&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">RandomAccessIndexType</span>&nbsp;requirement to&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">ForwardIndexType</span>&nbsp;(and used&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">!=</span>&nbsp;instead of&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">&lt;</span>&nbsp;in a few places). The&nbsp;<a href="https://github.com/knomi/Allsorts/commit/52e3d7abee2d01d90ecadea24e00c9067a64a327" class="">updated code</a>&nbsp;is in GitHub.</div><div><br class=""></div><div>That appears to work because of protocol inheritance magic in the standard library. So, even though my search algorithm only knows about&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">ForwardIndexType</span>, whenever <span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Index</span>&nbsp;happens to implement O(1) versions of&nbsp;<span style="color: rgb(61, 29, 129); font-family: Menlo; font-size: 11px;" class="">advancedBy(_:)</span>&nbsp;and&nbsp;<span style="color: rgb(61, 29, 129); font-family: Menlo; font-size: 11px;" class="">distanceTo(_:)</span>, then those get used&nbsp;instead of the O(N) default implementations (<a href="https://github.com/apple/swift/blob/de253d9b6f21498704f96aac712cf1b02e612255/stdlib/public/core/Index.swift#L243-L262" class="">Index.swift</a>&nbsp;on the Swift repository). Cool!</div><div><br class=""></div><div>There might still be place for improvement, since the internal use of <span style="font-family: Menlo; font-size: 11px;" class="">midIndex</span>&nbsp;still passes across the same index ranges several times during the search on&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">ForwardIndexType</span>. If I did my math right, the current implementation makes something like 3N index increments on&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">ForwardIndexType</span>s, while probably at least 2N can be achieved without much memory use.</div><br class=""><blockquote type="cite" class=""><div class=""><span style="font-family: Helvetica; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">I think maybe the<span class="Apple-converted-space">&nbsp;</span></span><i style="font-family: Helvetica; font-size: 13px; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Ordering</i><span style="font-family: Helvetica; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class=""><span class="Apple-converted-space">&nbsp;</span>enum should be broken out into a separate proposal since it's not currently part of the standard lib. That being said, it does make for a clean implementation.</span></div></blockquote><br class=""></div><div>You're absolutely right. I should also paint the bikeshed of naming its cases (<span style="font-family: Menlo; font-size: 11px;" class="">.LT</span>,&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">.EQ</span>, and&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">.GT</span>) less obscurely before officially proposing it to swift-evolution. Maybe either &nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">.ascending</span>,&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">.equivalent</span>, and&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">.descending</span>, or simply <span style="font-family: Menlo; font-size: 11px;" class="">.less</span>,&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">.equal</span>, and&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">.greater</span>. And I expect there to be resistance against adding yet another comparison operator, even if there's precedent on the use of something like "<span style="font-family: Menlo; font-size: 11px;" class="">a &lt;=&gt; b</span>" in several other languages.</div><br class=""><div class="">
<span class="Apple-style-span" style="border-collapse: separate; line-height: normal; border-spacing: 0px;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; border-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-stroke-width: 0px; font-size: 12px;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">— Pyry Jahkola</div></span></div></span></div></body></html>