Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/DatasetUtil.cs @ 15325

Last change on this file since 15325 was 15013, checked in by bburlacu, 7 years ago

#2723: Found a different solution to this issue, by using IList<T> as the return type for the GetValues<T> method.

File size: 2.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 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;
24using System.Collections.Generic;
25using System.ComponentModel;
26using System.Linq;
27using System.Text;
28using HeuristicLab.Core;
29using HeuristicLab.Random;
30
31namespace HeuristicLab.Problems.DataAnalysis {
32  public static class DatasetUtil {
33    /// <summary>
34    /// Shuffle all the lists with the same shuffling.
35    /// </summary>
36    /// <param name="values">The value lists to be shuffled.</param>
37    /// <param name="random">The random number generator</param>
38    /// <returns>A new list containing shuffled copies of the original value lists.</returns>
39    public static List<IList> ShuffleLists(this List<IList> values, IRandom random) {
40      int count = values.First().Count;
41      int[] indices = Enumerable.Range(0, count).Shuffle(random).ToArray();
42      List<IList> shuffled = new List<IList>(values.Count);
43      for (int col = 0; col < values.Count; col++) {
44
45        if (values[col] is IList<double>)
46          shuffled.Add(new List<double>());
47        else if (values[col] is IList<DateTime>)
48          shuffled.Add(new List<DateTime>());
49        else if (values[col] is IList<string>)
50          shuffled.Add(new List<string>());
51        else
52          throw new InvalidOperationException();
53
54        for (int i = 0; i < count; i++) {
55          shuffled[col].Add(values[col][indices[i]]);
56        }
57      }
58      return shuffled;
59
60    }
61  }
62}
Note: See TracBrowser for help on using the repository browser.