wasSharp – Diff between revs 12 and 27
?pathlinks?
Rev 12 | Rev 27 | |||
---|---|---|---|---|
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) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
16 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
|
17 | /////////////////////////////////////////////////////////////////////////// |
17 | /////////////////////////////////////////////////////////////////////////// |
|
18 | /// <summary> |
18 | /// <summary> |
|
19 | /// Returns true of an enumerable contains more than one element. |
19 | /// Returns true of an enumerable contains more than one element. |
|
20 | /// </summary> |
20 | /// </summary> |
|
21 | /// <typeparam name="T">the type of the enumeration</typeparam> |
21 | /// <typeparam name="T">the type of the enumeration</typeparam> |
|
22 | /// <param name="e">the enumeration</param> |
22 | /// <param name="e">the enumeration</param> |
|
23 | /// <returns>true if enumeration contains more than one element</returns> |
23 | /// <returns>true if enumeration contains more than one element</returns> |
|
24 | /// <remarks>O(2) worst case</remarks> |
24 | /// <remarks>O(2) worst case</remarks> |
|
25 | public static bool Some<T>(this IEnumerable<T> e) |
25 | public static bool Some<T>(this IEnumerable<T> e) |
|
26 | { |
26 | { |
|
27 | var i = 0; |
27 | var i = 0; |
|
28 | using (var iter = e.GetEnumerator()) |
28 | using (var iter = e.GetEnumerator()) |
|
29 | { |
29 | { |
|
30 | while (iter.MoveNext()) |
30 | while (iter.MoveNext()) |
|
31 | { |
31 | { |
|
32 | if (++i > 1) |
32 | if (++i > 1) |
|
33 | return true; |
33 | return true; |
|
34 | } |
34 | } |
|
35 | return false; |
35 | return false; |
|
36 | } |
36 | } |
|
37 | } |
37 | } |
|
38 | |
38 | |
|
39 | /// <summary> |
39 | /// <summary> |
|
40 | /// Sequentially removes all the elements from the first sequence that are in the second sequence. |
40 | /// Sequentially removes all the elements from the first sequence that are in the second sequence. |
|
41 | /// </summary> |
41 | /// </summary> |
|
42 | /// <typeparam name="T">the type o the collection</typeparam> |
42 | /// <typeparam name="T">the type o the collection</typeparam> |
|
43 | /// <param name="o">the first sequence to remove from</param> |
43 | /// <param name="o">the first sequence to remove from</param> |
|
44 | /// <param name="p">the second sequence to remove</param> |
44 | /// <param name="p">the second sequence to remove</param> |
|
45 | /// <returns>the first sequence excluding the second sequence</returns> |
45 | /// <returns>the first sequence excluding the second sequence</returns> |
|
46 | public static IEnumerable<T> SequenceExcept<T>(this IEnumerable<T> o, IEnumerable<T> p) where T : IEquatable<T> |
46 | public static IEnumerable<T> SequenceExcept<T>(this IEnumerable<T> o, IEnumerable<T> p) where T : IEquatable<T> |
|
47 | { |
47 | { |
|
48 | var l = new List<T>(o); |
48 | var l = new List<T>(o); |
|
49 | var r = new List<T>(p); |
49 | var r = new List<T>(p); |
|
50 | return l.Count > r.Count |
50 | return l.Count > r.Count |
|
51 | ? l.Zip(r, (x, y) => x.Equals(y) ? default(T) : y) |
51 | ? l.Zip(r, (x, y) => x.Equals(y) ? default(T) : y) |
|
52 | .Concat(l.Skip(r.Count())) |
52 | .Concat(l.Skip(r.Count())) |
|
53 | .Where(q => q != null && !q.Equals(default(T))) |
53 | .Where(q => q != null && !q.Equals(default(T))) |
|
54 | : r.Zip(l, (x, y) => x.Equals(y) ? default(T) : y) |
54 | : r.Zip(l, (x, y) => x.Equals(y) ? default(T) : y) |
|
55 | .Concat(r.Skip(l.Count())) |
55 | .Concat(r.Skip(l.Count())) |
|
56 | .Where(q => q != null && !q.Equals(default(T))); |
56 | .Where(q => q != null && !q.Equals(default(T))); |
|
57 | } |
57 | } |
|
58 | } |
58 | } |
|
59 | } |
- | ||
60 | |
59 | } |
|
- | 60 | |
||
61 |
|
- | ||
62 | |
- | ||
63 | |
- | ||
64 | |
- |