1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
/*
Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// C5 example: Views 2004-12-29 OBSOLETE
// Compile with
// csc /r:C5.dll Views.cs
using System;
using C5;
using SCG = System.Collections.Generic;
namespace Views {
class Views {
public static void Main(String[] args) {
IList<char> lst = new LinkedList<char>();
lst.AddAll<char>(new char[] { 'a', 'b', 'c', 'd' });
IList<char>
A = lst.View(0, 2),
B = lst.View(2, 0),
C = lst.View(2, 1),
D = lst.View(3, 1),
E = lst.View(4, 0),
F = lst.View(1, 2),
G = lst.View(0, 4);
IList<char>[] views = { A, B, C, D, E, F, G };
Console.WriteLine("ABCDEFG overlaps with:");
foreach (IList<char> u in views) {
foreach (IList<char> w in views)
Console.Write(Overlap(u, w) ? '+' : '-');
Console.WriteLine();
}
Console.WriteLine("ABCDEFG overlap length:");
foreach (IList<char> u in views) {
foreach (IList<char> w in views) {
int len = OverlapLength(u, w);
Console.Write(len >= 0 ? String.Format("{0}", len) : " ");
}
Console.WriteLine();
}
Console.WriteLine("ABCDEFG contained in:");
foreach (IList<char> u in views) {
foreach (IList<char> w in views)
Console.Write(ContainsView(u, w) ? '+' : '-');
Console.WriteLine();
}
}
public static int LeftEndIndex<T>(IList<T> u) {
return u.Offset;
}
public static int RightEndIndex<T>(IList<T> u) {
return u.Offset+u.Count;
}
public static bool Overlap<T>(IList<T> u, IList<T> w) {
if (u.Underlying == null || u.Underlying != w.Underlying)
throw new ArgumentException("views must have same underlying list");
else
return u.Offset < w.Offset+w.Count && w.Offset < u.Offset+u.Count;
}
public static int OverlapLength<T>(IList<T> u, IList<T> w) {
if (Overlap(u, w))
return Math.Min(u.Offset+u.Count, w.Offset+w.Count)
- Math.Max(u.Offset, w.Offset);
else
return -1; // No overlap
}
public static bool ContainsView<T>(IList<T> u, IList<T> w) {
if (u.Underlying == null || u.Underlying != w.Underlying)
throw new ArgumentException("views must have same underlying list");
else
if (w.Count > 0)
return u.Offset <= w.Offset && w.Offset+w.Count <= u.Offset+u.Count;
else
return u.Offset < w.Offset && w.Offset < u.Offset+u.Count;
}
public static bool SameUnderlying<T>(IList<T> u, IList<T> w) {
return (u.Underlying ?? u) == (w.Underlying ?? w);
}
// Replace the first occurrence of each x from xs by y in list:
public static void ReplaceXsByY<T>(HashedLinkedList<T> list, T[] xs, T y) {
foreach (T x in xs) {
using (IList<T> view = list.ViewOf(x)) {
if (view != null) {
view.Remove();
view.Add(y);
}
}
}
}
// Find first item that satisfies p
public static bool Find<T>(IList<T> list, Fun<T,bool> p, out T res) {
IList<T> view = list.View(0, 0);
while (view.Offset < list.Count) {
view.Slide(+1, 1);
if (p(view.First)) {
res = view.First;
return true;
}
}
res = default(T);
return false;
}
// Or, using that the list is enumerable:
public static bool Find1<T>(IList<T> list, Fun<T,bool> p, out T res) {
foreach (T x in list) {
if (p(x)) {
res = x;
return true;
}
}
res = default(T);
return false;
}
// Find last item that satisfies p
public static bool FindLast<T>(IList<T> list, Fun<T,bool> p, out T res) {
IList<T> view = list.View(list.Count, 0);
while (view.Offset > 0) {
view.Slide(-1, 1);
if (p(view.First)) {
res = view.First;
return true;
}
}
res = default(T);
return false;
}
}
}
|