Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ViewPatterns.cs « UserGuideExamples « Mono.C5 « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c1330ed6f109d8af362e4a77f7769cd53715ef34 (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
 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: ViewPatterns 2005-07-22

// Compile with 
//   csc /r:C5.dll ViewPatterns.cs 

using System;
using C5;
using SCG = System.Collections.Generic;

namespace ViewPatterns {
  class Views {
    public static void Main(String[] args) {
      IList<char> lst = new ArrayList<char>();
      lst.AddAll<char>(new char[] { 'a', 'b', 'c', 'd' });
      IList<char> v1 = lst.View(1, 1);
      Console.WriteLine("v1 = {0}", v1);
      InsertBeforeFirst(v1, '<', 'b');
      InsertAfterFirst(v1, '>', 'b');
      Console.WriteLine("v1 = {0}", v1);
      char x; 
      if (SequencePredecessor(v1, 'b', out x)) 
        Console.WriteLine("Predecessor of b is " + x);
      if (SequenceSuccessor(v1, 'b', out x)) 
        Console.WriteLine("Successor of b is " + x);
      if (!SequencePredecessor(v1, 'c', out x)) 
        Console.WriteLine("c has no predecessor");
      if (!SequenceSuccessor(v1, 'a', out x)) 
        Console.WriteLine("a has no successor");
      IList<char> lst2 = new ArrayList<char>();
      lst2.AddAll<char>(new char[] { 'a', 'b', 'c', 'A', 'a', 'd', 'a' });
      foreach (int i in IndexesOf(lst2, 'a')) 
        Console.Write("{0} ", i);
      Console.WriteLine();
      foreach (int i in ReverseIndexesOf(lst2, 'a')) 
        Console.Write("{0} ", i);
      Console.WriteLine();
      Console.WriteLine(lst2);
      IList<char> view = lst2.View(2,0);
      InsertAtView(lst2, view, 'y');
      Console.WriteLine(lst2);
      InsertIntoView(view, 'x');
      Console.WriteLine(lst2);
    }

    // --- Patterns for zero-item views -----------------------------

    // Number of items before zero-item view
   
    public static int ItemsBefore<T>(IList<T> view) {
      return view.Offset;
    }

    // Number of items after zero-item view
   
    public static int ItemsAfter<T>(IList<T> view) {
      return view.Underlying.Count - view.Offset;
    }

    // Move (zero-item) view one item to the left
   
    public static void MoveLeft<T>(IList<T> view) {
      // One of these:
      view.Slide(-1);
      view.TrySlide(-1);
    }

    // Move (zero-item) view one item to the right
   
    public static void MoveRight<T>(IList<T> view) {
      // One of these:
      view.Slide(+1);
      view.TrySlide(+1);
    }

    // Test whether (zero-item) view is at beginning of list
   
    public static bool AtBeginning<T>(IList<T> view) {
      return view.Offset == 0;
    }

    // Test whether (zero-item) view is at end of list
   
    public static bool AtEnd<T>(IList<T> view) {
      return view.Offset == view.Underlying.Count;
    }

    // Insert x into zero-item view and into underlying list
   
    public static void InsertIntoView<T>(IList<T> view, T x) {
      view.Add(x);
    }

    // Insert x into list at zero-item view 
   
    public static void InsertAtView<T>(IList<T> list, IList<T> view, T x) {
      list.Insert(view, x);
    }

    // Delete the item before zero-item view 
   
    public static void DeleteBefore<T>(IList<T> view) {
      view.Slide(-1,1).RemoveFirst();
    }

    // Delete the item after zero-item view 
   
    public static void DeleteAfter<T>(IList<T> view) {
      view.Slide(0,1).RemoveFirst();
    }

    // Get the zero-item view at left endpoint.  Succeeds on all lists
    // and valid views.

    public static IList<T> LeftEndView<T>(IList<T> list) {
      return list.View(0,0);
    }

    // Get the zero-item view at right endpoint.  Succeeds on all
    // lists and valid views.

    public static IList<T> RightEndView<T>(IList<T> list) {
      return list.View(list.Count,0);
    }


    // --- Patterns for one-item views ------------------------------

    // Find the sequence predecessor x of y; or throw exception

    public static T SequencePredecessor<T>(IList<T> list, T y) {
      return list.ViewOf(y).Slide(-1)[0];
    }  
    
    // Find the sequence predecessor x of y; or return false 

    public static bool SequencePredecessor<T>(IList<T> list, T y, out T x) {
      IList<T> view = list.ViewOf(y);
      bool ok = view != null && view.TrySlide(-1);
      x = ok ? view[0] : default(T);
      return ok;
    }  

    // Find the sequence successor x of y; or throw exception

    public static T SequenceSuccessor<T>(IList<T> list, T y) {
      return list.ViewOf(y).Slide(+1)[0];
    }  

    // Find the sequence successor x of y; or return false

    public static bool SequenceSuccessor<T>(IList<T> list, T y, out T x) {
      IList<T> view = list.ViewOf(y);
      bool ok = view != null && view.TrySlide(+1);
      x = ok ? view[0] : default(T);
      return ok;
    }  

    // Insert x into list after first occurrence of y (or throw
    // NullReferenceException).
    
    public static void InsertAfterFirst<T>(IList<T> list, T x, T y) {
      list.Insert(list.ViewOf(y), x);
    }

    // Insert x into list before first occurrence of y (or throw
    // NullReferenceException)
    
    public static void InsertBeforeFirst<T>(IList<T> list, T x, T y) {
      list.Insert(list.ViewOf(y).Slide(0, 0), x);
    }

    // Insert x into list after last occurrence of y (or throw
    // NullReferenceException).
    
    public static void InsertAfterLast<T>(IList<T> list, T x, T y) {
      list.Insert(list.LastViewOf(y), x);
    }

    // Insert x into list before last occurrence of y (or throw
    // NullReferenceException)
    
    public static void InsertBeforeLast<T>(IList<T> list, T x, T y) {
      list.Insert(list.LastViewOf(y).Slide(0, 0), x);
    }

    // Same meaning as InsertBeforeFirst on a proper list, but not on
    // a view

    public static void InsertBeforeFirstAlt<T>(IList<T> list, T x, T y) {
      list.ViewOf(y).InsertFirst(x);
    }

    // Delete the sequence predecessor of first y; or throw exception

    public static T RemovePredecessorOfFirst<T>(IList<T> list, T y) {
      return list.ViewOf(y).Slide(-1).Remove();
    }

    // Delete the sequence successor of first y; or throw exception

    public static T RemoveSuccessorOfFirst<T>(IList<T> list, T y) {
      return list.ViewOf(y).Slide(+1).Remove();
    }

    // --- Other view patterns --------------------------------------

    // 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);
          }
        }
      }
    }

    // Get index in underlying list of view's left end
    
    public static int LeftEndIndex<T>(IList<T> view) { 
      return view.Offset;
    }

    // Get index in underlying list of view's right end

    public static int RightEndIndex<T>(IList<T> view) { 
      return view.Offset + view.Count;
    }

    // Test whether views overlap 

    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;
    }

    // Find the length of the overlap between two views

    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
    }

    // Test whether view u contains view v 

    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;
    }

    // Test whether views u and v have (or are) the same underlying list

    public static bool SameUnderlying<T>(IList<T> u, IList<T> w) { 
      return (u.Underlying ?? u) == (w.Underlying ?? w);
    }

    // Find the index of the first item that satisfies p

    public static int FindFirstIndex<T>(IList<T> list, Fun<T,bool> p) {
      using (IList<T> view = list.View(0, 0)) {
        while (view.TrySlide(0, 1)) {
          if (p(view.First)) 
            return view.Offset;
          view.Slide(+1, 0);
        }
      }
      return -1;
    }

    // Find the index of the last item that satisfies p
    
    public static int FindLastIndex<T>(IList<T> list, Fun<T,bool> p) {
      using (IList<T> view = list.View(list.Count, 0)) {
        while (view.TrySlide(-1, 1)) {
          if (p(view.First)) 
            return view.Offset;
        }
      }
      return -1;
    }

    // Yield indexes of all items equal to x, in list order:

    public static SCG.IEnumerable<int> IndexesOf<T>(IList<T> list, T x) { 
      IList<T> tail = list.View(0, list.Count);
      tail = tail.ViewOf(x);
      while (tail != null) {
        yield return tail.Offset;
        tail = tail.Slide(+1,0).Span(list);
        tail = tail.ViewOf(x); 
      }
    }

    // Yield indexes of items equal to x, in reverse list order.

    public static SCG.IEnumerable<int> ReverseIndexesOf<T>(IList<T> list, T x) {
      IList<T> head = list.View(0, list.Count);
      head = head.LastViewOf(x);
      while (head != null) {
        yield return head.Offset;
        head = list.Span(head.Slide(0,0));
        head = head.LastViewOf(x);
      }
    }

  }
}