wasSharp – Blame information for rev 54

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