Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing.Views/3.3/Utils/FindPreprocessingItemsIterator.cs @ 10870

Last change on this file since 10870 was 10870, checked in by sbreuer, 10 years ago
  • offer search comparison options
File size: 3.6 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;
25
26namespace HeuristicLab.DataPreprocessing.Views {
27  class FindPreprocessingItemsIterator : IFindPreprocessingItemsIterator {
28    private IDictionary<int, IList<int>> items;
29    private Tuple<int, int> currentCell;
30
31    public FindPreprocessingItemsIterator(IDictionary<int, IList<int>> items) {
32      this.items = items;
33      Reset();
34    }
35
36
37    public void SetStartCell(int columnIndex, int rowIndex) {
38      Tuple<int, int> startCell = GetNextFoundCell(columnIndex, rowIndex);
39      int tmpColumnIndex = columnIndex + 1;
40      while (startCell == null && tmpColumnIndex != columnIndex) {
41        if (!items.ContainsKey(tmpColumnIndex)) {
42          tmpColumnIndex = 0;
43        }
44        startCell = GetNextFoundCell(tmpColumnIndex, 0);
45        ++tmpColumnIndex;
46      }
47      if (startCell == null && tmpColumnIndex == columnIndex && rowIndex != 0) {
48        startCell = GetNextFoundCell(columnIndex, 0);
49      }
50      currentCell = startCell;
51    }
52
53    private Tuple<int, int> GetNextFoundCell(int columnIndex, int rowIndex) {
54      Tuple<int, int> next = null;
55      for (int i = 0; i < items[columnIndex].Count; ++i) {
56        if (items[columnIndex][i] >= rowIndex) {
57          next = new Tuple<int, int>(columnIndex, i);
58          break;
59        }
60      }
61      return next;
62    }
63
64    public bool MoveNext() {
65      bool result = false;
66      bool endReached = false;
67      if (CurrentCellExists()) {
68        do {
69          if (currentCell.Item2 < items[currentCell.Item1].Count - 1) {
70            currentCell = new Tuple<int, int>(currentCell.Item1, currentCell.Item2 + 1);
71          } else if (currentCell.Item1 < items.Count - 1) {
72            currentCell = new Tuple<int, int>(currentCell.Item1 + 1, 0);
73          } else {
74            endReached = true;
75          }
76        } while (!endReached && !CurrentCellExists());
77        result = !endReached;
78      }
79      return result;
80    }
81
82    public Tuple<int, int> GetCurrent() {
83      Tuple<int, int> resultCell = null;
84      if (items != null && CurrentCellExists()) {
85        int cellRow = items[currentCell.Item1].ElementAt(currentCell.Item2);
86        resultCell = new Tuple<int, int>(currentCell.Item1, cellRow);
87      }
88      return resultCell;
89    }
90
91    public void Reset() {
92      currentCell = new Tuple<int, int>(0, 0);
93      if (!CurrentCellExists()) {
94        MoveNext();
95      }
96    }
97
98    private bool CurrentCellExists() {
99      bool result = false;
100      if (currentCell != null && items != null) {
101        result = items.ContainsKey(currentCell.Item1) && currentCell.Item2 < items[currentCell.Item1].Count;
102      }
103      return result;
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.