LRAUV  revA
Mtx.h
Go to the documentation of this file.
1 /*
2  * Mtx.h
3  *
4  * reads in 2D matrix "mtx" file where first two int32 are the
5  * dimensions, and the rest is a column major matrix of
6  * single precision.
7  *
8  * Created on: Feb 15, 2012
9  * Author: godin
10  */
11 
12 #ifndef MTX_H_
13 #define MTX_H_
14 
15 #include "data/I2D.h"
16 #include "logger/Logger.h"
17 #include "logger/Syslog.h"
18 
19 #include <math.h>
20 #include <sys/types.h>
21 
22 class MtxInstream;
23 
24 class Mtx: public I2D<float>
25 {
26 public:
27  Mtx( Logger& logger, int m = 0, int n = 0, float initVal = nanf( "" ), Syslog::Severity severity = Syslog::FAULT );
28  Mtx( Logger& logger, int m, int n, float* ptr, Syslog::Severity severity = Syslog::FAULT );
29  Mtx( const char* filename, Logger& logger, Syslog::Severity severity = Syslog::FAULT );
30  Mtx( const char* filename, Logger& logger, int minN, int maxN, Syslog::Severity severity = Syslog::FAULT );
31  Mtx( const Mtx& rhs );
32  virtual ~Mtx();
33  void setSize( int m, int n );
34  void clear();
35  Mtx& operator=( const Mtx& rhs );
36  Mtx& operator*=( const float rhs );
37  Mtx& operator+=( const Mtx& rhs );
38  Mtx& operator+=( const float rhs );
39  Mtx& operator-=( const Mtx& rhs );
40  Mtx operator*( const Mtx& rhs ) const;
41 
42  int getM() const
43  {
44  return m_;
45  }
46 
47  virtual int getN() const
48  {
49  return n_;
50  }
51 
52  virtual float** getPtr2d( void )
53  {
54  return value_;
55  }
56 
57  virtual float* getPtr1d( void )
58  {
59  return value_[0];
60  }
61 
62  float* operator[]( int index )
63  {
65  return value_[index];
66  };
67 
68  void set( int indexM, int indexN, float value )
69  {
70  if( indexM >= m_ || m_ + indexM < 0 || indexN >= n_ || n_ + indexN < 0 )
71  {
72  return ;
73  }
74  value_[indexM][indexN] = value;
75  }
76 
77  void setInt( int indexM, int indexN, int value )
78  {
79  if( indexM >= m_ || m_ + indexM < 0 || indexN >= n_ || n_ + indexN < 0 )
80  {
81  return ;
82  }
83  FloatInt floatInt;
84  floatInt.int_ = value;
85  value_[indexM][indexN] = floatInt.float_;
86  }
87 
88  float get( int indexM, int indexN ) const
89  {
90  if( indexM >= m_ || m_ + indexM < 0 || indexN >= n_ || n_ + indexN < 0 )
91  {
92  return nanf( "" );
93  }
94  return value_[indexM][indexN];
95  }
96 
97  int getInt( int indexM, int indexN ) const
98  {
99  if( indexM >= m_ || m_ + indexM < 0 || indexN >= n_ || n_ + indexN < 0 )
100  {
101  return 0;
102  }
103  FloatInt floatInt;
104  floatInt.float_ = value_[indexM][indexN];
105  return floatInt.int_;
106  }
107 
108  void setFromPtr( const int m, const int n, const void* ptr )
109  {
110  setSize( m, n );
111  memcpy( value_[0], ptr, m * n * sizeof( float ) ); // Don't usually like working at the level of memcpy, but it seems the most straightforward way to do this.
112 
113  }
114 
115  float operator()( int indexM, int indexN ) const
116  {
117  if( indexM >= m_ || m_ + indexM < 0 || indexN >= n_ || n_ + indexN < 0 )
118  {
119  return nanf( "" );
120  }
121  return value_[indexM >= 0 ? indexM : m_ + indexM][indexN >= 0 ? indexN : n_ + indexN];
122  };
123 
124  Mtx subset( int minM = 0, int maxM = -1, int minN = 0, int maxN = -1 ) const;
125 
126  static bool CopyValues( const Mtx& src, int srcStartM, int srcStartN, const Mtx& dst, int dstStartM, int dstStartN, int numM, int numN );
127 
128  bool write( const char* filename, Logger& logger,
129  int minM = 0, int maxM = -1, int minN = 0, int maxN = -1,
130  Syslog::Severity severity = Syslog::FAULT );
131 
132  bool append( const char* filename, Logger& logger,
133  Syslog::Severity severity = Syslog::FAULT );
134 
135  bool valid() const
136  {
137  return m_ > 0 && n_ > 0;
138  }
139 
140  bool isUnlimitedN() const
141  {
142  return unlimitedN_;
143  }
144 
145  void setUnlimitedN( bool unlimitedN )
146  {
147  unlimitedN_ = unlimitedN;
148  }
149 
152  bool shellSort( int ( &comp )( const Mtx&, int, const Mtx&, int ) );
153 
155  static int Comp0( const Mtx& lhs, int lhsJ, const Mtx& rhs, int rhsJ );
156 
158  static int Comp1( const Mtx& lhs, int lhsJ, const Mtx& rhs, int rhsJ );
159 
161  static int Comp2( const Mtx& lhs, int lhsJ, const Mtx& rhs, int rhsJ );
162 
164  static int Comp01( const Mtx& lhs, int lhsJ, const Mtx& rhs, int rhsJ );
165 
167  static int Comp02( const Mtx& lhs, int lhsJ, const Mtx& rhs, int rhsJ );
168 
169 
170 protected:
172  int m_;
173  int n_;
174  float** value_;
178  union FloatInt
179  {
180  float float_;
181  int32_t int_;
182  };
183 };
184 
185 #endif /* MTX_H_ */
Client-side interface for injecting log data into the log queue.
Definition: Logger.h:30
Mtx & operator=(const Mtx &rhs)
Definition: Mtx.cpp:112
int n_
Definition: Mtx.h:173
Mtx subset(int minM=0, int maxM=-1, int minN=0, int maxN=-1) const
Definition: Mtx.cpp:192
virtual float ** getPtr2d(void)
Definition: Mtx.h:52
static int Comp02(const Mtx &lhs, int lhsJ, const Mtx &rhs, int rhsJ)
Comparison function for indexM = 0 AND indexM = 2;.
Definition: Mtx.cpp:376
void clear()
Definition: Mtx.cpp:234
Mtx & operator+=(const Mtx &rhs)
Definition: Mtx.cpp:145
virtual ~Mtx()
Definition: Mtx.cpp:85
Mtx & operator*=(const float rhs)
Definition: Mtx.cpp:132
void set(int indexM, int indexN, float value)
Definition: Mtx.h:68
void setFromPtr(const int m, const int n, const void *ptr)
Definition: Mtx.h:108
Mtx & operator-=(const Mtx &rhs)
Definition: Mtx.cpp:175
bool write(const char *filename, Logger &logger, int minM=0, int maxM=-1, int minN=0, int maxN=-1, Syslog::Severity severity=Syslog::FAULT)
Definition: Mtx.cpp:211
bool append(const char *filename, Logger &logger, Syslog::Severity severity=Syslog::FAULT)
Definition: Mtx.cpp:223
float float_
Definition: Mtx.h:180
float * operator[](int index)
Definition: Mtx.h:62
void setInt(int indexM, int indexN, int value)
Definition: Mtx.h:77
int getM() const
Definition: Mtx.h:42
Definition: Mtx.h:178
int m_
Definition: Mtx.h:172
Definition: Mtx.h:24
bool isUnlimitedN() const
Definition: Mtx.h:140
Contains the Syslog class definition.
Logger & logger_
Definition: Mtx.h:175
static int Comp01(const Mtx &lhs, int lhsJ, const Mtx &rhs, int rhsJ)
Comparison function for indexM = 0 AND indexM = 1;.
Definition: Mtx.cpp:365
Mtx operator*(const Mtx &rhs) const
Definition: Mtx.cpp:267
float operator()(int indexM, int indexN) const
Definition: Mtx.h:115
virtual float * getPtr1d(void)
Definition: Mtx.h:57
float ** value_
Definition: Mtx.h:174
static int Comp2(const Mtx &lhs, int lhsJ, const Mtx &rhs, int rhsJ)
Comparison function for indexM = 2;.
Definition: Mtx.cpp:359
Mtx(Logger &logger, int m=0, int n=0, float initVal=nanf(""), Syslog::Severity severity=Syslog::FAULT)
Definition: Mtx.cpp:12
Definition: I2D.h:17
int getInt(int indexM, int indexN) const
Definition: Mtx.h:97
static bool CopyValues(const Mtx &src, int srcStartM, int srcStartN, const Mtx &dst, int dstStartM, int dstStartN, int numM, int numN)
Definition: Mtx.cpp:326
Contains the Logger class definition.
int32_t int_
Definition: Mtx.h:181
static int Comp1(const Mtx &lhs, int lhsJ, const Mtx &rhs, int rhsJ)
Comparison function for indexM = 1;.
Definition: Mtx.cpp:353
bool unlimitedN_
Definition: Mtx.h:177
static int Comp0(const Mtx &lhs, int lhsJ, const Mtx &rhs, int rhsJ)
Comparison function for indexM = 0;.
Definition: Mtx.cpp:347
bool valid() const
Definition: Mtx.h:135
void prepareValueForModification()
Definition: Mtx.cpp:252
Severity
An attempt to define different levels of syslog severity.
Definition: Syslog.h:28
virtual int getN() const
Definition: Mtx.h:47
Syslog::Severity severity_
Definition: Mtx.h:176
void setUnlimitedN(bool unlimitedN)
Definition: Mtx.h:145
Definition: Syslog.h:34
bool shellSort(int(&comp)(const Mtx &, int, const Mtx &, int))
Simple sort routine, invented by Donald Shell 1959.
Definition: Mtx.cpp:298
void setSize(int m, int n)
Definition: Mtx.cpp:90