libsequence  1.9.5
CountingOperators.tcc
1 /*
2 
3 Copyright (C) 2003-2009 Kevin Thornton, krthornt[]@[]uci.edu
4 
5 Remove the brackets to email me.
6 
7 This file is part of libsequence.
8 
9 libsequence is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 libsequence is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 long with libsequence. If not, see <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 // -*- C++ -*-
25 #ifndef __COUNTING_OPERATORS_TCC__
26 #define __COUNTING_OPERATORS_TCC__
27 #include <Sequence/CountingOperators.hpp>
28 
29 namespace Sequence
30 {
31  template<typename key, typename value>
32  std::vector<std::pair<key,value> >
33  operator+(const std::vector<std::pair<key,value> > &lhs,
34  const std::vector<std::pair<key,value> > &rhs)
35  {
36  typedef std::vector<std::pair<key,value> > rt; //return type
37  rt rv(lhs);
38  for(typename rt::const_iterator itr = rhs.begin();
39  itr < rhs.end();
40  ++itr)
41  {
42  typename rt::iterator i = std::find_if(rv.begin(),
43  rv.end(),
44  std::bind2nd(first_is_equal<key,value>(),*itr));
45  if (i != rv.end())
46  {
47  i->second += itr->second;
48  }
49  else
50  {
51  rv.push_back(*itr);
52  }
53  }
54  return rv;
55  }
56 
57  template<typename key, typename value>
58  std::vector<std::pair<key,value> >
59  operator+=( std::vector<std::pair<key,value> > &lhs,
60  const std::vector<std::pair<key,value> > &rhs)
61  {
62  return lhs=lhs+rhs;
63  }
64 
65  template< typename key, typename value,typename comparison>
66  std::map<key,value,comparison> operator+(const std::map<key,value,comparison> &lhs,
67  const std::map<key,value,comparison> &rhs)
68  {
69  typedef std::map<key,value,comparison> rt;
70  rt rv(lhs);
71  for(typename rt::const_iterator itr = rhs.begin() ;
72  itr != rhs.end() ;
73  ++itr)
74  {
75  typename rt::iterator i = rv.find(itr->first);
76  if ( i != rv.end() )
77  {
78  i->second += itr->second;
79  }
80  else
81  {
82  rv[itr->first] = itr->second;
83  }
84  }
85  return rv;
86  }
87 
88  template< typename key, typename value,typename comparison>
89  std::map<key,value,comparison> operator+=( std::map<key,value,comparison> &lhs,
90  const std::map<key,value,comparison> &rhs)
91  {
92  return lhs=lhs+rhs;
93  }
94 }
95 #endif