]> git.donarmstrong.com Git - lilypond.git/commitdiff
* flower/polynomial.cc (operator +): optimize += operator. No
authorHan-Wen Nienhuys <hanwen@xs4all.nl>
Sun, 25 Aug 2002 14:14:49 +0000 (14:14 +0000)
committerHan-Wen Nienhuys <hanwen@xs4all.nl>
Sun, 25 Aug 2002 14:14:49 +0000 (14:14 +0000)
copying.

* lily/source-file.cc (get_line): use binary search to determine
line number. This kills another quadratic time-complexity term.

* lily/include/source-file.hh (class Source_file): add newline_locations

* flower/include/array.icc (binary_search_bounds): new function.

ChangeLog
flower/include/array.icc
flower/include/parray.hh
flower/include/polynomial.hh
flower/polynomial.cc
lily/GNUmakefile
lily/bezier.cc
lily/include/lily-guile.hh
lily/include/source-file.hh
lily/lily-guile.cc
lily/source-file.cc

index 803c6f861f42d4c4cbeea2a100629c412ca76f31..29b58cc8847a52cbd1dc965243ef1303396184b8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2002-08-25  Han-Wen Nienhuys  <hanwen@cs.uu.nl>
+
+       * flower/polynomial.cc (operator +): optimize += operator. No
+       copying.
+
+       * lily/source-file.cc (get_line): use binary search to determine
+       line number. This kills another quadratic time-complexity term.
+
+       * lily/include/source-file.hh (class Source_file): add newline_locations
+
+       * flower/include/array.icc (binary_search_bounds): new function.
+
 2002-08-22  Han-Wen Nienhuys  <hanwen@cs.uu.nl>
 
        * input/regression/tuplet-properties.ly (texidoc): bugfix
index 839da9cce19f0ab6a48ef40b65560f3a8cff8a4d..7b94eab5fccff78e8c8be5bd794e07862b8ea11a 100644 (file)
@@ -108,6 +108,32 @@ Array<T>::slice (int lower, int upper) const
 }
 
 
+template<class T>
+void
+binary_search_bounds (Array<T> const &table,
+                     T const &key, int (*compare) (T const&, T const &),
+                     int *lo,
+                     int *hi)
+{
+  int cmp;
+  int result;
+
+  /* binary search */
+  do
+  {
+      cmp = (*lo + *hi) / 2;
+
+      result = (*compare)  (key, table[cmp]);
+
+      if (result < 0)
+          *hi = cmp;
+      else
+          *lo = cmp;
+    }
+  while (*hi - *lo > 1);
+}
+
+
 /*
   lookup with binsearch, return array index.
 */
@@ -119,24 +145,11 @@ binary_search (Array<T> const &table,
               int hi = -1
               ) 
 {
-  int cmp;
-  int result;
   if (hi < 0)
     hi = table.size ();
 
-  /* binary search */
-  do
-  {
-      cmp = (lo + hi) / 2;
-
-      result = (*compare)  (key, table[cmp]);
+  binary_search_bounds (table, key, compare, &lo, &hi);
 
-      if (result < 0)
-          hi = cmp;
-      else
-          lo = cmp;
-    }
-  while (hi - lo > 1);
   if (! (*compare) (key, table[lo]))
     {
       return lo;
@@ -144,4 +157,3 @@ binary_search (Array<T> const &table,
   else
     return -1;              /* not found */
 }
-
index 08fe766f5d3f2b9eb61a9d29ba5ab90d5a6637d9..21a40429c60af407a835c0525f9b7bfe56bc26b0 100644 (file)
@@ -12,6 +12,8 @@
 
 #include "array.hh"
 
+
+
 /**
   an array of pointers.
 
 template<class T>
 class Link_array : private Array<void *>
 {
-  static int default_compare (T *const& p1, T  *const&p2) {
-    /* can't do p1 -p2, since T might be an incomplete type */
-    if (p1 < p2)
-      return -1 ;
-    if (p2 < p1)
-      return 1;
-    return 0;
-  }
+
   Link_array (Array<void*> v)
     :Array<void*> (v)
     {
@@ -37,6 +32,15 @@ public:
   Link_array ()
     {}
 
+  static int default_compare (T *const& p1, T  *const&p2)
+  {
+    /* can't do p1 -p2, since T might be an incomplete type */
+    if (p1 < p2)
+      return -1 ;
+    if (p2 < p1)
+      return 1;
+    return 0;
+  }
   Link_array (T * const *tp, int n)
     : Array<void*> ((void **)tp, n)
     {
@@ -179,7 +183,6 @@ public:
       else
        return 0;
     }
-  
 };
 
 template<class T, class V>
@@ -295,5 +298,30 @@ binsearch_links (Link_array<T> const &arr, T *t,
 }
 
 
+template<class T>
+void
+binary_search_bounds (Link_array<T> const &table,
+                     T const *key, int (*compare) (T * const& , T *const &),
+                     int *lo,
+                     int *hi)
+{
+  int cmp;
+  int result;
+
+  /* binary search */
+  do
+  {
+      cmp = (*lo + *hi) / 2;
+
+      result = (*compare)  ((T*) key, table[cmp]);
+
+      if (result < 0)
+          *hi = cmp;
+      else
+          *lo = cmp;
+    }
+  while (*hi - *lo > 1);
+}
+
 #endif // PARRAY_HH
 
index b978910caf309d7a4b45c6e505f1ca557e0faafe..aade80fbbc899e39972df13b2bc194ba5c524f36 100644 (file)
@@ -39,7 +39,6 @@ struct Polynomial
 
   /// eliminate #x#  close to  zero
   void real_clean ();
-  static Polynomial add (const Polynomial & p1, const Polynomial & p2);
   void scalarmultiply (Real fact);
   void operator *= (Real f) { scalarmultiply (f); }
   void operator /= (Real f) { scalarmultiply (1/f); }
@@ -48,7 +47,6 @@ struct Polynomial
   void operator -= (Polynomial const &p2);
   Polynomial (Real a, Real b =0.0);
   Polynomial (){}
-  static Polynomial subtract (const Polynomial & p1, const Polynomial & p2);
   void set_negate (const Polynomial & src);
     
   /// take the derivative
index f5d4b38fe3c07bb14fccbabcbf3a3bb6b09e7e24..c8bf66811fef038004f283016ede22406edd4a64 100644 (file)
@@ -94,23 +94,29 @@ Polynomial::clean ()
 }
 
 
