Changeset 1385 for trunk/yat/utility


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

adding functions inverse

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.