* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / Mono.C5 / UserGuideExamples / RandomSelection.cs
1 /*\r
2  Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft\r
3  Permission is hereby granted, free of charge, to any person obtaining a copy\r
4  of this software and associated documentation files (the "Software"), to deal\r
5  in the Software without restriction, including without limitation the rights\r
6  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
7  copies of the Software, and to permit persons to whom the Software is\r
8  furnished to do so, subject to the following conditions:\r
9  \r
10  The above copyright notice and this permission notice shall be included in\r
11  all copies or substantial portions of the Software.\r
12  \r
13  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
14  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
15  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
16  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
17  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
18  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
19  SOFTWARE.\r
20 */\r
21 \r
22 // C5 example: RandomSelection.cs for pattern chapter\r
23 \r
24 // Compile with \r
25 //   csc /r:C5.dll RandomSelection.cs \r
26 \r
27 using System;\r
28 using C5;\r
29 using SCG = System.Collections.Generic;\r
30 \r
31 namespace RandomSelection {\r
32   class RandomSelection {\r
33     public static void Main(String[] args) {\r
34       ArrayList<int> list = new ArrayList<int>(), copy1, copy2;\r
35       list.AddAll(new int[] { 2, 3, 5, 7, 11, 13, 17, 19 });\r
36       copy1 = (ArrayList<int>)list.Clone();\r
37       copy2 = (ArrayList<int>)list.Clone();\r
38       const int N = 7;\r
39       Console.WriteLine("-- With replacement:");\r
40       foreach (int x in RandomWith(list, N))\r
41         Console.Write("{0} ", x);\r
42       Console.WriteLine("\n-- Without replacement:");\r
43       foreach (int x in RandomWithout1(copy1, N))\r
44         Console.Write("{0} ", x);\r
45       Console.WriteLine("\n-- Without replacement:");\r
46       foreach (int x in RandomWithout2(copy2, N))\r
47         Console.Write("{0} ", x);\r
48       Console.WriteLine();\r
49     }\r
50 \r
51     private static readonly C5Random rnd = new C5Random();\r
52 \r
53     // Select N random items from coll, with replacement.\r
54     // Does not modify the given list.\r
55 \r
56     public static SCG.IEnumerable<T> RandomWith<T>(IIndexed<T> coll, int N) {\r
57       for (int i=N; i>0; i--) { \r
58         T x = coll[rnd.Next(coll.Count)];\r
59         yield return x;\r
60       }\r
61     }\r
62 \r
63     // Select N random items from list, without replacement.\r
64     // Modifies the given list.\r
65 \r
66     public static SCG.IEnumerable<T> RandomWithout1<T>(IList<T> list, int N) {\r
67       list.Shuffle(rnd);     \r
68       foreach (T x in list.View(0, N)) \r
69         yield return x;\r
70     }\r
71 \r
72     // Select N random items from list, without replacement.\r
73     // Faster when list is efficiently indexable and modifiable.\r
74     // Modifies the given list.\r
75 \r
76     public static SCG.IEnumerable<T> RandomWithout2<T>(ArrayList<T> list, int N) {\r
77       for (int i=N; i>0; i--) { \r
78         int j = rnd.Next(list.Count);\r
79         T x = list[j], replacement = list.RemoveLast();\r
80         if (j < list.Count) \r
81           list[j] = replacement;\r
82         yield return x;\r
83       }\r
84     }\r
85   }\r
86 }\r