nmpp
tnmmtr.h
1
2// //
3// Header file of template of class nmmtr //
4// //
6#ifndef _TNMMTR_H_INCLUDED_
7#define _TNMMTR_H_INCLUDED_
8
9//#ifdef __cplusplus
10
11
12#include "tnmvec.h"
13#include <string.h>
14
16//
17// Class of nmmtres
18//
19
20
21 //--------------------------------------------------------------------
31
32 //--------------------------------------------------------------------
35 //--------------------------------------------------------------------
36
37template<class T> class nmmtr
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 nmmtr(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 nmvec<T> operator [](int y) const
68 {
69 ASSERTE(y>=-m_border);
70 ASSERTE(y<m_height+m_border);
71 return nmvec<T>(m_data+y*m_stride,m_width);
72 }
73//#endif
74
75 //nmmtr(const nmmtr<T>& mtr)
77 {
78 m_height=mtr.m_height;
79 m_width =mtr.m_width;
80 m_border=mtr.m_border;
81 m_size =mtr.m_size;
82 m_container=new T[(m_height+2*m_border)*m_width];
83 m_data=m_container+m_width*m_border;
84
85 //m_flag_new=true;
86 m_stride=m_width;
87 for(int y=0; y<m_height; y++)
88 memcpy(m_data+y*m_stride,mtr.m_data+y*mtr.m_stride,m_width*sizeof(T));
89
90 };
91 nmmtr(T* Data,int Height,int Width,int Stride=0):m_height(Height), m_width(Width), m_data(Data),m_stride(Stride),m_border(0) //m_flag_new(FALSE),
92 {
93 if (m_stride==0)
94 m_stride=m_width;
95 m_size =m_height*m_width;
96 m_container=0;
97 };
98
99 nmmtr(const T* Data,int Height,int Width,int Stride=0):m_height(Height), m_width(Width), m_data((T*)Data),m_stride(Stride),m_border(0) //m_flag_new(false),
100 {
101 if (m_stride==0)
102 m_stride=m_width;
103 m_size =m_height*m_width;
104 m_container=0;
105 };
106
107 ~nmmtr()
108 {
109 if (m_container)
110 delete m_container;
111 }
112
113 //nmmtr<T>& operator= (const nmmtr<T>& mtr)
114 nmmtr<T>& operator= ( const nmmtr<T>& mtr)
115 {
116 ASSERTE(mtr.m_height==m_height);
117 ASSERTE(mtr.m_width ==m_width);
118 for(int y=0; y<m_height; y++)
119 (*this)[y]=mtr[y];
120 return *this;
121 }
122
123 nmmtr<T>& operator*= (const nmint<T>& val)
124 {
125 T* pData=m_data;
126 for(int y=0; y< m_height; y++,pData+=m_stride)
127 for(int x=0; x<m_width; x++)
128 pData[x]*=val.m_value;
129 return *this;
130 }
131
132 template <class T2> nmmtr<T2> operator* (const nmint<T2>& val)
133 {
134 nmmtr<T2> mRes(m_height,m_width);
135 T* pResData=mRes.m_data;
136 T* pData =m_data;
137 for(int y=0; y< m_height; y++,pResData+=m_stride,pData+=m_stride)
138 for(int x=0; x<m_width; x++)
139 pResData[x]=pData[x]*val.m_value;
140 return mRes;
141 }
142
143 template <class T2> nmvec<T2> operator* (const nmvec<T2>& vec)
144 {
145 ASSERTE(m_width==vec.m_size);
146 nmvec<T2> vRes(m_height);
147 T* pData =m_data;
148 for(int y=0;y<m_height;y++,pData+=m_stride)
149 {
150 T2 nSum(0);
151 for(int x=0; x<m_width; x++)
152 nSum+=pData[x]*vec.m_data[x];
153 vRes[y]=nSum;
154 }
155 return vRes;
156 }
157
158 template <class T2> nmmtr<T2> operator* (const nmmtr<T2>& mtr)
159 {
160 ASSERTE(m_width==mtr.m_height);
161 nmmtr<T2> mRes(m_height,mtr.m_width);
162 for(int y=0; y<mRes.m_height; y++)
163 for(int x=0; x<mRes.m_width; x++)
164 {
165 T2 sum(0);
166 for(int i=0;i<m_width;i++)
167 sum+=m_data[m_stride*y+i]*mtr.m_data[mtr.m_stride*i+x];
168 mRes.m_data[y*mRes.m_stride+x]=sum;
169 }
170 return mRes;
171 }
172
173 nmvec<T>& operator+= (const nmint<T> &val)
174 {
175 T* pData =m_data;
176 for(int y=0; y< m_height; y++,pData+=m_stride)
177 for(int x=0; x<m_width; x++)
178 pData[x]+=val.m_value;
179 return (*this);
180 }
181
182 nmmtr<T>& operator+= (const nmmtr<T> &mtr)
183 {
184 ASSERTE (mtr.m_size==m_size);
185
186 T* pParData =mtr.m_data;
187 T* pData =m_data;
188 for(int y=0; y< m_height; y++,pParData+=mtr.m_stride,pData+=m_stride)
189 for(int x=0; x<m_width; x++)
190 pData[x]+=pParData[x];
191
192 return (*this);
193 }
194
195 nmmtr<T> operator+ (const nmmtr<T>& mtr) const
196 {
197 ASSERTE (mtr.m_size==m_size);
198 nmmtr<T> res(*this);
199 res+=mtr;
200 return res;
201 }
202
203 nmmtr<T>& operator-= (const nmmtr<T> &mtr)
204 {
205 ASSERTE (mtr.m_size==m_size);
206 T* pParData =mtr.m_data;
207 T* pData =m_data;
208 for(int y=0; y< m_height; y++,pParData+=mtr.m_stride,pData+=m_stride)
209 for(int x=0; x<m_width; x++)
210 pData[x]-=pParData[x];
211 return (*this);
212 }
213
214 nmmtr<T> operator- (const nmmtr<T>& mtr) const
215 {
216 ASSERTE (mtr.m_size==m_size);
217 nmmtr <T> res(*this);
218 res-=mtr;
219 return res;
220 }
221
222 nmmtr<T> operator- () const
223 {
224 nmmtr<T> mRes(m_height,m_width);
225 T* pResData =mRes.m_data;
226 T* pData =m_data;
227 for(int y=0; y< m_height; y++, pResData+=mRes.m_stride, pData+=m_stride)
228 for(int x=0; x<m_width; x++)
229 pResData[x]=-pData[x];
230 return mRes;
231 }
232
233 nmmtr<T>& operator/=(const T val)
234 {
235 //ASSERTE(val.m_value!=0);
236 T* pData =m_data;
237 for(int y=0; y<m_height; y++, pData+=m_stride)
238 for(int x=0; x<m_width; x++)
239 pData[x]/=val;
240
241 return (*this);
242 }
243
244 nmmtr<T> operator/ (const nmint<T> val) const
245 {
246 nmmtr<T> mRes(*this);
247 mRes/=val.m_value;
248 return mRes;
249 }
250
251 nmmtr<T>& operator>>=(const int shr)
252 {
253 ASSERTE(shr>=0);
254 if (shr==0)
255 return(*this);
256 T* pData =m_data;
257 for(int y=0; y<m_height; y++, pData+=m_stride)
258 for(int x=0; x<m_width; x++)
259 pData[x]>>=shr;
260 return (*this);
261 }
262
263 nmmtr<T> operator>> (const int shr) const
264 {
265 ASSERTE(shr>=0);
266 nmmtr<T> mRes(*this);
267 mRes>>=shr;
268 return mRes;
269 }
270
271 nmmtr<T>& operator<<=(const int shl)
272 {
273 ASSERTE(shl>=0);
274 if (shl==0)
275 return(*this);
276 T* pData =m_data;
277 for(int y=0; y<m_height; y++, pData+=m_stride)
278 for(int x=0; x<m_width; x++)
279 pData[x]<<=shl;
280 return (*this);
281 }
282
283 nmmtr<T> operator<< (const int shl) const
284 {
285 ASSERTE(shl>=0);
286 nmmtr<T> mRes(*this);
287 mRes<<=shl;
288 return mRes;
289 }
290
291 nmmtr<T>& operator|= (const nmmtr<T> &mtr)
292 {
293 ASSERTE (mtr.m_size==m_size);
294 T* pParData =mtr.m_data;
295 T* pData =m_data;
296 for(int y=0; y< m_height; y++,pParData+=mtr.m_stride,pData+=m_stride)
297 for(int x=0; x<m_width; x++)
298 pData[x]|=pParData[x];
299 return (*this);
300 }
301
302 nmmtr<T> operator| (const nmmtr<T>& mtr) const
303 {
304 ASSERTE (mtr.m_size==m_size);
305 nmmtr <T> res(*this);
306 res|=mtr;
307 return res;
308 }
309
310 nmmtr<T>& operator&= (const nmmtr<T> &mtr)
311 {
312 ASSERTE (mtr.m_size==m_size);
313 T* pParData =mtr.m_data;
314 T* pData =m_data;
315 for(int y=0; y< m_height; y++,pParData+=mtr.m_stride,pData+=m_stride)
316 for(int x=0; x<m_width; x++)
317 pData[x]&=pParData[x];
318 return (*this);
319 }
320
321 nmmtr<T> operator& (const nmmtr<T>& mtr) const
322 {
323 ASSERTE (mtr.m_size==m_size);
324 nmmtr<T> res(*this);
325 res&=mtr;
326 return res;
327 }
328
329 nmmtr<T>& operator^= (const nmint<T> &val)
330 {
331 T* pData =m_data;
332 for(int y=0; y< m_height; y++,pData+=m_stride)
333 for(int x=0; x<m_width; x++)
334 pData[x]^=val.m_value;
335 return (*this);
336 }
337
338 nmmtr<T> operator^ (const nmint<T>& val) const
339 {
340 nmmtr<T> res(*this);
341 res&=val;
342 return res;
343 }
344
345 nmmtr<T> operator~ () const
346 {
347 nmmtr<T> res(m_height,m_width);
348 T* pResData =res.m_data;
349 T* pData =m_data;
350 for(int y=0; y< m_height; y++,pResData+=res.m_stride,pData+=m_stride)
351 for(int x=0; x<m_width; x++)
352 pResData[x]=~pData[x];
353 return res;
354 }
355
356
357
358 inline T* addr(int y, int x)
359 {
360 return m_data+y*m_stride+x;
361 }
362
363
364 inline nmvec<T> vec(int y)
365 {
366 return nmvec<T>(m_data+y*m_stride,m_width,m_border);
367 }
368
369void fill(nmint<T> &nVal)
370 {
371 T* pData =m_data;
372 for(int y=0; y< m_height; y++, pData+=m_stride)
373 for(int x=0; x<m_width; x++)
374 pData[x]=nVal.m_value;;
375 }
376
377 nmmtr<T> transpose()
378 {
379 nmmtr<T> Z(m_width,m_height);
380 for(int y=0;y<m_height;y++)
381 for(int x=0;x<m_width;x++)
382 Z[x][y]=(*this)[y][x];
383 return Z;
384 }
385
386 template<class T2> void set(nmmtr<T2>& mSrcMtr) const
387 {
388 T* pSrcData=mSrcMtr.m_data;
389 T* pDstData=m_data;
390 for(int y=0; y< m_height; y++, pDstData+=m_stride, pSrcData+=mSrcMtr.m_stride)
391 for(int x=0; x<m_width; x++)
392 *pDstData[x]=pSrcData[x];
393 }
394
395 template<class T2> void set(mtr<T2> &Mtr)
396 {
397 T* pData =m_data;
398 for(int y=0; y< m_height; y++, pData+=m_stride)
399 for(int x=0; x<m_width; x++)
400 pData[x]=Mtr[y][x];
401 }
402
403 void set(const T val)
404 {
405 for(int y=0; y< m_height; y++)
406 vec(y).set(val);
407 }
408
409 void reset()
410 {
411 if (m_width==m_stride)
412 {
413 if (m_container)
414 memset(m_container,0,m_stride*(m_height+m_border*2)*sizeof(T));
415 else
416 memset(m_data,0,m_stride*m_height*sizeof(T));
417 }
418 else
419 {
420 T* p=m_data;
421 for(int y=0;y<m_width;y++,p+=m_stride)
422 memset(p,0,m_width*sizeof(T));
423 }
424 }
425
426};
427
428#ifdef __NM__
429#else
430typedef nmmtr<char> nmmtr8s;
431typedef nmmtr<short> nmmtr16s;
434#endif
435
436typedef nmmtr<int> nmmtr32s;
440
441#ifndef __NM__
442/*
443template <class T> inline ostream& operator<< (ostream& s, nmmtr<T>& mtr)
444{
445 s <<"{\n";
446 for(int y=0;y<mtr.m_height-1;y++)
447 {
448 s << "\t{ ";
449 for(int x=0;x<mtr.m_width-1;x++)
450 {
451 s<< (mtr[y][x]) << ", ";
452 }
453 s << (mtr[y][mtr.m_width-1]) << " },\n";
454 }
455
456 s << "\t{ ";
457 for(int x=0;x<mtr.m_width-1;x++)
458 {
459 s<< (mtr[mtr.m_height-1][x]) << ", ";
460 }
461 s << (mtr[mtr.m_height-1][mtr.m_width-1]) << " }\n";
462 s << "};\n";
463 return s;
464}
465
466
467
468inline ostream& AsmArray (ostream& s, nmmtr64s& mtr)
469{
470 s << " long[" << dec << mtr.m_width <<"*" << mtr.m_height << "]=(\n";
471 for(int y=0;y<mtr.m_height-1;y++)
472 {
473 for(int x=0;x<mtr.m_width;x++)
474 {
475
476 int hi,lo;
477 hi=int(mtr[y][x].m_value>>32);
478 lo=int(mtr[y][x].m_value);
479 s << "0"
480 << hex << setw(8) << setfill('0') << setiosflags(ios::uppercase) << hi << "_"
481 << hex << setw(8) << setfill('0') << setiosflags(ios::uppercase) << lo << "hl,";
482 }
483 s << "\n";
484 }
485
486 for(int x=0;x<mtr.m_width-1;x++)
487 {
488 int hi,lo;
489 hi=int(mtr[mtr.m_height-1][x].m_value>>32);
490 lo=int(mtr[mtr.m_height-1][x].m_value);
491 s << "0"
492 << hex << setw(8) << setfill('0') << setiosflags(ios::uppercase) << hi << "_"
493 << hex << setw(8) << setfill('0') << setiosflags(ios::uppercase) << lo << "hl,";
494 }
495
496 {
497 int hi,lo;
498 hi=int(mtr[mtr.m_height-1][mtr.m_width-1].m_value>>32);
499 lo=int(mtr[mtr.m_height-1][mtr.m_width-1].m_value);
500 s << "0"
501 << hex << setw(8) << setfill('0') << setiosflags(ios::uppercase) << hi << "_"
502 << hex << setw(8) << setfill('0') << setiosflags(ios::uppercase) << lo << "hl";
503 }
504 s << "\n);\n";
505
506 return s;
507}
508
509inline ostream& AsmArray (ostream& s, nmmtr8s& mtr)
510{
511 ASSERTE(mtr.m_width%8==0);
512 nmmtr64s mClone((__int64*)mtr.m_data,mtr.m_height,mtr.m_width/8,mtr.m_stride/8);
513 AsmArray(s,mClone);
514 return s;
515}
516*/
517 template<class T1, class T2> void DotMul(nmmtr<T1>& mSrcMtr1, nmmtr<T2>& mSrcMtr2, nmmtr<T2>& mDstMtr )
518 {
519 for(int y=0; y< mSrcMtr1.m_height; y++)
520 for(int x=0; x<mSrcMtr1.m_width; x++)
521 {
522 nmint<T2> res=mSrcMtr1[y][x]*mSrcMtr2[y][x];
523 mDstMtr[y][x]=res;
524 }
525 }
526
527 template<class T1, class T2> void GetSum(nmmtr<T1>& mSrcMtr1, nmint<T2>& nResSum)
528 {
529 nResSum=0;
530 for(int y=0; y< mSrcMtr1.m_height; y++)
531 for(int x=0; x<mSrcMtr1.m_width; x++)
532 nResSum+=nmint<T2> (mSrcMtr1[y][x].m_value);
533 }
534
535#endif
536
537#endif
538
539
540
541// template<class T2> void SetData(T2* Data)
542// {
543// for(int i=0;i<m_size;i++)
544// m_data[i]=Data[i];
545// }
546//
547// template<class T2> void GetData(T2* Data)
548// {
549// for(int i=0;i<m_size;i++)
550// Data[i]=(T2)m_data[i].value;
551// }
552
553// inline nmvec<T> GetVec(int y)
554// {
555// ASSERTE(y>=0);
556// ASSERTE(y<m_height);
557// return nmvec<T>(m_data+y*m_stride,m_width);
558// }
559//
560// inline nmvec<T> GetVec(int y,int x,int len)
561// {
562// ASSERTE(y>=0);
563// ASSERTE(y<m_height);
564// return nmvec<T>(m_data+y*m_stride+x,len);
565// }
566//
567// inline nmmtr<T>& SetMtr(int y,int x,nmmtr<T>& mSrc)
568// {
569// T* dst=m_data+y*m_width+x;
570// T* src=mSrc.m_data;
571// for(int i=0;i<mSrc.m_height;i++)
572// {
573// memcpy(dst,src,mSrc.m_stride*sizeof(T));
574// dst+=m_stride;
575// src+=mSrc.m_stride;
576// }
577// return *this;
578// }
579//
580// inline nmmtr<T>& GetMtr(int y,int x,nmmtr<T>& mRes)
581// {
582// T* src=m_data+y*m_stride+x;
583// T* dst=mRes.m_data;
584// for(int i=0;i<mRes.m_height;i++)
585// {
586// memcpy(dst,src,mRes.m_stride*sizeof(T));
587// dst+=mRes.m_stride;
588// src+=m_stride;
589// }
590// return mRes;
591// }
592//
593// inline nmmtr<T> GetMtr(int y,int x,int height,int width)
594// {
595// nmmtr<T> Res(height,width);
596// GetMtr(y,x,Res);
597// return Res;
598// }
599
600 // template<class T2> nmmtr<T2>& DotMul(const nmmtr<T2>& mMtr,nmmtr<T2>& mRes) const
601// {
602// T2* pMtrData =mMtr.m_data;
603// T2* pResData =mRes.m_data;
604// T* pData =m_data;
605// for(int y=0; y< m_height; y++, pResData+=mRes.m_stride, pMtrData+=mMtr.m_stride,pData +=m_stride)
606// for(int x=0; x<m_width; x++)
607// pResData[x]=pData[x]*pMtrData[x];
608// return mRes;
609// }
610//
611// template<class T2> nmmtr<T2> DotMul(const nmmtr<T2>& Mtr) const
612// {
613// nmmtr<T2> Res;
614// DotMul(Mtr,Res);
615// return Res;
616// }
617//
618// template<class T2> void GetSum(nmint<T2>& nRes) const
619// {
620// nRes=0;
621// T* pData =m_data;
622// for(int y=0; y< m_height; y++, pData+=m_stride)
623// for(int x=0; x<m_width; x++)
624// nRes+=pData[x];
625//
626// return nRes;
627// }
628
629// template<class T2> nmmtr<T>& DotMul(nmmtr<T2>& mMtr1, nmmtr<T>& mMtr2)
630// {
631// T2* pMtr1 =mMtr1.m_data;
632// T* pMtr2 =mMtr2.m_data;
633// T* pMtrRes=m_data;
634// for(int y=0; y< m_height; y++, pMtr1+=mMtr1.m_stride, pMtr2+=mMtr2.m_stride,pMtrRes+=m_stride)
635// for(int x=0; x<m_width; x++)
636// pMtrRes[x]=pMtr1[x]*pMtr2[x];
637// return (*this);
638// }
639
640// template<class T2> nmmtr<T2>& Convert(nmmtr<T2>& mRes) const
641// {
642// T* pData =m_data;
643// T* pResData=mRes.m_data;
644// for(int y=0; y< m_height; y++, pData+=m_stride, pResData+=mRes.m_stride)
645// for(int x=0; x<m_width; x++)
646// pResData[x]=pData[x];
647// return mRes;
648// }
649
650
651//#endif
Definition: tmatrix.h:88
Definition: tnmmtr.h:38
Definition: tvector.h:80