wasSharp – Blame information for rev 33
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
11 | office | 1 | /////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
||
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
||
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
||
5 | /////////////////////////////////////////////////////////////////////////// |
||
6 | |||
7 | using System; |
||
8 | using System.Collections.Generic; |
||
9 | using System.Linq; |
||
10 | |||
11 | namespace wasSharp.Linq |
||
12 | { |
||
13 | public static class Extensions |
||
14 | { |
||
15 | /////////////////////////////////////////////////////////////////////////// |
||
32 | office | 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 | /////////////////////////////////////////////////////////////////////////// |
||
11 | office | 236 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
237 | /////////////////////////////////////////////////////////////////////////// |
||
238 | /// <summary> |
||
239 | /// Returns true of an enumerable contains more than one element. |
||
240 | /// </summary> |
||
241 | /// <typeparam name="T">the type of the enumeration</typeparam> |
||
242 | /// <param name="e">the enumeration</param> |
||
243 | /// <returns>true if enumeration contains more than one element</returns> |
||
244 | /// <remarks>O(2) worst case</remarks> |
||
245 | public static bool Some<T>(this IEnumerable<T> e) |
||
246 | { |
||
247 | var i = 0; |
||
248 | using (var iter = e.GetEnumerator()) |
||
249 | { |
||
250 | while (iter.MoveNext()) |
||
251 | { |
||
252 | if (++i > 1) |
||
253 | return true; |
||
254 | } |
||
255 | return false; |
||
256 | } |
||
257 | } |
||
258 | |||
259 | /// <summary> |
||
29 | office | 260 | /// Sequentially removes all the elements from the first sequence that are in the second sequence |
261 | /// or all the elements from the second sequence that are in the first sequence. |
||
11 | office | 262 | /// </summary> |
263 | /// <typeparam name="T">the type o the collection</typeparam> |
||
264 | /// <param name="o">the first sequence to remove from</param> |
||
265 | /// <param name="p">the second sequence to remove</param> |
||
29 | office | 266 | /// <returns>the first sequence excluding the second sequence or the second sequence excluding the first sequence</returns> |
267 | public static IEnumerable<T> SequenceExceptAny<T>(this IEnumerable<T> o, IEnumerable<T> p) where T : IEquatable<T> |
||
11 | office | 268 | { |
269 | var l = new List<T>(o); |
||
270 | var r = new List<T>(p); |
||
271 | return l.Count > r.Count |
||
272 | ? l.Zip(r, (x, y) => x.Equals(y) ? default(T) : y) |
||
273 | .Concat(l.Skip(r.Count())) |
||
274 | .Where(q => q != null && !q.Equals(default(T))) |
||
275 | : r.Zip(l, (x, y) => x.Equals(y) ? default(T) : y) |
||
276 | .Concat(r.Skip(l.Count())) |
||
277 | .Where(q => q != null && !q.Equals(default(T))); |
||
278 | } |
||
29 | office | 279 | |
280 | /// <summary> |
||
281 | /// Sequentially removes all the elements from the first sequence that are in the second sequence. |
||
282 | /// </summary> |
||
283 | /// <typeparam name="T">the type o the collection</typeparam> |
||
284 | /// <param name="o">the first sequence to remove from</param> |
||
285 | /// <param name="p">the second sequence to remove</param> |
||
286 | /// <returns>the first sequence excluding the second sequence</returns> |
||
287 | public static IEnumerable<T> SequenceExcept<T>(this IEnumerable<T> a, IEnumerable<T> b) where T : IEquatable<T> |
||
288 | { |
||
289 | var ea = a.GetEnumerator(); |
||
290 | var eb = b.GetEnumerator(); |
||
291 | while (ea.MoveNext()) |
||
292 | { |
||
293 | if (eb.MoveNext() && ea.Current.Equals(eb.Current)) |
||
294 | continue; |
||
295 | yield return ea.Current; |
||
296 | } |
||
297 | } |
||
11 | office | 298 | } |
27 | office | 299 | } |