Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.Orienteering/HeuristicLab.DataPreprocessing.Views/3.4/Utils/FindPreprocessingItemsIterator.cs @ 11185

Last change on this file since 11185 was 11185, checked in by pfleck, 10 years ago

#2208 merged trunk and updated version info

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