#region License Information
/* HeuristicLab
* Copyright (C) 2002-2019 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.Algorithms.DataAnalysis {
internal static class TSNEUtils {
internal static void ForEach(this IEnumerable sequence, Action action) {
if (sequence == null) return;
if (action == null) throw new ArgumentException("Null Action can not be performed");
var i = 0;
foreach (var item in sequence) {
action(i, item);
i++;
}
}
internal static void Swap(this IList list, int indexA, int indexB) {
var tmp = list[indexA];
list[indexA] = list[indexB];
list[indexB] = tmp;
}
private static int Partition(this IList list, int left, int right, int pivotindex, IComparer comparer) {
var pivotValue = list[pivotindex];
list.Swap(pivotindex, right);
var storeIndex = left;
for (var i = left; i < right; i++)
if (comparer.Compare(list[i], pivotValue) < 0)
list.Swap(storeIndex++, i);
list.Swap(right, storeIndex);
return storeIndex;
}
///
/// Quick-Sort based partial ascending sorting
/// [0,left-1] not changed;
/// [left, n] elements are smaller than or equal to list[n]
/// [n, right] elements are grater than or equal to list[n]
/// [right, list.Count[ not changed;
///
///
/// list that shall be partially sorted
/// left index of sorted list part
/// right index of sorted list part
/// index around which the partial sorting occures
/// comparer for list elemnts
///
internal static void PartialSort(this IList list, int left, int right, int n, IComparer comparer) {
while (true) {
if (left == right) return;
var pivotindex = left + (int) Math.Floor(new System.Random().Next() % (right - (double) left + 1));
pivotindex = list.Partition(left, right, pivotindex, comparer);
if (n == pivotindex) return;
if (n < pivotindex) right = pivotindex - 1;
else left = pivotindex + 1;
}
}
}
}