-Polynomial 
-Polynomial::add (const Polynomial & p1, const Polynomial & p2)
+
+void
+Polynomial::operator += (Polynomial const &p)
 {
-  Polynomial dest;
-  int tempord =  p2.degree () >? p1.degree ();
-  for (int i = 0; i <= tempord; i++)
-    {
-      Real temp = 0.0;
-      if (i <= p1.degree ())
-       temp += p1.coefs_[i];
-      if (i <= p2.degree ())
-       temp += p2.coefs_[i];
-      dest.coefs_.push (temp);
-    }
-  return dest;
+  while (degree () < p.degree())
+    coefs_.push (0.0);
+
+  for (int i = 0; i <= p.degree(); i++)
+    coefs_[i] += p.coefs_[i];
 }
 
+
+void
+Polynomial::operator -= (Polynomial const &p)
+{
+  while (degree () < p.degree())
+    coefs_.push (0.0);
+
+  for (int i = 0; i <= p.degree(); i++)
+    coefs_[i] -= p.coefs_[i];
+}
+
+
 void
 Polynomial::scalarmultiply (Real fact)
 {
@@ -118,24 +124,7 @@ Polynomial::scalarmultiply (Real fact)
     coefs_[i] *= fact;
 }
 
-Polynomial
-Polynomial::subtract (const Polynomial & p1, const Polynomial & p2)
-{
-  Polynomial dest;
-  int tempord =  p2.degree () >? p1.degree ();
-  
-  for (int i = 0; i <= tempord; i++)
-    {
-      Real temp = 0.0; // can't store result directly.. a=a-b
-      if (i <= p1.degree ())
-       temp += p1.coefs_[i];
-      if (i <= p2.degree ())
-       temp -= p2.coefs_[i];
-      dest.coefs_.push (temp);
-    }
-  return dest;
-  
-}
+
 
 void
 Polynomial::set_negate (const Polynomial & src)
@@ -356,14 +345,4 @@ Polynomial:: operator *= (Polynomial const &p2)
   *this = multiply (*this,p2);
 }  
 
-void
-Polynomial::operator += (Polynomial const &p)
-{
-  *this = add ( *this, p);
-}
 
-void
-Polynomial::operator -= (Polynomial const &p)
-{
-  *this = subtract (*this, p);
-}
index 35ed40e707646291629832fa59d49b8d2df76f07..201e3e552fcfa084d7fb55882fc1d6be07c34dcb 100644 (file)
@@ -6,12 +6,15 @@ SUBDIRS = include
 
 MODULE_LIBS=$(depth)/flower 
 MODULE_INCLUDES= $(depth)/flower/include
