X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=flower%2Finclude%2Finterval.tcc;h=72be6e81e92161984a74cfb6128902bb3d2ade31;hb=47db9a3883d726ca53e2133a3b2298f78dd6a32e;hp=7c51bd18f3879ba6d554d890d507fe2cff9c49b8;hpb=bb36bac02a64770871780231ecc709cb18b20932;p=lilypond.git diff --git a/flower/include/interval.tcc b/flower/include/interval.tcc index 7c51bd18f3..72be6e81e9 100644 --- a/flower/include/interval.tcc +++ b/flower/include/interval.tcc @@ -1,50 +1,55 @@ /* - interval.tcc -- implement Interval_t + This file is part of LilyPond, the GNU music typesetter. - source file of the Flower Library + Copyright (C) 1996--2015 Han-Wen Nienhuys - (c) 1996, 1997--2000 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 +#include + #include "interval.hh" -#include "string.hh" -#include "flower-debug.hh" +#include "std-string.hh" -template -void -Interval_t::print () const -{ - if (flower_dstream) - *flower_dstream << str (); -} +// MacOS 10.3 problems: +// #include +using namespace std; template int -_Interval__compare (const Interval_t&a,Interval_t const&b) +_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; } template -bool -Interval_t::contains_b (Interval_t const& a) const +bool +Interval_t::superset (Interval_t const &a) const { - int c_i= _Interval__compare (*this, a); + int c_i = _Interval__compare (*this, a); if (c_i == -2) return false; return c_i >= 0; @@ -52,9 +57,9 @@ Interval_t::contains_b (Interval_t const& a) const template int -Interval__compare (const Interval_t&a,Interval_t const&b) +Interval__compare (Interval_t const &a, Interval_t const &b) { - int i = _Interval__compare (a,b); + int i = _Interval__compare (a, b); if (i < -1) assert (false); return i; @@ -62,82 +67,99 @@ Interval__compare (const Interval_t&a,Interval_t const&b) template void -Interval_t::set_empty() +Interval_t::set_empty () +{ + at (LEFT) = (T) infinity (); + at (RIGHT) = (T) - infinity (); +} + +template +void +Interval_t::set_full () { - elem (LEFT) = (T) infinity(); - elem (RIGHT) = (T) -infinity(); + at (LEFT) = (T) - infinity (); + at (RIGHT) = (T) infinity (); } template T -Interval_t::length() const +Interval_t::length () const { - if (elem (RIGHT) < elem (LEFT)) + if (at (RIGHT) <= at (LEFT)) return 0; - else - return elem (RIGHT)-elem (LEFT); + else + return at (RIGHT) - at (LEFT); } -/** - smallest Interval which includes *this and #h# - */ +template +T +Interval_t::delta () const +{ + return at (RIGHT) - at (LEFT); +} + +/* smallest Interval which includes *this and #h# */ template void Interval_t::unite (Interval_t h) { - elem (LEFT) = h.elem (LEFT) ?elem (RIGHT); - -#if 0 - if (h.elem (LEFT)elem (RIGHT)) - elem (RIGHT) = h.elem (RIGHT); -#endif + 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::intersect (Interval_t h) +Interval_t::unite_disjoint (Interval_t h, T padding, Direction d) { -#if defined (__GNUG__) && ! defined (__STRICT_ANSI__) - elem (LEFT) = h.elem (LEFT) >? elem (LEFT); - elem (RIGHT) = h.elem (RIGHT) (T) 0) + h.translate (translation); + unite (h); } template Interval_t -intersect (Interval_t x, Interval_t const &y) +Interval_t::union_disjoint (Interval_t h, T padding, Direction d) const { - x.intersect (y); - return x; + Interval_t iv = *this; + iv.unite_disjoint (h, padding, d); + return iv; } template -String -Interval_t::str() const +void +Interval_t::intersect (Interval_t h) { - if (empty_b()) + at (LEFT) = max (h.at (LEFT), at (LEFT)); + at (RIGHT) = min (h.at (RIGHT), at (RIGHT)); +} + +template +string +Interval_t::to_string () const +{ + if (is_empty ()) return "[empty]"; - String s ("["); - - return s + T_to_str (elem (LEFT)) + String ("," ) + T_to_str (elem (RIGHT) ) + String ("]" ); + string s ("["); + + return (s + T_to_string (at (LEFT)) + string (",") + + T_to_string (at (RIGHT)) + string ("]")); } template bool -Interval_t::elem_b (T r) +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;\ -template int Interval__compare(const Interval_t&,Interval_t const&) +#define INTERVAL__INSTANTIATE(T) struct Interval_t; \ + template int Interval__compare (const Interval_t &, Interval_t const &) #endif // INTERVAL_TCC