source: trunk/lib/utility/stl_utility.h @ 341

Last change on this file since 341 was 341, checked in by Jari Häkkinen, 18 years ago

Added exceptions for tracing file format problems when reading vectors.
Changed implentation for reading ints and doubles into stl vectors, now
unexpected characters are skipped instead of treated as zeros.
Some other minor things.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.8 KB
Line 
1// $Id: stl_utility.h 341 2005-06-07 14:41:25Z jari $
2
3#ifndef _theplu_utility_stl_utility_
4#define _theplu_utility_stl_utility_
5
6///
7/// \file stl_utility.h
8///
9/// There are a number of useful functionality missing in the Standard
10/// Template Library, STL. This file is an effort to provide
11/// extensions to STL functionality.
12///
13
14#include <string>
15#include <utility>
16#include <vector>
17
18namespace theplu {
19namespace utility {
20
21  ///
22  /// STL provides operator< for the pair.first element, but none for
23  /// pair.second. This template provides this and can be used as the
24  /// comparison object in generic functions such as the STL sort.
25  ///
26  template <class T1,class T2>
27  struct pair_value_compare
28  {
29    ///
30    /// @return true if x.second<y.second or (x.second==y.second and
31    /// x.first<y.first)
32    ///
33    inline bool operator()(const std::pair<T1,T2>& x,
34                           const std::pair<T1,T2>& y) {
35      return ((x.second<y.second) ||
36              (!(y.second<x.second) && (x.first<y.first))); 
37    }
38  };
39
40  ///
41  /// Function reading from istream to vector of doubles. Function
42  /// reads the line until next '\\n'. The line is splitted with
43  /// respect to whitespaces and push_backed into the vector. The
44  /// vector is emptied before the reading starts. Unexpected
45  /// characters are currently skipped with a warning message.
46  ///
47  /// @return false if end of stream
48  ///
49  /// @note The functionality of this function will change in the
50  /// future. The overall functionality will be the same but the
51  /// outcome of unexpected events will change.
52  ///
53  /// @todo Change the implementation of this function to follow C/C++
54  /// style treatment of files and streams.
55  ///
56  bool read_to_double(std::istream&, std::vector<double>&);
57
58  ///
59  /// Function reading from istream to vector of ints. Function
60  /// reads the line until next '\\n'. The line is splitted with
61  /// respect to whitespaces and push_backed into the vector. The
62  /// vector is emptied before the reading starts. Unexpected
63  /// characters are currently skipped with a warning message.
64  ///
65  /// @return false if end of stream
66  ///
67  /// @note The functionality of this function will change in the
68  /// future. The overall functionality will be the same but the
69  /// outcome of unexpected events will change.
70  ///
71  /// @todo Change the implementation of this function to follow C/C++
72  /// style treatment of files and streams.
73  ///
74  bool read_to_int(std::istream&, std::vector<int>&);
75
76  ///
77  /// Function reading from istream to vector of strings. Function
78  /// reads the line until next '\\n'. The line is splitted with
79  /// respect to whitespaces and push_backed into the vector. The
80  /// vector is emptied before the reading starts.
81  ///
82  /// @return false if end of stream
83  ///
84  bool read_to_string(std::istream&, std::vector<std::string>&);
85
86
87}} // of namespace utility and namespace theplu
88
89#endif
Note: See TracBrowser for help on using the repository browser.