[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [4953] Refactor
From: |
Greg Chicares |
Subject: |
[lmi-commits] [4953] Refactor |
Date: |
Sun, 16 May 2010 21:25:44 +0000 |
Revision: 4953
http://svn.sv.gnu.org/viewvc/?view=rev&root=lmi&revision=4953
Author: chicares
Date: 2010-05-16 21:25:43 +0000 (Sun, 16 May 2010)
Log Message:
-----------
Refactor
Modified Paths:
--------------
lmi/trunk/dbvalue.cpp
lmi/trunk/dbvalue.hpp
Modified: lmi/trunk/dbvalue.cpp
===================================================================
--- lmi/trunk/dbvalue.cpp 2010-05-15 20:41:53 UTC (rev 4952)
+++ lmi/trunk/dbvalue.cpp 2010-05-16 21:25:43 UTC (rev 4953)
@@ -32,6 +32,7 @@
#include "assert_lmi.hpp"
#include "contains.hpp"
#include "dbnames.hpp"
+#include "handle_exceptions.hpp"
#include "math_functors.hpp" // greater_of(), lesser_of()
#include "print_matrix.hpp"
#include "value_cast.hpp"
@@ -221,7 +222,7 @@
data_values_ = new_object.data_values_;
}
-/// Indexing operator for product editor only.
+/// Indexing operator for reshape() and product editor only.
///
/// Two indexing operators are provided. This one's argument includes
/// the number of durations--which, as far as the product editor is
@@ -395,46 +396,60 @@
}
}
-/// Calculate number of data required by lengths of axes.
+/// Calculate number of data required by lengths of object's axes.
int database_entity::getndata() const
{
- // Use a double for this purpose so that we can detect whether
- // the required number exceeds the maximum addressable number,
- // because a double has a wider range than an integer type.
- double n = std::accumulate
- (axis_lengths_.begin()
- ,axis_lengths_.end()
- ,1.0
- ,std::multiplies<double>()
- );
-
- // Meaningful iff a long int is bigger than an int.
- if(MaxPossibleElements < n)
+ try
{
+ return getndata(axis_lengths_);
+ }
+ catch(...)
+ {
+ report_exception();
fatal_error()
<< "Database item '"
<< GetDBNames()[key_].ShortName
<< "' with key "
<< key_
- << " contains more than the maximum possible number of elements."
+ << " has invalid dimensions."
<< LMI_FLUSH
;
}
+ throw "Unreachable--silences a compiler diagnostic.";
+}
- if(0 == n)
+/// Calculate number of data required by lengths of given axes.
+///
+/// Use a double-precision accumulator internally to avoid overflow.
+///
+/// Throw if the result equals zero or exceeds MaxPossibleElements.
+///
+/// Otherwise, return the result, cast to int. The enforced range
+/// contraints guarantee that the cast preserves value.
+
+int database_entity::getndata(std::vector<int> const& z)
+{
+ double n = std::accumulate(z.begin(), z.end(), 1.0,
std::multiplies<double>());
+
+ if(MaxPossibleElements < n)
{
fatal_error()
- << "Database item '"
- << GetDBNames()[key_].ShortName
- << "' with key "
- << key_
- << " has no data."
+ << "There are " << n
+ << " data, but at most " << MaxPossibleElements
+ << " are permitted."
<< LMI_FLUSH
;
}
- // Because MaxPossibleElements < n, this cast cannot lose information.
+ if(n <= 0)
+ {
+ fatal_error() << "Number of data must exceed zero." << LMI_FLUSH;
+ }
+
+ LMI_ASSERT(MaxPossibleElements <= std::numeric_limits<int>::max());
+ // Redundant but cheap guarantee that the cast preserves value:
+ LMI_ASSERT(1.0 <= n && n <= MaxPossibleElements);
return static_cast<int>(n);
}
Modified: lmi/trunk/dbvalue.hpp
===================================================================
--- lmi/trunk/dbvalue.hpp 2010-05-15 20:41:53 UTC (rev 4952)
+++ lmi/trunk/dbvalue.hpp 2010-05-16 21:25:43 UTC (rev 4953)
@@ -123,6 +123,7 @@
private:
void assert_invariants() const;
int getndata() const;
+ static int getndata(std::vector<int> const&);
void read (xml::element const&);
void write(xml::element&) const;
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [lmi-commits] [4953] Refactor,
Greg Chicares <=