X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=flower%2Finclude%2Finterval.tcc;h=2ad1fa606b7add18165099cde00bd34263af4b93;hb=b89b87d14b6a5b32f46ecdf10dfa0d023a34d3a2;hp=0ac8de264fba7a8a9353652405b248a4e33fc7d8;hpb=4ecdbd7d70ca7441be4dddd15ac01cc255bc2a35;p=lilypond.git diff --git a/flower/include/interval.tcc b/flower/include/interval.tcc index 0ac8de264f..2ad1fa606b 100644 --- a/flower/include/interval.tcc +++ b/flower/include/interval.tcc @@ -1,32 +1,45 @@ /* - interval.tcc -- implement Interval_t + This file is part of LilyPond, the GNU music typesetter. - source file of the Flower Library + Copyright (C) 1996--2012 Han-Wen Nienhuys - (c) 1996--2005 Han-Wen Nienhuys + 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 INTERVAL_TCC #define INTERVAL_TCC #include -#include -using namespace std; #include "interval.hh" -#include "string.hh" +#include "std-string.hh" + +// MacOS 10.3 problems: +// #include +using namespace std; template int _Interval__compare (const Interval_t &a, Interval_t const &b) { - if (a.elem (LEFT) == b.elem (LEFT) && a.elem (RIGHT) == b.elem (RIGHT)) + if (a.at (LEFT) == b.at (LEFT) && a.at (RIGHT) == b.at (RIGHT)) return 0; - if (a.elem (LEFT) <= b.elem (LEFT) && a.elem (RIGHT) >= b.elem (RIGHT)) + if (a.at (LEFT) <= b.at (LEFT) && a.at (RIGHT) >= b.at (RIGHT)) return 1; - if (a.elem (LEFT) >= b.elem (LEFT) && a.elem (RIGHT) <= b.elem (RIGHT)) + if (a.at (LEFT) >= b.at (LEFT) && a.at (RIGHT) <= b.at (RIGHT)) return -1; return -2; @@ -56,33 +69,33 @@ template void Interval_t::set_empty () { - elem_ref (LEFT) = (T) infinity (); - elem_ref (RIGHT) = (T) -infinity (); + at (LEFT) = (T) infinity (); + at (RIGHT) = (T) - infinity (); } template void Interval_t::set_full () { - elem_ref (LEFT) = (T) -infinity (); - elem_ref (RIGHT) = (T) infinity (); + at (LEFT) = (T) - infinity (); + at (RIGHT) = (T) infinity (); } template T Interval_t::length () const { - if (elem (RIGHT) <= elem (LEFT)) + if (at (RIGHT) <= at (LEFT)) return 0; else - return elem (RIGHT) - elem (LEFT); + return at (RIGHT) - at (LEFT); } template T Interval_t::delta () const { - return elem (RIGHT) - elem (LEFT); + return at (RIGHT) - at (LEFT); } /* smallest Interval which includes *this and #h# */ @@ -90,38 +103,63 @@ template void Interval_t::unite (Interval_t h) { - elem_ref (LEFT) = min (h.elem (LEFT), elem (LEFT)); - elem_ref (RIGHT) = max (h.elem (RIGHT), elem (RIGHT)); + at (LEFT) = min (h.at (LEFT), at (LEFT)); + at (RIGHT) = max (h.at (RIGHT), at (RIGHT)); +} + +/* Unites h and this interval, but in such a way + that h will lie in a particular direction from this + interval, with a minimum amount of space in between. + (That is, h will be translated before we unite, if + that is necessary to prevent overlap. */ +template +void +Interval_t::unite_disjoint (Interval_t h, T padding, Direction d) +{ + T dir = d; + T translation = dir * (at (d) + dir * padding - h.at (-d)); + if (translation > (T) 0) + h.translate (translation); + unite (h); +} + +template +Interval_t +Interval_t::union_disjoint (Interval_t h, T padding, Direction d) const +{ + Interval_t iv = *this; + iv.unite_disjoint (h, padding, d); + return iv; } template void Interval_t::intersect (Interval_t h) { - elem_ref (LEFT) = max (h.elem (LEFT), elem (LEFT)); - elem_ref (RIGHT) = min (h.elem (RIGHT), elem (RIGHT)); + at (LEFT) = max (h.at (LEFT), at (LEFT)); + at (RIGHT) = min (h.at (RIGHT), at (RIGHT)); } template -String +string Interval_t::to_string () const { if (is_empty ()) return "[empty]"; - String s ("["); + string s ("["); - return (s + T_to_string (elem (LEFT)) + String (",") - + T_to_string (elem (RIGHT)) + String ("]")); + return (s + T_to_string (at (LEFT)) + string (",") + + T_to_string (at (RIGHT)) + string ("]")); } template bool Interval_t::contains (T r) const { - return r >= elem (LEFT) && r <= elem (RIGHT); + return r >= at (LEFT) && r <= at (RIGHT); } -#define INTERVAL__INSTANTIATE(T) struct Interval_t; \ +#define INTERVAL__INSTANTIATE(T) struct Interval_t; \ template int Interval__compare (const Interval_t &, Interval_t const &) #endif // INTERVAL_TCC