Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Utils/EnumerableExtensions.cs @ 17777

Last change on this file since 17777 was 13071, checked in by gkronber, 8 years ago

#2499: added license headers and removed unused usings

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Diagnostics;
25using System.Linq;
26using HeuristicLab.Data;
27
28namespace HeuristicLab.BioBoost.Utils {
29
30  public class AnonymousEqualityComparer<T> : EqualityComparer<T> {
31
32    public Func<T, T, bool> ComparerFunction { get; private set; }
33
34    public AnonymousEqualityComparer(Func<T, T, bool> comparer) { ComparerFunction = comparer; }
35
36    public override bool Equals(T x, T y) { return ComparerFunction(x, y); }
37
38    public override int GetHashCode(T obj) { return obj == null ? 0 : obj.GetHashCode(); }
39  }
40
41  public static class ListExtensions {
42
43    public static void AddRange<T>(this IList<T> l, IEnumerable<T> elements) { foreach (var e in elements) l.Add(e); }
44
45    public static IEnumerable<T> RemoveAtIndices<T>(this IEnumerable<T> l, IEnumerable<int> orderedIndices) {
46      int i = 0;
47      var listIt = l.GetEnumerator();
48      var idxIt = orderedIndices.GetEnumerator();
49      bool done = !idxIt.MoveNext();
50      while (listIt.MoveNext()) {
51        Debug.Assert(done || idxIt.Current >= i);
52        if (!done && idxIt.Current == i) {
53          done = !idxIt.MoveNext();
54        } else {
55          yield return listIt.Current;
56        }
57        i++;
58      }
59    }
60
61    public static Queue<T> ToQueue<T>(this IEnumerable<T> l) { return new Queue<T>(l); }
62
63    public static IEnumerable<TResult> Select<T, TResult>(this IEnumerable<T> l, TResult defaultValue, Func<T, TResult> f) {
64      return l.Select(x => x == null ? defaultValue : f(x));
65    }
66
67    public static DoubleArray ToDoubleArray(this IEnumerable<double> t) { return new DoubleArray(t.ToArray()); }
68    public static StringArray ToStringArray(this IEnumerable<string> t) { return new StringArray(t.ToArray()); }
69
70    public static IEnumerable<IGrouping<TKey, TResult>> GroupBy<T, TKey, TResult>(this IEnumerable<T> source,
71      Func<T, TKey> keySelector, Func<T, TResult> resultSelector, Func<TKey, TKey, bool> equalityComparer) {
72      return source.GroupBy(keySelector, resultSelector, new AnonymousEqualityComparer<TKey>(equalityComparer));
73    }
74
75    public static HashSet<T> ToHashSet<T>(this IEnumerable<T> values) { return new HashSet<T>(values); }
76
77    public static TResult FuncOverIndices<T, TResult>(this IEnumerable<T> values, ISet<int> indices, Func<IEnumerable<T>, TResult> func) {
78      return func(values.Select((v, i) => new {v, i}).Where(p => indices.Contains(p.i)).Select(p => p.v));
79    }
80
81    public static IDictionary<int, int[]> SourceIndices(this IEnumerable<int> targets) {
82      return targets
83        .Select((t, idx) => new {t, idx})
84        .GroupBy(ti => ti.t)
85        .ToDictionary(
86          tis => tis.Key,
87          tis => tis.Select(ti => ti.idx).ToArray());
88    }
89  }
90
91}
Note: See TracBrowser for help on using the repository browser.