Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/29/19 13:53:26 (5 years ago)
Author:
mkommend
Message:

#2521: Integrated changes of #2943 into problem refactoring branch.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Common/3.3/EnumerableExtensions.cs

    r16723 r17225  
    2626namespace HeuristicLab.Common {
    2727  public static class EnumerableExtensions {
     28    public static T[,] ToMatrix<T>(this IEnumerable<IEnumerable<T>> source) {
     29      if (source == null) throw new ArgumentNullException("source");
     30      if (!source.Any()) return new T[0, 0];
     31
     32      int firstDimension = source.Count();
     33      int secondDimension = source.First().Count();
     34      var result = new T[firstDimension, secondDimension];
     35
     36      int i = 0;
     37      int j = 0;
     38      foreach (var row in source) {
     39        j = 0;
     40        foreach (var element in row) {
     41          result[i, j] = element;
     42          j++;
     43        }
     44        if (j != secondDimension) throw new InvalidOperationException("All enumerables must be of the same length.");
     45        i++;
     46      }
     47
     48      return result;
     49    }
     50
     51
    2852    /// <summary>
    2953    /// Selects all elements in the sequence that are maximal with respect to the given value.
Note: See TracChangeset for help on using the changeset viewer.