/* This file is part of LilyPond, the GNU music typesetter. Copyright (C) 2006--2015 Jan Nieuwenhuizen LilyPond is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. LilyPond is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with LilyPond. If not, see . */ #ifndef STD_VECTOR_HH #define STD_VECTOR_HH #if 0 /* leads to dubious crashes - libstdc++ bug? */ #ifdef DEBUG #define _GLIBCXX_DEBUG 1 #endif #endif #include "config.hh" /* needed at least for HAVE_STL_DATA_METHOD */ #include /* find, reverse, sort */ #include /* unary_function */ #include #include using namespace std; template int default_compare (T const &a, T const &b) { if (a < b) return -1; else if (b < a) return 1; else return 0; } template int default_compare (T *const &a, T *const &b) { if (a < b) return -1; else if (a > b) return 1; else return 0; } #include "compare.hh" #ifndef VSIZE #define VSIZE typedef size_t vsize; #define VPOS ((vsize) -1) #endif #include template T const & boundary (vector const &v, int dir, vsize i) { assert (dir); return v[dir == -1 ? i : v.size () - 1 - i]; } template T & boundary (vector &v, int dir, vsize i) { assert (dir); return v[dir == -1 ? i : v.size () - 1 - i]; } template T const & back (vector const &v, vsize i) { return v[v.size () - i - 1]; } template T & back (vector &v, vsize i) { return v[v.size () - i - 1]; } template void concat (vector &v, vector const &w) { v.insert (v.end (), w.begin (), w.end ()); } template vsize lower_bound (vector const &v, T const &key, Compare less, vsize b = 0, vsize e = VPOS) { if (e == VPOS) e = v.size (); typename vector::const_iterator i = lower_bound (v.begin () + b, v.begin () + e, key, less); return i - v.begin (); } template vsize upper_bound (vector const &v, T const &key, Compare less, vsize b = 0, vsize e = VPOS) { if (e == VPOS) e = v.size (); typename vector::const_iterator i = upper_bound (v.begin () + b, v.begin () + e, key, less); return i - v.begin (); } template vsize binary_search (vector const &v, T const &key, Compare less, vsize b = 0, vsize e = VPOS) { vsize lb = lower_bound (v, key, less, b, e); if (lb == v.size () || less (key, v[lb])) return VPOS; return lb; } template void vector_sort (vector &v, Compare less, vsize b = 0, vsize e = VPOS) { if (e == VPOS) e = v.size (); sort (v.begin () + b, v.begin () + e, less); } template void reverse (vector &v) { // CHECKME: for a simple vector, like vector, this should // expand to memrev. reverse (v.begin (), v.end ()); } template void uniq (vector &v) { v.erase (unique (v.begin (), v.end ()), v.end ()); } template typename vector::const_iterator find (vector const &v, T const &key) { return find (v.begin (), v.end (), key); } template struct del : public unary_function { void operator () (T x) { delete x; x = 0; } }; template void junk_pointers (vector &v) { // Hmm. for_each (v.begin (), v.end (), del ()); v.clear (); } vector string_split (string str, char c); string string_join (vector const &strs, const string &infix); #define iterof(i,s) typeof((s).begin()) i((s).begin()) #endif /* STD_VECTOR_HH */