wasSharp – Blame information for rev 29

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