Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2695_dataset-ids/HeuristicLab.Algorithms.DataAnalysis/3.4/TSNE/TSNEUtils.cs @ 16864

Last change on this file since 16864 was 14414, checked in by bwerth, 8 years ago

#2700 forgot to add files

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 IList<T> 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      return list;
42    }
43
44    internal static int Partition<T>(this IList<T> list, int left, int right, int pivotindex, IComparer<T> comparer) {
45      var pivotValue = list[pivotindex];
46      list.Swap(pivotindex, right);
47      var storeIndex = left;
48      for (var i = left; i < right; i++)
49        if (comparer.Compare(list[i], pivotValue) < 0)
50          list.Swap(storeIndex++, i);
51      list.Swap(right, storeIndex);
52      return storeIndex;
53    }
54
55    /// <summary>
56    /// Quick-Sort based partial ascending sorting
57    /// [0,left-1] not changed;
58    /// [left, n] elements are smaller than or equal to list[n]
59    /// [n, right] elements are grater than or equal to list[n]
60    /// [right, list.Count[ not changed;
61    /// </summary>
62    /// <typeparam name="T"></typeparam>
63    /// <param name="list">list that shall be partially sorted</param>
64    /// <param name="left">left index of sorted list part</param>
65    /// <param name="right">right index of sorted list part</param>
66    /// <param name="n">index around which the partial sorting occures</param>
67    /// <param name="comparer">comparer for list elemnts </param>
68    /// <returns></returns>
69    internal static T NthElement<T>(this IList<T> list, int left, int right, int n, IComparer<T> comparer) {
70      while (true) {
71        if (left == right) return list[left];
72        var pivotindex = left + (int)Math.Floor(new System.Random().Next() % (right - (double)left + 1));
73        pivotindex = list.Partition(left, right, pivotindex, comparer);
74        if (n == pivotindex) return list[n];
75        if (n < pivotindex) right = pivotindex - 1;
76        else left = pivotindex + 1;
77      }
78    }
79  }
80}
Note: See TracBrowser for help on using the repository browser.