<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Recursive iterator performance, part 3</title><link>http://blogs.msdn.com/b/toub/archive/2004/11/04/252415.aspx</link><description>Last week and weekend I discussed performance of iterators in C# 2.0, specifically recursive iterators. Recursive iterator performance Recursive iterator performance, part 2 
 To briefly recap, the ability to write recursive iterators is, quite frankly</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Recursive iterator performance, part 3</title><link>http://blogs.msdn.com/b/toub/archive/2004/11/04/252415.aspx#8331051</link><pubDate>Sat, 22 Mar 2008 14:51:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8331051</guid><dc:creator>Alexandr K.</dc:creator><description>&lt;p&gt;I forget my simple Swap function :-D&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;#region . Swap 2 elements in Array .&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private static void Swap&amp;lt;T&amp;gt;(List&amp;lt;T&amp;gt; arr, int pos1, int pos2)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (pos1 != pos2)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;T temp = arr[pos1]; arr[pos1] = arr[pos2]; arr[pos2] = temp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;} &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;#endregion&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8331051" width="1" height="1"&gt;</description></item><item><title>re: Recursive iterator performance, part 3</title><link>http://blogs.msdn.com/b/toub/archive/2004/11/04/252415.aspx#8331047</link><pubDate>Sat, 22 Mar 2008 14:49:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8331047</guid><dc:creator>Alexandr K.</dc:creator><description>&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;#region . Permute .&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static IEnumerable&amp;lt;List&amp;lt;T&amp;gt;&amp;gt; Permute&amp;lt;T&amp;gt;(List&amp;lt;T&amp;gt; arr)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;foreach (List&amp;lt;T&amp;gt; permutedArr in Permute(arr, 0, arr.Count - 1))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;yield return permutedArr;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private static IEnumerable&amp;lt;List&amp;lt;T&amp;gt;&amp;gt; Permute&amp;lt;T&amp;gt;(List&amp;lt;T&amp;gt; arr, int lowerBound, int upperBound)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (lowerBound == upperBound) yield return arr;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;for (int i = lowerBound; i &amp;lt;= upperBound; i++)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Swap(arr, i, lowerBound);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;foreach (List&amp;lt;T&amp;gt; permutedArr in Permute(arr, lowerBound + 1, upperBound))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;yield return permutedArr;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Swap(arr, i, lowerBound);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;} &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;#endregion&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public permutationsForm()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;InitializeComponent();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private void buttonPermute_Click(object sender, EventArgs e)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;List&amp;lt;string&amp;gt; array = new List&amp;lt;string&amp;gt;();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;array.AddRange( textPermuteSize.Text.Split(' ') );&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;foreach (List&amp;lt;string&amp;gt; s in Permute&amp;lt;string&amp;gt;(array))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;foreach (string s1 in s)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;textPremutations.Text += s1;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;textPremutations.Text += &amp;quot;; &amp;quot;;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8331047" width="1" height="1"&gt;</description></item><item><title>I disagree</title><link>http://blogs.msdn.com/b/toub/archive/2004/11/04/252415.aspx#4386468</link><pubDate>Tue, 14 Aug 2007 20:33:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4386468</guid><dc:creator>warsaw hotels</dc:creator><description>&lt;p&gt;Last week and weekend I discussed performance of iterators in C# 2.0, specifically recursive iterators.Recursive iterator performanceRecursive iterator performance, part 2&lt;/p&gt;
