~vbursian/research-assistant/intervers

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
173
174
175
176
177
178
179
////////////////////////////////////////////////////////////////////////////////
/*! @file Ranges.h   Диапазон изменения физвеличины.
- Part of RANet - Research Assistant Net Library (based on ANSI C++).
- Copyright(C) 2010, Viktor E. Bursian, St.Petersburg, Russia.
                     Viktor.Bursian@mail.ioffe.ru
*///////////////////////////////////////////////////////////////////////////////
#ifndef Ranges_H
#define Ranges_H
#include "PhysValue.h"
namespace RA {
//------------------------------------------------------------------------------

ANNOUNCE_CLASS(sRange)
ANNOUNCE_CLASS(sPhysRange)

//------------------------------------------------------------------- sRange ---
/*! Диапазон изменения вещественных (real) чисел.

@note
Допускает "перевёрнутые" значения, при которых @a To < @a From.
Причём диапазон от @a a до @a b не считается равным диапазону от @a b до @a a.

*/
class RANet_EXPORT  sRange
{
  public:
                              sRange ()
                                  :From(-real_inf),To(real_inf)
                                {}
                              sRange (real from ,real to)
                                  :From(from),To(to)
                                {}
                              sRange (rcsRange R)
                                  :From(R.From),To(R.To)
                                {}
    rsRange                   operator = (rcsRange R)
                                { From=R.From; To=R.To; return *this; }
    bool                      operator == (rcsRange R) const
                                { return (From==R.From) && (To==R.To); }
    real                      Size () const
                                { return std::abs(From-To); }
    bool                      IsInverted () const
                                { return (From > To); }
    rsRange                   Invert ();

  public: //fields
    real                      From;
    real                      To;
};

//--------------------------------------------------------------- sPhysRange ---
/*! Диапазон изменения физвеличины.

@todo{Fucking_Qt!} Q_DECLARE_METATYPE not in a right place
                  (requered for my templates AddTransition,..)

*/
class RANet_EXPORT  sPhysRange : public virtual sStorable
{
  STORABLE(sPhysRange)
  public:
                              sPhysRange ();
    explicit                  sPhysRange (rcsUnits  units);
                              sPhysRange (real      from
                                         ,real      to
                                         ,rcsUnits  units
                                         ,real      precision = 0.0
                                         );
    explicit                  sPhysRange (sString  unitsstr);
                              sPhysRange (real     from
                                         ,real     to
                                         ,sString  unitsstr
                                         ,real     precision = 0.0
                                         );
                              sPhysRange (rcsPhysValue from ,rcsPhysValue to);
                              sPhysRange (rcsPhysRange);

  public:
    bool                      UnitsAreFixed () const
                                { return UnitsAreFixedFlag; }
    void                      FixTheUnits ()
                                { UnitsAreFixedFlag = true; }
    sUnits                    Units () const
                                { return TheUnits; }
    sPhysValue                From () const
                                {
                                  return sPhysValue(TheFromMantissa
                                                           *power10(TheOrder)
                                                   ,TheUnits);
                                }
    sPhysValue                To () const
                                { return sPhysValue(TheToMantissa
                                                           *power10(TheOrder)
                                                   ,TheUnits);
                                }
    sPhysValue                Precision () const
                                { return sPhysValue(ThePrecisionMantissa
                                                           *power10(TheOrder)
                                                   ,TheUnits);
                                }
    sPhysValue                Size () const
                                {
                                  return sPhysValue(std::abs(TheFromMantissa
                                                             *power10(TheOrder)
                                                        -TheToMantissa
                                                             *power10(TheOrder))
                                                   ,TheUnits);
                                }
    bool                      IsInf () const
                                {
                                  return isinf(TheFromMantissa)
                                      || isinf(TheToMantissa);
                                }
    bool                      IsNaN () const
                                {
                                  return isnan(TheFromMantissa)
                                      || isnan(TheToMantissa);
                                }

  public://operators
    rsPhysRange               operator = (rcsPhysRange);
    rsPhysRange               operator &= (sPhysRange);
                                  //!< operator 'intersection'
    rsPhysRange               operator |= (sPhysRange);
                                  //!< operator 'expand to contain both'
    rsPhysRange               operator |= (sPhysValue);
                                  //!< operator 'expand to contain the value'
    bool                      operator <= (rcsPhysRange) const;
                                  //!< operator 'approximately inside'


  public://but for internal needs in RANet and RAGUI
    real                      FromMantissa () const
                                { return TheFromMantissa; }
    real                      ToMantissa () const
                                { return TheToMantissa; }
    real                      PrecisionMantissa () const
                                { return ThePrecisionMantissa; }
    int                       Order () const
                                { return TheOrder; }
    unsigned short int        Digits () const
                                //!< число цифр после запятой при выводе
                                { return TheDigits; }
    sString                   Text (eTextFormat  F=Plain);
                                //!< Visual representation for user
    sString                   Text (eTextFormat  F=Plain) const;
                                //!< Visual representation for user
    void                      Normalize ()
                                { Normalize(sPhysValue::SmartNormalization); }

  private:
    void                      Normalize (unsigned short int  level);
    sString                   Txt (eTextFormat  F=Plain) const;

  private: //fields
    sUnits                    TheUnits;
    bool                      UnitsAreFixedFlag;
    int                       TheOrder;
    real                      TheFromMantissa;
    real                      TheToMantissa;
    real                      ThePrecisionMantissa;
    unsigned short int        TheDigits;
    unsigned short int        NormalizationLevel;
                                  //!< 0 - possibly not normalized;
                                  //!< 1 - units - untouched,
                                  //!<     mantissa&order - OK;
                                  //!< 2 - units - within permissions;
                                  //!< n - units are looking better,
                                  //!<     time consuming limited by c*exp(n)
};

//------------------------------------------------------------------------------
} //namespace RA


#include <QMetaType>
Q_DECLARE_METATYPE(RA::sPhysRange)

#endif