]> git.donarmstrong.com Git - lilypond.git/blobdiff - flower/include/interval.tcc
Run grand replace for 2015.
[lilypond.git] / flower / include / interval.tcc
index 01dbbb169592e16778b15f213faf8218a07ba4c7..72be6e81e92161984a74cfb6128902bb3d2ade31 100644 (file)
 /*
-  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 <hanwen@xs4all.nl>
 
-  (c) 1996,1997 Han-Wen Nienhuys <hanwen@stack.nl>
-*/
+  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 <http://www.gnu.org/licenses/>.
+*/
 
 #ifndef INTERVAL_TCC
 #define INTERVAL_TCC
 
-#include <assert.h> 
-#include <math.h>
+#include <cassert>
+
 #include "interval.hh"
-#include "string.hh"
-#include "flower-debug.hh"
+#include "std-string.hh"
 
-template<class T>
-void
-Interval_t<T>::print () const
-{
-  fdebug << str ();
-}
+// MacOS 10.3 problems:
+// #include <cmath>
+using namespace std;
 
 template<class T>
 int
-_Interval__compare (const Interval_t<T>&a,Interval_t<T> const&b)
+_Interval__compare (const Interval_t<T> &a, Interval_t<T> const &b)
 {
-  if (a.left == b.left && a.right == b.right)
-       return 0;
-  
-  if (a.left <= b.left && a.right >= b.right)
-       return 1;
+  if (a.at (LEFT) == b.at (LEFT) && a.at (RIGHT) == b.at (RIGHT))
+    return 0;
+
+  if (a.at (LEFT) <= b.at (LEFT) && a.at (RIGHT) >= b.at (RIGHT))
+    return 1;
 
-  if (a.left >= b.left && a.right <= b.right)
-       return -1;
+  if (a.at (LEFT) >= b.at (LEFT) && a.at (RIGHT) <= b.at (RIGHT))
+    return -1;
 
   return -2;
 }
 
 template<class T>
-bool 
-Interval_t<T>::contains_b (Interval_t<T> const& a) const
+bool
+Interval_t<T>::superset (Interval_t<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 false;
   return c_i >= 0;
 }
 
 template<class T>
 int
-Interval__compare (const Interval_t<T>&a,Interval_t<T> const&b)
+Interval__compare (Interval_t<T> const &a, Interval_t<T> const &b)
 {
-  int i = _Interval__compare (a,b);
+  int i = _Interval__compare (a, b);
   if (i < -1)
-       assert (false);
+    assert (false);
   return i;
 }
 
 template<class T>
 void
-Interval_t<T>::set_empty()
+Interval_t<T>::set_empty ()
+{
+  at (LEFT) = (T) infinity ();
+  at (RIGHT) = (T) - infinity ();
+}
+
+template<class T>
+void
+Interval_t<T>::set_full ()
 {
-  left = (T) infinity();
-  right = (T) -infinity();
+  at (LEFT) = (T) - infinity ();
+  at (RIGHT) = (T) infinity ();
 }
 
 template<class T>
 T
-Interval_t<T>::length() const 
+Interval_t<T>::length () const
 {
-  if (right < left) 
+  if (at (RIGHT) <= at (LEFT))
     return 0;
-  else 
-    return right-left;
+  else
+    return at (RIGHT) - at (LEFT);
 }
 
+template<class T>
+T
+Interval_t<T>::delta () const
+{
+  return at (RIGHT) - at (LEFT);
+}
+
+/* smallest Interval which includes *this and #h#  */
 template<class T>
 void
 Interval_t<T>::unite (Interval_t<T> h)
 {
-  if (h.left<left)
-       left = h.left;
-  if (h.right>right)
-       right = h.right;
+  at (LEFT) = min (h.at (LEFT), at (LEFT));
+  at (RIGHT) = max (h.at (RIGHT), at (RIGHT));
 }
 
-/**
-  smallest Interval which includes *this and #h#
- */
-
+/* 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<class T>
 void
-Interval_t<T>::intersect (Interval_t<T> h)
+Interval_t<T>::unite_disjoint (Interval_t<T> h, T padding, Direction d)
 {
-#if defined (__GNUG__) && ! defined (__STRICT_ANSI__)
-  left = h.left >? left;
-  right = h.right <?right;
-#else
-  left = max (h.left, left);
-  right = min (h.right, right);
-#endif
+  T dir = d;
+  T translation = dir * (at (d) + dir * padding - h.at (-d));
+  if (translation > (T) 0)
+    h.translate (translation);
+  unite (h);
 }
 
 template<class T>
 Interval_t<T>
-intersect (Interval_t<T> x, Interval_t<T> const &y)
+Interval_t<T>::union_disjoint (Interval_t<T> h, T padding, Direction d) const
 {
-  x.intersect (y);
-  return x;
+  Interval_t<T> iv = *this;
+  iv.unite_disjoint (h, padding, d);
+  return iv;
 }
 
 template<class T>
-String
-Interval_t<T>::str() const
+void
+Interval_t<T>::intersect (Interval_t<T> h)
 {
-  if (empty_b())
-       return "[empty]";
-  String s ("[");
-  return s + T_to_str (left) + String ("," ) + T_to_str (right ) + String ("]" );
+  at (LEFT) = max (h.at (LEFT), at (LEFT));
+  at (RIGHT) = min (h.at (RIGHT), at (RIGHT));
 }
 
 template<class T>
-bool
-Interval_t<T>::elt_b (T r)
+string
+Interval_t<T>::to_string () const
 {
-  return r >= left && r <= right;
+  if (is_empty ())
+    return "[empty]";
+  string s ("[");
+
+  return (s + T_to_string (at (LEFT)) + string (",")
+          + T_to_string (at (RIGHT)) + string ("]"));
 }
 
+template<class T>
+bool
+Interval_t<T>::contains (T r) const
+{
+  return r >= at (LEFT) && r <= at (RIGHT);
+}
 
-#define INTERVAL__INSTANTIATE(T) struct Interval_t<T>;\
-  template  int Interval__compare(const Interval_t<T>&,Interval_t<T> const&)
+#define INTERVAL__INSTANTIATE(T) struct Interval_t<T>;                  \
+  template int Interval__compare (const Interval_t<T> &, Interval_t<T> const &)
 
 #endif // INTERVAL_TCC