&lt;p&gt;I do not agree. Go to &lt;a rel="nofollow" target="_new" href="http://apartments.waw.pl/"&gt;http://apartments.waw.pl/&lt;/a&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4386468" width="1" height="1"&gt;</description></item><item><title>re: Recursive iterator performance, part 3</title><link>http://blogs.msdn.com/b/toub/archive/2004/11/04/252415.aspx#252509</link><pubDate>Thu, 04 Nov 2004 23:37:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:252509</guid><dc:creator>Stephen Toub</dc:creator><description>Of course, there are a plethora of ways to implement a permutation enumeration, some that use recursion and some that don't, some that maintain lexicographical ordering, some that maintain other orderings, etc.  The code you show above doesn't compile (and fixing the compiler errors appropriately results in IndexOutOfRange exceptions being thrown at runtime), but I think you intended something like the following which does work correctly:&lt;br&gt;&lt;br&gt;    public static IEnumerable&amp;lt;T[]&amp;gt; Permute&amp;lt;T&amp;gt;(T[] arr) where T : IComparable&lt;br&gt;    {&lt;br&gt;        Array.Sort(arr);&lt;br&gt;        int upperBound = arr.Length - 1;&lt;br&gt;        while (true)&lt;br&gt;        {&lt;br&gt;            yield return arr;&lt;br&gt;&lt;br&gt;            int i, j;&lt;br&gt;            for (i = upperBound - 1; i &amp;gt;= 0 &amp;amp;&amp;amp; arr[i].CompareTo(arr[i + 1]) &amp;gt; 0; i--);&lt;br&gt;            if (i &amp;lt; 0) break;&lt;br&gt;&lt;br&gt;            for (j = upperBound; arr[i].CompareTo(arr[j]) &amp;gt; 0; j--) ;&lt;br&gt;&lt;br&gt;            Swap(arr, i, j);&lt;br&gt;            Array.Reverse(arr, i + 1, upperBound - i);&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;&lt;br&gt;Note that I've restricted the generic T parameter to be an IComparable so that I can use the CompareTo method on each element of the array (operators won't work).&lt;br&gt;&lt;br&gt;In the previous recursive-&amp;gt;iterative example I was just demonstrating one way to do it, and more specifically showing that there's a pattern you can follow to go from recursive to iterative.  That doesn't mean it's always something you want to do, as I've mentioned.&lt;br&gt;&lt;br&gt;In any event, if you're interested in permutation algorithms, Robert Sedgewick wrote a good survey of a bunch of them in the 70s, and it's available at &lt;a target="_new" href="http://portal.acm.org/ft_gateway.cfm?id=356692&amp;amp;type=pdf"&gt;http://portal.acm.org/ft_gateway.cfm?id=356692&amp;amp;type=pdf&lt;/a&gt;.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=252509" width="1" height="1"&gt;</description></item><item><title>re: Recursive iterator performance, part 3</title><link>http://blogs.msdn.com/b/toub/archive/2004/11/04/252415.aspx#252454</link><pubDate>Thu, 04 Nov 2004 21:41:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:252454</guid><dc:creator>Steve Newman</dc:creator><description>Sometimes, there's an iterative algorithm that doesn't require a stack.  Iterating over permutations is an example:&lt;br&gt;&lt;br&gt;public static IEnumerable&amp;lt;T[]&amp;gt; Permute&amp;lt;T&amp;gt;(T[] arr) &lt;br&gt;{&lt;br&gt;   Array.Sort(arr);&lt;br&gt;   while (true)&lt;br&gt;   {&lt;br&gt;      yield return arr;&lt;br&gt;      for (int i=arr.Length-2; ; i--)&lt;br&gt;         if (arr[i] &amp;lt; arr[i+1])&lt;br&gt;         {&lt;br&gt;         Array.Reverse(arr, i+1, arr.Length-i-1);&lt;br&gt;         for (int j=i+1; ; j++)&lt;br&gt;            if (arr[i] &amp;lt; arr[j])&lt;br&gt;               { T t = arr[i]; arr[i] = arr[j]; arr[j] = t; break; }&lt;br&gt;         &lt;br&gt;         break;&lt;br&gt;         }&lt;br&gt;         else if (i == 0)&lt;br&gt;            return;&lt;br&gt;   }&lt;br&gt;}&lt;br&gt;&lt;br&gt;This algorithm has the advantages of returning the permutations in lexicographic order, and it works even if the array has duplicate entries (each unique permutation is only returned once).  It does require that T define a sorting order.  (Meaning that it won't compile as written -- T needs to be restricted to IComparable, and it should use CompareTo instead of the &amp;lt; and &amp;gt; operators.  Apologies, I don't have access to a C# 2.0 compiler -- this code is adapted from an algorithm to permute ints.)&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=252454" width="1" height="1"&gt;</description></item></channel></rss>