wasSharp – Diff between revs 53 and 54
?pathlinks?
Rev 53 | Rev 54 | |||
---|---|---|---|---|
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.Sets |
11 | namespace wasSharp.Sets |
|
12 | { |
12 | { |
|
13 | public static class Extensions |
13 | public static class Extensions |
|
14 | { |
14 | { |
|
15 | /// <summary> |
15 | /// <summary> |
|
16 | /// Sequentially removes all the elements from the first sequence that are in the second sequence |
16 | /// Sequentially removes all the elements from the first sequence that are in the second sequence |
|
17 | /// or all the elements from the second sequence that are in the first sequence. |
17 | /// or all the elements from the second sequence that are in the first sequence. |
|
18 | /// </summary> |
18 | /// </summary> |
|
19 | /// <typeparam name="T">the type o the collection</typeparam> |
19 | /// <typeparam name="T">the type o the collection</typeparam> |
|
20 | /// <param name="o">the first sequence to remove from</param> |
20 | /// <param name="o">the first sequence to remove from</param> |
|
21 | /// <param name="p">the second sequence to remove</param> |
21 | /// <param name="p">the second sequence to remove</param> |
|
22 | /// <returns>the first sequence excluding the second sequence or the second sequence excluding the first sequence</returns> |
22 | /// <returns>the first sequence excluding the second sequence or the second sequence excluding the first sequence</returns> |
|
23 | public static IEnumerable<T> SequenceExceptAny<T>(this IEnumerable<T> o, IEnumerable<T> p) where T : IEquatable<T> |
23 | public static IEnumerable<T> SequenceExceptAny<T>(this IEnumerable<T> o, IEnumerable<T> p) where T : IEquatable<T> |
|
24 | { |
24 | { |
|
25 | var l = new List<T>(o); |
25 | var l = new List<T>(o); |
|
26 | var r = new List<T>(p); |
26 | var r = new List<T>(p); |
|
27 | return l.Count > r.Count |
27 | return l.Count() > r.Count() |
|
28 | ? l.Zip(r, (x, y) => x.Equals(y) ? default(T) : y) |
28 | ? l.Zip(r, (x, y) => x.Equals(y) ? default(T) : y) |
|
29 | .Concat(l.Skip(r.Count)) |
29 | .Concat(l.Skip(r.Count())) |
|
30 | .Where(q => q != null && !q.Equals(default(T))) |
30 | .Where(q => q != null && !q.Equals(default(T))) |
|
31 | : r.Zip(l, (x, y) => x.Equals(y) ? default(T) : y) |
31 | : r.Zip(l, (x, y) => x.Equals(y) ? default(T) : y) |
|
32 | .Concat(r.Skip(l.Count)) |
32 | .Concat(r.Skip(l.Count())) |
|
33 | .Where(q => q != null && !q.Equals(default(T))); |
33 | .Where(q => q != null && !q.Equals(default(T))); |
|
34 | } |
34 | } |
|
35 | |
35 | |
|
36 | /// <summary> |
36 | /// <summary> |
|
37 | /// Sequentially removes all the elements from the first sequence that are in the second sequence. |
37 | /// Sequentially removes all the elements from the first sequence that are in the second sequence. |
|
38 | /// </summary> |
38 | /// </summary> |
|
39 | /// <typeparam name="T">the type o the collection</typeparam> |
39 | /// <typeparam name="T">the type o the collection</typeparam> |
|
40 | /// <param name="o">the first sequence to remove from</param> |
40 | /// <param name="o">the first sequence to remove from</param> |
|
41 | /// <param name="p">the second sequence to remove</param> |
41 | /// <param name="p">the second sequence to remove</param> |
|
42 | /// <returns>the first sequence excluding the second sequence</returns> |
42 | /// <returns>the first sequence excluding the second sequence</returns> |
|
43 | public static IEnumerable<T> SequenceExcept<T>(this IEnumerable<T> a, IEnumerable<T> b) where T : IEquatable<T> |
43 | public static IEnumerable<T> SequenceExcept<T>(this IEnumerable<T> a, IEnumerable<T> b) where T : IEquatable<T> |
|
44 | { |
44 | { |
|
45 | using (var ea = a.GetEnumerator()) |
45 | using (var ea = a.GetEnumerator()) |
|
46 | { |
46 | { |
|
47 | using (var eb = b.GetEnumerator()) |
47 | using (var eb = b.GetEnumerator()) |
|
48 | { |
48 | { |
|
49 | while (ea.MoveNext()) |
49 | while (ea.MoveNext()) |
|
50 | { |
50 | { |
|
51 | if (eb.MoveNext() && ea.Current.Equals(eb.Current)) |
51 | if (eb.MoveNext() && ea.Current.Equals(eb.Current)) |
|
52 | continue; |
52 | continue; |
|
53 | yield return ea.Current; |
53 | yield return ea.Current; |
|
54 | } |
54 | } |
|
55 | } |
55 | } |
|
56 | } |
56 | } |
|
57 | } |
57 | } |
|
58 | |
58 | |
|
59 | /// <summary> |
59 | /// <summary> |
|
60 | /// Determines whether a sequence is contained within another sequence. |
60 | /// Determines whether a sequence is contained within another sequence. |
|
61 | /// </summary> |
61 | /// </summary> |
|
62 | /// <returns>true if and only if the first set is contained in the second set</returns> |
62 | /// <returns>true if and only if the first set is contained in the second set</returns> |
|
63 | /// <param name="a">The set to check</param> |
63 | /// <param name="a">The set to check</param> |
|
64 | /// <param name="b">The set to check against</param> |
64 | /// <param name="b">The set to check against</param> |
|
65 | /// <typeparam name="T">the set type</typeparam> |
65 | /// <typeparam name="T">the set type</typeparam> |
|
66 | public static bool SubsetEquals<T>(this IEnumerable<T> a, IEnumerable<T> b) where T : IEquatable<T> => |
66 | public static bool SubsetEquals<T>(this IEnumerable<T> a, IEnumerable<T> b) where T : IEquatable<T> => |
|
67 | !a.OrderBy(s => s).SequenceExcept(b.OrderBy(s => s)).Any(); |
67 | !a.OrderBy(s => s).SequenceExcept(b.OrderBy(s => s)).Any(); |
|
68 | } |
68 | } |
|
69 | } |
69 | } |
|
70 | |
70 | |