Bringing C5 1.0 into the main branch.
[mono.git] / mcs / class / Mono.C5 / current / UserGuideExamples / ListPatterns.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: ListPatterns.cs for pattern chapter\r
23 \r
24 // Compile with \r
25 //   csc /r:C5.dll ListPatterns.cs \r
26 \r
27 using System;\r
28 using C5;\r
29 using SCG = System.Collections.Generic;\r
30 \r
31 namespace ListPatterns {\r
32   class ListPatterns {\r
33     public static void Main(String[] args) {\r
34       IList<int> list = new ArrayList<int>();\r
35       list.AddAll(new int[] { 23, 29, 31, 37, 41, 43, 47, 53 });\r
36       // Reversing and swapping\r
37       Console.WriteLine(list);\r
38       list.Reverse();\r
39       Console.WriteLine(list);\r
40       ReverseInterval(list, 2, 3);\r
41       Console.WriteLine(list);\r
42       SwapInitialFinal(list, 2);\r
43       Console.WriteLine(list);\r
44       // Clearing all or part of list\r
45       list.CollectionCleared \r
46         += delegate(Object c, ClearedEventArgs eargs) {\r
47              ClearedRangeEventArgs ceargs = eargs as ClearedRangeEventArgs;\r
48              if (ceargs != null) \r
49                Console.WriteLine("Cleared [{0}..{1}]", \r
50                                  ceargs.Start, ceargs.Start+ceargs.Count-1);\r
51            };\r
52       RemoveSublist1(list, 1, 2);\r
53       Console.WriteLine(list);\r
54       RemoveSublist2(list, 1, 2);\r
55       Console.WriteLine(list);\r
56       RemoveTail1(list, 3);\r
57       Console.WriteLine(list);\r
58       RemoveTail2(list, 2);\r
59       Console.WriteLine(list);\r
60     }\r
61 \r
62     // Reverse list[i..i+n-1]\r
63 \r
64     public static void ReverseInterval<T>(IList<T> list, int i, int n) {\r
65       list.View(i,n).Reverse();\r
66     }\r
67 \r
68     // Swap list[0..i-1] with list[i..Count-1]\r
69 \r
70     public static void SwapInitialFinal<T>(IList<T> list, int i) {\r
71       list.View(0,i).Reverse();\r
72       list.View(i,list.Count-i).Reverse();\r
73       list.Reverse();\r
74     }\r
75 \r
76     // Remove sublist of a list\r
77 \r
78     public static void RemoveSublist1<T>(IList<T> list, int i, int n) {\r
79       list.RemoveInterval(i, n);\r
80     }\r
81 \r
82     public static void RemoveSublist2<T>(IList<T> list, int i, int n) {\r
83       list.View(i, n). Clear();\r
84     }\r
85 \r
86 \r
87     // Remove tail of a list\r
88 \r
89     public static void RemoveTail1<T>(IList<T> list, int i) {\r
90       list.RemoveInterval(i, list.Count-i);\r
91     }\r
92 \r
93     public static void RemoveTail2<T>(IList<T> list, int i) {\r
94       list.View(i, list.Count-i).Clear();\r
95     }\r
96 \r
97     // Pattern for finding and using first (leftmost) x in list\r
98 \r
99     private static void PatFirst<T>(IList<T> list, T x) { \r
100       int j = list.IndexOf(x);\r
101       if (j >= 0) { \r
102         // x is a position j in list\r
103       } else {\r
104         // x is not in list\r
105       }\r
106     }\r
107 \r
108     // Pattern for finding and using last (rightmost) x in list\r
109 \r
110     private static void PatLast<T>(IList<T> list, T x) { \r
111       int j = list.LastIndexOf(x);\r
112       if (j >= 0) { \r
113         // x is at position j in list\r
114       } else {\r
115         // x is not in list\r
116       }\r
117     }\r
118 \r
119     // Pattern for finding and using first (leftmost) x in list[i..i+n-1]\r
120 \r
121     private static void PatFirstSublist<T>(IList<T> list, T x, int i, int n) { \r
122       int j = list.View(i,n).IndexOf(x);\r
123       if (j >= 0) { \r
124         // x is at position j+i in list\r
125       } else {\r
126         // x is not in list[i..i+n-1]\r
127       }\r
128     }\r
129 \r
130     // Pattern for finding and using last (rightmost) x in list[i..i+n-1]\r
131 \r
132     private static void PatLastSublist<T>(IList<T> list, T x, int i, int n) { \r
133       int j = list.View(i,n).LastIndexOf(x);\r
134       if (j >= 0) { \r
135         // x is at position j+i in list\r
136       } else {\r
137         // x is not in list[i..i+n-1]\r
138       }\r
139     }\r
140   }\r
141 }\r