]> git.donarmstrong.com Git - lilypond.git/commitdiff
* lily/paper-score.cc (find_break_indices): move from Break_algorithm.
authorHan-Wen Nienhuys <hanwen@xs4all.nl>
Tue, 21 Feb 2006 23:26:58 +0000 (23:26 +0000)
committerHan-Wen Nienhuys <hanwen@xs4all.nl>
Tue, 21 Feb 2006 23:26:58 +0000 (23:26 +0000)
* lily/break-algorithm.cc (find_break_indices): remove linewidth <
0 case.
(find_breaks): idem.

ChangeLog
lily/break-algorithm.cc
lily/constrained-breaking.cc
lily/gourlay-breaking.cc
lily/include/break-algorithm.hh
lily/include/constrained-breaking.hh
lily/include/gourlay-breaking.hh
lily/include/paper-score.hh
lily/paper-score.cc

index 72739a5298276aa83f55a9cf9440a03940da613c..95945a22036f7f2e177aaed3d9c3fd4de8f1326a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2006-02-22  Han-Wen Nienhuys  <hanwen@xs4all.nl>
+
+       * lily/paper-score.cc (find_break_indices): move from Break_algorithm.
+
+       * lily/break-algorithm.cc (find_break_indices): remove linewidth <
+       0 case.
+       (find_breaks): idem.
+
 2006-02-21  Graham Percival  <gpermus@gmail.com>
 
        * Documentation/user/advanced-notation.itely: fixed make-moment]
index bf83b36def3e5044ff49d4ad33063bc08df87523..923627f44c5a00384fac90adf1581aeb484611e8 100644 (file)
 #include "cpu-timer.hh"
 #include "simple-spacer.hh"
 
-vector<int>
-Break_algorithm::find_break_indices () const
-{
-  vector<Grob*> all = pscore_->root_system ()->columns ();
-  vector<int> retval;
-
-  for (vsize i = 0; i < all.size (); i++)
-    if (Item::is_breakable (all[i]))
-      retval.push_back (i);
-
-  if (linewidth_ <= 0)
-    while (retval.size () > 2)
-      retval.erase (retval.begin () + 1);
-
-  return retval;
-}
-
 vector<Grob*>
 Break_algorithm::find_breaks () const
 {
@@ -42,9 +25,6 @@ Break_algorithm::find_breaks () const
     if (Item::is_breakable (all[i]))
       retval.push_back (all[i]);
 
-  if (linewidth_ <= 0)
-    while (retval.size () > 2)
-      retval.erase (retval.begin () + 1);
 
   return retval;
 }
@@ -89,10 +69,9 @@ Break_algorithm::set_pscore (Paper_score *s)
 }
 
 vector<Column_x_positions>
-Break_algorithm::solve () const
+Break_algorithm::solve () 
 {
-  vector<Column_x_positions> h= do_solve ();
-
+  vector<Column_x_positions> h;
   return h;
 }
 
index 8b5e11ca8ba694434637bff1055baf14ae9601c6..1f3171803bc025efd34f41a4eef067423f4e069b 100644 (file)
@@ -7,17 +7,46 @@
   (c) 2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
 */
 
+/*
+  TODO:
+
+  * vsize vs. int: casts should not be necessary. Use VPOS iso -1 as
+  magic signaling value?
+
+  * The specification uses A j, k, n and m as variables.
+  
+  Functions use start,end,sys_count,calc_subproblem as variables. Use the same naming
+  for the specification as for the code.
+  
+
+  FURTHER REMARKS:
+
+  *
+  
+   int a;
+   int b;
+
+  iso.
+
+   int a, b;
+
+
+   * no spurious * in <slash><star> <star><slash> comments.
+
+
+ */
+
+
 #include "constrained-breaking.hh"
 
-#include "warn.hh"
+#include "international.hh"
 #include "main.hh"
+#include "output-def.hh"
 #include "paper-column.hh"
 #include "paper-score.hh"
-#include "output-def.hh"
 #include "simple-spacer.hh"
 #include "system.hh"
-
-#include "international.hh"
+#include "warn.hh"
 
 void
 print_constrained_break_nodes (vector<Constrained_break_node> const &arr)
@@ -37,15 +66,17 @@ print_constrained_break_nodes (vector<Constrained_break_node> const &arr)
    the piece)
 
    Then A_{k+1, m} is contructed from
