[10698] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) 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 {
|
---|
| 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;
|
---|
[15242] | 33 | Reset();
|
---|
[10698] | 34 | }
|
---|
| 35 |
|
---|
[10852] | 36 | public void SetStartCell(int columnIndex, int rowIndex) {
|
---|
| 37 | Tuple<int, int> startCell = GetNextFoundCell(columnIndex, rowIndex);
|
---|
[10873] | 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);
|
---|
[10852] | 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 |
|
---|
[10698] | 66 | public bool MoveNext() {
|
---|
| 67 | bool result = false;
|
---|
[10705] | 68 | bool endReached = false;
|
---|
[10870] | 69 | if (CurrentCellExists()) {
|
---|
[10698] | 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);
|
---|
[10705] | 75 | } else {
|
---|
| 76 | endReached = true;
|
---|
[10698] | 77 | }
|
---|
[10705] | 78 | } while (!endReached && !CurrentCellExists());
|
---|
| 79 | result = !endReached;
|
---|
[10698] | 80 | }
|
---|
| 81 | return result;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | public Tuple<int, int> GetCurrent() {
|
---|
[10852] | 85 | Tuple<int, int> resultCell = null;
|
---|
[10698] | 86 | if (items != null && CurrentCellExists()) {
|
---|
| 87 | int cellRow = items[currentCell.Item1].ElementAt(currentCell.Item2);
|
---|
[10852] | 88 | resultCell = new Tuple<int, int>(currentCell.Item1, cellRow);
|
---|
[10698] | 89 | }
|
---|
[10852] | 90 | return resultCell;
|
---|
[10698] | 91 | }
|
---|
| 92 |
|
---|
| 93 | public void Reset() {
|
---|
[10873] | 94 | SetStartCell(0, 0);
|
---|
[10705] | 95 | if (!CurrentCellExists()) {
|
---|
| 96 | MoveNext();
|
---|
| 97 | }
|
---|
[10698] | 98 | }
|
---|
| 99 |
|
---|
| 100 | private bool CurrentCellExists() {
|
---|
| 101 | bool result = false;
|
---|
[10870] | 102 | if (currentCell != null && items != null) {
|
---|
[10698] | 103 | result = items.ContainsKey(currentCell.Item1) && currentCell.Item2 < items[currentCell.Item1].Count;
|
---|
| 104 | }
|
---|
| 105 | return result;
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | }
|
---|