nmpp
tnmmtrpack.h
1
2// //
3// Header file of template of class nmmtrpack //
4// //
6#ifndef _Tnmmtrpack_H_INCLUDED_
7#define _Tnmmtrpack_H_INCLUDED_
8
9
10
11
12#include "tnmvec.h"
13#include <string.h>
14
16//
17// Class of nmmtrpackes
18//
19
20 //--------------------------------------------------------------------
30 //--------------------------------------------------------------------
31
32 //--------------------------------------------------------------------
35 //--------------------------------------------------------------------
36
37template<class T> class nmmtrpack
38{
39protected:
40 T* m_container;
41 bool m_flag_new;
42public:
43 int m_height,m_width,m_size,m_stride,m_border;
44 T* m_data;
45
46 nmmtrpack(int Height, int Width,int Border=0):m_height(Height), m_width(Width),m_flag_new(true)
47 {
48 m_border=Border;
49 //m_container=new T[(m_height+2*m_border)*m_width];
50 //m_data=m_container+m_width*m_border;
51 nmppsMalloc_64s(&m_container,(m_height+2*m_border)*m_width);
52 m_data=nmppsAddr_(m_container,m_border*m_width);
53 m_size=m_width*m_height;
54 m_stride=m_width;
55 _ASSERTE(m_data!=NULL);
56 }
57
58//#ifndef NMTL_DISABLE_INDEX_CHECK
59// __INLINE__ nmint<T>* operator[](int y)
60// {
61// _ASSERTE(y>=-m_border);
62// _ASSERTE(y<m_height+m_border);
63//
64// return (nmint<T>*) (m_data+y*m_stride);
65// }
66//#else
67 __INLINE__ nmvecpack<T> operator [](int y) const
68 {
69 _ASSERTE(y>=-m_border);
70 _ASSERTE(y<m_height+m_border);
71 return nmvecpack<T>(nmppsAddr_(m_data,y*m_stride),m_width);
72 }
73//#endif
74
76 {
77 m_height=mtr.m_height;
78 m_width =mtr.m_width;
79 m_border=mtr.m_border;
80 m_size =mtr.m_size;
81 //m_container=new T[(m_height+2*m_border)*m_width];
82 //m_data=m_container+m_width*m_border;
83 nmppsMalloc_64s(&m_container,(m_height+2*m_border)*m_width);
84 m_data=nmppsAddr_(m_container,m_border*m_width);
85
86 m_flag_new=true;
87 m_stride=m_width;
88 for(int y=0; y<m_height; y++)
89 //memcpy(m_data+y*m_stride,mtr.m_data+y*mtr.m_stride,m_width*sizeof(T));
90 //nmppsCopy_(nmppsAddr_(mtr.m_data),y*mtr.m_stride)
91 (*this)[y]=mtr[y];
92
93 };
94 nmmtrpack(T* Data,int Height,int Width,int Stride=0):m_height(Height), m_width(Width), m_data(Data),m_flag_new(false),m_stride(Stride),m_border(0)
95 {
96 if (m_stride==0)
97 m_stride=m_width;
98 m_size =m_height*m_width;
99 m_container=0;
100 };
101
102 nmmtrpack(const T* Data,int Height,int Width,int Stride=0):m_height(Height), m_width(Width), m_data((T*)Data),m_flag_new(false),m_stride(Stride),m_border(0)
103 {
104 if (m_stride==0)
105 m_stride=m_width;
106 m_size =m_height*m_width;
107 m_container=0;
108 };
109
110 ~nmmtrpack()
111 {
112 if (m_flag_new)
113 //delete m_container;
114 nmppsFree(m_container);
115 }
116
117 nmmtrpack<T>& operator= (const nmmtrpack<T>& mtr)
118 {
119 _ASSERTE(mtr.m_height==m_height);
120 _ASSERTE(mtr.m_width ==m_width);
121 for(int y=0; y<m_height; y++)
122 (*this)[y]=mtr[y];
123 return *this;
124 }
125
126 nmmtrpack<T>& operator*= (const nmint<T>& val)
127 {
128 T* pData=m_data;
129 for(int y=0; y< m_height; y++,pData+=m_stride)
130 for(int x=0; x<m_width; x++)
131 pData[x]*=val.m_value;
132 return *this;
133 }
134
135 template <class T2> nmmtrpack<T2> operator* (const nmint<T2>& val)
136 {
137 nmmtrpack<T2> mRes(m_height,m_width);
138 T* pResData=mRes.m_data;
139 T* pData =m_data;
140 for(int y=0; y< m_height; y++,pResData+=m_stride,pData+=m_stride)
141 for(int x=0; x<m_width; x++)
142 pResData[x]=pData[x]*val.m_value;
143 return mRes;
144 }
145
146 template <class T2> nmvec<T2> operator* (const nmvec<T2>& vec)
147 {
148 _ASSERTE(m_width==vec.m_size);
149 nmvec<T2> vRes(m_height);
150 T* pData =m_data;
151 for(int y=0;y<m_height;y++,pData+=m_stride)
152 {
153 T2 nSum(0);
154 for(int x=0; x<m_width; x++)
155 nSum+=pData[x]*vec.m_data[x];
156 vRes[y]=nSum;
157 }
158 return vRes;
159 }
160
161 template <class T2> nmmtrpack<T2> operator* (const nmmtrpack<T2>& mtr)
162 {
163 _ASSERTE(m_width==mtr.m_height);
164 nmmtrpack<T2> mRes(m_height,mtr.m_width);
165 for(int y=0; y<mRes.m_height; y++)
166 for(int x=0; x<mRes.m_width; x++)
167 {
168 T2 sum(0);
169 for(int i=0;i<m_width;i++)
170 sum+=m_data[m_stride*y+i]*mtr.m_data[mtr.m_stride*i+x];
171 mRes.m_data[y*mRes.m_stride+x]=sum;
172 }
173 return mRes;
174 }
175
176 nmvec<T>& operator+= (const nmint<T> &val)
177 {
178 T* pData =m_data;
179 for(int y=0; y< m_height; y++,pData+=m_stride)
180 for(int x=0; x<m_width; x++)
181 pData[x]+=val.m_value;
182 return (*this);
183 }
184
185 nmmtrpack<T>& operator+= (const nmmtrpack<T> &mtr)
186 {
187 _ASSERTE (mtr.m_size==m_size);
188
189 T* pParData =mtr.m_data;
190 T* pData =m_data;
191 for(int y=0; y< m_height; y++,pParData+=mtr.m_stride,pData+=m_stride)
192 for(int x=0; x<m_width; x++)
193 pData[x]+=pParData[x];
194
195 return (*this);
196 }
197
198 nmmtrpack<T> operator+ (const nmmtrpack<T>& mtr) const
199 {
200 _ASSERTE (mtr.m_size==m_size);
201 nmmtrpack<T> res(*this);
202 res+=mtr;
203 return res;
204 }
205
206 nmmtrpack<T>& operator-= (const nmmtrpack<T> &mtr)
207 {
208 _ASSERTE (mtr.m_size==m_size);
209 T* pParData =mtr.m_data;
210 T* pData =m_data;
211 for(int y=0; y< m_height; y++,pParData+=mtr.m_stride,pData+=m_stride)
212 for(int x=0; x<m_width; x++)
213 pData[x]-=pParData[x];
214 return (*this);
215 }
216
217 nmmtrpack<T> operator- (const nmmtrpack<T>& mtr) const
218 {
219 _ASSERTE (mtr.m_size==m_size);
220 nmmtrpack <T> res(*this);
221 res-=mtr;
222 return res;
223 }
224
225 nmmtrpack<T> operator- () const
226 {
227 nmmtrpack<T> mRes(m_height,m_width);
228 T* pResData =mRes.m_data;
229 T* pData =m_data;
230 for(int y=0; y< m_height; y++, pResData+=mRes.m_stride, pData+=m_stride)
231 for(int x=0; x<m_width; x++)
232 pResData[x]=-pData[x];
233 return mRes;
234 }
235
236 nmmtrpack<T>& operator/=(const T val)
237 {
238 //_ASSERTE(val.m_value!=0);
239 T* pData =m_data;
240 for(int y=0; y<m_height; y++, pData+=m_stride)
241 for(int x=0; x<m_width; x++)
242 pData[x]/=val;
243
244 return (*this);
245 }
246
247 nmmtrpack<T> operator/ (const nmint<T> val) const
248 {
249 nmmtrpack<T> mRes(*this);
250 mRes/=val.m_value;
251 return mRes;
252 }
253
254 nmmtrpack<T>& operator>>=(const int shr)
255 {
256 _ASSERTE(shr>=0);
257 if (shr==0)
258 return(*this);
259 T* pData =m_data;
260 for(int y=0; y<m_height; y++, pData+=m_stride)
261 for(int x=0; x<m_width; x++)
262 pData[x]>>=shr;
263 return (*this);
264 }
265
266 nmmtrpack<T> operator>> (const int shr) const
267 {
268 _ASSERTE(shr>=0);
269 nmmtrpack<T> mRes(*this);
270 mRes>>=shr;
271 return mRes;
272 }
273
274 nmmtrpack<T>& operator<<=(const int shl)
275 {
276 _ASSERTE(shl>=0);
277 if (shl==0)
278 return(*this);
279 T* pData =m_data;
280 for(int y=0; y<m_height; y++, pData+=m_stride)
281 for(int x=0; x<m_width; x++)
282 pData[x]<<=shl;
283 return (*this);
284 }
285
286 nmmtrpack<T> operator<< (const int shl) const
287 {
288 _ASSERTE(shl>=0);
289 nmmtrpack<T> mRes(*this);
290 mRes<<=shl;
291 return mRes;
292 }
293
294 nmmtrpack<T>& operator|= (const nmmtrpack<T> &mtr)
295 {
296 _ASSERTE (mtr.m_size==m_size);
297 T* pParData =mtr.m_data;
298 T* pData =m_data;
299 for(int y=0; y< m_height; y++,pParData+=mtr.m_stride,pData+=m_stride)
300 for(int x=0; x<m_width; x++)
301 pData[x]|=pParData[x];
302 return (*this);
303 }
304
305 nmmtrpack<T> operator| (const nmmtrpack<T>& mtr) const
306 {
307 _ASSERTE (mtr.m_size==m_size);
308 nmmtrpack <T> res(*this);
309 res|=mtr;
310 return res;
311 }
312
313 nmmtrpack<T>& operator&= (const nmmtrpack<T> &mtr)
314 {
315 _ASSERTE (mtr.m_size==m_size);
316 T* pParData =mtr.m_data;
317 T* pData =m_data;
318 for(int y=0; y< m_height; y++,pParData+=mtr.m_stride,pData+=m_stride)
319 for(int x=0; x<m_width; x++)
320 pData[x]&=pParData[x];
321 return (*this);
322 }
323
324 nmmtrpack<T> operator& (const nmmtrpack<T>& mtr) const
325 {
326 _ASSERTE (mtr.m_size==m_size);
327 nmmtrpack<T> res(*this);
328 res&=mtr;
329 return res;
330 }
331
332 nmmtrpack<T>& operator^= (const nmint<T> &val)
333 {
334 T* pData =m_data;
335 for(int y=0; y< m_height; y++,pData+=m_stride)
336 for(int x=0; x<m_width; x++)
337 pData[x]^=val.m_value;
338 return (*this);
339 }
340
341 nmmtrpack<T> operator^ (const nmint<T>& val) const
342 {
343 nmmtrpack<T> res(*this);
344 res&=val;
345 return res;
346 }
347
348 nmmtrpack<T> operator~ () const
349 {
350 nmmtrpack<T> res(m_height,m_width);
351 T* pResData =res.m_data;
352 T* pData =m_data;
353 for(int y=0; y< m_height; y++,pResData+=res.m_stride,pData+=m_stride)
354 for(int x=0; x<m_width; x++)
355 pResData[x]=~pData[x];
356 return res;
357 }
358
359
360// template<class T2> void SetData(T2* Data)
361// {
362// for(int i=0;i<m_size;i++)
363// m_data[i]=Data[i];
364// }
365//
366// template<class T2> void GetData(T2* Data)
367// {
368// for(int i=0;i<m_size;i++)
369// Data[i]=(T2)m_data[i].value;
370// }
371
372 __INLINE__ nmvec<T> GetVec(int y)
373 {
374 _ASSERTE(y>=0);
375 _ASSERTE(y<m_height);
376 return nmvec<T>(m_data+y*m_stride,m_width);
377 }
378
379 __INLINE__ nmvec<T> GetVec(int y,int x,int len)
380 {
381 _ASSERTE(y>=0);
382 _ASSERTE(y<m_height);
383 return nmvec<T>(m_data+y*m_stride+x,len);
384 }
385
386 __INLINE__ nmmtrpack<T>& SetMtr(int y,int x,nmmtrpack<T>& mSrc)
387 {
388 T* dst=m_data+y*m_width+x;
389 T* src=mSrc.m_data;
390 for(int i=0;i<mSrc.m_height;i++)
391 {
392 memcpy(dst,src,mSrc.m_stride*sizeof(T));
393 dst+=m_stride;
394 src+=mSrc.m_stride;
395 }
396 return *this;
397 }
398
399 __INLINE__ nmmtrpack<T>& GetMtr(int y,int x,nmmtrpack<T>& mRes)
400 {
401 T* src=m_data+y*m_stride+x;
402 T* dst=mRes.m_data;
403 for(int i=0;i<mRes.m_height;i++)
404 {
405 memcpy(dst,src,mRes.m_stride*sizeof(T));
406 dst+=mRes.m_stride;
407 src+=m_stride;
408 }
409 return mRes;
410 }
411
412 __INLINE__ nmmtrpack<T> GetMtr(int y,int x,int height,int width)
413 {
414 nmmtrpack<T> Res(height,width);
415 GetMtr(y,x,Res);
416 return Res;
417 }
418
419 __INLINE__ T* Addr(int y, int x)
420 {
421 return m_data+y*m_stride+x;
422 }
423
424// template<class T2> nmmtrpack<T2>& DotMul(const nmmtrpack<T2>& mMtr,nmmtrpack<T2>& mRes) const
425// {
426// T2* pMtrData =mMtr.m_data;
427// T2* pResData =mRes.m_data;
428// T* pData =m_data;
429// for(int y=0; y< m_height; y++, pResData+=mRes.m_stride, pMtrData+=mMtr.m_stride,pData +=m_stride)
430// for(int x=0; x<m_width; x++)
431// pResData[x]=pData[x]*pMtrData[x];
432// return mRes;
433// }
434//
435// template<class T2> nmmtrpack<T2> DotMul(const nmmtrpack<T2>& Mtr) const
436// {
437// nmmtrpack<T2> Res;
438// DotMul(Mtr,Res);
439// return Res;
440// }
441//
442// template<class T2> void GetSum(nmint<T2>& nRes) const
443// {
444// nRes=0;
445// T* pData =m_data;
446// for(int y=0; y< m_height; y++, pData+=m_stride)
447// for(int x=0; x<m_width; x++)
448// nRes+=pData[x];
449//
450// return nRes;
451// }
452
453// template<class T2> nmmtrpack<T>& DotMul(nmmtrpack<T2>& mMtr1, nmmtrpack<T>& mMtr2)
454// {
455// T2* pMtr1 =mMtr1.m_data;
456// T* pMtr2 =mMtr2.m_data;
457// T* pMtrRes=m_data;
458// for(int y=0; y< m_height; y++, pMtr1+=mMtr1.m_stride, pMtr2+=mMtr2.m_stride,pMtrRes+=m_stride)
459// for(int x=0; x<m_width; x++)
460// pMtrRes[x]=pMtr1[x]*pMtr2[x];
461// return (*this);
462// }
463
464// template<class T2> nmmtrpack<T2>& Convert(nmmtrpack<T2>& mRes) const
465// {
466// T* pData =m_data;
467// T* pResData=mRes.m_data;
468// for(int y=0; y< m_height; y++, pData+=m_stride, pResData+=mRes.m_stride)
469// for(int x=0; x<m_width; x++)
470// pResData[x]=pData[x];
471// return mRes;
472// }
473
474 template<class T2> void Set(nmmtrpack<T2>& mSrcMtr) const
475 {
476 T* pSrcData=mSrcMtr.m_data;
477 T* pDstData=m_data;
478 for(int y=0; y< m_height; y++, pDstData+=m_stride, pSrcData+=mSrcMtr.m_stride)
479 for(int x=0; x<m_width; x++)
480 *pDstData[x]=pSrcData[x];
481 }
482
483
484 void InitConst(nmint<T> &nVal)
485 {
486 T* pData =m_data;
487 for(int y=0; y< m_height; y++, pData+=m_stride)
488 for(int x=0; x<m_width; x++)
489 pData[x]=nVal.m_value;;
490 }
491
492 nmmtrpack<T> transpose()
493 {
494 nmmtrpack<T> Z(m_width,m_height);
495 for(int y=0;y<m_height;y++)
496 for(int x=0;x<m_width;x++)
497 Z[x][y]=(*this)[y][x];
498 return Z;
499 }
500
501
502// template<class T2> void SetMatrix(mtr<T2> &Mtr)
503// {
504// T* pData =m_data;
505// for(int y=0; y< m_height; y++, pData+=m_stride)
506// for(int x=0; x<m_width; x++)
507// pData[x]=Mtr[y][x];
508// }
509
510 void reset()
511 {
512 if (m_width==m_stride)
513 {
514 if (m_container)
515 memset(m_container,0,m_stride*(m_height+m_border*2)*sizeof(T));
516 else
517 memset(m_data,0,m_stride*m_height*sizeof(T));
518 }
519 else
520 {
521 T* p=m_data;
522 for(int y=0;y<m_width;y++,p+=m_stride)
523 memset(p,0,m_width*sizeof(T));
524 }
525 }
526
527};
528
529typedef nmmtrpack<nm1> nmmtr1;
534
535#ifndef WIN32
540#endif
541
542
543
544
545
546
547# endif
548
Definition: tmatrix.h:88
Definition: tnmmtr.h:38
Definition: tnmmtrpack.h:38
Definition: tnmvecpack.h:138
Definition: tvector.h:80
#define GetVec
Definition: tmatrix.h:60