std::shared_ptr<atn::transition> atn_state::remove_optimized_transition(size_t index) { assert(optimized()); std::shared_ptr<atn::transition> result(std::move(_optimized_transitions.at(index))); _optimized_transitions.erase(_optimized_transitions.begin() + static_cast<ptrdiff_t>(index)); return std::move(result); }
char run_test(const char *filename) { std::fstream file; file.open(filename, std::fstream::in); size_t width, height; file >> width >> height; if(!width || !height) return 1; Matrix::matrix_t values(height, Vector(width, 0.)); for(int i = 0; i < height; ++i) for(int j = 0; j < width; ++j) file >> values[i][j]; Vector optimized(width - 1, 0.); for(int i = 0; i < width - 1; ++i) file >> optimized[i]; Matrix A(values); try { const static Vector solution(Matrix::SimplexMethod(A, optimized.GetLine())); #ifndef VERBOSE for(size_t i = 0; i < solution.Size(); ++i) std::cout << solution[i] << " "; std::cout << std::endl; #endif return verify_solution_validity(A, solution, optimized); } catch(std::exception &e) { return 1; } file.close(); }
std::string execute(int variation) { switch (variation) { case 0: return to_string(bruteForce()); case 1: case -1: return to_string(optimized()); default: return std::string("unavailable"); } return NULL; }
void execute(int variation) { switch (variation) { case 0: case -1: brute(); break; case 1: optimized(); break; default: std::cout << "NOOP!" << std::endl; break; } }
Changeset JS::optimize(const Changeset & changeset, const QString & oldText) { QString changesetBank = changeset.bank_; QString text = oldText; Changeset optimized(changeset.oldLength_, changeset.newLength_, Changeset::Ops(), QString()); const Changeset::Ops::ConstIterator opsEnd = changeset.ops_.end(); Changeset::Ops::ConstIterator prevPart = opsEnd; for (auto it = changeset.ops_.begin(); it != opsEnd; ++it) { if (prevPart != opsEnd && it->first == Changeset::InsertChars) { QString textPart = text.mid(0, prevPart->second.opLength); QString potPart = changesetBank.mid(0, it->second.opLength); int i = textPart.length()-1; int j = potPart.length()-1; int newlines = 0; while ((i >= 0) && (j > 0) && (textPart[i] == potPart[j])) { if (textPart[i] == '\n') { newlines++; } --i; --j; } const int len = textPart.length() - 1 - i; // qDebug() << "i:" << i << " j: " << j << " len: " << len; Changeset::Op prevOp = *prevPart; prevOp.second.opLength -= len; prevOp.second.newlines -= newlines; Changeset::Op curOp = *it; curOp.second.opLength -= len; curOp.second.newlines -= newlines; // qDebug() << "PrevOp " << prevOp.first << ", " << prevOp.second.toString(); // qDebug() << "CurOp " << curOp.first << ", " << curOp.second.toString(); const Changeset::Op newOpPost(qMakePair(Changeset::KeepChars, Changeset::OperationData(len, newlines, -1))); // qDebug() << "NewOp " << newOpPost.first << ", " << newOpPost.second.toString(); textPart = textPart.mid(0, i+1); potPart = potPart.mid(0, j+1); i = 0; newlines = 0; for (i = 0; (i < textPart.length()) && (i < potPart.length()) && textPart[i] == potPart[i] ; ++i) { if (textPart[i] == '\n') ++newlines; } if (i > 0) { // qDebug("BigI"); prevOp.second.opLength -= i; prevOp.second.newlines -= newlines; curOp.second.opLength -= i; curOp.second.newlines -= newlines; const Changeset::Op newOp(qMakePair(Changeset::KeepChars, Changeset::OperationData(i, newlines, -1))); optimized.ops_.push_back(newOp); text = text.mid(i); } if (prevOp.second.opLength > 0) { optimized.ops_.push_back(prevOp); text = text.mid(prevOp.second.opLength); } if (curOp.second.opLength > 0) { optimized.ops_.push_back(curOp); } if (newOpPost.second.opLength > 0) { optimized.ops_.push_back(newOpPost); } optimized.bank_ += changesetBank.mid(i, curOp.second.opLength); changesetBank = changesetBank.mid(i + curOp.second.opLength + len); prevPart = opsEnd; } else { if (prevPart != opsEnd) { //unoptimized '-' op text = text.mid(prevPart->second.opLength); optimized.ops_.push_back(*prevPart); prevPart = opsEnd; } switch (it->first) { case Changeset::KeepChars: text = text.mid(it->second.opLength); optimized.ops_.push_back(*it); break; case Changeset::InsertChars: optimized.bank_ += changesetBank.mid(0, it->second.opLength); changesetBank = changesetBank.mid(it->second.opLength); optimized.ops_.push_back(*it); break; case Changeset::SkipOverChars: prevPart = it; break; } } } if (prevPart != opsEnd) { optimized.ops_.push_back(*prevPart); } return optimized; }
void atn_state::set_optimized_transition(size_t index, std::shared_ptr<atn::transition> const& transition) { assert(optimized()); _optimized_transitions[index] = transition; }