wasSharp – Blame information for rev 11

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