root/ggpa/VariableAssignmentIterator.cpp
| Revision 1, 3.9 kB (checked in by pantley2, 4 years ago) |
|---|
| Line | |
|---|---|
| 1 | /* VariableAssignmentIterator.cpp |
| 2 | * Definitions for functions in VariableAssignmentIterator.h |
| 3 | */ |
| 4 | |
| 5 | #include "VariableAssignmentIterator.h" |
| 6 | |
| 7 | /** |
| 8 | * Constructor |
| 9 | * Parameters: variableMap - a map from variable names to their set |
| 10 | * of possible values |
| 11 | * Initializes the VariableAssignmentIterator |
| 12 | */ |
| 13 | VariableAssignmentIterator::VariableAssignmentIterator(const map<string, vector<string> >& variableMap) |
| 14 | { /* |
| 15 | map<string, vector<string> >::const_iterator varIter; |
| 16 | for(varIter = variableMap.begin(); varIter != variableMap.end(); varIter++){ |
| 17 | indexValues.insert(make_pair(varIter->first, 0)); |
| 18 | possibleValues.push_back(varIter->second); |
| 19 | } |
| 20 | */ |
| 21 | |
| 22 | possibleValues = variableMap; |
| 23 | done = false; |
| 24 | map<string, vector<string> >::const_iterator mapItr; |
| 25 | for (mapItr = variableMap.begin(); mapItr != variableMap.end(); mapItr++) { |
| 26 | string variable = mapItr->first; |
| 27 | variables.push_back(variable); |
| 28 | indexValues[variable] = 0; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * next |
| 34 | * No parameters or return type. |
| 35 | * Move to the next value mapping |
| 36 | */ |
| 37 | void VariableAssignmentIterator::next() |
| 38 | { |
| 39 | /* |
| 40 | map<string, int>::iterator ourIter; |
| 41 | vector<vector<string> >::iterator vecIter; |
| 42 | bool flag = true; |
| 43 | ourIter = indexValues.begin(); |
| 44 | vecIter = possibleValues.begin(); |
| 45 | while(flag && ourIter != indexValues.end()){ |
| 46 | if(unsigned (ourIter->second) >= vecIter->size() - 1){ |
| 47 | ourIter->second = 0; |
| 48 | ourIter++; |
| 49 | vecIter++; |
| 50 | } |
| 51 | else{ |
| 52 | ourIter->second++; |
| 53 | flag = false; |
| 54 | } |
| 55 | } |
| 56 | */ |
| 57 | |
| 58 | if (done) { |
| 59 | return; // we can't go any further. |
| 60 | } |
| 61 | bool carry = true; |
| 62 | for (unsigned int index = 0; carry && index < variables.size(); index++) { |
| 63 | // get the current variable |
| 64 | string variable = variables[index]; |
| 65 | |
| 66 | // increment for this variable |
| 67 | indexValues[variable]++; |
| 68 | if (indexValues[variable] >= possibleValues[variable].size()) { |
| 69 | // if the count is too big, set it to zero and carry over |
| 70 | indexValues[variable] = 0; |
| 71 | carry = 1; |
| 72 | } else { |
| 73 | carry = 0; // we don't have to go any further |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // if we still have a carry, then we have reached the end of the iterator |
| 78 | if (carry) { |
| 79 | done = true; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * getCurrent |
| 85 | * No parameters. |
| 86 | * Return type: map from strings to strings |
| 87 | * Returns the current mapping from variable name to variable value |
| 88 | */ |
| 89 | map<string, string> VariableAssignmentIterator::getCurrent() |
| 90 | { |
| 91 | /* |
| 92 | map<string, string> retMap; |
| 93 | map<string, int>::const_iterator ourIter; |
| 94 | vector<vector<string> >::const_iterator vecIter; |
| 95 | vecIter = possibleValues.begin(); |
| 96 | for(ourIter = indexValues.begin(); ourIter != indexValues.end(); ourIter++){ |
| 97 | retMap.insert(make_pair(ourIter->first, (*vecIter)[ourIter->second])); |
| 98 | } |
| 99 | return retMap; |
| 100 | */ |
| 101 | |
| 102 | map<string, string> thisMapping; |
| 103 | // go through each of the variables and set a mapping for it |
| 104 | for (unsigned int index = 0; index < variables.size(); index++) { |
| 105 | string variable = variables[index]; |
| 106 | // get the index for the current value of this variable |
| 107 | int curIndex = indexValues[variable]; |
| 108 | // get the value from the index and put it in the map |
| 109 | thisMapping[variable] = possibleValues[variable][curIndex]; |
| 110 | } |
| 111 | return thisMapping; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * atEnd |
| 116 | * Parameters: none |
| 117 | * Return type: bool |
| 118 | * Returns true if there are no more mappings of variables, and false |
| 119 | * otherwise. |
| 120 | */ |
| 121 | bool VariableAssignmentIterator::atEnd() const |
| 122 | { |
| 123 | /* |
| 124 | bool retVal; |
| 125 | map<string, int>::const_reverse_iterator ourIter; |
| 126 | vector<vector<string> >::const_reverse_iterator vecIter; |
| 127 | ourIter = indexValues.rbegin(); |
| 128 | //ourIter--; |
| 129 | vecIter = possibleValues.rbegin(); |
| 130 | //vecIter--; |
| 131 | if(unsigned (ourIter->second) < vecIter->size() - 1) |
| 132 | retVal = false; |
| 133 | else |
| 134 | retVal = true; |
| 135 | return retVal; |
| 136 | */ |
| 137 | return done; |
| 138 | } |
Note: See TracBrowser for help on using the browser.
