Changeset 1203


Ignore:
Timestamp:
Oct 5, 2010, 2:33:57 PM (10 years ago)
Author:
Peter Johansson
Message:

refs #475. prefer operator+= and remove operator+ for Vector class to ensure we use +=

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Functor.h

    r1194 r1203  
    194194  };
    195195
     196
     197  template <typename Key, typename T>
     198  struct PairValuePlusAssign :
     199    public std::binary_function<T&, const std::pair<const Key, T>&, void>
     200  {
     201    T operator()(T& x, const std::pair<const Key, T>& p)
     202    {
     203      x += p.second;
     204    }
     205  };
     206
     207
     208  /**
     209     T1 must be mutable
     210   */
     211  template<typename T1, typename T2>
     212  struct PlusAssign : public std::binary_function<T1, T2, void>
     213  {
     214    void operator()(T1 t1, T2 t2) const
     215    { t1+=t2; }
     216  };
     217
    196218}} // end of namespace svndigest end of namespace theplu
    197219
  • trunk/lib/Stats.cc

    r1201 r1203  
    372372      // keys are equivalent
    373373      else {
    374         first2->second = first1->second + first2->second;
     374        first2->second += first1->second;
    375375        ++first1;
    376376        ++first2;
     
    474474      vec_type::iterator j(i);
    475475      i+=authskip;
    476       SumVector init;
    477       SumVector others =
    478         std::accumulate(j, i, init, PairValuePlus<std::string, SumVector>());
     476      SumVector others;
     477      sum(j, i, others, PairValuePlusAssign<std::string, SumVector>());
    479478      unsigned char r, g, b;
    480479      std::string label("others");
  • trunk/lib/Vector.h

    r1196 r1203  
    201201  };
    202202
    203   template<typename T, class Policy>
    204   Vector<T, Policy> operator+(const Vector<T, Policy>& lhs,
    205                               const Vector<T, Policy>& rhs)
    206   {
    207     Vector<T, Policy> result(lhs);
    208     result+=rhs;
    209     assert(result.size()>=lhs.size());
    210     assert(result.size()>=rhs.size());
    211     return result;
    212   }
    213 
    214 
    215203  template<typename T>
    216204  class SparsePolicy
  • trunk/lib/utility.h

    r1186 r1203  
    2424  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
    2525*/
     26
     27#include "Functor.h"
    2628
    2729#include <algorithm>
     
    212214  time_t str2time(const std::string&);
    213215
     216  /**
     217     same as sum(3) but using binary(result, *first) rather than
     218     result += *first
     219   */
     220  template<typename InputIterator, typename T, typename BinaryOperation>
     221  void sum(InputIterator first, InputIterator last, T& result,
     222           BinaryOperation binary)
     223  {
     224    for (; first!=last; ++first)
     225      binary(result, *first);
     226  }
     227
     228  /**
     229     Add all values of [first, last) to result
     230   */
     231  template<typename InputIterator, typename T>
     232  void sum(InputIterator first, InputIterator last, T& result)
     233  {
     234    typedef typename std::iterator_traits<InputIterator>::const_reference ref;
     235    typedef PlusAssign<T&, ref> binary;
     236    return sum(first, last, result, binary());
     237  }
     238
    214239  ///
    215240  /// If file does not exist create empty file.
  • trunk/test/vector.cc

    r1196 r1203  
    6565  }
    6666
    67   suite.out() << "testing operator+\n";
    68   vec3 = vec + vec2;
    69   if (vec3.size()!=8) {
    70     suite.add(false);
    71     suite.out() << "incorrect size: " << vec3.size() << " expected: "
    72                 << 8 << "\n";
    73   }
    74   for (size_t i=0; i<8; ++i)
    75     if (!suite.add(vec3[5]==vec[5]+vec2[5]))
    76       suite.out() << "operator+ failed: result: " << vec3[i]
    77                   << " expected: " << vec[i] + vec2[i] << "\n";
    78 
    7967  suite.out() << "testing back()\n";
    8068  vec.resize(0);
Note: See TracChangeset for help on using the changeset viewer.