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
10 The above copyright notice and this permission notice shall be included in
\r
11 all copies or substantial portions of the Software.
\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
22 // C5 example: ListPatterns.cs for pattern chapter
\r
25 // csc /r:C5.dll ListPatterns.cs
\r
29 using SCG = System.Collections.Generic;
\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
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
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
62 // Reverse list[i..i+n-1]
\r
64 public static void ReverseInterval<T>(IList<T> list, int i, int n) {
\r
65 list.View(i,n).Reverse();
\r
68 // Swap list[0..i-1] with list[i..Count-1]
\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
76 // Remove sublist of a list
\r
78 public static void RemoveSublist1<T>(IList<T> list, int i, int n) {
\r
79 list.RemoveInterval(i, n);
\r
82 public static void RemoveSublist2<T>(IList<T> list, int i, int n) {
\r
83 list.View(i, n). Clear();
\r
87 // Remove tail of a list
\r
89 public static void RemoveTail1<T>(IList<T> list, int i) {
\r
90 list.RemoveInterval(i, list.Count-i);
\r
93 public static void RemoveTail2<T>(IList<T> list, int i) {
\r
94 list.View(i, list.Count-i).Clear();
\r
97 // Pattern for finding and using first (leftmost) x in list
\r
99 private static void PatFirst<T>(IList<T> list, T x) {
\r
100 int j = list.IndexOf(x);
\r
102 // x is a position j in list
\r
104 // x is not in list
\r
108 // Pattern for finding and using last (rightmost) x in list
\r
110 private static void PatLast<T>(IList<T> list, T x) {
\r
111 int j = list.LastIndexOf(x);
\r
113 // x is at position j in list
\r
115 // x is not in list
\r
119 // Pattern for finding and using first (leftmost) x in list[i..i+n-1]
\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
124 // x is at position j+i in list
\r
126 // x is not in list[i..i+n-1]
\r
130 // Pattern for finding and using last (rightmost) x in list[i..i+n-1]
\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
135 // x is at position j+i in list
\r
137 // x is not in list[i..i+n-1]
\r