Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Data/Command/SortCommand.cs @ 12009

Last change on this file since 12009 was 9615, checked in by mkommend, 12 years ago

#1734: Updated copyright information in all DataImporter classes.

File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.DataImporter.Data.CommandBase;
27using HeuristicLab.DataImporter.Data.Model;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29namespace HeuristicLab.DataImporter.Data.Command {
30  [StorableClass]
31  public class SortCommand : ColumnGroupCommandBase {
32    [Storable]
33    private bool addToSortedColumns;
34    [Storable]
35    private int sortColumnIndex;
36
37    private ICollection<int> oldSortedColumnIndices;
38    private IEnumerable<SortOrder> oldSortOrdersForColumns;
39    private int[] indices;
40
41    [StorableConstructor]
42    protected SortCommand(bool deserializing) : base(deserializing) { }
43
44    public SortCommand(DataSet dataSet, string columnGroupName, int sortColumnIndex) :
45      this(dataSet, columnGroupName, sortColumnIndex, false) {
46    }
47
48    public SortCommand(DataSet dataSet, string columnGroupName, int sortColumnIndex, bool addToSortedColumns)
49      : base(dataSet, columnGroupName) {
50      this.addToSortedColumns = addToSortedColumns;
51      this.sortColumnIndex = sortColumnIndex;
52    }
53
54    public override void Execute() {
55      base.Execute();
56      oldSortedColumnIndices = new List<int>(ColumnGroup.SortedColumnIndexes);
57      oldSortOrdersForColumns = ColumnGroup.SortOrdersForColumns.ToList();
58
59      if (!addToSortedColumns) {
60        ColumnGroup.SortedColumnIndexes.Clear();
61        for (int i = 0; i < ColumnGroup.Columns.Count(); i++)
62          if (i != sortColumnIndex)
63            ColumnGroup.Columns.ElementAt(i).SortOrder = SortOrder.None;
64      }
65      if (!ColumnGroup.SortedColumnIndexes.Contains(sortColumnIndex))
66        ColumnGroup.SortedColumnIndexes.Add(sortColumnIndex);
67      ICollection<int> tempSortedColumnIndices = new List<int>(ColumnGroup.SortedColumnIndexes);
68
69      RowComparer rowComparer = new RowComparer(ColumnGroup, sortColumnIndex);
70      indices = Enumerable.Range(0, ColumnGroup.RowCount).OrderBy(k => k, rowComparer).ToArray();
71
72      List<ColumnBase> newColumns = Sort();
73      ColumnGroup.ClearColumns();
74      ColumnGroup.AddColumns(newColumns);
75      ColumnGroup.SortedColumnIndexes = tempSortedColumnIndices;
76      ColumnGroup.FireChanged();
77      this.ColumnGroup = null;
78    }
79
80    public override void UndoExecute() {
81      base.UndoExecute();
82      indices = Enumerable.Range(0, ColumnGroup.RowCount).OrderBy<int, int>(i => indices[i]).ToArray();
83      List<ColumnBase> newColumns = Sort();
84      ColumnGroup.ClearColumns();
85      ColumnGroup.AddColumns(newColumns);
86      ColumnGroup.SortedColumnIndexes = this.oldSortedColumnIndices;
87      ColumnGroup.SortOrdersForColumns = oldSortOrdersForColumns;
88      indices = null;
89      oldSortedColumnIndices = null;
90      oldSortOrdersForColumns = null;
91      ColumnGroup.FireChanged();
92      this.ColumnGroup = null;
93    }
94
95    public override string Description {
96      get { return "Sort ColumnGroup"; }
97    }
98
99    private List<ColumnBase> Sort() {
100      List<ColumnBase> newColumns = new List<ColumnBase>();
101      ColumnBase newColumn;
102      ColumnBase oldColumn;
103
104      for (int col = 0; col < ColumnGroup.Columns.Count(); col++) {
105        oldColumn = ColumnGroup.GetColumn(col);
106        newColumn = oldColumn.CreateCopyOfColumnWithoutValues();
107        newColumn.SortOrder = oldColumn.SortOrder;
108        if (col == sortColumnIndex) {
109          if (newColumn.SortOrder == SortOrder.None || newColumn.SortOrder == SortOrder.Descending)
110            newColumn.SortOrder = SortOrder.Ascending;
111          else
112            newColumn.SortOrder = SortOrder.Descending;
113        }
114        newColumns.Add(newColumn);
115      }
116
117      for (int j = 0; j < indices.Length; j++) {
118        for (int col = 0; col < ColumnGroup.Columns.Count(); col++)
119          newColumns[col].AddValue(ColumnGroup.Columns.ElementAt(col).GetValue(indices[j]));
120      }
121      return newColumns;
122    }
123
124    private class RowComparer : IComparer<int> {
125      private ColumnGroup columnGroup;
126      private int sortColumnIndex;
127      public RowComparer(ColumnGroup columnGroup, int sortColumnIndex) {
128        this.columnGroup = columnGroup;
129        this.sortColumnIndex = sortColumnIndex;
130      }
131
132      #region IComparer<int> Members
133      public int Compare(int i, int j) {
134        IEnumerator<int> e = columnGroup.SortedColumnIndexes.GetEnumerator();
135        int result = 0;
136        while (result == 0 && e.MoveNext()) {
137          IComparable x = columnGroup.GetColumn(e.Current).GetValue(i);
138          IComparable y = columnGroup.GetColumn(e.Current).GetValue(j);
139          if (x == null) {
140            result = -1;
141            if (y == null)
142              result = 0;
143          } else
144            result = x.CompareTo(y);
145          if (columnGroup.SortOrdersForColumns.ElementAt(e.Current) == SortOrder.Descending)
146            result *= -1;
147          if (e.Current == sortColumnIndex && columnGroup.Columns.ElementAt(e.Current).SortOrder != SortOrder.None)
148            result *= -1;
149        }
150        return result;
151      }
152      #endregion
153    }
154  }
155}
Note: See TracBrowser for help on using the repository browser.