Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.DataPreprocessing.Views/3.4/Utils/FindPreprocessingItemsIterator.cs @ 15888

Last change on this file since 15888 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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    public void SetStartCell(int columnIndex, int rowIndex) {
37      Tuple<int, int> startCell = GetNextFoundCell(columnIndex, rowIndex);
38      if (startCell == null) {
39        int tmpColumnIndex = columnIndex;
40        do {
41          ++tmpColumnIndex;
42          if (!items.ContainsKey(tmpColumnIndex)) {
43            tmpColumnIndex = 0;
44          }
45          startCell = GetNextFoundCell(tmpColumnIndex, 0);
46        } while (startCell == null && tmpColumnIndex != columnIndex);
47
48        if (startCell == null && tmpColumnIndex == columnIndex && rowIndex != 0) {
49          startCell = GetNextFoundCell(columnIndex, 0);
50        }
51      }
52      currentCell = startCell;
53    }
54
55    private Tuple<int, int> GetNextFoundCell(int columnIndex, int rowIndex) {
56      Tuple<int, int> next = null;
57      for (int i = 0; i < items[columnIndex].Count; ++i) {
58        if (items[columnIndex][i] >= rowIndex) {
59          next = new Tuple<int, int>(columnIndex, i);
60          break;
61        }
62      }
63      return next;
64    }
65
66    public bool MoveNext() {
67      bool result = false;
68      bool endReached = false;
69      if (CurrentCellExists()) {
70        do {
71          if (currentCell.Item2 < items[currentCell.Item1].Count - 1) {
72            currentCell = new Tuple<int, int>(currentCell.Item1, currentCell.Item2 + 1);
73          } else if (currentCell.Item1 < items.Count - 1) {
74            currentCell = new Tuple<int, int>(currentCell.Item1 + 1, 0);
75          } else {
76            endReached = true;
77          }
78        } while (!endReached && !CurrentCellExists());
79        result = !endReached;
80      }
81      return result;
82    }
83
84    public Tuple<int, int> GetCurrent() {
85      Tuple<int, int> resultCell = null;
86      if (items != null && CurrentCellExists()) {
87        int cellRow = items[currentCell.Item1].ElementAt(currentCell.Item2);
88        resultCell = new Tuple<int, int>(currentCell.Item1, cellRow);
89      }
90      return resultCell;
91    }
92
93    public void Reset() {
94      SetStartCell(0, 0);
95      if (!CurrentCellExists()) {
96        MoveNext();
97      }
98    }
99
100    private bool CurrentCellExists() {
101      bool result = false;
102      if (currentCell != null && items != null) {
103        result = items.ContainsKey(currentCell.Item1) && currentCell.Item2 < items[currentCell.Item1].Count;
104      }
105      return result;
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.