CPPLapack
 All Classes Files Functions Variables Friends Pages
dcovector-misc.hpp
Go to the documentation of this file.
1 //=============================================================================
2 /*! clear vector */
3 inline void dcovector::clear()
4 {CPPL_VERBOSE_REPORT;
5  l =0;
6  cap =0;
7  delete [] array;
8  array =NULL;
9 }
10 
11 //=============================================================================
12 /*! make vector into zero vector */
14 {CPPL_VERBOSE_REPORT;
15  for(CPPL_INT i=0; i<l; i++){ array[i] =0.0; }
16  return *this;
17 }
18 
19 //=============================================================================
20 /*! change sign(+/-) of the vector */
21 inline void dcovector::chsign()
22 {CPPL_VERBOSE_REPORT;
23  for(CPPL_INT i=0; i<l; i++){ array[i] =-array[i]; }
24 }
25 
26 //=============================================================================
27 /*! make a deep copy of the dcovector */
28 inline void dcovector::copy(const dcovector& vec)
29 {CPPL_VERBOSE_REPORT;
30  l =vec.l;
31  cap =vec.cap;
32  delete [] array;
33  array =new double[cap];
34  CPPL_INT inc =1;
35  dcopy_(&vec.l, vec.array, &inc, array, &inc);
36 }
37 
38 //=============================================================================
39 /*! make a shallow copy of the vector\n
40  This function is not desinged to be used in project codes. */
41 inline void dcovector::shallow_copy(const _dcovector& vec)
42 {CPPL_VERBOSE_REPORT;
43  l =vec.l;
44  cap =vec.cap;
45  delete [] array;
46  array =vec.array;
47 
48  vec.nullify();
49 }
50 
51 //=============================================================================
52 /*! make an alias of the vector\n
53  Be carefull to use this function not to cause double free. */
54 inline void dcovector::alias(const dcovector& vec)
55 {CPPL_VERBOSE_REPORT;
56  l =vec.l;
57  cap =vec.cap;
58  delete [] array;
59  array =vec.array;
60 }
61 
62 //=============================================================================
63 /*! unalias the vector */
64 inline void dcovector::unalias()
65 {CPPL_VERBOSE_REPORT;
66  l =0;
67  cap =0;
68  array =NULL;
69 }
70 
71 //=============================================================================
72 /*! resize vector */
73 inline dcovector& dcovector::resize(const CPPL_INT& _l, const CPPL_INT margin)
74 {CPPL_VERBOSE_REPORT;
75 #ifdef CPPL_DEBUG
76  if( _l<0 ){
77  ERROR_REPORT;
78  std::cerr << "Vector size must be positive integers." << std::endl
79  << "Your input was (" << _l << ", " << margin << ")." << std::endl;
80  exit(1);
81  }
82 #endif//CPPL_DEBUG
83 
84  l =_l;
85  cap =l+margin;
86  delete [] array;
87  array =new double[cap];
88 
89  return *this;
90 }
91 
92 //=============================================================================
93 /*! stretch or shrink vector */
94 inline void dcovector::stretch(const CPPL_INT& dl)
95 {CPPL_VERBOSE_REPORT;
96 #ifdef CPPL_DEBUG
97  if( l+dl<0 ){
98  ERROR_REPORT;
99  std::cerr << "Vector size must be positive integers." << std::endl
100  << "Your input was (" << dl << ")." << std::endl;
101  exit(1);
102  }
103 #endif//CPPL_DEBUG
104 
105  //////// zero ////////
106  if(dl==0){ return; }
107 
108  //////// non-zero ////////
109  l +=dl;
110  if(l>cap){
111  while(l>cap){
112  cap++;
113  cap*=2;
114  }
115  CPPL_INT newl =l-dl;
116  CPPL_INT inc =1;
117  double* newArray(new double[cap]);
118  dcopy_(&newl, array, &inc, newArray, &inc);
119  delete [] array;
120  array =newArray;
121  }
122 }
123 
124 //=============================================================================
125 /*! swap two vectors */
126 inline void swap(dcovector& u, dcovector& v)
127 {CPPL_VERBOSE_REPORT;
128  CPPL_INT u_cap(u.cap), u_l(u.l);
129  double* u_array(u.array);
130  u.l=v.l; u.cap=v.cap; u.array=v.array;
131  v.l=u_l; v.cap=u_cap; v.array=u_array;
132 }
133 
134 //=============================================================================
135 /*! convert user object to smart-temporary object */
136 inline _dcovector _(dcovector& vec)
137 {CPPL_VERBOSE_REPORT;
138  _dcovector newvec;
139 
140  //////// shallow copy ////////
141  newvec.l =vec.l;
142  newvec.cap =vec.cap;
143  newvec.array =vec.array;
144 
145  //////// nullify ////////
146  vec.l =0;
147  vec.cap =0;
148  vec.array =NULL;
149 
150  return newvec;
151 }
CPPL_INT l
vector size
Definition: _dcovector.hpp:9
CPPL_INT l
vector size
Definition: dcovector.hpp:9
dcovector & zero()
_dgematrix i(const _dgbmatrix &mat)
void stretch(const CPPL_INT &)
dcovector & resize(const CPPL_INT &, const CPPL_INT=0)
CPPL_INT cap
vector capacity
Definition: _dcovector.hpp:10
double * array
1D array to store vector data
Definition: dcovector.hpp:11
void clear()
void swap(dcovector &u, dcovector &v)
void alias(const dcovector &)
void unalias()
void copy(const dcovector &)
Real Double-precision Column Vector Class.
Definition: dcovector.hpp:3
(DO NOT USE) Smart-temporary Real Double-precision Column Vector Class
Definition: _dcovector.hpp:3
double * array
1D array to store vector data
Definition: _dcovector.hpp:11
void shallow_copy(const _dcovector &)
_dcovector _(dcovector &vec)
void nullify() const
CPPL_INT cap
vector capacity
Definition: dcovector.hpp:10