From: Han-Wen Nienhuys Date: Sun, 25 Aug 2002 14:14:49 +0000 (+0000) Subject: * flower/polynomial.cc (operator +): optimize += operator. No X-Git-Tag: release/1.6.1~18 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=c3b06d34b930cc16e404c56eec161cb62b818381;p=lilypond.git * 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. --- diff --git a/ChangeLog b/ChangeLog index 803c6f861f..29b58cc884 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2002-08-25 Han-Wen Nienhuys + + * 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 * input/regression/tuplet-properties.ly (texidoc): bugfix diff --git a/flower/include/array.icc b/flower/include/array.icc index 839da9cce1..7b94eab5fc 100644 --- a/flower/include/array.icc +++ b/flower/include/array.icc @@ -108,6 +108,32 @@ Array::slice (int lower, int upper) const } +template +void +binary_search_bounds (Array 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 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 const &table, else return -1; /* not found */ } - diff --git a/flower/include/parray.hh b/flower/include/parray.hh index 08fe766f5d..21a40429c6 100644 --- a/flower/include/parray.hh +++ b/flower/include/parray.hh @@ -12,6 +12,8 @@ #include "array.hh" + + /** an array of pointers. @@ -21,14 +23,7 @@ template class Link_array : private 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 (Array v) :Array (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 **)tp, n) { @@ -179,7 +183,6 @@ public: else return 0; } - }; template @@ -295,5 +298,30 @@ binsearch_links (Link_array const &arr, T *t, } +template +void +binary_search_bounds (Link_array 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 diff --git a/flower/include/polynomial.hh b/flower/include/polynomial.hh index b978910caf..aade80fbbc 100644 --- a/flower/include/polynomial.hh +++ b/flower/include/polynomial.hh @@ -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 diff --git a/flower/polynomial.cc b/flower/polynomial.cc index f5d4b38fe3..c8bf66811f 100644 --- a/flower/polynomial.cc +++ b/flower/polynomial.cc @@ -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); -} diff --git a/lily/GNUmakefile b/lily/GNUmakefile index 35ed40e707..201e3e552f 100644 --- a/lily/GNUmakefile +++ b/lily/GNUmakefile @@ -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: diff --git a/lily/bezier.cc b/lily/bezier.cc index e165817f24..ffe48f240d 100644 --- a/lily/bezier.cc +++ b/lily/bezier.cc @@ -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; diff --git a/lily/include/lily-guile.hh b/lily/include/lily-guile.hh index f72a67f5ec..dc737884af 100644 --- a/lily/include/lily-guile.hh +++ b/lily/include/lily-guile.hh @@ -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) /* diff --git a/lily/include/source-file.hh b/lily/include/source-file.hh index b587a594fd..fff428f8ac 100644 --- a/lily/include/source-file.hh +++ b/lily/include/source-file.hh @@ -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 newline_locations_; std::istream* istream_; char * contents_str0_; int length_; diff --git a/lily/lily-guile.cc b/lily/lily-guile.cc index 2784781400..cf82fa6130 100644 --- a/lily/lily-guile.cc +++ b/lily/lily-guile.cc @@ -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 diff --git a/lily/source-file.cc b/lily/source-file.cc index cbe4967b52..02add4f0cf 100644 --- a/lily/source-file.cc +++ b/lily/source-file.cc @@ -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::default_compare, + &lo, &hi); + + return lo; } int