wasSharp – Blame information for rev 27

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>
40 /// Sequentially removes all the elements from the first sequence that are in the second sequence.
41 /// </summary>
42 /// <typeparam name="T">the type o the collection</typeparam>
43 /// <param name="o">the first sequence to remove from</param>
44 /// <param name="p">the second sequence to remove</param>
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>
47 {
48 var l = new List<T>(o);
49 var r = new List<T>(p);
50 return l.Count > r.Count
51 ? l.Zip(r, (x, y) => x.Equals(y) ? default(T) : y)
52 .Concat(l.Skip(r.Count()))
53 .Where(q => q != null && !q.Equals(default(T)))
54 : r.Zip(l, (x, y) => x.Equals(y) ? default(T) : y)
55 .Concat(r.Skip(l.Count()))
56 .Where(q => q != null && !q.Equals(default(T)));
57 }
58 }
27 office 59 }