[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [4938] Rearrange functions in logical order
From: |
Greg Chicares |
Subject: |
[lmi-commits] [4938] Rearrange functions in logical order |
Date: |
Mon, 10 May 2010 00:48:39 +0000 |
Revision: 4938
http://svn.sv.gnu.org/viewvc/?view=rev&root=lmi&revision=4938
Author: chicares
Date: 2010-05-10 00:48:39 +0000 (Mon, 10 May 2010)
Log Message:
-----------
Rearrange functions in logical order
Modified Paths:
--------------
lmi/trunk/database.cpp
lmi/trunk/database.hpp
lmi/trunk/dbvalue.cpp
lmi/trunk/dbvalue.hpp
Modified: lmi/trunk/database.cpp
===================================================================
--- lmi/trunk/database.cpp 2010-05-09 14:05:19 UTC (rev 4937)
+++ lmi/trunk/database.cpp 2010-05-10 00:48:39 UTC (rev 4938)
@@ -173,13 +173,6 @@
return length_;
}
-void product_database::initialize()
-{
- index_ = database_index(Gender, Class, Smoker, IssueAge, UWBasis, State);
- length_ = static_cast<int>(Query(DB_EndtAge)) - IssueAge;
- LMI_ASSERT(0 < length_ && length_ <= methuselah);
-}
-
double product_database::Query(e_database_key k) const
{
database_entity const& v = GetEntry(k);
@@ -203,6 +196,13 @@
}
}
+void product_database::initialize()
+{
+ index_ = database_index(Gender, Class, Smoker, IssueAge, UWBasis, State);
+ length_ = static_cast<int>(Query(DB_EndtAge)) - IssueAge;
+ LMI_ASSERT(0 < length_ && length_ <= methuselah);
+}
+
database_entity const& product_database::GetEntry(e_database_key k) const
{
return map_lookup(DBDictionary::instance().GetDictionary(), k);
Modified: lmi/trunk/database.hpp
===================================================================
--- lmi/trunk/database.hpp 2010-05-09 14:05:19 UTC (rev 4937)
+++ lmi/trunk/database.hpp 2010-05-10 00:48:39 UTC (rev 4938)
@@ -69,16 +69,17 @@
// Return scalar: use double because it's convertible to int, bool, etc.
// Someday, consider doing something like:
// template<typename T, typename DBValue>
- // void Query(T& dst, e_database_key k) const;
- double Query(e_database_key k) const;
+ // void Query(T&, e_database_key) const;
+ double Query(e_database_key) const;
+ void Query(std::vector<double>&, e_database_key) const;
- void Query(std::vector<double>& dst, e_database_key k) const;
-
- database_entity const& GetEntry(e_database_key k) const;
-
private:
void initialize();
+ public: // for the nonce...
+ database_entity const& GetEntry(e_database_key) const;
+ private: // ...for the nonce
+
database_index index_;
int length_;
Modified: lmi/trunk/dbvalue.cpp
===================================================================
--- lmi/trunk/dbvalue.cpp 2010-05-09 14:05:19 UTC (rev 4937)
+++ lmi/trunk/dbvalue.cpp 2010-05-10 00:48:39 UTC (rev 4938)
@@ -46,21 +46,6 @@
static int const ScalarDims[database_entity::e_number_of_axes] = {1, 1, 1, 1,
1, 1, 1};
static int const MaxPossibleElements = std::numeric_limits<int>::max();
-std::vector<int> const& database_entity::maximum_dimensions()
-{
- static int const d[e_number_of_axes] =
- {e_max_dim_gender
- ,e_max_dim_class
- ,e_max_dim_smoking
- ,e_max_dim_issue_age
- ,e_max_dim_uw_basis
- ,e_max_dim_state
- ,e_max_dim_duration
- };
- static std::vector<int> const z(d, d + e_number_of_axes);
- return z;
-}
-
/// Ascertain whether two database entities are equivalent.
///
/// Equivalence here means that the dimensions and data are identical.
@@ -141,183 +126,6 @@
{
}
-void database_entity::assert_invariants() const
-{
- if
- (
- axis_lengths_.end()
- != std::find
- (axis_lengths_.begin()
- ,axis_lengths_.end()
- ,0
- )
- )
- {
- fatal_error()
- << "Database item '"
- << GetDBNames()[key_].ShortName
- << "' with key "
- << key_
- << " has zero in at least one dimension."
- << LMI_FLUSH
- ;
- }
-
- LMI_ASSERT(getndata() == static_cast<int>(data_values_.size()));
- LMI_ASSERT
- ( 0 < static_cast<int>(data_values_.size())
- && static_cast<int>(data_values_.size()) < MaxPossibleElements
- );
- LMI_ASSERT(DB_FIRST <= key_ && key_ < DB_LAST);
- LMI_ASSERT(e_number_of_axes == axis_lengths_.size());
-
- std::vector<int> const& max_dims(maximum_dimensions());
- LMI_ASSERT(e_number_of_axes == max_dims.size());
- std::vector<int>::const_iterator ai = axis_lengths_.begin();
- std::vector<int>::const_iterator mi = max_dims.begin();
-
- while(ai != axis_lengths_.end()) // not duration!
- {
- if(*ai != 1 && *ai != *mi && *ai != axis_lengths_.back())
- {
- fatal_error()
- << "Database item '"
- << GetDBNames()[key_].ShortName
- << "' with key "
- << key_
- << " has invalid length "
- << *ai
- << " in a dimension where "
- << *mi
- << " was expected."
- << LMI_FLUSH
- ;
- }
- ai++;
- mi++;
- }
-
- if(max_dims.back() < axis_lengths_.back())
- {
- fatal_error()
- << "Database item '"
- << GetDBNames()[key_].ShortName
- << "' with key "
- << key_
- << " has invalid duration."
- << LMI_FLUSH
- ;
- }
-}
-
-/// Calculate number of data required by lengths of 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)
- {
- fatal_error()
- << "Database item '"
- << GetDBNames()[key_].ShortName
- << "' with key "
- << key_
- << " contains more than the maximum possible number of elements."
- << LMI_FLUSH
- ;
- }
-
- if(0 == n)
- {
- fatal_error()
- << "Database item '"
- << GetDBNames()[key_].ShortName
- << "' with key "
- << key_
- << " has no data."
- << LMI_FLUSH
- ;
- }
-
- // Because MaxPossibleElements < n, this cast cannot lose information.
- return static_cast<int>(n);
-}
-
-/// Indexing operator for 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
-/// concerned, is much like the other axes. However, for illustration
-/// production, product_database::Query() handles the last (duration)
-/// axis, replicating the last value as needed to extend to maturity.
-
-double& database_entity::operator[](std::vector<int> const& index)
-{
- assert_invariants();
- LMI_ASSERT(e_number_of_axes == index.size());
-
- int z = 0;
- for(unsigned int j = 0; j < e_number_of_axes; j++)
- {
- if(1 != axis_lengths_[j])
- {
- LMI_ASSERT(index[j] < axis_lengths_[j]);
- z = z * axis_lengths_[j] + index[j];
- }
- }
- if(static_cast<int>(data_values_.size()) <= z)
- {
- z = 0;
- fatal_error()
- << "Trying to index database item with key "
- << key_
- << " past end of data."
- << LMI_FLUSH
- ;
- }
- return data_values_[z];
-}
-
-/// Indexing operator for illustration production.
-
-double const* database_entity::operator[](database_index const& idx) const
-{
- std::vector<int> const& index(idx.index_vector());
- LMI_ASSERT(e_number_of_axes == 1 + index.size());
-
- int z = 0;
- for(unsigned int j = 0; j < e_number_of_axes - 1; j++)
- {
- if(1 != axis_lengths_[j])
- {
- LMI_ASSERT(index[j] < axis_lengths_[j]);
- z = z * axis_lengths_[j] + index[j];
- }
- }
- z *= axis_lengths_.back();
- if(static_cast<int>(data_values_.size()) <= z)
- {
- z = 0;
- fatal_error()
- << "Trying to index database item with key "
- << key_
- << " past end of data."
- << LMI_FLUSH
- ;
- }
- return &data_values_[z];
-}
-
void database_entity::Reshape(std::vector<int> const& dims)
{
LMI_ASSERT(e_number_of_axes == dims.size());
@@ -429,6 +237,92 @@
data_values_ = new_object.data_values_;
}
+/// Indexing operator for 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
+/// concerned, is much like the other axes. However, for illustration
+/// production, product_database::Query() handles the last (duration)
+/// axis, replicating the last value as needed to extend to maturity.
+
+double& database_entity::operator[](std::vector<int> const& index)
+{
+ assert_invariants();
+ LMI_ASSERT(e_number_of_axes == index.size());
+
+ int z = 0;
+ for(unsigned int j = 0; j < e_number_of_axes; j++)
+ {
+ if(1 != axis_lengths_[j])
+ {
+ LMI_ASSERT(index[j] < axis_lengths_[j]);
+ z = z * axis_lengths_[j] + index[j];
+ }
+ }
+ if(static_cast<int>(data_values_.size()) <= z)
+ {
+ z = 0;
+ fatal_error()
+ << "Trying to index database item with key "
+ << key_
+ << " past end of data."
+ << LMI_FLUSH
+ ;
+ }
+ return data_values_[z];
+}
+
+/// Indexing operator for illustration production.
+
+double const* database_entity::operator[](database_index const& idx) const
+{
+ std::vector<int> const& index(idx.index_vector());
+ LMI_ASSERT(e_number_of_axes == 1 + index.size());
+
+ int z = 0;
+ for(unsigned int j = 0; j < e_number_of_axes - 1; j++)
+ {
+ if(1 != axis_lengths_[j])
+ {
+ LMI_ASSERT(index[j] < axis_lengths_[j]);
+ z = z * axis_lengths_[j] + index[j];
+ }
+ }
+ z *= axis_lengths_.back();
+ if(static_cast<int>(data_values_.size()) <= z)
+ {
+ z = 0;
+ fatal_error()
+ << "Trying to index database item with key "
+ << key_
+ << " past end of data."
+ << LMI_FLUSH
+ ;
+ }
+ return &data_values_[z];
+}
+
+int database_entity::GetKey() const
+{
+ return key_;
+}
+
+int database_entity::GetLength() const
+{
+ return axis_lengths_.at(6);
+}
+
+int database_entity::GetLength(int axis) const
+{
+ LMI_ASSERT(0 <= axis && axis < e_number_of_axes);
+ return axis_lengths_.at(axis);
+}
+
+std::vector<int> const& database_entity::GetAxisLengths() const
+{
+ return axis_lengths_;
+}
+
std::ostream& database_entity::write(std::ostream& os) const
{
os
@@ -465,25 +359,116 @@
return os;
}
-int database_entity::GetKey() const
+void database_entity::assert_invariants() const
{
- return key_;
-}
+ if
+ (
+ axis_lengths_.end()
+ != std::find
+ (axis_lengths_.begin()
+ ,axis_lengths_.end()
+ ,0
+ )
+ )
+ {
+ fatal_error()
+ << "Database item '"
+ << GetDBNames()[key_].ShortName
+ << "' with key "
+ << key_
+ << " has zero in at least one dimension."
+ << LMI_FLUSH
+ ;
+ }
-int database_entity::GetLength() const
-{
- return axis_lengths_.at(6);
+ LMI_ASSERT(getndata() == static_cast<int>(data_values_.size()));
+ LMI_ASSERT
+ ( 0 < static_cast<int>(data_values_.size())
+ && static_cast<int>(data_values_.size()) < MaxPossibleElements
+ );
+ LMI_ASSERT(DB_FIRST <= key_ && key_ < DB_LAST);
+ LMI_ASSERT(e_number_of_axes == axis_lengths_.size());
+
+ std::vector<int> const& max_dims(maximum_dimensions());
+ LMI_ASSERT(e_number_of_axes == max_dims.size());
+ std::vector<int>::const_iterator ai = axis_lengths_.begin();
+ std::vector<int>::const_iterator mi = max_dims.begin();
+
+ while(ai != axis_lengths_.end()) // not duration!
+ {
+ if(*ai != 1 && *ai != *mi && *ai != axis_lengths_.back())
+ {
+ fatal_error()
+ << "Database item '"
+ << GetDBNames()[key_].ShortName
+ << "' with key "
+ << key_
+ << " has invalid length "
+ << *ai
+ << " in a dimension where "
+ << *mi
+ << " was expected."
+ << LMI_FLUSH
+ ;
+ }
+ ai++;
+ mi++;
+ }
+
+ if(max_dims.back() < axis_lengths_.back())
+ {
+ fatal_error()
+ << "Database item '"
+ << GetDBNames()[key_].ShortName
+ << "' with key "
+ << key_
+ << " has invalid duration."
+ << LMI_FLUSH
+ ;
+ }
}
-int database_entity::GetLength(int axis) const
+/// Calculate number of data required by lengths of axes.
+
+int database_entity::getndata() const
{
- LMI_ASSERT(0 <= axis && axis < e_number_of_axes);
- return axis_lengths_.at(axis);
-}
+ // 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>()
+ );
-std::vector<int> const& database_entity::GetAxisLengths() const
-{
- return axis_lengths_;
+ // Meaningful iff a long int is bigger than an int.
+ if(MaxPossibleElements < n)
+ {
+ fatal_error()
+ << "Database item '"
+ << GetDBNames()[key_].ShortName
+ << "' with key "
+ << key_
+ << " contains more than the maximum possible number of elements."
+ << LMI_FLUSH
+ ;
+ }
+
+ if(0 == n)
+ {
+ fatal_error()
+ << "Database item '"
+ << GetDBNames()[key_].ShortName
+ << "' with key "
+ << key_
+ << " has no data."
+ << LMI_FLUSH
+ ;
+ }
+
+ // Because MaxPossibleElements < n, this cast cannot lose information.
+ return static_cast<int>(n);
}
void database_entity::read(xml::element const& e)
@@ -508,3 +493,18 @@
xml_serialize::set_element(e, "gloss" , gloss_ );
}
+std::vector<int> const& database_entity::maximum_dimensions()
+{
+ static int const d[e_number_of_axes] =
+ {e_max_dim_gender
+ ,e_max_dim_class
+ ,e_max_dim_smoking
+ ,e_max_dim_issue_age
+ ,e_max_dim_uw_basis
+ ,e_max_dim_state
+ ,e_max_dim_duration
+ };
+ static std::vector<int> const z(d, d + e_number_of_axes);
+ return z;
+}
+
Modified: lmi/trunk/dbvalue.hpp
===================================================================
--- lmi/trunk/dbvalue.hpp 2010-05-09 14:05:19 UTC (rev 4937)
+++ lmi/trunk/dbvalue.hpp 2010-05-10 00:48:39 UTC (rev 4938)
@@ -95,6 +95,8 @@
);
~database_entity();
+ void Reshape(std::vector<int> const& dims);
+
double const* operator[](database_index const& idx) const;
double& operator[](std::vector<int> const& idx);
@@ -103,17 +105,14 @@
int GetLength(int axis) const;
std::vector<int> const& GetAxisLengths() const;
- void Reshape(std::vector<int> const& dims);
-
std::ostream& write(std::ostream&) const;
- static std::vector<int> const& maximum_dimensions();
static bool Equivalent(database_entity const&, database_entity const&);
static bool VariesByState(database_entity const&);
private:
+ void assert_invariants() const;
int getndata() const;
- void assert_invariants() const;
void read (xml::element const&);
void write(xml::element&) const;
@@ -122,6 +121,9 @@
std::vector<int> axis_lengths_;
std::vector<double> data_values_;
std::string gloss_;
+
+ public: // for the nonce...
+ static std::vector<int> const& maximum_dimensions();
};
/*
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [lmi-commits] [4938] Rearrange functions in logical order,
Greg Chicares <=