lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master 177f78f 3/3: Rewrite the paginator logic so t


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 177f78f 3/3: Rewrite the paginator logic so that it has a single exit point
Date: Sun, 9 Sep 2018 14:55:09 -0400 (EDT)

branch: master
commit 177f78f217b56b232fb029c6be330841c1de6101
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>

    Rewrite the paginator logic so that it has a single exit point
    
    Generalized the logic in two places so that it's correct even for zero
    rows of data. Moved special handling for the zero-rows case to the exit
    point: printing one empty page in this case is properly an optional
    behavior rather than an inherent part of the design, and someday it
    might be desirable instead to handle that special case outside this
    function.
---
 report_table.cpp | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/report_table.cpp b/report_table.cpp
index b201d0b..0317507 100644
--- a/report_table.cpp
+++ b/report_table.cpp
@@ -202,23 +202,18 @@ paginator::paginator(int total_rows, int rows_per_group, 
int max_lines_per_page)
     LMI_ASSERT(0 <  rows_per_group                      );
     LMI_ASSERT(     rows_per_group <= max_lines_per_page);
 
-    // If there are zero rows of data, then one empty page is wanted.
-    if(0 == total_rows_)
-        {
-        lines_on_last_page_ = 0;
-        page_count_ = 1;
-        return;
-        }
-
     page_count_ = outward_quotient(total_rows_, rows_per_page_);
 
-    int const rows_on_last_page = total_rows_ - (page_count_ - 1) * 
rows_per_page_;
+    int const pages_before_last = (0 == page_count_) ? 0 : page_count_ - 1;
+    int const rows_on_last_page = total_rows_ - rows_per_page_ * 
pages_before_last;
     int const full_groups_on_last_page = rows_on_last_page / rows_per_group_;
     int const extra_rows_on_last_page  = rows_on_last_page % rows_per_group_;
     lines_on_last_page_ =
           lines_per_group_ * full_groups_on_last_page
         + extra_rows_on_last_page
-        - (0 == extra_rows_on_last_page)
+        - (   0 != full_groups_on_last_page // (there is a separator
+          &&  0 == extra_rows_on_last_page  // and it is not wanted)
+          )
         ;
 
     // Avoid widowing a partial group on the last page, by moving it
@@ -234,4 +229,10 @@ paginator::paginator(int total_rows, int rows_per_group, 
int max_lines_per_page)
             lines_on_last_page_ = lines_on_full_page_ + 1 + rows_on_last_page;
             }
         }
+
+    // If there are zero rows of data, then one empty page is wanted.
+    if(0 == total_rows_)
+        {
+        page_count_ = 1;
+        }
 }



reply via email to

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