lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master 6843f90 2/2: Facilitate dimensional analysis


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 6843f90 2/2: Facilitate dimensional analysis
Date: Mon, 3 Sep 2018 08:53:20 -0400 (EDT)

branch: master
commit 6843f90d22fbb07e14c4e1a4cb24c38a5f843b64
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>

    Facilitate dimensional analysis
    
    In vim,
      /.*lines\&.*rows/
    highlights potential dimensional-analysis errors here.
    
    This is okay because the conversion is deliberate:
      int const lines_per_group = rows_per_group + 1;
    and has been explained by adding a comment.
    
    This is not obviously okay:
      if(rows_on_last_page <= free_lines)
    but has been explained by adding an assertion: rows and lines are
    comparable for a terminal group when that group is partial.
---
 miscellany.cpp | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)

diff --git a/miscellany.cpp b/miscellany.cpp
index 4a530b1..83e9450 100644
--- a/miscellany.cpp
+++ b/miscellany.cpp
@@ -340,25 +340,23 @@ int page_count
     if(0 == total_rows)
         return 1;
 
-    // Each group actually takes rows_per_group+1 lines because of the
-    // separator row between groups, hence the second +1, but there is no
-    // need for the separator after the last group, hence the first +1.
-    int const groups_per_page = (lines_per_page + 1) / (rows_per_group + 1);
+    // "+ 1": blank-line separator after each group.
+    int const lines_per_group = rows_per_group + 1;
 
-    // But we are actually interested in the number of rows we can fit per page
-    // and not the number of groups.
-    int const rows_per_page = groups_per_page * rows_per_group;
+    // "+ 1": no blank-line separator after the last group.
+    int const groups_per_page = (lines_per_page + 1) / lines_per_group;
+
+    int const rows_per_page = rows_per_group * groups_per_page;
 
-    // Finally determine how many pages are needed to show all the rows.
     int number_of_pages = outward_quotient(total_rows, rows_per_page);
 
-    // The last page may not be needed if all the rows on it can fit into the
-    // remaining space, too small for a full group, but perhaps sufficient for
-    // these rows, in the last page but one.
+    // Avoid widowing a partial group on the last page, by moving it
+    // to the preceding page if there's room.
     if(1 < number_of_pages)
         {
         auto const rows_on_last_page = total_rows - (number_of_pages - 1) * 
rows_per_page;
-        auto const free_lines = lines_per_page - groups_per_page * 
(rows_per_group + 1);
+        auto const free_lines = lines_per_page - lines_per_group * 
groups_per_page;
+        LMI_ASSERT(free_lines < rows_per_group);
         if(rows_on_last_page <= free_lines)
             {
             --number_of_pages;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]