Xmipp  v3.23.11-Nereus
VectorChar.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 #include <stdio.h>
27 #include <memory.h>
28 #include "VectorChar.h"
29 
30 #define CHECK(p) if ((p)==NULL) { printf("memory allocation error\n"); exit(253); }
31 
32 void VectorChar::alloc()
33 {
34  if (extention==0) { p=NULL; return; };
35  CHECK(p=(char*)malloc(extention*sizeof(char)));
36 }
37 
38 VectorChar::VectorChar(int _n): np(_n), extention(_n), n(np)
39 {
40  alloc();
41  memset(p,0,extention*sizeof(char));
42 }
43 
44 VectorChar::VectorChar(int _n, int _ext): np(_n), extention(_ext), n(np)
45 {
46  alloc();
47  memset(p,0,extention*sizeof(char));
48 }
49 
51 {
52  alloc();
53  memcpy(p,v->p,n*sizeof(char));
54 }
55 
56 VectorChar::VectorChar(int _n, char *d): np(_n), extention(_n), n(np)
57 {
58  alloc();
59  if (d) memcpy(p,d,_n*sizeof(char));
60  else memset(p,0,extention*sizeof(char));
61 }
62 
63 void VectorChar::prepareExtend(int new_extention)
64 {
65  if (extention<new_extention)
66  {
67  CHECK(p=(char*)realloc(p,new_extention*sizeof(char)));
68  memset(p+extention,0,(new_extention-extention)*sizeof(char));
69  extention=new_extention;
70  };
71 }
72 
73 void VectorChar::setSize(int _n)
74 {
75  np=_n;
76  if (_n==0) { free(p); p=NULL; return; }
77  prepareExtend(_n);
78 }
79 
81 {
82  np++;
84 }
85 
87 {
88  if (extention!=0)
89  {
90  CHECK(p=(char*)realloc(p,n*sizeof(char)));
91  extention=np;
92  };
93 }
94 
96 {
97  if (p) free(p);
98 }
99 
101 {
102  if (n != Q.n) return 0;
103 
104  char *cP = p, *cQ = Q.p;
105  int i = n;
106 
107  while( i-- )
108  {
109  if (*cP!=*cQ) return 0;
110  cP++; cQ++;
111  }
112 
113  return 1;
114 }
115 
117 {
118  if (extention<P.n)
119  {
120  extention=P.n;
121  free(p); alloc();
122  }
123  np=P.n;
124  memcpy(p,P.p,n*sizeof(char));
125  return *this;
126 }
127 
129 {
130  alloc();
131  memcpy(p,P.p,n*sizeof(char));
132 }
133 
134 //ostream& VectorChar::PrintToStream( ostream& out ) const
136 {
137  printf("[");
138  if (!n || !p) { printf("]"); return; }
139 
140  int N=n;
141  char *up=p;
142  while (--N) printf("%i,",*(up++));
143  printf("%i]",*up);
144 }
145 
146 void VectorChar::set(char c)
147 {
148  memset(p,c,n);
149 }
void extend()
Definition: VectorChar.cpp:80
void print()
Definition: VectorChar.cpp:135
const int & n
Definition: VectorChar.h:38
void prepareExtend(int new_extention)
Definition: VectorChar.cpp:63
doublereal * c
char * p
Definition: VectorChar.h:37
#define CHECK(p)
Definition: VectorChar.cpp:30
#define i
doublereal * d
char operator==(const VectorChar &)
Definition: VectorChar.cpp:100
free((char *) ob)
void setSize(int _n)
Definition: VectorChar.cpp:73
void exactshape()
Definition: VectorChar.cpp:86
int extention
Definition: VectorChar.h:35
void set(char c)
Definition: VectorChar.cpp:146
VectorChar & operator=(const VectorChar &P)
Definition: VectorChar.cpp:116