void Intervals::difference(Intervals is) { for (int i = 0; i < size(); i++) { for (int j = 0; j < is.size(); j++) { Intervals temp = _int[i].difference(is[j]); if (temp.size() > 1) { erase(begin() + i); insert(begin() + i, temp.begin(), temp.end()); } else if (temp.size() == 0) { erase(begin() + i); } else if (temp[0] != _int[i]) { _int[i] = temp[0]; } } } }
IntervalScheduler::IntervalScheduler(Intervals &inputIntervals) : n(inputIntervals.size()), bestGeneScore(vector<double>(n+1, 0)), p(vector<size_t>(n+1, 0)) { sort(inputIntervals.begin(), inputIntervals.end()); // We want the gene list to start at index 1 ivals.push_back(WeightedInterval(-1,-1,-1)); for (auto i : inputIntervals) ivals.push_back(i); for (size_t j = 1; j < n+1; j++) { p[j] = previousDisjointInterval(j); } }
void Intervals::intersect(Intervals is) { Intervals result; for (int i = 0; i < size(); i++) { if (_int[i].isEmpty()) continue; for (int j = 0; j < is.size(); j++) { if (is[j].isEmpty()) { continue; } if (Interval::intersects(_int[i], is[j])) { result.push_back(_int[i].intersect(is[j])); } } } _int = result._int; }
void Intervals::unionize() { std::sort(begin(), end(), compare); for (int i = 0; i < size(); i++) { for (int j = i + 1; j < size(); j++) { Intervals temp = _int[i].unionize(_int[j]); //std::cout << ints[i] << " " << ints[j] << std::endl; //std::cout << "unioned: " << temp << std::endl; if (temp.size() == 1) { auto b = begin() + j; _int.erase(b); _int[i] = temp[0]; j = i; } } } }
// Static solve function, returns a list of interval indexes that spans the // first input, returns an empty list if no such collection exists. // Throws a range_error if any interval [a, b] has [b < a] static vector<int> Solve(Interval a, Intervals const & v) { // check input if(!validateInput(a, v)) throw range_error("Invalid interval ranges"); // sort by lower bound and keep reference to original order typedef pair<Interval, int> P; // pair of interval and original index vector<P> p; rep(i, 0, (int)v.size()) p.push_back(P(v[i], i)); sort(all(p)); // solve the problem vector<int> ret; D minVal = a.first; typename vector<P>::iterator from = p.begin(); // while interval is not covered while(minVal < a.second || minVal == a.second && ret.empty()) { // find intervals which includes left boundary, pick the one with the // highest right boundary int index = -1; D maxVal = minVal; for(auto curr = from; curr != p.end(); ++curr) { D f = curr->first.first, t = curr->first.second; if(f <= minVal) { if(t > maxVal || ret.empty() && t == maxVal) { maxVal = t; index = curr->second; } continue; } from = curr; break; } // if no such vector can be found problem is unsolvable if(index == -1) return vector<int>(); // otherwise add vector to answer and repeat ret.push_back(index); minVal = maxVal; } return ret; }