*** empty log message ***
This commit is contained in:
111
vec.h
111
vec.h
@@ -1,6 +1,8 @@
|
||||
#ifndef _LA_VEC_H_
|
||||
#define _LA_VEC_H_
|
||||
|
||||
|
||||
|
||||
extern "C" {
|
||||
#include "cblas.h"
|
||||
}
|
||||
@@ -44,6 +46,7 @@ template<class T> \
|
||||
#include "smat.h"
|
||||
#include "mat.h"
|
||||
|
||||
|
||||
// NRVec class
|
||||
template <typename T>
|
||||
class NRVec {
|
||||
@@ -438,5 +441,113 @@ NRVECMAT_OPER2(Vec,-)
|
||||
|
||||
// Few forward declarations
|
||||
|
||||
//basic stuff which has to be in .h
|
||||
// dtor
|
||||
template <typename T>
|
||||
NRVec<T>::~NRVec()
|
||||
{
|
||||
if(!count) return;
|
||||
if(--(*count) <= 0) {
|
||||
if(v) delete[] (v);
|
||||
delete count;
|
||||
}
|
||||
}
|
||||
|
||||
// detach from a physical vector and make own copy
|
||||
template <typename T>
|
||||
void NRVec<T>::copyonwrite()
|
||||
{
|
||||
#ifdef DEBUG
|
||||
if(!count) laerror("probably an assignment to undefined vector");
|
||||
#endif
|
||||
if(*count > 1)
|
||||
{
|
||||
(*count)--;
|
||||
count = new int;
|
||||
*count = 1;
|
||||
T *newv = new T[nn];
|
||||
memcpy(newv, v, nn*sizeof(T));
|
||||
v = newv;
|
||||
}
|
||||
}
|
||||
|
||||
// Asignment
|
||||
template <typename T>
|
||||
NRVec<T> & NRVec<T>::operator=(const NRVec<T> &rhs)
|
||||
{
|
||||
if (this != &rhs)
|
||||
{
|
||||
if(count)
|
||||
if(--(*count) == 0)
|
||||
{
|
||||
delete[] v;
|
||||
delete count;
|
||||
}
|
||||
v = rhs.v;
|
||||
nn = rhs.nn;
|
||||
count = rhs.count;
|
||||
if(count) (*count)++;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Resize
|
||||
template <typename T>
|
||||
void NRVec<T>::resize(const int n)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
if(n<=0) laerror("illegal vector dimension");
|
||||
#endif
|
||||
if(count)
|
||||
if(*count > 1) {
|
||||
(*count)--;
|
||||
count = 0;
|
||||
v = 0;
|
||||
nn = 0;
|
||||
}
|
||||
if(!count) {
|
||||
count = new int;
|
||||
*count = 1;
|
||||
nn = n;
|
||||
v = new T[nn];
|
||||
return;
|
||||
}
|
||||
// *count = 1 in this branch
|
||||
if (n != nn) {
|
||||
nn = n;
|
||||
delete[] v;
|
||||
v = new T[nn];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// assignmet with a physical copy
|
||||
template <typename T>
|
||||
NRVec<T> & NRVec<T>::operator|=(const NRVec<T> &rhs)
|
||||
{
|
||||
if (this != &rhs) {
|
||||
#ifdef DEBUG
|
||||
if (!rhs.v) laerror("unallocated rhs in NRVec operator |=");
|
||||
#endif
|
||||
if (count)
|
||||
if (*count > 1) {
|
||||
--(*count);
|
||||
nn = 0;
|
||||
count = 0;
|
||||
v = 0;
|
||||
}
|
||||
if (nn != rhs.nn) {
|
||||
if (v) delete[] (v);
|
||||
nn = rhs.nn;
|
||||
}
|
||||
if(!v) v = new T[nn];
|
||||
if(!count) count = new int;
|
||||
*count = 1;
|
||||
memcpy(v, rhs.v, nn*sizeof(T));
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif /* _LA_VEC_H_ */
|
||||
|
||||
Reference in New Issue
Block a user