[yoda-svn] r413 - trunk/include/YODA

blackhole at projects.hepforge.org blackhole at projects.hepforge.org
Wed Dec 7 10:55:21 GMT 2011


Author: buckley
Date: Wed Dec  7 10:55:21 2011
New Revision: 413

Log:
Adding a mechanism to initialise a Weights object's keys th first time that a combining operation is used on it. Not sure if we want to continue along this route, though: I tried using Weights in place of doubles yesterday and they go *everywhere*, in ways which are going to confuse people in 99% of use cases. So I think we perhaps want to do it a more stupid/inefficient way...

Modified:
   trunk/include/YODA/Weights.h

Modified: trunk/include/YODA/Weights.h
==============================================================================
--- trunk/include/YODA/Weights.h	Wed Dec  7 10:52:52 2011	(r412)
+++ trunk/include/YODA/Weights.h	Wed Dec  7 10:55:21 2011	(r413)
@@ -101,7 +101,7 @@
       return _values.find(keys()[index])->second;
     }
 
-    /// Number of weights entries
+    /// Number of weights keys
     unsigned int size() const {
       return _values.size();
     }
@@ -134,6 +134,7 @@
 
     /// Add another weights to this
     Weights& operator += (const Weights& toAdd) {
+      if (keys().empty()) _initToMatch(toAdd);
       if (keys() != toAdd.keys()) {
         throw WeightError("Mismatch in args to Weights += operator");
       }
@@ -145,6 +146,7 @@
 
     /// Subtract another weights from this
     Weights& operator -= (const Weights& toSubtract) {
+      if (keys().empty()) _initToMatch(toSubtract);
       if (keys() != toSubtract.keys()) {
         throw WeightError("Mismatch in args to Weights -= operator");
       }
@@ -156,6 +158,7 @@
 
     /// Multiply by another weights
     Weights& operator *= (const Weights& toMultiplyBy) {
+      if (keys().empty()) _initToMatch(toMultiplyBy);
       if (keys() != toMultiplyBy.keys()) {
         throw WeightError("Mismatch in args to Weights *= operator");
       }
@@ -167,6 +170,7 @@
 
     /// Divide by another weights
     Weights& operator /= (const Weights& toDivideBy) {
+      if (keys().empty()) _initToMatch(toDivideBy);
       if (keys() != toDivideBy.keys()) {
         throw WeightError("Mismatch in args to Weights /= operator");
       }
@@ -222,6 +226,18 @@
     /// @todo Allow implicit casting to double, if single-entried? Or too dangerous and not useful enough?
     // double operator (double) () {}
 
+  private:
+
+    /// Initialise an empty list of weights keys to match those of another Weights object
+    void _initToMatch(const Weights& other) {
+      if (keys().empty()) {
+        raise LogicError("Weights::_initToMatch shouldn't ever be called if there are already defined weights keys");
+      }
+      for (size_t i = 0; i < other.size(); ++i) {
+        _values[other.keys()[i]] = 0;
+      }
+    }
+
 
   private:
 


More information about the yoda-svn mailing list