-                        min_ {k < j < m} ( W(A_{k,j} :: m) )
+   min_ {k < j < m} ( W(A_{k,j} :: m) )
    where by A::m we denote appending m to the list A
+
 */
 
 /* start and sys here are indexed from 0.
- * max_break is indexed from starting_breakpoints_[start]
- * (for max_break, starting_breakpoints_[start] is the beginning
- * of the piece; the smallest value we should ever see here is
- * starting_breakpoints_[start] + 1) */
+
+max_break is indexed from starting_breakpoints_[start] (for
+ max_break, starting_breakpoints_[start] is the beginning of the
+ piece; the smallest value we should ever see here is
+ starting_breakpoints_[start] + 1) */
 bool
 Constrained_breaking::calc_subproblem (int start, int sys, int max_break)
 {
@@ -58,7 +89,7 @@ Constrained_breaking::calc_subproblem (int start, int sys, int max_break)
   vector<Constrained_break_node> &st = state_[start];
   int rank = breaks_.size () - start_col;
   int max_index = max_break - start_col;
-  for (int j=sys; j < max_index; j++)
+  for (int j = sys; j < max_index; j++)
     {
       if (0 == sys && j > 0)
         break; /* the first line cannot have its first break after the beginning */
@@ -81,31 +112,36 @@ Constrained_breaking::calc_subproblem (int start, int sys, int max_break)
       if (isinf (dem))
         continue;
 
-      if (isinf (st[sys*rank + max_index].demerits_)
-          || dem < st[sys*rank + max_index].demerits_)
+      int k = sys*rank + max_index;
+      if (isinf (st[k].demerits_)
+          || dem < st[k].demerits_)
         {
           found_something = true;
-          st[sys*rank + max_index].demerits_ = dem;
-          st[sys*rank + max_index].force_ = force;
-          st[sys*rank + max_index].penalty_ = pen;
-          st[sys*rank + max_index].prev_ = j;
-          st[sys*rank + max_index].line_config_ = cur;
+
+         /*
+           TODO:  maybe just copy a Constrained_break_node ? 
+          */
+          st[k].demerits_ = dem;
+          st[k].force_ = force;
+          st[k].penalty_ = pen;
+          st[k].prev_ = j;
+          st[k].line_config_ = cur;
         }
     }
   return found_something;
 }
 
 vector<Column_x_positions>
-Constrained_breaking::do_solve ()
+Constrained_breaking::solve ()
 {
   if (!systems_)
     {
       programming_error (_f ("no system number set in constrained-breaking"));
-      systems_ = 4;
+      systems_ = start_.size () / 2;
     }
 
   resize ();
-  return get_solution(0, systems_, -1);
+  return get_solution (0, systems_, -1);
 }
 
 void
@@ -117,34 +153,33 @@ Constrained_breaking::resize ()
       bool ragged_last = to_boolean (pscore_->layout ()->c_variable ("ragged-last"));
 
       /* do all the rod/spring problems */
-      breaks_ = find_break_indices ();
+      breaks_ = pscore_->find_break_indices ();
       cols_rank_ = breaks_.size ();
       all_ = pscore_->root_system ()->columns ();
       cols_.resize (breaks_.size () * breaks_.size ());
       for (vsize i = 0; i < breaks_.size () - 1; i++)
-          for (vsize j = i + 1; j < breaks_.size (); j++)
-            {
-              vector<Grob*> line (all_.begin () + breaks_[i],
-                                  all_.begin() + breaks_[j] + 1);
+       for (vsize j = i + 1; j < breaks_.size (); j++)
+         {
+           vector<Grob*> line (all_.begin () + breaks_[i],
+                               all_.begin() + breaks_[j] + 1);
 
-              line[0] = dynamic_cast<Item *> (line[0])->find_prebroken_piece (RIGHT);
-              line.back () = dynamic_cast<Item *> (line.back ())->find_prebroken_piece (LEFT);
+           line[0] = dynamic_cast<Item *> (line[0])->find_prebroken_piece (RIGHT);
+           line.back () = dynamic_cast<Item *> (line.back ())->find_prebroken_piece (LEFT);
 
-              cols_[i*cols_rank_ + j].cols_ = line;
+           cols_[i*cols_rank_ + j].cols_ = line;
 
-              /* we have no idea what line this will be -- only whether it is the first */
-              Interval line_dims = line_dimensions_int (pscore_->layout (), i);
-              Simple_spacer_wrapper *sp = generate_spacing_problem (line, line_dims);
+           /* we have no idea what line this will be -- only whether it is the first */
+           Interval line_dims = line_dimensions_int (pscore_->layout (), i);
+           Simple_spacer_wrapper *sp = generate_spacing_problem (line, line_dims);
 
-              /* TODO: support raggedness */
-              bool last = j == breaks_.size () - 1;
-              bool ragged = ragged_right || (last && ragged_last);
-              sp->solve (&cols_[i*cols_rank_ + j], ragged);
+           bool last = j == breaks_.size () - 1;
+           bool ragged = ragged_right || (last && ragged_last);
+           sp->solve (&cols_[i*cols_rank_ + j], ragged);
 
-              if (!cols_[i*cols_rank_ + j].satisfies_constraints_)
-                break;
-              delete sp;
-            }
+           if (!cols_[i*cols_rank_ + j].satisfies_constraints_)
+             break;
+           delete sp;
+         }
 
       /* work out all the starting indices */
       for (vsize i = 0; i < start_.size (); i++)
