<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=""><div class=""><br class=""></div><div class=""><br class=""></div><div class="">There are 3 main algorithms: forward iteration, random access iteration and bidirectional iteration. All excerpts from the book Alexander A. Stepanov. “From Mathematics to Generic Programming”, Chapters 11.3 - 11.6</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">1. The forward iteration can be implemented by using Gries-Mills algorithm. This algorithm returns a new middle: a position where the first element moved. </div><div class=""><br class=""></div><div class=""><div class=""><font face="Menlo" class="">template <ForwardIterator I></font></div><div class=""><font face="Menlo" class="">I rotate(I f, I m, I l, std::forward_iterator_tag) {</font></div><div class=""><font face="Menlo" class=""> if (f == m) return l;</font></div><div class=""><font face="Menlo" class=""> if (m == l) return f;</font></div><div class=""><font face="Menlo" class=""> pair<I, I> p = swap_ranges(f, m, m, l);</font></div><div class=""><font face="Menlo" class=""> while (p.first != m || p.second != l) {</font></div><div class=""><font face="Menlo" class=""> if (p.second == l) {</font></div><div class=""><font face="Menlo" class=""> rotate_unguarded(p.first, m, l);</font></div><div class=""><font face="Menlo" class=""> return p.first;</font></div><div class=""><font face="Menlo" class=""> }</font></div><div class=""><font face="Menlo" class=""> f = m;</font></div><div class=""><font face="Menlo" class=""> m = p.second;</font></div><div class=""><font face="Menlo" class=""> p = swap_ranges(f, m, m, l);</font></div><div class=""><font face="Menlo" class=""> }</font></div><div class=""><font face="Menlo" class=""> return m;</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">2. The random access iteration can be implement in this way:</div><div class=""><br class=""></div><div class=""><div class=""><font face="Menlo" class="">template <RandomAccessIterator I></font></div><div class=""><font face="Menlo" class="">I rotate(I f, I m, I l, std::random_access_iterator_tag) {</font></div><div class=""><font face="Menlo" class=""> if (f == m) return l;</font></div><div class=""><font face="Menlo" class=""> if (m == l) return f;</font></div><div class=""><font face="Menlo" class=""> DifferenceType<I> cycles = gcd(m - f, l - m);</font></div><div class=""><font face="Menlo" class=""> rotate_transform<I> rotator(f, m, l);</font></div><div class=""><font face="Menlo" class=""> while (cycles-- > 0) rotate_cycle_from(f + cycles, rotator);</font></div><div class=""><font face="Menlo" class=""> return rotator.m1;</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><div class="">3. The bidirectional iteration can be implement by using reverse algorithm in this way:</div><div class=""><br class=""></div><div class=""><div class=""><div class=""><font face="Menlo" class="">template <BidirectionalIterator I></font></div><div class=""><font face="Menlo" class="">I rotate(I f, I m, I l, bidirectional_iterator_tag) {</font></div><div class=""><font face="Menlo" class=""> reverse(f, m);</font></div><div class=""><font face="Menlo" class=""> reverse(m, l);</font></div><div class=""><font face="Menlo" class=""> pair<I, I> p = reverse_until(f, m, l);</font></div><div class=""><font face="Menlo" class=""> reverse(p.first, p.second);</font></div><div class=""><font face="Menlo" class=""> if (m == p.first) return p.second;</font></div><div class=""><font face="Menlo" class=""> return p.first;</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><br class=""></div></div><div class=""><div class=""><br class=""></div></div><div class="">We need to hide the complexity of these algorithms, therefore we need to write a simple version that works for any type of iterations.</div><div class=""><br class=""></div><div class="">Shall I create a formal PR to swift-evolution with a proposed solution and detailed design?</div></div></div><div class=""><br class=""></div></div></div><div class="">Sergey</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On 14 Dec 2015, at 08:51, Dave Abrahams <<a href="mailto:dabrahams@apple.com" class="">dabrahams@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><blockquote type="cite" class="" style="font-family: Helvetica; font-size: 12px; 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;"><br class="Apple-interchange-newline">On Dec 13, 2015, at 2:20 AM, Sergey Bolshedvorsky via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class="">Hi everyone,<br class=""><br class="">I’ve selected a ticket SR-125 as my first task (<a href="https://bugs.swift.org/browse/SR-125" class="">https://bugs.swift.org/browse/SR-125</a>).<br class=""><br class="">I would like to propose an implementation of this method in Swift stdlib.<br class=""><br class="">std::rotate() method performs a left rotation on a range of elements.<br class="">C++ declaration is void rotate (ForwardIterator first, ForwardIterator middle, ForwardIterator last)<br class="">Specifically, it swaps the elements in the range [first, last) in such a way that the element middle becomes the first element of the new range and middle - 1 becomes the last element.<br class="">A precondition of this function is that [first, n_first) and [middle, last) are valid ranges.<br class=""><br class="">What are your thoughts?<br class=""></blockquote><div class="" style="font-family: Helvetica; font-size: 12px; 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;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; 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;">This is a really important algorithm, with applications even in GUI programming (see <a href="http://www.bfilipek.com/2014/12/top-5-beautiful-c-std-algorithms.html#slide" class="">slide</a> and <a href="http://www.bfilipek.com/2014/12/top-5-beautiful-c-std-algorithms.html#gather" class="">gather</a>), so I'm really happy someone is taking it on. You'll need different implementations <a href="http://stackoverflow.com/questions/21160875/why-is-stdrotate-so-fast" class="">depending on the index's protocol conformance</a>. C++ implementations can get <a href="http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/algorithm?view=markup&pathrev=251836" class="">pretty sophisticated</a>. Would you like additional thoughts (and if so, of what nature), or will those do? ;-)</div><div class="" style="font-family: Helvetica; font-size: 12px; 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;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; 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;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; 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;"><div class="">-Dave</div></div></div></blockquote></div><br class=""></body></html>