Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/26/12 14:50:56 (13 years ago)
Author:
abeham
Message:

#1614: improved results output of GQAP

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common/3.3/ExtensionMethods.cs

    r7407 r7415  
    2727namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common {
    2828  public static class ExtensionMethods {
    29 
    30     /// <summary>
    31     /// Simply enumerates the sequence by moving over all elements.
    32     /// </summary>
    33     /// <typeparam name="T">The type of the items that are to be enumerated.</typeparam>
    34     /// <param name="source">The enumerable that contains the items.</param>
    35     public static void Enumerate<T>(this IEnumerable<T> source) {
    36       var enumerator = source.GetEnumerator();
    37       while (enumerator.MoveNext()) ;
    38     }
    3929
    4030    /// <summary>
     
    10999
    110100    /// <summary>
     101    /// Selects the first element in the sequence where the selected key is the maximum.
     102    /// </summary>
     103    /// <remarks>
     104    /// Runtime complexity of the operation is O(N).
     105    /// </remarks>
     106    /// <typeparam name="T">The type of the elements.</typeparam>
     107    /// <typeparam name="TComparable">The selected key (needs to be an IComparable).</typeparam>
     108    /// <param name="source">The enumeration in which the maximum item should be found.</param>
     109    /// <param name="keySelector">The function that selects the key.</param>
     110    /// <returns>The element in the enumeration where the selected key is the maximum.</returns>
     111    public static T SelectMax<T, TComparable>(this IEnumerable<T> source, Func<T, TComparable> keySelector) where TComparable : IComparable {
     112      IEnumerator<T> enumerator = source.GetEnumerator();
     113      if (!enumerator.MoveNext()) throw new InvalidOperationException("Sequence is empty!");
     114      T result = enumerator.Current;
     115      TComparable max = keySelector(result);
     116      while (enumerator.MoveNext()) {
     117        T item = enumerator.Current;
     118        TComparable comparison = keySelector(item);
     119        if (comparison.CompareTo(max) > 0) {
     120          result = item;
     121          max = comparison;
     122        }
     123      }
     124      return result;
     125    }
     126
     127    /// <summary>
     128    /// Selects the first element in the sequence where the selected key is the minimum.
     129    /// </summary>
     130    /// <remarks>
     131    /// Runtime complexity of the operation is O(N).
     132    /// </remarks>
     133    /// <typeparam name="T">The type of the elements.</typeparam>
     134    /// <typeparam name="TComparable">The selected key (needs to be an IComparable).</typeparam>
     135    /// <param name="source">The enumeration in which the minimum item should be found.</param>
     136    /// <param name="keySelector">The function that selects the key.</param>
     137    /// <returns>The element in the enumeration where the selected key is the minimum.</returns>
     138    public static T SelectMin<T, TComparable>(this IEnumerable<T> source, Func<T, TComparable> keySelector) where TComparable : IComparable {
     139      IEnumerator<T> enumerator = source.GetEnumerator();
     140      if (!enumerator.MoveNext()) throw new InvalidOperationException("Sequence is empty!");
     141      T result = enumerator.Current;
     142      TComparable min = keySelector(result);
     143      while (enumerator.MoveNext()) {
     144        T item = enumerator.Current;
     145        TComparable comparison = keySelector(item);
     146        if (comparison.CompareTo(min) < 0) {
     147          result = item;
     148          min = comparison;
     149        }
     150      }
     151      return result;
     152    }
     153
     154    /// <summary>
    111155    /// Walks an operator graph in that it jumps from one operator to all its operator parameters and yields each operator it touches.
    112156    /// Cycles are detected and not walked twice.
Note: See TracChangeset for help on using the changeset viewer.