]> git.donarmstrong.com Git - lilypond.git/blobdiff - flower/include/interval.tcc
release: 0.1.11
[lilypond.git] / flower / include / interval.tcc
index 32eec44580228b76f322b47d79a026c031a548d3..1a483b5f188a58a8f527a3429767e283b0b310ba 100644 (file)
@@ -1,3 +1,15 @@
+/*
+  interval.tcc -- implement Interval_t
+
+  source file of the Flower Library
+
+  (c) 1996,1997 Han-Wen Nienhuys <hanwen@stack.nl>
+*/
+
+
+#ifndef INTERVAL_TCC
+#define INTERVAL_TCC
+
 #include <assert.h> 
 #include <math.h>
 #include "interval.hh"
 
 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)
+  if (a.left == b.left && a.right == b.right)
        return 0;
-    
-    if (a.left <= b.left && a.right >= b.right)
+  
+  if (a.left <= b.left && a.right >= b.right)
        return 1;
 
-    if (a.left >= b.left && a.right <= b.right)
+  if (a.left >= b.left && a.right <= b.right)
        return -1;
 
-    return -2;
+  return -2;
 }
 
+template<class T>
+bool 
+Interval_t<T>::contains_b (Interval_t<T> const& a) const
+{
+  int c_i= _Interval__compare (*this, a);
+  if (c_i == -2)
+       return false;
+  return c_i >= 0;
+}
 
 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)
 {
-    int i = _Interval__compare(a,b);
-    if (i < -1)
-       assert(false);
-    return i;
+  int i = _Interval__compare (a,b);
+  if (i < -1)
+       assert (false);
+  return i;
 }
 
-#ifdef AIX
-const Real INFTY = 1e8;        // ARGh. AIX sucks
-#else
-const Real INFTY = HUGE_VAL;
-#endif
-
 template<class T>
 void
 Interval_t<T>::set_empty()
 {
-    left = INFTY;
-    right = -INFTY;
+  left = (T) infinity();
+  right = (T) -infinity();
 }
 
 template<class T>
 T
-Interval_t<T>::length() const {
-    assert(right >= left);
+Interval_t<T>::length() const 
+{
+  if (right < left) 
+    return 0;
+  else 
     return right-left;
 }
 
 template<class T>
 void
-Interval_t<T>::unite(Interval_t<T> h)
+Interval_t<T>::unite (Interval_t<T> h)
 {
-    if (h.left<left)
+  if (h.left<left)
        left = h.left;
-    if (h.right>right)
+  if (h.right>right)
        right = h.right;
 }
 
@@ -68,39 +86,45 @@ Interval_t<T>::unite(Interval_t<T> h)
 
 template<class T>
 void
-Interval_t<T>::intersect(Interval_t<T> h)
+Interval_t<T>::intersect (Interval_t<T> h)
 {
 #if defined (__GNUG__) && ! defined (__STRICT_ANSI__)
-    left = h.left >? left;
-    right = h.right <?right;
+  left = h.left >? left;
+  right = h.right <?right;
 #else
-    left = max(h.left, left);
-    right = min(h.right, right);
+  left = max (h.left, left);
+  right = min (h.right, right);
 #endif
 }
 
 template<class T>
 Interval_t<T>
-intersect(Interval_t<T> x, Interval_t<T> const &y)
+intersect (Interval_t<T> x, Interval_t<T> const &y)
 {
-    x.intersect(y);
-    return x;
+  x.intersect (y);
+  return x;
 }
 
 template<class T>
 String
 Interval_t<T>::str() const
 {
-    if (empty_b())
+  if (empty_b())
        return "[empty]";
-    String s("[");
+  String s ("[");
  
-    return s + left + "," + right +"]";
+  return s + String (left) + String ("," ) + String (right ) + String ("]" );
 }
 
 template<class T>
 bool
-Interval_t<T>::elt_b(T r)
+Interval_t<T>::elt_b (T r)
 {
-    return r >= left && r <= right;
+  return r >= left && r <= right;
 }
+
+
+#define INTERVAL__INSTANTIATE(T) struct Interval_t<T>;\
+  template  int Interval__compare(const Interval_t<T>&,Interval_t<T> const&)
+
+#endif // INTERVAL_TCC