1
|
/*
|
2
|
Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
|
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
of this software and associated documentation files (the "Software"), to deal
|
5
|
in the Software without restriction, including without limitation the rights
|
6
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
copies of the Software, and to permit persons to whom the Software is
|
8
|
furnished to do so, subject to the following conditions:
|
9
|
|
10
|
The above copyright notice and this permission notice shall be included in
|
11
|
all copies or substantial portions of the Software.
|
12
|
|
13
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
SOFTWARE.
|
20
|
*/
|
21
|
|
22
|
// C5 example: ListPatterns.cs for pattern chapter
|
23
|
|
24
|
// Compile with
|
25
|
// csc /r:C5.dll ListPatterns.cs
|
26
|
|
27
|
using System;
|
28
|
using C5;
|
29
|
using SCG = System.Collections.Generic;
|
30
|
|
31
|
namespace ListPatterns {
|
32
|
class ListPatterns {
|
33
|
public static void Main(String[] args) {
|
34
|
IList<int> list = new ArrayList<int>();
|
35
|
list.AddAll(new int[] { 23, 29, 31, 37, 41, 43, 47, 53 });
|
36
|
// Reversing and swapping
|
37
|
Console.WriteLine(list);
|
38
|
list.Reverse();
|
39
|
Console.WriteLine(list);
|
40
|
ReverseInterval(list, 2, 3);
|
41
|
Console.WriteLine(list);
|
42
|
SwapInitialFinal(list, 2);
|
43
|
Console.WriteLine(list);
|
44
|
// Clearing all or part of list
|
45
|
list.CollectionCleared
|
46
|
+= delegate(Object c, ClearedEventArgs eargs) {
|
47
|
ClearedRangeEventArgs ceargs = eargs as ClearedRangeEventArgs;
|
48
|
if (ceargs != null)
|
49
|
Console.WriteLine("Cleared [{0}..{1}]",
|
50
|
ceargs.Start, ceargs.Start+ceargs.Count-1);
|
51
|
};
|
52
|
RemoveSublist1(list, 1, 2);
|
53
|
Console.WriteLine(list);
|
54
|
RemoveSublist2(list, 1, 2);
|
55
|
Console.WriteLine(list);
|
56
|
RemoveTail1(list, 3);
|
57
|
Console.WriteLine(list);
|
58
|
RemoveTail2(list, 2);
|
59
|
Console.WriteLine(list);
|
60
|
}
|
61
|
|
62
|
// Reverse list[i..i+n-1]
|
63
|
|
64
|
public static void ReverseInterval<T>(IList<T> list, int i, int n) {
|
65
|
list.View(i,n).Reverse();
|
66
|
}
|
67
|
|
68
|
// Swap list[0..i-1] with list[i..Count-1]
|
69
|
|
70
|
public static void SwapInitialFinal<T>(IList<T> list, int i) {
|
71
|
list.View(0,i).Reverse();
|
72
|
list.View(i,list.Count-i).Reverse();
|
73
|
list.Reverse();
|
74
|
}
|
75
|
|
76
|
// Remove sublist of a list
|
77
|
|
78
|
public static void RemoveSublist1<T>(IList<T> list, int i, int n) {
|
79
|
list.RemoveInterval(i, n);
|
80
|
}
|
81
|
|
82
|
public static void RemoveSublist2<T>(IList<T> list, int i, int n) {
|
83
|
list.View(i, n). Clear();
|
84
|
}
|
85
|
|
86
|
|
87
|
// Remove tail of a list
|
88
|
|
89
|
public static void RemoveTail1<T>(IList<T> list, int i) {
|
90
|
list.RemoveInterval(i, list.Count-i);
|
91
|
}
|
92
|
|
93
|
public static void RemoveTail2<T>(IList<T> list, int i) {
|
94
|
list.View(i, list.Count-i).Clear();
|
95
|
}
|
96
|
|
97
|
// Pattern for finding and using first (leftmost) x in list
|
98
|
|
99
|
private static void PatFirst<T>(IList<T> list, T x) {
|
100
|
int j = list.IndexOf(x);
|
101
|
if (j >= 0) {
|
102
|
// x is a position j in list
|
103
|
} else {
|
104
|
// x is not in list
|
105
|
}
|
106
|
}
|
107
|
|
108
|
// Pattern for finding and using last (rightmost) x in list
|
109
|
|
110
|
private static void PatLast<T>(IList<T> list, T x) {
|
111
|
int j = list.LastIndexOf(x);
|
112
|
if (j >= 0) {
|
113
|
// x is at position j in list
|
114
|
} else {
|
115
|
// x is not in list
|
116
|
}
|
117
|
}
|
118
|
|
119
|
// Pattern for finding and using first (leftmost) x in list[i..i+n-1]
|
120
|
|
121
|
private static void PatFirstSublist<T>(IList<T> list, T x, int i, int n) {
|
122
|
int j = list.View(i,n).IndexOf(x);
|
123
|
if (j >= 0) {
|
124
|
// x is at position j+i in list
|
125
|
} else {
|
126
|
// x is not in list[i..i+n-1]
|
127
|
}
|
128
|
}
|
129
|
|
130
|
// Pattern for finding and using last (rightmost) x in list[i..i+n-1]
|
131
|
|
132
|
private static void PatLastSublist<T>(IList<T> list, T x, int i, int n) {
|
133
|
int j = list.View(i,n).LastIndexOf(x);
|
134
|
if (j >= 0) {
|
135
|
// x is at position j+i in list
|
136
|
} else {
|
137
|
// x is not in list[i..i+n-1]
|
138
|
}
|
139
|
}
|
140
|
}
|
141
|
}
|