Changeset 1385 for trunk


Ignore:
Timestamp:
Jul 22, 2008, 7:32:53 PM (13 years ago)
Author:
Peter
Message:

adding functions inverse

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/utility_test.cc

    r1313 r1385  
    3232#include <fstream>
    3333#include <iostream>
     34#include <map>
    3435#include <string>
     36#include <vector>
    3537
    3638using namespace theplu::yat;
    3739void test_quantile_normalize(test::Suite&);
     40void test_inverse(test::Suite&);
     41
     42template<typename InputIterator, typename Key>
     43void test_inverse_validate(InputIterator first, InputIterator last,
     44                           const std::map<Key, std::vector<size_t> >& m,
     45                           test::Suite& suite);
     46template<typename InputIterator, typename Key>
     47void test_inverse_validate(InputIterator first,
     48                           const std::multimap<Key, size_t>& m,
     49                           test::Suite& suite);
    3850
    3951int main(int argc, char* argv[])
     
    140152
    141153  test_quantile_normalize(suite);
     154  test_inverse(suite);
    142155
    143156  return suite.return_value();
    144157}
     158
     159
     160void test_inverse(test::Suite& suite)
     161{
     162  suite.err() << "Testing inverse\n";
     163  std::vector<std::string> vec;
     164  vec.push_back("No");
     165  vec.push_back("one");
     166  vec.push_back("shall");
     167  vec.push_back("be");
     168  vec.push_back("subjected");
     169  std::map<std::string, std::vector<size_t> > map;
     170  utility::inverse(vec.begin(), vec.end(), map);
     171  test_inverse_validate(vec.begin(), vec.end(), map, suite);
     172  utility::inverse(vec.begin()+1, vec.end(), map);
     173  test_inverse_validate(vec.begin()+1, vec.end(), map, suite);
     174  const std::vector<std::string> vec2(vec);
     175  utility::inverse(vec2.begin(), vec2.end(), map);
     176  test_inverse_validate(vec2.begin(), vec2.end(), map, suite);
     177  std::multimap<std::string, size_t> mmap;
     178  utility::inverse(vec.begin(), vec.end(), mmap);
     179  test_inverse_validate(vec.begin(), mmap, suite);
     180  utility::inverse(vec.begin()+1, vec.end(), mmap);
     181  test_inverse_validate(vec.begin()+1, mmap, suite);
     182}
     183
     184
     185template<typename InputIterator, typename Key>
     186void test_inverse_validate(InputIterator first, InputIterator last,
     187                           const std::map<Key, std::vector<size_t> >& m,
     188                           test::Suite& suite)
     189{
     190  typedef typename std::map<Key, std::vector<size_t> >::const_iterator Iter;
     191  for ( InputIterator iter=first; iter != last; ++iter) {
     192    Iter map_iter = m.find(*iter);
     193    if (!suite.add(map_iter!=m.end())) {
     194      suite.err() << "test_inverse_validate() failed\n";
     195      suite.err() << "  could not find: " << *first << " in map m\n";
     196    }
     197    for (size_t i=0; i<map_iter->second.size(); ++i)
     198      if (!suite.add(map_iter->second[i] ==
     199                     static_cast<size_t>(distance(first, iter))) ) {
     200        suite.err() << "test_inverse_validate() failed\n";
     201        suite.err() << "  comparing: " << map_iter->second[i]
     202                    << " and " << distance(first, iter)
     203                    << " expected them to be equal\n";
     204      }
     205  }
     206}
     207
     208
     209template<typename InputIterator, typename Key>
     210void test_inverse_validate(InputIterator first,
     211                           const std::multimap<Key, size_t>& m,
     212                           test::Suite& suite)
     213{
     214  for (typename std::multimap<Key, size_t>::const_iterator iter=m.begin();
     215       iter!=m.end(); ++iter) {
     216    suite.add(*(first + iter->second) == iter->first);
     217  }
     218 
     219}
     220
    145221
    146222void test_quantile_normalize(test::Suite& suite)
  • trunk/yat/utility/stl_utility.h

    r1341 r1385  
    3939#include <cmath>
    4040#include <functional>
     41#include <map>
    4142#include <ostream>
    4243#include <string>
    4344#include <utility>
     45#include <vector>
     46
     47// debug
     48#include <iostream>
    4449
    4550namespace std {
     
    133138    { return std::exp(x); }
    134139  };
     140
     141  /**
     142     Creating a map from a range [first, last) such that m[key]
     143     returns a vector with indices of which element in [first, last)
     144     that is equal to \a key, or more technically: m[element].size()
     145     returns number of elements equal to \a element, and
     146     m[*element][i] = distance(first, element) for every \a element in
     147     [first, last) and \a i smaller than m[element].size().
     148
     149     Requirement: InputIterator's value type is assignable to Key
     150
     151     \since New in yat 0.5
     152   */
     153  template<typename InputIterator, typename Key>
     154  void inverse(InputIterator first, InputIterator last,
     155               std::map<Key, std::vector<size_t> >& m)
     156  {
     157    m.clear();
     158    for (size_t i=0; first!=last; ++i, ++first)
     159      m[*first].push_back(i);
     160  }
     161
     162  /**
     163     In the created multimap each element e will fulfill: \f$ *(first
     164     + e->second) == e->first \f$
     165
     166     Requirement: InputIterator's value type is assignable to Key
     167
     168     \since New in yat 0.5
     169   */
     170  template<typename Key, typename InputIterator>
     171  void inverse(InputIterator first, InputIterator last,
     172               std::multimap<Key, size_t>& m)
     173  {
     174    m.clear();
     175    for (size_t i=0; first!=last; ++i, ++first)
     176      m.insert(std::make_pair(*first, i));
     177  }
     178
    135179
    136180  /**
Note: See TracChangeset for help on using the changeset viewer.