Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / libs / c5 / UserGuideExamples / WrappedArray.cs @ 3

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: WrappedArray 2005-07-21
23

    
24
// Compile with 
25
//   csc /r:C5.dll WrappedArray.cs 
26

    
27
using System;
28
using C5;
29
using SCG = System.Collections.Generic;
30

    
31
namespace WrappedArray {
32
  class WrappedArray {
33
    public static void Main(String[] args) {
34
    }
35

    
36

    
37
    // System.Array.Exists
38

    
39
    public static bool Exists<T>(T[] arr, Fun<T,bool> p) {
40
      return new WrappedArray<T>(arr).Exists(p);
41
    }  
42

    
43
    // System.Array.TrueForAll
44

    
45
    public static bool TrueForAll<T>(T[] arr, Fun<T,bool> p) {
46
      return new WrappedArray<T>(arr).All(p);
47
    }  
48

    
49
    // System.Array.Find(T[], Predicate)
50
    // This loses the valuable bool returned by C5 Find.
51

    
52
    public static T Find<T>(T[] arr, Fun<T,bool> p) {
53
      T res; 
54
      new WrappedArray<T>(arr).Find(p, out res);
55
      return res;
56
    }  
57

    
58
    // System.Array.FindAll(T[], Predicate)
59

    
60
    public static T[] FindAll<T>(T[] arr, Fun<T,bool> p) {
61
      return new WrappedArray<T>(arr).FindAll(p).ToArray();
62
    }  
63

    
64
    // System.Array.FindIndex(T[], Predicate)
65

    
66
    public static int FindIndex<T>(T[] arr, Fun<T,bool> p) {
67
      return new WrappedArray<T>(arr).FindIndex(p);
68
    }  
69

    
70
    // System.Array.FindIndex(T[], int, Predicate)
71

    
72
    public static int FindIndex<T>(T[] arr, int i, Fun<T,bool> p) {
73
      int j = new WrappedArray<T>(arr).View(i,arr.Length-i).FindIndex(p);
74
      return j < 0 ? j : j+i;
75
    }  
76

    
77
    // System.Array.FindIndex(T[], int, int, Predicate)
78

    
79
    public static int FindIndex<T>(T[] arr, int i, int n, Fun<T,bool> p) {
80
      int j = new WrappedArray<T>(arr).View(i,n).FindIndex(p);
81
      return j < 0 ? j : j+i;
82
    }  
83

    
84
    // System.Array.FindLast(T[], Predicate)
85
    // This loses the valuable bool returned by C5 Find.
86

    
87
    public static T FindLast<T>(T[] arr, Fun<T,bool> p) {
88
      T res; 
89
      new WrappedArray<T>(arr).FindLast(p, out res);
90
      return res;
91
    }  
92

    
93
    // System.Array.FindLastIndex(T[], Predicate)
94

    
95
    public static int FindLastIndex<T>(T[] arr, Fun<T,bool> p) {
96
      return new WrappedArray<T>(arr).FindIndex(p);
97
    }  
98

    
99
    // System.Array.FindLastIndex(T[], int, Predicate)
100

    
101
    public static int FindLastIndex<T>(T[] arr, int i, Fun<T,bool> p) {
102
      int j = new WrappedArray<T>(arr).View(i,arr.Length-i).FindIndex(p);
103
      return j < 0 ? j : j+i;
104
    }  
105

    
106
    // System.Array.FindLastIndex(T[], int, int, Predicate)
107

    
108
    public static int FindLastIndex<T>(T[] arr, int i, int n, Fun<T,bool> p) {
109
      int j = new WrappedArray<T>(arr).View(i,n).FindIndex(p);
110
      return j < 0 ? j : j+i;
111
    }  
112
    
113
    // System.Array.ForEach(T[], Action)
114

    
115
    public static void ForEach<T>(T[] arr, Act<T> act) {
116
      new WrappedArray<T>(arr).Apply(act);
117
    }  
118

    
119
    // System.Array.IndexOf(T[], T)
120

    
121
    public static int IndexOf<T>(T[] arr, T x) {
122
      int j = new WrappedArray<T>(arr).IndexOf(x);
123
      return j < 0 ? -1 : j;
124
    }  
125
    
126
    // System.Array.IndexOf(T[], T, int)
127

    
128
    public static int IndexOf<T>(T[] arr, T x, int i) {
129
      int j = new WrappedArray<T>(arr).View(i, arr.Length-i).IndexOf(x);
130
      return j < 0 ? -1 : j+i;
131
    }  
132
    
133
    // System.Array.IndexOf(T[], T, int, int)
134

    
135
    public static int IndexOf<T>(T[] arr, T x, int i, int n) {
136
      int j = new WrappedArray<T>(arr).View(i, n).IndexOf(x);
137
      return j < 0 ? -1 : j+i;
138
    }  
139

    
140
    // System.Array.LastIndexOf(T[], T)
141

    
142
    public static int LastIndexOf<T>(T[] arr, T x) {
143
      int j = new WrappedArray<T>(arr).LastIndexOf(x);
144
      return j < 0 ? -1 : j;
145
    }  
146
    
147
    // System.Array.LastIndexOf(T[], T, int)
148

    
149
    public static int LastIndexOf<T>(T[] arr, T x, int i) {
150
      int j = new WrappedArray<T>(arr).View(i, arr.Length-i).LastIndexOf(x);
151
      return j < 0 ? -1 : j+i;
152
    }  
153
    
154
    // System.Array.LastIndexOf(T[], T, int, int)
155

    
156
    public static int LastIndexOf<T>(T[] arr, T x, int i, int n) {
157
      int j = new WrappedArray<T>(arr).View(i, n).LastIndexOf(x);
158
      return j < 0 ? -1 : j+i;
159
    }  
160

    
161
    // System.Array.Sort(T[])
162

    
163
    public static void Sort<T>(T[] arr) {
164
      new WrappedArray<T>(arr).Sort();
165
    }  
166

    
167
    // System.Array.Sort(T[], int, int)
168

    
169
    public static void Sort<T>(T[] arr, int i, int n) {
170
      new WrappedArray<T>(arr).View(i, n).Sort();
171
    }  
172

    
173
    // System.Array.Sort(T[], SCG.IComparer<T>)
174

    
175
    public static void Sort<T>(T[] arr, SCG.IComparer<T> cmp) {
176
      new WrappedArray<T>(arr).Sort(cmp);
177
    }  
178
    
179
    // System.Array.Sort(T[], int, int, SCG.IComparer<T>)
180

    
181
    public static void Sort<T>(T[] arr, int i, int n, SCG.IComparer<T> cmp) {
182
      new WrappedArray<T>(arr).View(i, n).Sort(cmp);
183
    }  
184
    
185
    // System.Array.Sort(T[], Comparison)
186

    
187
    public static void Sort<T>(T[] arr, Comparison<T> csn) {
188
      new WrappedArray<T>(arr).Sort(new DelegateComparer<T>(csn));
189
    }  
190
  }
191
}