stec-platform
algo.h
1 #ifndef ALGO_H
2 #define ALGO_H
3 
4 
5 #include "platform_global.h"
6 #include <memoryheap.h>
7 #include <QDateTime>
8 #include <QVariant>
9 #include <QVariantList>
10 #include <QString>
11 #include <QtAlgorithms>
12 #include <random>
13 #include <QDateTime>
14 
15 
16 #include <math.h>
17 #include <complex>
18 
19 typedef std::complex<double> CPLX;
20 
21 #define THRESHOLD 1E-6
22 #define MAXVAL 1.0E38
23 
25 enum COMPOSITION { PEAK, INTEGRATE, PEAK_AVG, TROUGH };
26 
27 namespace algo {
28 
38 template <typename InputIterator, typename OutputIterator>
39 inline OutputIterator vCopy(InputIterator begin, InputIterator end, OutputIterator dest)
40 {
41  while (begin != end)
42  *dest++ = *begin++;
43  return dest;
44 }
45 
54 template <typename BiIterator1, typename BiIterator2>
55 inline BiIterator2 vCopyBackward(BiIterator1 begin, BiIterator1 end, BiIterator2 dest)
56 {
57  while (begin != end)
58  *--dest = *--end;
59  return dest;
60 }
61 
70 template <typename InputIterator1, typename InputIterator2>
71 inline bool vEqual(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)
72 {
73  for (; first1 != last1; ++first1, ++first2)
74  if (!(*first1 == *first2))
75  return false;
76  return true;
77 }
78 
79 
86 template <class T>
87 void VCast( const VremVArray &si1, T *dst)
88 {
89  int i;
90  for( i = 0; i < si1.size(); ++i)
91  {
92  dst[i] = si1[i].value<T>();
93  }
94 }
95 
96 
103 template <class T>
104 void VCast( const QVariantList &si1, T *dst)
105 {
106  int i;
107  for( i = 0; i < si1.size(); ++i)
108  {
109  dst[i] = si1[i].value<T>();
110  }
111 }
112 
113 
121 template <typename ForwardIterator, typename T>
122 inline void vFill(ForwardIterator first, ForwardIterator last, const T &val)
123 {
124  for (; first != last; ++first)
125  *first = val;
126 }
127 
128 
135 template <typename Container, typename T>
136 inline void vFill(Container &container, const T &val)
137 {
138  vFill(container.begin(), container.end(), val);
139 }
140 
141 
148 class PLATFORMSHARED_EXPORT smath
149 {
150 public :
152  static QVariant DefaultValue(
153  QString key,
154  QVariant src,
155  QVariant defaultValue = "",
156  bool ignorKeyCase = true);
157 
159  static QVariant DefaultValue(
160  QString key,
161  VremVArray src,
162  QVariant defaultValue = "",
163  bool ignorKeyCase = true);
164 
166  static QVariant DefaultValue(
167  QString key,
168  VremList src,
169  QVariant defaultValue = "",
170  bool ignorKeyCase = true);
171 
173  static bool Contains(QString pattern, QVariant src, bool ignorKeyCase = true);
174 
176  static QVariant Normalize(const QString str );
177 
178 };
179 
185 class PLATFORMSHARED_EXPORT vmath
186 {
187 public :
188 
189  typedef struct
190  {
191  double Y_SUM; /* used interally */
192  double X_SUM; /* used interally */
193  double N; /* used interally */
194  double XY_SUM; /* used interally */
195  double X_SQ_SUM; /* used interally */
196  double Y_SQ_SUM; /* used interally */
197 
198  double slope; /* can be used by caller after regression_a() */
199  double intercept; /* can be used by caller after regression_a() */
200  double correlation; /* can be used by caller after regression_a() */
201 
202  } REGRESSION;
203 
210  static void Inc(VremVArray &i1, int count = -1, int index = 0);
211 
218  static void Dec(VremVArray &i1, int count = -1, int index = 0);
219 
226  static void Atan(VremVArray &i1,int count = -1, int index = 0);
227 
234  static void AtanH(VremVArray &i1,int count = -1,int index = 0);
235 
242  static void Cos(VremVArray &i1, int count = -1, int index = 0);
243 
250  static void CosH(VremVArray &i1,int count = -1, int index = 0);
251 
258  static void ACos(VremVArray &i1,int count = -1, int index = 0);
259 
266  static void Sin(VremVArray &i1, int count = -1, int index = 0);
267 
274  static void SinH(VremVArray &i1,int count = -1, int index = 0);
275 
282  static void Tan(VremVArray &i1, int count = -1, int index = 0);
283 
290  static void TanH(VremVArray &i1,int count = -1, int index = 0);
291 
292 
293  static void Log(VremVArray &i1, int count = -1, int index = 0);
294 
301  static void Exp(VremVArray &i1, int count = -1, int index = 0);
302 
309  static void Sqrt(VremVArray &i1,int count = -1, int index = 0);
310 
317  static void Fabs(VremVArray &i1,int count = -1, int index = 0);
318 
325  static void Floor(VremVArray &i1,int count = -1, int index = 0);
326 
332  static void Rev(VremVArray &i1, int index = 0);
333 
339  static void Rrl(VremVArray &i1, int index = 0);
340 
346  static void Rrr(VremVArray &i1, int index = 0);
347 
355  static void Fill(VremVArray &i1, QVariant with, int count = -1, int index = 0);
356 
364  static QVariant Highest( const VremVArray &i1, int count = -1, int index = 0);
365 
374  static QVariant Highest( const VremVArray &i1, QVariant except, int count = -1, int index = 0);
375 
383  static QVariant Lowest( const VremVArray &i1, int count = -1, int index = 0);
384 
393  static QVariant Lowest( const VremVArray &i1, QVariant except, int count = -1, int index = 0);
394 
402  static QVariant Mean ( const VremVArray &i1, int count = -1, int index = 0);
403 
412  static QVariant Mean ( const VremVArray &i1, QVariant except, int count = -1, int index = 0);
413 
421  static QVariant Mean ( const QVariantList &i1, int count = -1, int index = 0);
422 
431  static QVariant Mean ( const QVariantList &i1, QVariant except, int count = -1, int index = 0);
432 
433 
441  static QVariant Sum ( const VremVArray &i1, int count = -1, int index = 0);
442 
451  static QVariant Sum ( const VremVArray &i1, QVariant except, int count = -1, int index = 0);
452 
460  static void Balance(
461  VremVArray &d1 , // destination double array
462  const VremVArray &s1 // source double array
463  , int count = -1
464  , int index = 0);
465 
475  static void Mask(
476  VremVArray &d1 // destination double array
477  ,const VremVArray &s1 // source double array
478  ,int countDst = -1
479  ,int indexDst = 0
480  ,int countSrc = -1
481  ,int indexSrc = 0
482  );
483 
490  static int getRand(int min, int max){
491  unsigned int ms = static_cast<unsigned>(QDateTime::currentMSecsSinceEpoch());
492  std::mt19937 gen(ms);
493  std::uniform_int_distribution<> uid(min, max);
494  return uid(gen);
495  }
496 
497 
505  static QVariant ComputerVar ( const VremVArray &i1, int count = -1, int index = 0);
506 
514  static QVariant ComputerVarNZ ( const VremVArray &i1, int count = -1, int index = 0);
515 
521  static void AlignL(
522  VremVArray &d, // destination double array
523  const VremVArray &s // source double array
524  );
525 
527  static int check_bounds(double left, double right, int start, int end, int invert );
528 
530  static bool map_doubles (
531  double d[], /* destination double array */
532  double s[], /* source double array */
533  int d_count, /* d element count */
534  int start_s, /* s boundaries */
535  int end_s,
536  int left_s,
537  int right_s,
538  double offst,
539  double total_s_width,
540  double min_d_width,
541  double d_width[], /* zone size array in multiples of min_d_width */
542  COMPOSITION composition,
543  bool edge_allow_0,
544  bool middle_allow_0
545  );
546 
548  static void MapIt(
549  VremVArray &d, // destination double array
550  const VremVArray &s );
551 
556  static void FFT(VremVArray &s);
557 
565  static bool IdenticalArray(const VremVArray i1, const VremVArray i2);
566 
572  static QByteArray ConvertToStream( const VremVArray &i1);
573 
574 
580  static VremVArray ConvertToVremVArray(QByteArray &stream);
581 
587  static VremVArray ConvertToVremVArray(QVariantList &lst);
588 
594  static QVariantList ConvertToQVariantList(QByteArray &stream);
595 
601  static QVariantList ConvertToQVariantList(VremVArray &varray);
602 
607  template <class T>
608  static void displayPointer ( T *ptr )
609  {
610 
611  QString ptrStr = QString("0x%1").arg( reinterpret_cast<quintptr>(ptr),
612  QT_POINTER_SIZE * 2, 16, QChar('0'));
613  qDebug() << ptrStr;
614  }
615 
616 
617 
628 static QVariant
630  QVariant x,
631  int rs
632  )
633 {
634  double temp = x.toDouble();
635 
636  while (--rs >= 1)
637  {
638  temp = temp * x.toDouble();
639  }
640  return(temp);
641 }
642 
653 QVariant poly(QVariant x, int degree, QVariantList coeff)
654 {
655  double value;
656 
657  value = 0;
658 
659  while (degree)
660  {
661  value = value + (coeff[degree].toDouble() * algo::vmath::pow_xy(x, degree).toDouble());
662  degree--;
663  }
664 
665  value = value + coeff[degree].toDouble();
666  return(value);
667 }
668 
669 
670 
671 
689 static void
690  regression_clear(REGRESSION *rg);
691 
719  static void
720  regression_accumulator(REGRESSION *rg, double x, double y);
721 
734  static void
735  regression_compute(REGRESSION *rg);
736 
737 
753  static void
754  correlation_compute(REGRESSION *rg);
755 
756 
757  /*-------------------------------------------------------------------------
758  --.i.regression_a
759  -- Computes linear regression of x and y variables
760  --
761  -- Where *rg = setpoint (REGRESSION *)
762  -- *x = x values (double *)
763  -- *y = y values (double *)
764  -- count = points to plot (int )
765  --
766  -- Returns Void
767  --
768  -- Created by: EWC 11.July.1989
769  --------------------------------------------------------------------------*/
770 
789  static void
790  regression_a
791  (
792  REGRESSION *rg, /* regression structure */
793  double *x, /* x values */
794  double *y, /* y values corresponding to the x values */
795  int count /* number of points to plot */
796  );
797 
798 };
799 
800 
810 class PLATFORMSHARED_EXPORT DbxDistributionFilter
811 {
813 #define EPSILON 1E-6
814 
815 protected :
817  typedef enum
818  {
819  Forward,
820  Reverse
821  }
822  Direction;
823 
826 
829 
832 
834  double VAxisStep;
835 
837  double StartVAxis;
838 
840  double TotalValue;
841 
843  double LastVAxis;
844 
845 public:
847  DbxDistributionFilter ( QVariant stepSize = static_cast<double>(1.0) ); // DbxDistributionFilter
848 
850  DbxDistributionFilter ( const DbxDistributionFilter &ref ); // DbxDistributionFilter
851 
852  virtual ~ DbxDistributionFilter() {}
853 
855  QVariant step ( void )
856  {
857  return VAxisStep;
858  } // step
859 
860 
862  bool setStep ( QVariant stepSize ); // setStep
863 
864 
866  bool forward ( void ) { return direction == Forward; }
867 
868  void setDirection( DbxDistributionFilter::Direction dir = Forward );
869 
871  virtual bool insertValue ( const QVariant v, const QVariant vA);
872 
874  virtual bool insertAValue ( const QVariant v ); // insertValue
875 
877  virtual void flushValue ( void ); // flushValue
878 
880  virtual void interpolatedValue ( const QVariant value, const QVariant vAxis )
881  {
882  Q_UNUSED ( value )
883  Q_UNUSED ( vAxis )
884  }
885 
886 
887 }; // DbxDistributionFilter
888 
893 class PLATFORMSHARED_EXPORT MeanAverage {
894 
895 private :
896  QVarLengthArray<int> N;
897  VremVArray array;
898 public :
899  MeanAverage()
900  {
901  }
902 
903  void Reset()
904  {
905  array.clear();
906  N.clear();
907  }
908 
909  VremVArray GetValues()
910  {
911  return array;
912  }
913 
914  VremVArray GetMean()
915  {
916  VremVArray mean( array.size());
917  for( int i = 0; i < array.size() && i < mean.size() && i < N.size(); ++i)
918  {
919  int NN = N[i];
920  if( NN )
921  mean[i] = array[i].toDouble() / static_cast<double>(NN);
922  else
923  mean[i] = 0;
924  }
925  return mean;
926  }
927 
928  void AddValues( VremVArray &v)
929  {
930  if( !N.size())
931  {
932  array = v;
933  N.reserve(v.size());
934  N.resize(v.size());
935  std::fill(N.begin(), N.end(), static_cast<int>(1));
936  // qFill(N,1);
937 
938  }
939  else
940  {
941 
942 
943  for( int i = 0; i < v.size(); ++i)
944  {
945  if( i < array.size() && i < N.size())
946  {
947  N[i] = N[i] + 1;
948  array[i] = array[i].toDouble() + v[i].toDouble();
949  }
950  else
951  {
952  array.push_back(v[i]);
953  N.push_back(1);
954  }
955  }
956  }
957  }
958 };
959 
964 template <class T>
965 class SUMMER
966 {
967 private:
968  int _N;
969  T Sum;
970 public:
971  SUMMER() : _N(0), Sum( 0)
972  {}
973 
974  SUMMER &operator+= (T v)
975  {
976  _N++;
977  Sum += v;
978  return *this;
979  }
980 
981  void Reset() {
982  _N = 0;
983  Sum = 0;
984  }
985  T SumOf( void) {
986  return Sum;
987  }
988  int N(void)
989  {
990  return _N;
991  }
992 
993  T Mean( )
994  {
995  if( _N)
996  {
997  return Sum / static_cast<T>(_N);
998  }
999  return 0;
1000  }
1001 
1002 
1003 };
1004 
1014 {
1015 private :
1016  QStringList listOfMySubnet;
1017 
1018 public:
1019 
1021  VremMask( QString ip, QString mask)
1022  {
1023 
1024  int network, broadcast, range;
1025  QString NetworkAddress, BroadCastAddress;
1026 
1027  QString IpAddress = ip;
1028  QString NetMask = mask;
1029 
1031  int cIP = IpAddress.lastIndexOf('.');
1032 
1034  int nMsk = NetMask.lastIndexOf('.');
1035 
1037  if ( cIP == -1 || nMsk == -1)
1038  {
1039  range = 1;
1040  network = 0;
1041  broadcast = 255;
1042  }
1043 
1045  int cMask = NetMask.mid(nMsk + 1).toInt();
1046  int classC = IpAddress.mid(cIP + 1).toInt();
1047 
1049  QString classB = NetworkAddress = BroadCastAddress = IpAddress.left(cIP + 1);
1050 
1052  if ( cMask == 0)
1053  {
1054  range = 256;
1055  network = 0;
1056  broadcast = 255;
1057  }
1058  else
1059  {
1060  range = (255 - cMask) + 1;
1061  network = (classC & cMask) & 0xff;
1062  broadcast = network + (range -1);
1064  }
1065  {
1067  QVariant v(broadcast);
1068  BroadCastAddress += v.toString();
1069  }
1070 
1071  {
1072  QVariant v(network);
1073  NetworkAddress += v.toString();
1074  }
1075 
1077  for ( int i = network; i < network + range; ++ i)
1078  {
1079  QVariant v(i);
1080  QString ipInSubnet = classB + v.toString();
1081  listOfMySubnet.push_back(ipInSubnet);
1082 
1083  }
1084  }
1085 
1086 
1088  const QStringList SubNet(void)
1089  {
1090  return listOfMySubnet;
1091  }
1092 
1094  const QString Network(void)
1095 
1096  {
1097  if (listOfMySubnet.size() )
1098  return listOfMySubnet[0];
1099  return "localhost";
1100  }
1101 
1103  const QString BroadCast(void)
1104  {
1105  if (listOfMySubnet.size() )
1106  return listOfMySubnet[listOfMySubnet.size() -1 ];
1107 
1108  return "localhost";
1109  }
1110 
1112  static const QStringList ListOfSubnetAddresses( QString ip, QString mask)
1113  {
1114  VremMask v( ip, mask);
1115  return v.SubNet();
1116  }
1117 };
1118 
1119 }
1120 
1121 
1122 #endif // ALGO_H
class DbxDistributionFilter DbxDistributionFilter is a simple class for online filtering of non-equid...
Definition: algo.h:811
Direction direction
we'll need to know about the direction of filtering in order to properly assign the axis....
Definition: algo.h:825
double TotalValue
total value so far of the element currently being calculated..
Definition: algo.h:840
Direction
we'll need to know which direction we're going in order for proper axis and boundary calculations....
Definition: algo.h:818
bool forward(void)
return true if the actual filtering direction is forward..
Definition: algo.h:866
double StartVAxis
starting boundary of the element currently being calculated..
Definition: algo.h:837
bool WaitForValue
wait for the next value after we had reached exactly the edge of the previous one....
Definition: algo.h:831
bool WaitForFirst
wait for the first data point after construction or after reversing filtering direction....
Definition: algo.h:828
double LastVAxis
last value axis from the previous insertion of a value (pair)..
Definition: algo.h:843
double VAxisStep
step size of the output axis / coordinates, default is 1, e.g. 1 databox, or 1 second....
Definition: algo.h:834
virtual void interpolatedValue(const QVariant value, const QVariant vAxis)
virtual function for a new filtered value pair, will be called as soon as a new value pair could be c...
Definition: algo.h:880
QVariant step(void)
get the step size of the output axis..
Definition: algo.h:855
class MeanAverage in group platformlib
Definition: algo.h:893
SUMMER.
Definition: algo.h:966
The VremMask class Network Mask Utility for Automap.
Definition: algo.h:1014
VremMask(QString ip, QString mask)
Constructor, enter with any IP within the segment used within the Netmask.
Definition: algo.h:1021
const QString Network(void)
returns the network address if ever needed
Definition: algo.h:1094
static const QStringList ListOfSubnetAddresses(QString ip, QString mask)
returns a list of IP addresses based on a netmask
Definition: algo.h:1112
const QString BroadCast(void)
return the boardcast addess which everyone listens
Definition: algo.h:1103
const QStringList SubNet(void)
Returns all the ids allowed in that class C network, within the confines of that netmask.
Definition: algo.h:1088
The smath class in group platformlib.
Definition: algo.h:149
vmath mostly used for clar, does array math
Definition: algo.h:186
static int getRand(int min, int max)
getRand
Definition: algo.h:490
static void displayPointer(T *ptr)
displayPointer
Definition: algo.h:608
void VCast(const QVariantList &si1, T *dst)
VCast.
Definition: algo.h:104
static QVariant pow_xy(QVariant x, int rs)
pow_xy Raises X to power of rs
Definition: algo.h:629
BiIterator2 vCopyBackward(BiIterator1 begin, BiIterator1 end, BiIterator2 dest)
vCopyBackward
Definition: algo.h:55
bool vEqual(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)
vEqual
Definition: algo.h:71
OutputIterator vCopy(InputIterator begin, InputIterator end, OutputIterator dest)
template vCopy in group platformlib copy from one to another
Definition: algo.h:39
void vFill(Container &container, const T &val)
vFill
Definition: algo.h:136
QVariant poly(QVariant x, int degree, QVariantList coeff)
poly retuns the value of y based on degrees of freedom and coeffe
Definition: algo.h:653
Definition: algo.h:190