#ifndef SVECTOR_H_ #define SVECTOR_H_ using namespace std; #include #include #include #include #include "base/ErrorHandling.h" template class svec : public vector { public: int isize() const {return (int)vector::size();} long long lsize() const {return vector::size();} const T & operator[] (long long i) const { #ifndef NDEBUG if (i>= lsize()) { cout << "ERROR: i=" << i << " lsize=" << lsize() << endl; ThrowError("i>=lsize()", "svec"); } if (i<0) { cout << "ERROR: i=" << i << " lsize=" << lsize() << endl; ThrowError("i<0", "svec"); } //assert(i=0); #endif return vector::operator[](i); } T & operator[] (long long i) { #ifndef NDEBUG if (i>= lsize()) { cout << "ERROR: i=" << i << " lsize=" << lsize() << endl; ThrowError("i>=lsize()", "svec"); } if (i<0) { cout << "ERROR: i=" << i << " lsize=" << lsize() << endl; ThrowError("i<0", "svec"); } //assert(i=0); #endif return vector::operator[](i); } }; template class vec : public svec { }; template void Sort(svec& v) { sort(v.begin(), v.end()); } template void UniqueSort(svec& v) { sort(v.begin(), v.end()); long long i; long long k = 0; for (i=0; i Iter binary_find(Iter begin, Iter end, T val) { // Finds the lower bound in at most log(last - first) + 1 comparisons Iter i = std::lower_bound(begin, end, val); if (i != end && !(val < *i)) return i; // found else return end; // not found } template long long BinSearch(svec & v, const T & item) { typename svec::iterator iter = binary_find(v.begin(), v.end(), item); if (iter == v.end()) return -1; return iter - v.begin(); } template long long BinSearchFuzzy(svec & v, const T & item) { typename svec::iterator iter; typename svec::iterator begin = v.begin(); typename svec::iterator end = v.end(); iter = lower_bound(begin, end, item); long pos = distance(begin, iter); if (pos < 0) return -1; return pos; } #endif //SVECTOR_H_