Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis/3.4/TSNE/TSNEUtils.cs @ 17529

Last change on this file since 17529 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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;
24
25namespace HeuristicLab.Algorithms.DataAnalysis {
26  internal static class TSNEUtils {
27    internal static void ForEach<T>(this IEnumerable<T> sequence, Action<int, T> action) {
28      if (sequence == null) return;
29      if (action == null) throw new ArgumentException("Null Action can not be performed");
30      var i = 0;
31      foreach (var item in sequence) {
32        action(i, item);
33        i++;
34      }
35    }
36
37    internal static void Swap<T>(this IList<T> list, int indexA, int indexB) {
38      var tmp = list[indexA];
39      list[indexA] = list[indexB];
40      list[indexB] = tmp;
41    }
42
43    private static int Partition<T>(this IList<T> list, int left, int right, int pivotindex, IComparer<T> comparer) {
44      var pivotValue = list[pivotindex];
45      list.Swap(pivotindex, right);
46      var storeIndex = left;
47      for (var i = left; i < right; i++)
48        if (comparer.Compare(list[i], pivotValue) < 0)
49          list.Swap(storeIndex++, i);
50      list.Swap(right, storeIndex);
51      return storeIndex;
52    }
53
54    /// <summary>
55    /// Quick-Sort based partial ascending sorting
56    /// [0,left-1] not changed;
57    /// [left, n] elements are smaller than or equal to list[n]
58    /// [n, right] elements are grater than or equal to list[n]
59    /// [right, list.Count[ not changed;
60    /// </summary>
61    /// <typeparam name="T"></typeparam>
62    /// <param name="list">list that shall be partially sorted</param>
63    /// <param name="left">left index of sorted list part</param>
64    /// <param name="right">right index of sorted list part</param>
65    /// <param name="n">index around which the partial sorting occures</param>
66    /// <param name="comparer">comparer for list elemnts </param>
67    /// <returns></returns>
68    internal static void PartialSort<T>(this IList<T> list, int left, int right, int n, IComparer<T> comparer) {
69      while (true) {
70        if (left == right) return;
71        var pivotindex = left + (int) Math.Floor(new System.Random().Next() % (right - (double) left + 1));
72        pivotindex = list.Partition(left, right, pivotindex, comparer);
73        if (n == pivotindex) return;
74        if (n < pivotindex) right = pivotindex - 1;
75        else left = pivotindex + 1;
76      }
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.