wasSharp – Diff between revs 29 and 32
?pathlinks?
Rev 29 | Rev 32 | |||
---|---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////// |
1 | /////////////////////////////////////////////////////////////////////////// |
|
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
|
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
|
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
|
5 | /////////////////////////////////////////////////////////////////////////// |
5 | /////////////////////////////////////////////////////////////////////////// |
|
6 | |
6 | |
|
7 | using System; |
7 | using System; |
|
8 | using System.Collections.Generic; |
8 | using System.Collections.Generic; |
|
9 | using System.Linq; |
9 | using System.Linq; |
|
10 | |
10 | |
|
11 | namespace wasSharp.Linq |
11 | namespace wasSharp.Linq |
|
12 | { |
12 | { |
|
13 | public static class Extensions |
13 | public static class Extensions |
|
14 | { |
14 | { |
|
15 | /////////////////////////////////////////////////////////////////////////// |
15 | /////////////////////////////////////////////////////////////////////////// |
|
- | 16 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 17 | /////////////////////////////////////////////////////////////////////////// |
||
- | 18 | /// <summary> |
||
- | 19 | /// A functional implementation of a switch clause. |
||
- | 20 | /// </summary> |
||
- | 21 | /// <typeparam name="T">the type of items in the query</typeparam> |
||
- | 22 | /// <param name="query">the selector query</param> |
||
- | 23 | /// <param name="default">the function to execute when no case matches</param> |
||
- | 24 | /// <param name="case">a list of predicates representing the switch cases, |
||
- | 25 | /// where each predicate has to return True or False indicating whether |
||
- | 26 | /// fallthrough should occur. |
||
- | 27 | /// </param> |
||
- | 28 | public static void Switch<T>(this IEnumerable<T> query, |
||
- | 29 | // default |
||
- | 30 | Action<T> @default, |
||
- | 31 | // case |
||
- | 32 | // case |
||
- | 33 | // ... |
||
- | 34 | params Predicate<T>[] @case) |
||
- | 35 | { |
||
- | 36 | if (@case.Length % 2 != 0) |
||
- | 37 | throw new ArgumentException("Pairs of predicates expected."); |
||
- | 38 | |
||
- | 39 | try |
||
- | 40 | { |
||
- | 41 | using (var iter = query.GetEnumerator()) |
||
- | 42 | { |
||
- | 43 | while (iter.MoveNext()) |
||
- | 44 | { |
||
- | 45 | for (var i = 0; i < @case.Length; i += 2) |
||
- | 46 | { |
||
- | 47 | if (@case[i].Invoke(iter.Current) && @case[i + 1].Invoke(iter.Current)) |
||
- | 48 | throw new Exception(); |
||
- | 49 | } |
||
- | 50 | } |
||
- | 51 | } |
||
- | 52 | @default.Invoke(query.Last()); |
||
- | 53 | } |
||
- | 54 | catch |
||
- | 55 | { |
||
- | 56 | } |
||
- | 57 | } |
||
- | 58 | |
||
- | 59 | /////////////////////////////////////////////////////////////////////////// |
||
- | 60 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 61 | /////////////////////////////////////////////////////////////////////////// |
||
- | 62 | /// <summary> |
||
- | 63 | /// Invokes pf in case the predicate p resolves or qf in case the predicate q resolves, or ef otherwise. |
||
- | 64 | /// </summary> |
||
- | 65 | /// <typeparam name="T">the type of items in the query</typeparam> |
||
- | 66 | /// <param name="query">the selector query</param> |
||
- | 67 | /// <param name="p">the condition for invoking pf</param> |
||
- | 68 | /// <param name="pf">the action to invoke in case p resolves</param> |
||
- | 69 | /// <param name="q">the condition for invoking qf</param> |
||
- | 70 | /// <param name="qf">the action to invoke in case q resolves</param> |
||
- | 71 | /// <param name="ef">the action to invoke otherwise</param> |
||
- | 72 | public static void ForAll<T>(this ParallelQuery<T> query, Predicate<T> p, Action<T> pf, Predicate<T> q, Action<T> qf, Action<T> ef) |
||
- | 73 | { |
||
- | 74 | query.ForAll(o => |
||
- | 75 | { |
||
- | 76 | if (p.Invoke(o)) |
||
- | 77 | { |
||
- | 78 | pf.Invoke(o); |
||
- | 79 | } |
||
- | 80 | else if (q.Invoke(o)) |
||
- | 81 | { |
||
- | 82 | qf.Invoke(o); |
||
- | 83 | } |
||
- | 84 | else |
||
- | 85 | { |
||
- | 86 | ef.Invoke(o); |
||
- | 87 | } |
||
- | 88 | }); |
||
- | 89 | } |
||
- | 90 | |
||
- | 91 | /////////////////////////////////////////////////////////////////////////// |
||
- | 92 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 93 | /////////////////////////////////////////////////////////////////////////// |
||
- | 94 | /// <summary> |
||
- | 95 | /// Invokes pf in case the predicate p resolves or qf in case the predicate q resolves. |
||
- | 96 | /// </summary> |
||
- | 97 | /// <typeparam name="T">the type of items in the query</typeparam> |
||
- | 98 | /// <param name="query">the selector query</param> |
||
- | 99 | /// <param name="p">the condition for invoking pf</param> |
||
- | 100 | /// <param name="pf">the action to invoke in case p resolves</param> |
||
- | 101 | /// <param name="q">the condition for invoking qf</param> |
||
- | 102 | /// <param name="qf">the action to invoke in case q resolves</param> |
||
- | 103 | public static void ForAll<T>(this ParallelQuery<T> query, Predicate<T> p, Action<T> pf, Predicate<T> q, Action<T> qf) |
||
- | 104 | { |
||
- | 105 | query.ForAll(o => |
||
- | 106 | { |
||
- | 107 | if (p.Invoke(o)) |
||
- | 108 | { |
||
- | 109 | pf.Invoke(o); |
||
- | 110 | return; |
||
- | 111 | } |
||
- | 112 | |
||
- | 113 | if (q.Invoke(o)) |
||
- | 114 | { |
||
- | 115 | qf.Invoke(o); |
||
- | 116 | return; |
||
- | 117 | } |
||
- | 118 | }); |
||
- | 119 | } |
||
- | 120 | |
||
- | 121 | /////////////////////////////////////////////////////////////////////////// |
||
- | 122 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 123 | /////////////////////////////////////////////////////////////////////////// |
||
- | 124 | /// <summary> |
||
- | 125 | /// Invokes pass if and only if predicate resovles or fail otherwise. |
||
- | 126 | /// </summary> |
||
- | 127 | /// <typeparam name="T">the type of items in the query</typeparam> |
||
- | 128 | /// <param name="query">the selector query</param> |
||
- | 129 | /// <param name="condition">the condition for invoking pf</param> |
||
- | 130 | /// <param name="pass">the function to invoke in case the predicate resolves</param> |
||
- | 131 | /// <param name="fail">the function to invoke otherwise</param> |
||
- | 132 | public static void ForAll<T>(this ParallelQuery<T> query, Predicate<T> condition, Action<T> pass, Action<T> fail) |
||
- | 133 | { |
||
- | 134 | query.ForAll(o => |
||
- | 135 | { |
||
- | 136 | switch (condition.Invoke(o)) |
||
- | 137 | { |
||
- | 138 | case true: |
||
- | 139 | pass.Invoke(o); |
||
- | 140 | return; |
||
- | 141 | |
||
- | 142 | default: |
||
- | 143 | fail.Invoke(o); |
||
- | 144 | return; |
||
- | 145 | } |
||
- | 146 | }); |
||
- | 147 | } |
||
- | 148 | |
||
- | 149 | /////////////////////////////////////////////////////////////////////////// |
||
- | 150 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 151 | /////////////////////////////////////////////////////////////////////////// |
||
- | 152 | /// <summary> |
||
- | 153 | /// Invokes pass if and only if condition holds or fail otherwise. |
||
- | 154 | /// </summary> |
||
- | 155 | /// <typeparam name="T">the return type of the pass and fail functions</typeparam> |
||
- | 156 | /// <param name="condition">the branch condition</param> |
||
- | 157 | /// <param name="pass">function with no parameters and return type T in case condition passes</param> |
||
- | 158 | /// <param name="fail">function with no parameters and return type T in case condition fails</param> |
||
- | 159 | /// <returns>the result of pass in case condition holds or the result of fail otherwise</returns> |
||
- | 160 | public static T IfElse<T>(this bool condition, Func<T> pass, Func<T> fail) |
||
- | 161 | { |
||
- | 162 | return condition ? pass.Invoke() : fail.Invoke(); |
||
- | 163 | } |
||
- | 164 | |
||
- | 165 | /////////////////////////////////////////////////////////////////////////// |
||
- | 166 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 167 | /////////////////////////////////////////////////////////////////////////// |
||
- | 168 | /// <summary> |
||
- | 169 | /// Invokes pass if and only if condition holds or fail otherwise. |
||
- | 170 | /// </summary> |
||
- | 171 | /// <typeparam name="U">the type of the argument to pass and fail</typeparam> |
||
- | 172 | /// <typeparam name="V">the return type of pass and fail</typeparam> |
||
- | 173 | /// <param name="condition">the branch condition</param> |
||
- | 174 | /// <param name="pass">function that takes argument arg and returns type V in case condition holds</param> |
||
- | 175 | /// <param name="fail">function that takes argument arg and returns type V in case condition fails</param> |
||
- | 176 | /// <param name="arg">the argument passed to pass or fail functions</param> |
||
- | 177 | /// <returns>the result of pass in case condition holds or the result of fail otherwise</returns> |
||
- | 178 | public static V IfElse<U, V>(this bool condition, Func<U, V> pass, Func<U, V> fail, U arg = default(U)) |
||
- | 179 | { |
||
- | 180 | return condition ? pass.Invoke(arg) : fail.Invoke(arg); |
||
- | 181 | } |
||
- | 182 | |
||
- | 183 | /////////////////////////////////////////////////////////////////////////// |
||
- | 184 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 185 | /////////////////////////////////////////////////////////////////////////// |
||
- | 186 | /// <summary> |
||
- | 187 | /// Invokes pass if and only if condition holds or fail otherwise. |
||
- | 188 | /// </summary> |
||
- | 189 | /// <typeparam name="T">the type of the argument to pass and fail</typeparam> |
||
- | 190 | /// <param name="condition">the branch condition</param> |
||
- | 191 | /// <param name="pass">function that takes argument arg and returns nothing in case condition holds</param> |
||
- | 192 | /// <param name="fail">function that takes argument arg and returns nothing in case condition fails</param> |
||
- | 193 | /// <param name="arg">the optional argument passed to pass or fail functions</param> |
||
- | 194 | public static void IfElse<T>(this bool condition, Action<T> pass, Action<T> fail, T arg = default(T)) |
||
- | 195 | { |
||
- | 196 | switch (condition) |
||
- | 197 | { |
||
- | 198 | case true: |
||
- | 199 | pass.Invoke(arg); |
||
- | 200 | return; |
||
- | 201 | |
||
- | 202 | default: |
||
- | 203 | fail.Invoke(arg); |
||
- | 204 | return; |
||
- | 205 | } |
||
- | 206 | } |
||
- | 207 | |
||
- | 208 | /////////////////////////////////////////////////////////////////////////// |
||
- | 209 | // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 210 | /////////////////////////////////////////////////////////////////////////// |
||
- | 211 | /// <summary> |
||
- | 212 | /// Invokes pass if and only if condition holds or fail otherwise. |
||
- | 213 | /// </summary> |
||
- | 214 | /// <typeparam name="U">the type of the first argument to the pass or fail functions</typeparam> |
||
- | 215 | /// <typeparam name="V">the type of the second argument to the pass or fail functions</typeparam> |
||
- | 216 | /// <param name="condition">the branch condition</param> |
||
- | 217 | /// <param name="pass">function that takes argument arg and returns nothing in case condition holds</param> |
||
- | 218 | /// <param name="fail">function that takes argument arg and returns nothing in case condition fails</param> |
||
- | 219 | /// <param name="arga">first optional argument passed to pass or fail functions</param> |
||
- | 220 | /// <param name="argb">second optional argument passed to pass or fail functions</param> |
||
- | 221 | public static void IfElse<U, V>(this bool condition, Action<U, V> pass, Action<U, V> fail, U arga = default(U), V argb = default(V)) |
||
- | 222 | { |
||
- | 223 | switch (condition) |
||
- | 224 | { |
||
- | 225 | case true: |
||
- | 226 | pass.Invoke(arga, argb); |
||
- | 227 | return; |
||
- | 228 | |
||
- | 229 | default: |
||
- | 230 | fail.Invoke(arga, argb); |
||
- | 231 | return; |
||
- | 232 | } |
||
- | 233 | } |
||
- | 234 | |
||
- | 235 | /////////////////////////////////////////////////////////////////////////// |
||
16 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
236 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
|
17 | /////////////////////////////////////////////////////////////////////////// |
237 | /////////////////////////////////////////////////////////////////////////// |
|
18 | /// <summary> |
238 | /// <summary> |
|
19 | /// Returns true of an enumerable contains more than one element. |
239 | /// Returns true of an enumerable contains more than one element. |
|
20 | /// </summary> |
240 | /// </summary> |
|
21 | /// <typeparam name="T">the type of the enumeration</typeparam> |
241 | /// <typeparam name="T">the type of the enumeration</typeparam> |
|
22 | /// <param name="e">the enumeration</param> |
242 | /// <param name="e">the enumeration</param> |
|
23 | /// <returns>true if enumeration contains more than one element</returns> |
243 | /// <returns>true if enumeration contains more than one element</returns> |
|
24 | /// <remarks>O(2) worst case</remarks> |
244 | /// <remarks>O(2) worst case</remarks> |
|
25 | public static bool Some<T>(this IEnumerable<T> e) |
245 | public static bool Some<T>(this IEnumerable<T> e) |
|
26 | { |
246 | { |
|
27 | var i = 0; |
247 | var i = 0; |
|
28 | using (var iter = e.GetEnumerator()) |
248 | using (var iter = e.GetEnumerator()) |
|
29 | { |
249 | { |
|
30 | while (iter.MoveNext()) |
250 | while (iter.MoveNext()) |
|
31 | { |
251 | { |
|
32 | if (++i > 1) |
252 | if (++i > 1) |
|
33 | return true; |
253 | return true; |
|
34 | } |
254 | } |
|
35 | return false; |
255 | return false; |
|
36 | } |
256 | } |
|
37 | } |
257 | } |
|
38 | |
258 | |
|
39 | /// <summary> |
259 | /// <summary> |
|
40 | /// Sequentially removes all the elements from the first sequence that are in the second sequence |
260 | /// Sequentially removes all the elements from the first sequence that are in the second sequence |
|
41 | /// or all the elements from the second sequence that are in the first sequence. |
261 | /// or all the elements from the second sequence that are in the first sequence. |
|
42 | /// </summary> |
262 | /// </summary> |
|
43 | /// <typeparam name="T">the type o the collection</typeparam> |
263 | /// <typeparam name="T">the type o the collection</typeparam> |
|
44 | /// <param name="o">the first sequence to remove from</param> |
264 | /// <param name="o">the first sequence to remove from</param> |
|
45 | /// <param name="p">the second sequence to remove</param> |
265 | /// <param name="p">the second sequence to remove</param> |
|
46 | /// <returns>the first sequence excluding the second sequence or the second sequence excluding the first sequence</returns> |
266 | /// <returns>the first sequence excluding the second sequence or the second sequence excluding the first sequence</returns> |
|
47 | public static IEnumerable<T> SequenceExceptAny<T>(this IEnumerable<T> o, IEnumerable<T> p) where T : IEquatable<T> |
267 | public static IEnumerable<T> SequenceExceptAny<T>(this IEnumerable<T> o, IEnumerable<T> p) where T : IEquatable<T> |
|
48 | { |
268 | { |
|
49 | var l = new List<T>(o); |
269 | var l = new List<T>(o); |
|
50 | var r = new List<T>(p); |
270 | var r = new List<T>(p); |
|
51 | return l.Count > r.Count |
271 | return l.Count > r.Count |
|
52 | ? l.Zip(r, (x, y) => x.Equals(y) ? default(T) : y) |
272 | ? l.Zip(r, (x, y) => x.Equals(y) ? default(T) : y) |
|
53 | .Concat(l.Skip(r.Count())) |
273 | .Concat(l.Skip(r.Count())) |
|
54 | .Where(q => q != null && !q.Equals(default(T))) |
274 | .Where(q => q != null && !q.Equals(default(T))) |
|
55 | : r.Zip(l, (x, y) => x.Equals(y) ? default(T) : y) |
275 | : r.Zip(l, (x, y) => x.Equals(y) ? default(T) : y) |
|
56 | .Concat(r.Skip(l.Count())) |
276 | .Concat(r.Skip(l.Count())) |
|
57 | .Where(q => q != null && !q.Equals(default(T))); |
277 | .Where(q => q != null && !q.Equals(default(T))); |
|
58 | } |
278 | } |
|
59 | |
279 | |
|
60 | /// <summary> |
280 | /// <summary> |
|
61 | /// Sequentially removes all the elements from the first sequence that are in the second sequence. |
281 | /// Sequentially removes all the elements from the first sequence that are in the second sequence. |
|
62 | /// </summary> |
282 | /// </summary> |
|
63 | /// <typeparam name="T">the type o the collection</typeparam> |
283 | /// <typeparam name="T">the type o the collection</typeparam> |
|
64 | /// <param name="o">the first sequence to remove from</param> |
284 | /// <param name="o">the first sequence to remove from</param> |
|
65 | /// <param name="p">the second sequence to remove</param> |
285 | /// <param name="p">the second sequence to remove</param> |
|
66 | /// <returns>the first sequence excluding the second sequence</returns> |
286 | /// <returns>the first sequence excluding the second sequence</returns> |
|
67 | public static IEnumerable<T> SequenceExcept<T>(this IEnumerable<T> a, IEnumerable<T> b) where T : IEquatable<T> |
287 | public static IEnumerable<T> SequenceExcept<T>(this IEnumerable<T> a, IEnumerable<T> b) where T : IEquatable<T> |
|
68 | { |
288 | { |
|
69 | var ea = a.GetEnumerator(); |
289 | var ea = a.GetEnumerator(); |
|
70 | var eb = b.GetEnumerator(); |
290 | var eb = b.GetEnumerator(); |
|
71 | while (ea.MoveNext()) |
291 | while (ea.MoveNext()) |
|
72 | { |
292 | { |
|
73 | if (eb.MoveNext() && ea.Current.Equals(eb.Current)) |
293 | if (eb.MoveNext() && ea.Current.Equals(eb.Current)) |
|
74 | continue; |
294 | continue; |
|
75 | yield return ea.Current; |
295 | yield return ea.Current; |
|
76 | } |
296 | } |
|
77 | } |
297 | } |
|
78 | } |
298 | } |
|
79 | } |
299 | } |
|
80 | |
300 | |