-MODULE_CXXFLAGS=
+MODULE_CXXFLAGS= 
+
+
 HELP2MAN_EXECS = lilypond
 STEPMAKE_TEMPLATES= c++ executable po help2man
 
 include $(depth)/make/stepmake.make 
 
+USER_LDFLAGS += -static -lltdl -ldl
 ETAGS_FLAGS += -r '/^LY_DEFINE *(\([^,]+\),/\1/'  -r '/^LY_DEFINE *([^,]+, *"\([^"]+\)"/\1/' 
 
 default: 
index e165817f24d27ce025faef5fb1f77aa429bd6cdc..ffe48f240d241ab2fa483774141121d2ef950476 100644 (file)
@@ -112,10 +112,9 @@ Bezier::polynomial (Axis a)const
   Polynomial p (0.0);
   for (int j=0; j <= 3; j++)
     {
-      p += control_[j][a]
+      p += (control_[j][a] *   binomial_coefficient (3, j))
        * Polynomial::power (j , Polynomial (0,1))*
-       Polynomial::power (3 - j, Polynomial (1,-1))*
-       binomial_coefficient (3, j);
+       Polynomial::power (3 - j, Polynomial (1,-1));
     }
 
   return p;
index f72a67f5ecda835133624358981f87729d2e4987..dc737884afb31e2c4773bd60896479ba4d7130b6 100644 (file)
@@ -104,6 +104,8 @@ SCM ly_write2scm (SCM s);
 SCM ly_deep_copy (SCM);
 SCM ly_truncate_list (int k, SCM l );
 
+#define CACHE_SYMBOLS
+
 
 #if (__GNUC__ > 2)
 /*
index b587a594fd62fee7c46aac6876b6d2394c49454b..fff428f8acd67083610c96f676cc2003995c81d4 100644 (file)
@@ -8,6 +8,7 @@
 #include "string.hh"
 #include "interval.hh"
 #include "protected-scm.hh"
+#include "parray.hh"
 
 /**
   class for reading and mapping a file. 
@@ -50,13 +51,16 @@ public:
   int get_char (char const* pos_str0) const;
 
   /*
-    DOCUMENT-ME
+    JUNKME.
+
+    This thing doubles as a file-storage/file-iterator object.
    */
   char const* pos_str0_;
 
   SCM get_port()const { return str_port_; }
 private:
   String name_string_;
+  Link_array<char> newline_locations_; 
   std::istream* istream_;
   char  * contents_str0_;
   int length_;
index 278478140091c88f62bf2612478b99a267541955..cf82fa61300234c6bbc7f40c94d829a411a868e1 100644 (file)
@@ -319,11 +319,6 @@ ly_scm2offset (SCM s)
                 gh_scm2double (ly_cdr (s)));
 }
 
-
-/*
-  convert without too many decimals, and leave  a space at the end.
- */
-   
    
 LY_DEFINE(ly_number2string,  "ly-number->string", 1, 0,0,
          (SCM s),
@@ -359,7 +354,7 @@ leaves a space at the end.
   Undef this to see if GUILE GC is causing too many swaps.
  */
 
-// #define TEST_GC
+//#define TEST_GC
 
 #ifdef TEST_GC
 #include <libguile/gc.h>
index cbe4967b52537339c7b665ae2c92811ebf6c58be..02add4f0cf8853436fa1f3ad826e41aa76cc5887 100644 (file)
@@ -99,6 +99,10 @@ Source_file::Source_file (String filename_string)
   pos_str0_ = to_str0 ();
 
   init_port();
+
+  for (int i = 0; i < length_; i++)
+    if (contents_str0_[i] == '\n')
+      newline_locations_.push (contents_str0_ + i);
 }
 
 void
@@ -121,11 +125,6 @@ Source_file::tell () const
 std::istream*
 Source_file::get_istream ()
 {
-  /*
-    if (!name_string_.length ())
-      return &cin;
-    */
-
   if (!istream_)
     {
       if (length ()) // can-t this be done without such a hack?
@@ -261,15 +260,15 @@ Source_file::get_line (char const* pos_str0) const
   if (!in_b (pos_str0))
     return 0;
 
-  int i = 1;
-  char const* scan_str0 = to_str0 ();
-  if (!scan_str0)
-    return 0;
-
-  while (scan_str0 < pos_str0)
-    if (*scan_str0++ == '\n')
-      i++;
-  return i;
+  int lo=0;
+  int hi = newline_locations_.size();
+  
+  binary_search_bounds (newline_locations_,
+                       pos_str0, 
+                       Link_array<char>::default_compare,
+                       &lo, &hi);
+  
+  return lo;
 }
 
 int