#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; namespace HeuristicLab.Analysis.FitnessLandscape { public static class Utils { public static IEnumerable Delta(this IEnumerable values, Func f) { var it = values.GetEnumerator(); T last = default(T); if (it.MoveNext()) last = it.Current; while (it.MoveNext()) { yield return f(last, it.Current); last = it.Current; } } public static IEnumerable> GroupConsecutive( this IEnumerable source, Func keySelector) { if (source == null) throw new ArgumentNullException("source"); if (keySelector == null) throw new ArgumentNullException("keySelector"); var comparer = EqualityComparer.Default; var grouped = new List(); using (var iter = source.GetEnumerator()) { if (!iter.MoveNext()) yield break; grouped.Add(iter.Current); var last = iter.Current; while (iter.MoveNext()) { if (!comparer.Equals(keySelector(iter.Current), keySelector(last))) { yield return grouped.AsReadOnly(); grouped = new List(); } grouped.Add(iter.Current); last = iter.Current; } yield return grouped.AsReadOnly(); } } } }