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: WrappedArray 2005-07-21
\r
25 // csc /r:C5.dll WrappedArray.cs
\r
29 using SCG = System.Collections.Generic;
\r
31 namespace WrappedArray {
\r
32 class WrappedArray {
\r
33 public static void Main(String[] args) {
\r
37 // System.Array.Exists
\r
39 public static bool Exists<T>(T[] arr, Fun<T,bool> p) {
\r
40 return new WrappedArray<T>(arr).Exists(p);
\r
43 // System.Array.TrueForAll
\r
45 public static bool TrueForAll<T>(T[] arr, Fun<T,bool> p) {
\r
46 return new WrappedArray<T>(arr).All(p);
\r
49 // System.Array.Find(T[], Predicate)
\r
50 // This loses the valuable bool returned by C5 Find.
\r
52 public static T Find<T>(T[] arr, Fun<T,bool> p) {
\r
54 new WrappedArray<T>(arr).Find(p, out res);
\r
58 // System.Array.FindAll(T[], Predicate)
\r
60 public static T[] FindAll<T>(T[] arr, Fun<T,bool> p) {
\r
61 return new WrappedArray<T>(arr).FindAll(p).ToArray();
\r
64 // System.Array.FindIndex(T[], Predicate)
\r
66 public static int FindIndex<T>(T[] arr, Fun<T,bool> p) {
\r
67 return new WrappedArray<T>(arr).FindIndex(p);
\r
70 // System.Array.FindIndex(T[], int, Predicate)
\r
72 public static int FindIndex<T>(T[] arr, int i, Fun<T,bool> p) {
\r
73 int j = new WrappedArray<T>(arr).View(i,arr.Length-i).FindIndex(p);
\r
74 return j < 0 ? j : j+i;
\r
77 // System.Array.FindIndex(T[], int, int, Predicate)
\r
79 public static int FindIndex<T>(T[] arr, int i, int n, Fun<T,bool> p) {
\r
80 int j = new WrappedArray<T>(arr).View(i,n).FindIndex(p);
\r
81 return j < 0 ? j : j+i;
\r
84 // System.Array.FindLast(T[], Predicate)
\r
85 // This loses the valuable bool returned by C5 Find.
\r
87 public static T FindLast<T>(T[] arr, Fun<T,bool> p) {
\r
89 new WrappedArray<T>(arr).FindLast(p, out res);
\r
93 // System.Array.FindLastIndex(T[], Predicate)
\r
95 public static int FindLastIndex<T>(T[] arr, Fun<T,bool> p) {
\r
96 return new WrappedArray<T>(arr).FindIndex(p);
\r
99 // System.Array.FindLastIndex(T[], int, Predicate)
\r
101 public static int FindLastIndex<T>(T[] arr, int i, Fun<T,bool> p) {
\r
102 int j = new WrappedArray<T>(arr).View(i,arr.Length-i).FindIndex(p);
\r
103 return j < 0 ? j : j+i;
\r
106 // System.Array.FindLastIndex(T[], int, int, Predicate)
\r
108 public static int FindLastIndex<T>(T[] arr, int i, int n, Fun<T,bool> p) {
\r
109 int j = new WrappedArray<T>(arr).View(i,n).FindIndex(p);
\r
110 return j < 0 ? j : j+i;
\r
113 // System.Array.ForEach(T[], Action)
\r
115 public static void ForEach<T>(T[] arr, Act<T> act) {
\r
116 new WrappedArray<T>(arr).Apply(act);
\r
119 // System.Array.IndexOf(T[], T)
\r
121 public static int IndexOf<T>(T[] arr, T x) {
\r
122 int j = new WrappedArray<T>(arr).IndexOf(x);
\r
123 return j < 0 ? -1 : j;
\r
126 // System.Array.IndexOf(T[], T, int)
\r
128 public static int IndexOf<T>(T[] arr, T x, int i) {
\r
129 int j = new WrappedArray<T>(arr).View(i, arr.Length-i).IndexOf(x);
\r
130 return j < 0 ? -1 : j+i;
\r
133 // System.Array.IndexOf(T[], T, int, int)
\r
135 public static int IndexOf<T>(T[] arr, T x, int i, int n) {
\r
136 int j = new WrappedArray<T>(arr).View(i, n).IndexOf(x);
\r
137 return j < 0 ? -1 : j+i;
\r
140 // System.Array.LastIndexOf(T[], T)
\r
142 public static int LastIndexOf<T>(T[] arr, T x) {
\r
143 int j = new WrappedArray<T>(arr).LastIndexOf(x);
\r
144 return j < 0 ? -1 : j;
\r
147 // System.Array.LastIndexOf(T[], T, int)
\r
149 public static int LastIndexOf<T>(T[] arr, T x, int i) {
\r
150 int j = new WrappedArray<T>(arr).View(i, arr.Length-i).LastIndexOf(x);
\r
151 return j < 0 ? -1 : j+i;
\r
154 // System.Array.LastIndexOf(T[], T, int, int)
\r
156 public static int LastIndexOf<T>(T[] arr, T x, int i, int n) {
\r
157 int j = new WrappedArray<T>(arr).View(i, n).LastIndexOf(x);
\r
158 return j < 0 ? -1 : j+i;
\r
161 // System.Array.Sort(T[])
\r
163 public static void Sort<T>(T[] arr) {
\r
164 new WrappedArray<T>(arr).Sort();
\r
167 // System.Array.Sort(T[], int, int)
\r
169 public static void Sort<T>(T[] arr, int i, int n) {
\r
170 new WrappedArray<T>(arr).View(i, n).Sort();
\r
173 // System.Array.Sort(T[], SCG.IComparer<T>)
\r
175 public static void Sort<T>(T[] arr, SCG.IComparer<T> cmp) {
\r
176 new WrappedArray<T>(arr).Sort(cmp);
\r
179 // System.Array.Sort(T[], int, int, SCG.IComparer<T>)
\r
181 public static void Sort<T>(T[] arr, int i, int n, SCG.IComparer<T> cmp) {
\r
182 new WrappedArray<T>(arr).View(i, n).Sort(cmp);
\r
185 // System.Array.Sort(T[], Comparison)
\r
187 public static void Sort<T>(T[] arr, Comparison<T> csn) {
\r
188 new WrappedArray<T>(arr).Sort(new DelegateComparer<T>(csn));
\r