~vbursian/research-assistant/intervers

« back to all changes in this revision

Viewing changes to RANet/Units.h

  • Committer: Viktor Bursian
  • Date: 2013-06-06 15:10:08 UTC
  • Revision ID: vbursian@gmail.com-20130606151008-6641eh62f0lgx8jt
Tags: version_0.3.0
version 0.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
////////////////////////////////////////////////////////////////////////////////
2
2
/*! @file Units.h   Единицы измерения физвеличин.
3
3
- Part of RANet - Research Assistant Net Library (based on ANSI C++).
4
 
- Copyright(C) 2007-2010, Viktor E. Bursian, St.Petersburg, Russia.
 
4
- Copyright(C) 2007-2011, Viktor E. Bursian, St.Petersburg, Russia.
5
5
                     Viktor.Bursian@mail.ioffe.ru
6
6
*///////////////////////////////////////////////////////////////////////////////
7
7
#ifndef Units_H
11
11
#include <map>
12
12
#include <list>
13
13
#include <cmath>
 
14
#include <valarray>
14
15
namespace RA {
15
16
//------------------------------------------------------------------------------
16
17
 
17
18
ANNOUNCE_CLASS(sUnit)
18
19
ANNOUNCE_CLASS(sUnits)
19
20
ANNOUNCE_CLASS(sPhysValue)
20
 
 
21
 
RANet_EXPORT extern real const          ln10;
 
21
  //! @todo{PhysValues} friend class sPhysRange - хорошо ли?
 
22
ANNOUNCE_CLASS(sPhysRange)
22
23
 
23
24
//------------------------------------------------------------------------------
24
 
 
25
 
inline  real  power10 (real E)
26
 
{
27
 
  return exp(ln10*E);
28
 
};
 
25
/*! Константа "пустые единицы".
 
26
 
 
27
Используется в определениях переменных или членов, когда нужно создать заведомо
 
28
безразмерную переменную или член типа sPhysValue или sPhysRange. Введена по двум
 
29
причинам.  Во-первых, C++  не даёт использовать в этих местах выражение
 
30
@a sUnits() - думает, что это объявление функции.  Во-вторых, просто
 
31
улучшает читаемость.
 
32
*/
 
33
RANet_EXPORT extern csUnits   _Unitsless_;
 
34
 
29
35
 
30
36
//------------------------------------------------------------ sMetricPrefix ---
31
37
/*! Provides transformations between metric prefixes and scale multipliers
59
65
{
60
66
  public://static
61
67
    static sString            Symbol (int  Log);
 
68
    static sString            CombinedSymbol (int  Log);
62
69
    static int                Exp (sString  Prefix);
63
 
    static real               Multiplier (sString  Prefix);
 
70
    static real               Multiplier (int  Log);
 
71
    static real               Multiplier (sString  Prefix)
 
72
                                { return Multiplier(Exp(Prefix)); };
64
73
    static sString            Name (int  Log);
 
74
    static csString           StandardAllowance;
 
75
    static csString           NoPrefixAllowed;
65
76
};
66
77
 
67
78
//-------------------------------------------------------------------- sUnit ---
84
95
class RANet_EXPORT  sUnit : public sStorable
85
96
{
86
97
  STORABLE(sUnit)
 
98
  public://types
 
99
    typedef bool              fRegisterDialog (rsString);
 
100
    typedef fRegisterDialog * pfRegisterDialog;
 
101
 
 
102
    ANNOUNCE_CLASS(sSystem)
 
103
    class  sSystem
 
104
    {
 
105
      public:
 
106
                                  ~sSystem ();
 
107
                                  sSystem ();
 
108
      public://fields
 
109
        map<sString,psUnit>       UnitList;
 
110
 
 
111
//      friend class sUnit;
 
112
    };
 
113
 
87
114
  public://static
88
 
    static psUnit             Register (sString  symbol  ,sString  sense);
89
 
    static psUnit             Register (sString  symbol)
90
 
                                { return Register(symbol,sString()); };
91
 
    static psUnit             Unit (sString  id);
 
115
    static psUnit             Register (sString     symbol
 
116
                                       ,sString     prefix_allowance
 
117
                                       ,sPhysValue  definition);
 
118
    static psUnit             Register (sString  symbol
 
119
                                       ,sString  prefix_allowance);
 
120
    static psUnit             Register (sString     symbol
 
121
                                       ,sPhysValue  definition);
 
122
    static psUnit             Register (sString  symbol);
 
123
    static psUnit             Find (sString  symbol);
 
124
    static bool               ParseUnitWithPrefix (sString   lexem
 
125
                                                  ,psUnit &  unit
 
126
                                                  ,int &     prefixlg);
92
127
 
93
 
  public://static field
 
128
  public://static fields
 
129
    static sSystem            System;
94
130
    static psUnit             _1_;
 
131
    static pfRegisterDialog   RegisterDialog;
95
132
 
96
133
  public:
97
134
    sString                   Symbol () const
98
135
                                { return TheSymbol; };
99
 
    sString                   Sense () const
100
 
                                { return TheSense; };
101
 
    sString                   Id () const
102
 
                                { return TheSymbol + " (" + TheSense + ")"; };
 
136
    bool                      PrefixAllowed (int  prefix_index) const;
 
137
    sString                   PrefixAllowance ()
 
138
                                { return ThePrefixAllowance; };
 
139
//    void                      SetPrefixAllowance (sString);
 
140
    psPhysValue               Definition () const
 
141
                                { return TheDefinition; };
 
142
//    void                      SetDefinition (sPhysValue);
 
143
    bool                      IsBasic () const
 
144
                                { return (TheDefinition != NULL); };
103
145
 
104
146
  private:
105
 
                              sUnit ();
106
 
                              sUnit (sString  symbol  ,sString  sense);
 
147
                              sUnit (); //!< makes _1_
 
148
                              sUnit (sString      symbol
 
149
                                    ,sString      prefix_allowance
 
150
                                    ,psPhysValue  definition);
107
151
                              sUnit (rcsUnit);
108
 
    rsUnit                    operator = (sUnit);
 
152
    rsUnit                    operator = (rcsUnit);
109
153
 
110
 
  private://static fields
111
 
    static map<sString
112
 
              ,psUnit >       UnitList;
 
154
  private://static
 
155
    void                      MakeVeryBasicDefinitions ();
113
156
 
114
157
  private://fields
115
158
    bool                      Is_1_Flag;
 
159
    bool                      IsObsoleteFlag;
116
160
    sString                   TheSymbol;
117
 
    sString                   TheSense;
118
 
//    psPhysValue               TheDefinition;
119
 
//    psUnit IsAliasOf // cm-1 (Photon energy) == cm-1 (Energy quantum)
120
 
                       // cm-1 (Reverse length) != cm-1 (Energy quantum)
121
 
//    bitset PrefixAllowance
 
161
    sString                   ThePrefixAllowance;
 
162
    psPhysValue               TheDefinition;
 
163
//    sString                   TheComment;
 
164
 
 
165
  friend class sSystem;
122
166
};
123
167
 
124
168
//------------------------------------------------------------------- sUnits ---
128
172
class RANet_EXPORT  sUnits : public sStorable
129
173
{
130
174
  STORABLE(sUnits)
131
 
  public://types
 
175
//  public://types
 
176
//
 
177
  public://static
 
178
    static sUnits             Parse (sString);
 
179
 
 
180
  public:
 
181
                              ~sUnits ();
 
182
                              sUnits ();
 
183
                              sUnits (psUnit unit
 
184
                                     ,int    power     = 1
 
185
                                     ,int    prefix_lg = 0);
 
186
                              sUnits (sString);
 
187
                              sUnits (rcsUnits);
 
188
    bool                      Is_1_ () const;
 
189
    sString                   Text (eTextFormat  F = Plain) const;
 
190
                                //!< visual representation for user
 
191
    sPhysValue                ToBasic () const;
 
192
 
 
193
  public://operators
 
194
    rsUnits                   operator = (rcsUnits);
 
195
    sPhysValue                operator * (rcsUnits) const;
 
196
    sPhysValue                operator / (rcsUnits) const;
 
197
//    sPhysValue                operator * (rcsPhysValue) const;
 
198
//    sPhysValue                operator / (rcsPhysValue) const;
 
199
 
132
200
  private://types
133
201
    ANNOUNCE_CLASS(sTerm)
134
202
    class sTerm
140
208
                                      ,PrefixLg(prefix_lg)
141
209
                                    {};
142
210
        bool                      operator < (rcsTerm T) const
143
 
                                    { return Unit->Id() < T.Unit->Id(); };
 
211
                                    { return Unit->Symbol()
 
212
                                              < T.Unit->Symbol();
 
213
                                    };
144
214
      public://fields
145
215
        psUnit                    Unit;
146
216
        int                       Power;
147
217
        int                       PrefixLg;
148
218
    };
149
219
 
150
 
  public:
151
 
                              ~sUnits ();
152
 
                              sUnits (psUnit unit      = sUnit::_1_
153
 
                                     ,int    power     = 1
154
 
                                     ,int    prefix_lg = 0);
155
 
                              sUnits (sString);
156
 
                              sUnits (rcsUnits);
157
 
    rsUnits                   operator = (sUnits);
158
 
    bool                      Is_1_ () const
159
 
                                { return Product.empty(); };
160
 
    sString                   Text (eTextFormat  F = Plain) const;
161
 
                                //!< visual representation for user
162
 
//    sString                   TextRefined () const;
163
 
    bool                      Comparable (rcsUnits U, real &  coeff) const;
164
 
 
165
 
  public:
166
 
    sPhysValue                operator * (sUnits) const;
167
 
    sPhysValue                operator / (sUnits) const;
168
 
 
169
 
  public:
170
 
//    void                      DissolveOrder (int &  log_shift);
171
 
//    void                      Normalize (int  log_shift = 0);
172
 
//    sUnits                    Refine () const;
 
220
    typedef std::list<sTerm>    tProduct;
 
221
    typedef std::valarray<int>  tPrefixes;
 
222
 
 
223
  private://static
 
224
    static void               SubstituteDefinitions (real &   coeff
 
225
                                                    ,rsUnits  units);
173
226
 
174
227
  private:
175
 
    void                      SimplifyProduct (int &  log_shift);
 
228
    int                       MultBy (rcsUnits);
 
229
    int                       DivideBy (rcsUnits);
 
230
//    bool                      Comparable (rcsUnits  units
 
231
//                                         ,real &    ratio
 
232
//                                         ,bool &    power_of_ten) const;
 
233
    bool                      Comparable (rcsUnits  units
 
234
                                         ,real &    ratio) const;
 
235
    int                       DissolveOrder (unsigned short int  level
 
236
                                            ,int  log_shift = 0
 
237
                                            ,int  min_rest_expected = 0
 
238
                                            ,int  max_rest_expected = -1);
 
239
    int                       SimplifyProduct (int  log_shift = 0);
 
240
//    void                      OrderPushIn (int  log_shift);
 
241
    void                      SmartSort ();
 
242
    void                      FindBestChoice (tPrefixes &  best_choice
 
243
                                             ,int       &  best_weight
 
244
                                             ,int       &  best_rest_order
 
245
                                             ,int          min_rest_expected
 
246
                                             ,int          max_rest_expected
 
247
                                             ,tPrefixes    considered_product
 
248
                                             ,int          weight
 
249
                                             ,int          rest_order
 
250
                                             ,size_t       term_to_vary
 
251
                                             ,unsigned short int  level);
176
252
 
177
253
  private://fields
178
 
    std::list<sTerm>          Product;
 
254
    tProduct                  Product;
 
255
    bool                      InBasicTerms;
 
256
    bool                      Refined;
 
257
 
 
258
  friend class sPhysValue;
 
259
  //! @todo{PhysValues} friend class sPhysRange - хорошо ли?
 
260
  friend class sPhysRange;
179
261
};
180
262
 
181
263
//------------------------------------------------------------------------------