Xmipp  v3.23.11-Nereus
VectorInt.cpp
Go to the documentation of this file.
1 /*
2 
3 CONDOR 1.06 - COnstrained, Non-linear, Direct, parallel Optimization
4  using trust Region method for high-computing load,
5  noisy functions
6 Copyright (C) 2004 Frank Vanden Berghen
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation version 2
11 of the License.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 
22 If you want to include this tools in any commercial product,
23 you can contact the author at fvandenb@iridia.ulb.ac.be
24 
25 */
26 
27 #include <stdio.h>
28 #include <memory.h>
29 #include "VectorInt.h"
30 
32 
33 void VectorInt::alloc(int _n, int _ext)
34 {
35  d=(VectorIntData*)malloc(sizeof(VectorIntData));
36  d->n=_n;
37  d->extention=_ext;
38  d->ref_count=1;
39 
40  if (_ext==0) { d->p=NULL; return; };
41 
42  d->p=(int*)malloc(_ext*sizeof(int));
43  if ((d->p)==NULL) { printf("memory allocation error\n"); exit(253); }
44 }
45 
47 {
48  alloc(_n, _n);
49  memset(d->p,0,d->extention*sizeof(int));
50 }
51 
52 VectorInt::VectorInt(int _n, int _ext)
53 {
54  alloc(_n, _ext);
55  memset(d->p,0,d->extention*sizeof(int));
56 }
57 
58 VectorInt::VectorInt(int _n, int *_d)
59 {
60  alloc(_n,_n);
61  if (_d) memcpy(d->p,_d,_n*sizeof(int));
62  else memset(d->p,0,d->extention*sizeof(int));
63 }
64 
65 void VectorInt::prepareExtend(int new_extention)
66 {
67  if (d->extention<new_extention)
68  {
69  d->p=(int*)realloc(d->p,new_extention*sizeof(int));
70  memset(d->p+d->extention,0,(new_extention-d->extention)*sizeof(int));
71  d->extention=new_extention;
72  };
73 }
74 
76 {
78 }
79 
81 {
82  if (!d) return;
83  (d->ref_count) --;
84  if (d->ref_count==0)
85  {
86  if (d->p) free(d->p);
87  free(d);
88  };
89 }
90 
92 {
93  // shallow copy
94  d=A.d;
95  (d->ref_count)++ ;
96 }
97 
99 {
100  // shallow copy
101  if (this != &A)
102  {
104  d=A.d;
105  (d->ref_count) ++ ;
106  }
107  return *this;
108 }
109 
111 {
112  // a deep copy
113  VectorInt r(sz());
114  r.copyFrom(*this);
115  return r;
116 }
117 
119 {
120  unsigned n=r.sz();
121  if (n==0) return;
122  setSize(n);
123  memcpy(d->p,r.d->p,n*sizeof(double));
124 }
125 
126 void VectorInt::setSize(int _n)
127 {
128  d->n=_n;
129  if (_n==0) { free(d->p); d->p=NULL; d->extention=0; return; }
130  prepareExtend(_n);
131 }
132 
134 {
135  d->n++;
136  if (d->n>d->extention) prepareExtend(d->extention+100);
137 }
138 
140 {
141  if (d->extention!=d->n)
142  {
143  d->p=(int*)realloc(d->p,d->n*sizeof(int));
144  d->extention=d->n;
145  };
146 }
147 
149 {
150  if (d->n != Q.d->n) return 0;
151 
152  int *cP = d->p, *cQ = Q.d->p;
153  int i = d->n;
154 
155  while( i-- )
156  {
157  if (*cP!=*cQ) return 0;
158  cP++; cQ++;
159  }
160 
161  return 1;
162 }
163 
164 //ostream& VectorInt::PrintToStream( ostream& out ) const
166 {
167  printf("[");
168  if (!d->n || !d->p) { printf("]\n"); return; }
169 
170  int N=d->n,*up=d->p;
171  while (--N) printf("%i,",*(up++));
172  printf("%i]\n",*up);
173 }
int sz()
Definition: VectorInt.h:63
void print()
Definition: VectorInt.cpp:165
void destroyCurrentBuffer()
Definition: VectorInt.cpp:80
int equals(const VectorInt &Q)
Definition: VectorInt.cpp:148
VectorInt(int _n=0)
Definition: VectorInt.cpp:46
#define i
void setSize(int _n)
Definition: VectorInt.cpp:126
struct VectorInt::VectorIntDataTag VectorIntData
void copyFrom(VectorInt r)
Definition: VectorInt.cpp:118
void exactshape()
Definition: VectorInt.cpp:139
free((char *) ob)
void prepareExtend(int new_extention)
Definition: VectorInt.cpp:65
VectorIntData * d
Definition: VectorInt.h:40
void extend()
Definition: VectorInt.cpp:133
VectorInt & operator=(const VectorInt &P)
Definition: VectorInt.cpp:98
static VectorInt emptyVectorInt
Definition: VectorInt.h:70
VectorInt clone()
Definition: VectorInt.cpp:110
int * n