source: branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Utils/EnumerableExtensions.cs @ 13069

Last change on this file since 13069 was 13069, checked in by gkronber, 7 years ago

#2499: imported source code for HeuristicLab.BioBoost from private repository with some changes

File size: 2.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Diagnostics;
5using System.Linq;
6using System.Runtime.CompilerServices;
7using HeuristicLab.Data;
8using HeuristicLab.Persistence.Mono;
9
10namespace HeuristicLab.BioBoost.Utils {
11
12  public class AnonymousEqualityComparer<T> : EqualityComparer<T> {
13
14    public Func<T, T, bool> ComparerFunction { get; private set; }
15
16    public AnonymousEqualityComparer(Func<T, T, bool> comparer) { ComparerFunction = comparer; }
17
18    public override bool Equals(T x, T y) { return ComparerFunction(x, y); }
19
20    public override int GetHashCode(T obj) { return obj == null ? 0 : obj.GetHashCode(); }
21  }
22
23  public static class ListExtensions {
24
25    public static void AddRange<T>(this IList<T> l, IEnumerable<T> elements) { foreach (var e in elements) l.Add(e); }
26
27    public static IEnumerable<T> RemoveAtIndices<T>(this IEnumerable<T> l, IEnumerable<int> orderedIndices) {
28      int i = 0;
29      var listIt = l.GetEnumerator();
30      var idxIt = orderedIndices.GetEnumerator();
31      bool done = !idxIt.MoveNext();
32      while (listIt.MoveNext()) {
33        Debug.Assert(done || idxIt.Current >= i);
34        if (!done && idxIt.Current == i) {
35          done = !idxIt.MoveNext();
36        } else {
37          yield return listIt.Current;
38        }
39        i++;
40      }
41    }
42
43    public static Queue<T> ToQueue<T>(this IEnumerable<T> l) { return new Queue<T>(l); }
44
45    public static IEnumerable<TResult> Select<T, TResult>(this IEnumerable<T> l, TResult defaultValue, Func<T, TResult> f) {
46      return l.Select(x => x == null ? defaultValue : f(x));
47    }
48
49    public static DoubleArray ToDoubleArray(this IEnumerable<double> t) { return new DoubleArray(t.ToArray()); }
50    public static StringArray ToStringArray(this IEnumerable<string> t) { return new StringArray(t.ToArray()); }
51
52    public static IEnumerable<IGrouping<TKey, TResult>> GroupBy<T, TKey, TResult>(this IEnumerable<T> source,
53      Func<T, TKey> keySelector, Func<T, TResult> resultSelector, Func<TKey, TKey, bool> equalityComparer) {
54      return source.GroupBy(keySelector, resultSelector, new AnonymousEqualityComparer<TKey>(equalityComparer));
55    }
56
57    public static HashSet<T> ToHashSet<T>(this IEnumerable<T> values) { return new HashSet<T>(values); }
58
59    public static TResult FuncOverIndices<T, TResult>(this IEnumerable<T> values, ISet<int> indices, Func<IEnumerable<T>, TResult> func) {
60      return func(values.Select((v, i) => new {v, i}).Where(p => indices.Contains(p.i)).Select(p => p.v));
61    }
62
63    public static IDictionary<int, int[]> SourceIndices(this IEnumerable<int> targets) {
64      return targets
65        .Select((t, idx) => new {t, idx})
66        .GroupBy(ti => ti.t)
67        .ToDictionary(
68          tis => tis.Key,
69          tis => tis.Select(ti => ti.idx).ToArray());
70    }
71  }
72
73}
Note: See TracBrowser for help on using the repository browser.