File: io.cpp

package info (click to toggle)
boost1.35 1.35.0-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 203,856 kB
  • ctags: 337,867
  • sloc: cpp: 938,683; xml: 56,847; ansic: 41,589; python: 18,999; sh: 11,566; makefile: 664; perl: 494; yacc: 456; asm: 353; csh: 6
file content (172 lines) | stat: -rw-r--r-- 4,998 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* Boost examples/io.cpp
 * show some exampleso of i/o operators
 * thanks to all the people who commented on this point, particularly on
 * the Boost mailing-list
 *
 * Copyright 2003 Guillaume Melquiond
 *
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying file LICENSE_1_0.txt or
 * copy at http://www.boost.org/LICENSE_1_0.txt)
 */

#include <boost/numeric/interval.hpp>
#include <boost/io/ios_state.hpp>
#include <cmath>
#include <cassert>

namespace io_std {

template<class T, class Policies, class CharType, class CharTraits>
std::basic_ostream<CharType, CharTraits> &operator<<
  (std::basic_ostream<CharType, CharTraits> &stream,
   const boost::numeric::interval<T, Policies> &value)
{
  if (empty(value)) {
    return stream << "[]";
  } else {
    return stream << '[' << lower(value) << ',' << upper(value) << ']';
  }
}

} // namespace io_std

namespace io_sngl {

template<class T, class Policies, class CharType, class CharTraits>
std::basic_ostream<CharType, CharTraits> &operator<<
  (std::basic_ostream<CharType, CharTraits> &stream,
   const boost::numeric::interval<T, Policies> &value)
{
  if (empty(value)) {
    return stream << "[]";
  } else if (singleton(value)) {
    return stream << '[' << lower(value) << ']';
  } else {
    return stream << '[' << lower(value) << ',' << upper(value) << ']';
  }
}

} // namespace io_sngl

namespace io_wdth {

template<class T, class Policies, class CharType, class CharTraits>
std::basic_ostream<CharType, CharTraits> &operator<<
  (std::basic_ostream<CharType, CharTraits> &stream,
   const boost::numeric::interval<T, Policies> &value)
{
  if (empty(value)) {
    return stream << "nothing";
  } else {
    return stream << median(value) << "  " << width(value) / 2;
  }
}

} // namespace io_wdth

namespace io_prec {

template<class T, class Policies, class CharType, class CharTraits>
std::basic_ostream<CharType, CharTraits> &operator<<
  (std::basic_ostream<CharType, CharTraits> &stream,
   const boost::numeric::interval<T, Policies> &value)
{
  if (empty(value)) {
    return stream << "nothing";
  } else if (singleton(value)) {
    boost::io::ios_precision_saver state(stream, std::numeric_limits<T>::digits10);
    return stream << lower(value);
  } else if (zero_in(value)) {
    return stream << "0~";
  } else {
    const T rel = width(value) / norm(value);
    int range = - (int)std::log10(rel);
    boost::io::ios_precision_saver state(stream, range);
    return stream << median(value);
  }
}

} // namespace io_prec

namespace io_wide {

template<class T, class Policies, class CharType, class CharTraits>
std::basic_ostream<CharType, CharTraits> &operator<<
  (std::basic_ostream<CharType, CharTraits> &stream,
   const boost::numeric::interval<T, Policies> &value)
{
  if (empty(value)) {
    return stream << "nothing";
  } else if (singleton(value)) {
    boost::io::ios_precision_saver state(stream, std::numeric_limits<T>::digits10);
    return stream << lower(value);
  } else if (zero_in(value)) {
    return stream << "0~";
  } else {
    std::streamsize p = stream.precision();
    // FIXME poor man's power of 10, only up to 1E-15
    p = (p > 15) ? 15 : p - 1;
    double eps = 1.0; for(; p > 0; --p) { eps /= 10; }
    T eps2 = static_cast<T>(eps / 2) * norm(value);
    boost::numeric::interval<T, Policies> r = widen(value, eps2);
    return stream << '[' << lower(r) << ',' << upper(r) << ']';
  }
}

} // namespace io_wide

template<class T, class Policies, class CharType, class CharTraits> inline
std::basic_istream<CharType, CharTraits> &operator>>
  (std::basic_istream<CharType, CharTraits> &stream,
   boost::numeric::interval<T, Policies> &value)
{
  T l, u;
  char c = 0;
  stream >> c;
  if (c == '[') {
    stream >> l >> c;
    if (c == ',') stream >> u >> c; else u = l;
    if (c != ']') stream.setstate(stream.failbit);
  } else {
    stream.putback(c);
    stream >> l;
    u = l;
  }
  if (stream)
    value.assign(l, u);
  else
    value = boost::numeric::interval<T, Policies>::empty();
  return stream;
}

// Test program

#include <iostream>

int main()
{
  using namespace boost;
  using namespace numeric;
  using namespace interval_lib;

  typedef interval<double,
                   policies<rounded_math<double>,
                            checking_base<double> > > I;

  I tab[] = { I::empty(), I(1,1), I(1,2), I(-1,1), I(12.34,12.35),
              I(1234.56,1234.57), I(123456.78, 123456.79), I::empty() };
  unsigned int len = sizeof(tab) / sizeof(I);
  std::cout << "Enter an interval: (it will be the last shown)\n";
  std::cin >> tab[len - 1];

  for(unsigned int i = 0; i < len; ++i) {
    { using namespace io_std;  std::cout << tab[i] << '\n'; }
    { using namespace io_sngl; std::cout << tab[i] << '\n'; }
    { using namespace io_wdth; std::cout << tab[i] << '\n'; }
    { using namespace io_prec; std::cout << tab[i] << '\n'; }
    { using namespace io_wide; std::cout << tab[i] << '\n'; }
    std::cout << '\n';
  }

}