[mathic] 45/62: Added a few extra formatting functions to ColumnPrinter.

Doug Torrance dtorrance-guest at moszumanska.debian.org
Wed Apr 1 11:36:23 UTC 2015


This is an automated email from the git hooks/post-receive script.

dtorrance-guest pushed a commit to branch master
in repository mathic.

commit 1e48445b64ce8de9329bdcf1d09802ef6bdb3a19
Author: Bjarke Hammersholt Roune <bjarkehr.code at gmail.com>
Date:   Fri Dec 21 21:41:02 2012 +0100

    Added a few extra formatting functions to ColumnPrinter.
---
 src/mathic/ColumnPrinter.cpp | 74 ++++++++++++++++++++++++++++++++++----------
 src/mathic/ColumnPrinter.h   | 15 +++++++--
 2 files changed, 70 insertions(+), 19 deletions(-)

diff --git a/src/mathic/ColumnPrinter.cpp b/src/mathic/ColumnPrinter.cpp
index 92a760b..a0d4708 100755
--- a/src/mathic/ColumnPrinter.cpp
+++ b/src/mathic/ColumnPrinter.cpp
@@ -120,7 +120,7 @@ namespace mathic {
 	}
   }
 
-  std::string ColumnPrinter::commafy(unsigned long long l) {
+  std::string ColumnPrinter::commafy(const unsigned long long l) {
     std::stringstream out;
     out << l;
     std::string str;
@@ -133,38 +133,75 @@ namespace mathic {
   }
 
   std::string ColumnPrinter::percent(
-      unsigned long long numerator,
-      unsigned long long denominator) {
-    return percent(static_cast<double>(numerator) / denominator);
-  }
-
-  std::string ColumnPrinter::percent(double ratio) {
+    const unsigned long long numerator,
+    const unsigned long long denominator
+  ) {
+    return ratio(numerator * 100, denominator) + '%';
+  }
+
+  std::string ColumnPrinter::percentFixed(
+    const unsigned long long numerator,
+    const unsigned long long denominator
+  ) {
+    auto str = percent(numerator, denominator);
+    const size_t maxSize = 6;
+    MATHIC_ASSERT(maxSize == std::string("100.0%").size());
+    const auto size = str.size();
+    if (size > maxSize)
+      return std::move(str);
+    return std::string(maxSize - str.size(), ' ') + std::move(str);
+  }
+
+  std::string ColumnPrinter::percent(const double ratio) {
     return oneDecimal(ratio * 100) + '%';
   }
 
   std::string ColumnPrinter::ratio(
-    unsigned long long numerator,
-    unsigned long long denominator) {
+    const unsigned long long numerator,
+    const unsigned long long denominator
+  ) {
+    if (denominator == 0)
+      return std::string(numerator == 0 ? "0/0" : "?/0");
     return oneDecimal(static_cast<double>(numerator) / denominator);
   }
 
-  std::string ColumnPrinter::oneDecimal(double d) {
+  std::string ColumnPrinter::oneDecimal(const double d) {
     std::ostringstream out;
     unsigned long long l = static_cast<unsigned long long>(d * 10 + 0.5);
     out << l / 10 << '.' << l % 10;
 	return out.str();
   }
 
-  std::string ColumnPrinter::bytesInUnit(unsigned long long bytes) {
+  std::string ColumnPrinter::withSIPrefix(unsigned long long l) {
+	std::ostringstream out;
+	if (l < 1000)
+      out << l;
+    else {
+	  const char* const units[] = {"k", "M", "G", "T"};
+	  const size_t unitCount = sizeof(units) / sizeof(*units);
+	  double amount = static_cast<double>(l) / 1000.0;
+	  size_t i = 0;
+      // the stop condition is at 999.5 because that value will get
+      // rounded to 1000.0.
+	  for (i = 0; i < unitCount && amount >= 999.95; ++i)
+        amount /= 1000.0;
+	  out << oneDecimal(amount) << units[i];
+    }
+    return out.str();
+  }
+
+  std::string ColumnPrinter::bytesInUnit(const unsigned long long bytes) {
 	std::ostringstream out;
-	if (bytes < 1024) {
-	  out << bytes << 'b';
-    } else {
-	  const char* units[] = {"kb", "mb", "gb", "tb"};
+	if (bytes < 1024)
+	  out << bytes << 'B';
+    else {
+	  const char* const units[] = {"kB", "MB", "GB", "TB"};
 	  const size_t unitCount = sizeof(units) / sizeof(*units);
 	  double amount = static_cast<double>(bytes) / 1024.0;
 	  size_t i = 0;
-	  for (i = 0; i < unitCount && amount >= 1024; ++i)
+      // the stop condition is 1023.95 because that value will get
+      // rounded to 1024.0.
+	  for (i = 0; i < unitCount && amount >= 1023.95; ++i)
         amount /= 1024.0;
 	  out << oneDecimal(amount) << units[i];
     }
@@ -172,7 +209,10 @@ namespace mathic {
   }
 
 
-  std::ostream& operator<<(std::ostream& out, const ColumnPrinter& printer) {
+  std::ostream& operator<<(
+    std::ostream& out,
+    const ColumnPrinter& printer
+  ) {
 	printer.print(out);
 	return out;
   }
diff --git a/src/mathic/ColumnPrinter.h b/src/mathic/ColumnPrinter.h
index 96ede3a..9282ece 100755
--- a/src/mathic/ColumnPrinter.h
+++ b/src/mathic/ColumnPrinter.h
@@ -24,14 +24,25 @@ namespace mathic {
 
 	void print(std::ostream& out) const;
 
-    /** returns 123456789 as "123,456,789". */
+    /// Returns "123,456,789" for parameter value 123456789.
     static std::string commafy(unsigned long long l);
 
-    /** returns (3,4) as "75.0%". */
+    /// Returns "123.4G" for parameter value 123456789. So the SI prefix is a
+    /// suffix of the returned string, yet it is still called an SI *prefix*
+    /// because these are usually used as prefixes to units such as in Km.
+    static std::string withSIPrefix(unsigned long long l);
+
+    /** returns (3,100) as "3.0%". */
     static std::string percent(
       unsigned long long numerator,
       unsigned long long denominator);
 
+    /** returns (3,100) as "  3.0%". The string always has the same length for
+      ratios equal to or less than 999.9%. */
+    static std::string percentFixed(
+      unsigned long long numerator,
+      unsigned long long denominator);
+
     /** returns 0.7565 as "75.7%". */
     static std::string percent(double ratio);
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mathic.git



More information about the debian-science-commits mailing list