@@ -167,13 +202,15 @@ Constrained_breaking::resize ()
       for (vsize k = starting_breakpoints_[i] + j + 1; k < breaks_.size (); k++)
         if (!calc_subproblem (i, j, k))
           break; /* if we couldn't break this, it is too cramped already */
+  
   valid_systems_ = systems_;
 }
 
 vector<Column_x_positions>
 Constrained_breaking::get_solution (int start, int end, int sys_count)
 {
-  int rank, brk;
+  int rank;
+  int brk;
   prepare_solution (start, end, sys_count, &rank, &brk);
 
   vector<Constrained_break_node> const &st = state_[start];
@@ -182,10 +219,11 @@ Constrained_breaking::get_solution (int start, int end, int sys_count)
   for (int sys = sys_count-1; sys >= 0; sys--)
     {
       assert (brk > 0);
-      ret.push_back( st[sys*rank + brk].line_config_ );
+      ret.push_back (st[sys*rank + brk].line_config_);
       brk = st[sys*rank + brk].prev_;
     }
   assert (brk == 0);
+
   reverse (ret);
   return ret;
 }
@@ -193,7 +231,8 @@ Constrained_breaking::get_solution (int start, int end, int sys_count)
 Real
 Constrained_breaking::get_demerits (int start, int end, int sys_count)
 {
-  int rank, brk;
+  int rank;
+  int brk;
   prepare_solution (start, end, sys_count, &rank, &brk);
 
   return state_[start][(sys_count-1)*rank + brk].demerits_;
@@ -202,7 +241,8 @@ Constrained_breaking::get_demerits (int start, int end, int sys_count)
 Real
 Constrained_breaking::get_force (int start, int end, int sys_count)
 {
-  int rank, brk;
+  int rank;
+  int brk;
   prepare_solution (start, end, sys_count, &rank, &brk);
   vector<Constrained_break_node> const &st = state_[start];
   Real f = 0;
@@ -221,7 +261,8 @@ Constrained_breaking::get_force (int start, int end, int sys_count)
 Real
 Constrained_breaking::get_penalty (int start, int end, int sys_count)
 {
-  int rank, brk;
+  int rank;
+  int brk;
   prepare_solution (start, end, sys_count, &rank, &brk);
 
   return state_[start][(sys_count-1)*rank + brk].penalty_;
@@ -230,12 +271,13 @@ Constrained_breaking::get_penalty (int start, int end, int sys_count)
 Real
 Constrained_breaking::get_page_penalty (int start, int end, int sys_count, int sys_num)
 {
-  int rank, brk;
+  int rank;
+  int brk;
   prepare_solution (start, end, sys_count, &rank, &brk);
 
   int sys;
   for (sys = sys_count-1; sys > sys_num; sys--)
-      brk = state_[start][sys*rank + brk].prev_;
+    brk = state_[start][sys*rank + brk].prev_;
 
   if (brk < 0) /* we didn't satisfy constraints */
     return 0;
@@ -256,7 +298,8 @@ Constrained_breaking::get_page_penalty (int start, int end, int sys_count, int s
 int
 Constrained_breaking::get_min_systems (int start, int end)
 {
-  int rank, brk;
+  int rank;
+  int brk;
   prepare_solution (start, end, 1, &rank, &brk);
   int sys_count;
   vector<Constrained_break_node> const &st = state_[start];
@@ -284,10 +327,10 @@ Constrained_breaking::get_max_systems (int start, int end)
 }
 
 void
-Constrained_breaking::prepare_solution (int start, int end, int sys_count, int *rank, int *brk)
+Constrained_breaking::prepare_solution (vsize start, int end, int sys_count, int *rank, int *brk)
 {
-  assert (start < (int)start_.size () && end <= (int)start_.size ());
-  assert (end < 0 || start < end);
+  assert (start < start_.size () && end <= int (start_.size ()));
+  assert (end < 0 || int (start) < end);
   assert (sys_count > 0);
 
   if (sys_count >= valid_systems_)
@@ -309,8 +352,8 @@ Constrained_breaking::Constrained_breaking ()
   start_.push_back (0);
 }
 
-Constrained_breaking::Constrained_breaking (vector<int> const &start):
-  start_ (start)
+Constrained_breaking::Constrained_breaking (vector<int> const &start)
+  start_ (start)
 {
   valid_systems_ = systems_ = 0;
 }
index a02b4a553371ff798556a11992a5ddcaf1761880..62f537507395e7b83451eadf2179c1f036d1e77f 100644 (file)
@@ -75,13 +75,13 @@ print_break_nodes (vector<Break_node> const &arr)
    inspiration.
 */
 vector<Column_x_positions>
-Gourlay_breaking::do_solve () const
+Gourlay_breaking::solve () 
 {
   vector<Break_node> optimal_paths;
   vector<Grob*> all
     = pscore_->root_system ()->columns ();
 
-  vector<int> breaks = find_break_indices ();
+  vector<int> breaks = pscore_->find_break_indices ();
 
   Break_node first_node;
   optimal_paths.push_back (first_node);
index c4afa2d373f9254edf46768728de6945e03eb916..9f5e281121a0e958d28c24e43f81c0f4eb532ff7 100644 (file)
@@ -24,20 +24,17 @@ protected:
   Real linewidth_;
 
   vector<Grob*> find_breaks () const;
-  vector<int> find_break_indices () const;
   void solve_line (Column_x_positions *) const;
   bool feasible (vector<Grob*> const &) const;
 
   Simple_spacer_wrapper *generate_spacing_problem (vector<Grob*> const &,
                                                   Interval) const;
-  virtual vector<Column_x_positions> do_solve () const = 0;
-
 public:
   virtual ~Break_algorithm ();
   Simple_spacer *(*get_line_spacer) ();
   Break_algorithm ();
   void set_pscore (Paper_score *);
-  vector<Column_x_positions> solve () const;
+  virtual vector<Column_x_positions> solve ();
 };
 
 #endif // BREAK_HH
index 11965e8b649a54ec1023df08ee2cd9ac608c8395..57e71984a4fc9e05be09d38fb03773d33420d475 100644 (file)
@@ -18,7 +18,7 @@
 struct Constrained_break_node
 {
   /** the number of bars in all the systems before this one
-  */
+   */
   int prev_;
 
   /** unlike the Gourlay breaker, this is the sum of all demerits up to,
@@ -49,46 +49,46 @@ struct Constrained_break_node
 */
 class Constrained_breaking : public Break_algorithm
 {
-  public:
-    std::vector<Column_x_positions> do_solve ();
-    Constrained_breaking ();
-    Constrained_breaking (std::vector<int> const &start_col_posns);
+public:
+  std::vector<Column_x_positions> solve ();
+  Constrained_breaking ();
+  Constrained_breaking (std::vector<int> const &start_col_posns);
 
-    std::vector<Column_x_positions> get_solution(int start, int end, int sys_count);
-    Real get_demerits (int start, int end, int sys_count);
-    Real get_force (int start, int end, int sys_count);
-    Real get_penalty (int start, int end, int sys_count);
-    int get_max_systems (int start, int end);
-    int get_min_systems (int start, int end);
+  std::vector<Column_x_positions> get_solution(int start, int end, int sys_count);
+  Real get_demerits (int start, int end, int sys_count);
+  Real get_force (int start, int end, int sys_count);
+  Real get_penalty (int start, int end, int sys_count);
+  int get_max_systems (int start, int end);
+  int get_min_systems (int start, int end);
 
-    /* get the page penalty of system number sys with the given breaking */
-    Real get_page_penalty (int start, int end, int sys_count, int sys);
+  /* get the page penalty of system number sys with the given breaking */
+  Real get_page_penalty (int start, int end, int sys_count, int sys);
 
-  private:
-    int valid_systems_;
-    int systems_;
+  int systems_;
+private:
+  int valid_systems_;
 
-    /* the (i,j)th entry is the column configuration for breaking between
-     * columns i and j */
-    std::vector<Column_x_positions> cols_;
-    int cols_rank_;
+  /* the (i,j)th entry is the column configuration for breaking
+  between columns i and j */
+  std::vector<Column_x_positions> cols_;
+  int cols_rank_;
 
-    /* the [i](j,k)th entry is the score for fitting the first k bars onto the
-     * first j systems, starting at the i'th allowed starting column */
-    std::vector<std::vector<Constrained_break_node> > state_;
+  /* the [i](j,k)th entry is the score for fitting the first k bars onto the
+   first j systems, starting at the i'th allowed starting column */
+  std::vector<std::vector<Constrained_break_node> > state_;
 
-    vector<int> start_;         /* the columns at which we might be asked to start breaking */
-    vector<int> starting_breakpoints_; /* the corresponding index in breaks_ */
+  vector<int> start_;         /* the columns at which we might be asked to start breaking */
+  vector<int> starting_breakpoints_; /* the corresponding index in breaks_ */
 
-    vector<Grob*> all_;
-    std::vector<int> breaks_;
+  vector<Grob*> all_;
+  std::vector<int> breaks_;
 
-    void prepare_solution (int start, int end, int sys_count, int *rank, int *brk);
+  void prepare_solution (vsize start, int end, int sys_count, int *rank, int *brk);
 
-    void combine_demerits (Column_x_positions const &, Column_x_positions const &,
-                           Real *force, Real *pen, Real *dem) const;
+  void combine_demerits (Column_x_positions const &, Column_x_positions const &,
+                        Real *force, Real *pen, Real *dem) const;
 
-    bool calc_subproblem(int start, int systems, int max_break_index);
-    void resize ();
+  bool calc_subproblem(int start, int systems, int max_break_index);
+  void resize ();
 };
 #endif /* CONSTRAINED_BREAKING_HH */
index 23bb173d2831ab5ee595a6629dcdd1c3c240a0e6..34a92f8dea2ac632e19f0e20c1962bf4449cddfd 100644 (file)
@@ -16,7 +16,7 @@
 */
 struct Gourlay_breaking : public Break_algorithm
 {
-  vector<Column_x_positions> do_solve () const;
+  vector<Column_x_positions> solve ();
   Gourlay_breaking ();
   Real combine_demerits (Column_x_positions const &, Column_x_positions const &) const;
 };
index 1082b3330a6e3d8bfeaafae0673ac7725af6dde4..f3a594cade0b9ea1a14b27819156e341411cad58 100644 (file)
@@ -29,7 +29,7 @@ public:
 
   void typeset_system (System *);
   vector<Column_x_positions> calc_breaking ();
-
+  vector<int> find_break_indices () const;
   SCM get_paper_systems () const;
 protected:
   virtual void process ();
index 330bf3ae4965dde62c1b75e4023ab747bd7ae420..2798b759385f145814b7ff682d4ddd48001a8de9 100644 (file)
@@ -22,6 +22,7 @@
 #include "stencil.hh"
 #include "system.hh"
 #include "warn.hh"
+#include "constrained-breaking.hh"
 
 Paper_score::Paper_score (Output_def *layout)
 {
@@ -58,6 +59,21 @@ Paper_score::typeset_system (System *system)
   system->unprotect ();
 }
 
+
+vector<int>
+Paper_score::find_break_indices () const
+{
+  vector<Grob*> all = root_system ()->columns ();
+  vector<int> retval;
+
+  for (vsize i = 0; i < all.size (); i++)
+    if (Item::is_breakable (all[i]))
+      retval.push_back (i);
+
+  return retval;
+}
+
+
 vector<Column_x_positions>
 Paper_score::calc_breaking ()
 {
@@ -67,8 +83,9 @@ Paper_score::calc_breaking ()
   int system_count = robust_scm2int (layout ()->c_variable ("system-count"), 0);
   if (system_count)
     {
-      Constrained_breaking *b = new Constrained_breaking;
+      Constrained_breaking *b = new Constrained_breaking (/* FIXME */);
       algorithm = b;
+      b->systems_ = system_count;
     }
   else
     algorithm = new Gourlay_breaking;