[10698] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11594] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10698] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 |
|
---|
| 26 | namespace HeuristicLab.DataPreprocessing.Views {
|
---|
| 27 | class FindPreprocessingItemsIterator : IFindPreprocessingItemsIterator {
|
---|
[10998] | 28 |
|
---|
[10698] | 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;
|
---|
[10998] | 34 | Reset();
|
---|
[10698] | 35 | }
|
---|
| 36 |
|
---|
[10852] | 37 | public void SetStartCell(int columnIndex, int rowIndex) {
|
---|
| 38 | Tuple<int, int> startCell = GetNextFoundCell(columnIndex, rowIndex);
|
---|
[10873] | 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);
|
---|
[10852] | 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 |
|
---|
[10698] | 67 | public bool MoveNext() {
|
---|
| 68 | bool result = false;
|
---|
[10705] | 69 | bool endReached = false;
|
---|
[10870] | 70 | if (CurrentCellExists()) {
|
---|
[10698] | 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);
|
---|
[10705] | 76 | } else {
|
---|
| 77 | endReached = true;
|
---|
[10698] | 78 | }
|
---|
[10705] | 79 | } while (!endReached && !CurrentCellExists());
|
---|
| 80 | result = !endReached;
|
---|
[10698] | 81 | }
|
---|
| 82 | return result;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | public Tuple<int, int> GetCurrent() {
|
---|
[10852] | 86 | Tuple<int, int> resultCell = null;
|
---|
[10698] | 87 | if (items != null && CurrentCellExists()) {
|
---|
| 88 | int cellRow = items[currentCell.Item1].ElementAt(currentCell.Item2);
|
---|
[10852] | 89 | resultCell = new Tuple<int, int>(currentCell.Item1, cellRow);
|
---|
[10698] | 90 | }
|
---|
[10852] | 91 | return resultCell;
|
---|
[10698] | 92 | }
|
---|
| 93 |
|
---|
| 94 | public void Reset() {
|
---|
[10873] | 95 | SetStartCell(0, 0);
|
---|
[10705] | 96 | if (!CurrentCellExists()) {
|
---|
| 97 | MoveNext();
|
---|
| 98 | }
|
---|
[10698] | 99 | }
|
---|
| 100 |
|
---|
| 101 | private bool CurrentCellExists() {
|
---|
| 102 | bool result = false;
|
---|
[10870] | 103 | if (currentCell != null && items != null) {
|
---|
[10698] | 104 | result = items.ContainsKey(currentCell.Item1) && currentCell.Item2 < items[currentCell.Item1].Count;
|
---|
| 105 | }
|
---|
| 106 | return result;
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 | }
|
---|