home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 41 / IOPROG_41.ISO / soft / c++ / NUMCPP11.ZIP / datamanp.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-04-10  |  3.0 KB  |  100 lines

  1. //===================================================================
  2. // datamanp.hpp
  3. //
  4. // Version 1.0
  5. //
  6. // Written by:
  7. //   Brent Worden
  8. //   WordenWare
  9. //   email:  brent.worden@poboxes.com
  10. //
  11. // Copyright (c) 1998-1999 WordenWare
  12. //
  13. // Created:  August 28, 1998
  14. // Revised:  
  15. //===================================================================
  16.  
  17. #ifndef _DATA_MANP_HPP_
  18. #define _DATA_MANP_HPP_
  19.  
  20. #include "descript.hpp"
  21.  
  22. template<class T>
  23. inline void empty(T& con)
  24. //-------------------------------------------------------------------
  25. // Removes all elements from the container con.
  26. //-------------------------------------------------------------------
  27. {
  28.     con.erase(con.begin(), con.end());
  29. }
  30.  
  31. template<class T>
  32. std::vector<T> unique_values(T* first, T* last)
  33. //-------------------------------------------------------------------
  34. // Returns a vector containing the unique values in [first1, last1).
  35. //-------------------------------------------------------------------
  36. {
  37.     std::vector<T> dest(first, last);
  38.  
  39.     sort(dest.begin(), dest.end());
  40.     dest.erase(unique(dest.begin(), dest.end()), dest.end());
  41.     
  42.     return dest;
  43. };
  44.  
  45. template<class T>
  46. vector< pair<T, int> > frequencies(T* first, T* last)
  47. //-------------------------------------------------------------------
  48. // Returns a vector containing the unique values in [ffirst, flast),
  49. // together with the frequencies with which they occur.
  50. //-------------------------------------------------------------------
  51. {
  52.     vector< pair<T, int> > ret;
  53.     vector<T> unq = unique_values(first, last);
  54.     vector<T>::iterator iter = unq.begin();
  55.     
  56.     while(iter != unq.end()){
  57.         cnt = count(first, last, *iter);
  58.         ret.push_back(make_pair(*iter, cnt));
  59.         ++iter;
  60.     }
  61.     
  62.     return ret;
  63. };
  64.  
  65. template<class S, class T>
  66. vector< pair<S, vector<T> > > group(S* ffirst, S* flast, T* dfirst)
  67. //-------------------------------------------------------------------
  68. // Returns a vector containing the unique values in [ffirst, flast),
  69. // together with the vector of elements from [dfirst, dfirst +
  70. // (flast - ffirst)) in that group.
  71. //-------------------------------------------------------------------
  72. {
  73.     vector< pair<S, vector<T> > > ret;
  74.     vector<S> unq = unique_values(ffirst, flast);
  75.     vector<S>::iterator iter = unq.begin();
  76.     vector<T> elements;
  77.     vector<S>::iterator found, start;
  78.     
  79.     while(iter != unq.end()){
  80.         empty(elements);
  81.         found = start = ffirst;
  82.         while((found = find(start, flast, *iter)) != flast){
  83.             elements.push_back(*(dfirst + (found - ffirst)));
  84.             start = found + 1;
  85.         }
  86.         ret.push_back(make_pair(*iter, elements));
  87.         ++iter;
  88.     }
  89.  
  90.     return ret;
  91. }
  92.  
  93. #endif
  94.  
  95. //===================================================================
  96. // Revision History
  97. //
  98. // Version 1.0 - 08/28/1998 - New.
  99. //===================================================================
  100.