C++ main module for gpm Package  1.0
CORE_Vector.hpp
Go to the documentation of this file.
1 #ifndef CORE_Vector_CPP
2 #define CORE_Vector_CPP
3 
4 #include "CORE_Vector.h"
5 #include <algorithm>
6 
7 using namespace std;
8 
9 
10 template<class T>
12 
13  mVector.resize(0);
14 }
15 
16 template<class T>
18 
19  mVector.resize(n);
20 }
21 template<class T>
23 
24  int n=v.getSize();
25  mVector.resize(n);
26  for (int i=0;i<n;i++) {
27  mVector[i]=v[i];
28  }
29 }
30 
31 
32 
33 template<class T>
35  //clear();
36 }
37 
38 
39 template<class T>
41  if (obj==null) return false;
42  typename vector<T >::iterator p;
43  tBoolean found=false;
44  tBoolean exists=false;
45  do {
46  p=find(mVector.begin(),mVector.end(),obj);
47  found=(p!=mVector.end());
48  if (found) {
49  exists=true;
50  mVector.erase(p);
51  }
52  } while(found);
53  return exists;
54 }
55 template<class T>
57  if (index>=size()) return false;
58  if (index<0) return false;
59  typename vector<T >::iterator p=mVector.begin()+index;
60  mVector.erase(p);
61  return true;
62 }
63 
64 template<class T>
65 void CORE_Vector<T>::permute(const int& i,const int& j) {
66  T temp=mVector[i];
67  mVector[i]=mVector[j];
68  mVector[j]=temp;
69 }
70 
71 
72 template<class T>
73 tBoolean CORE_Vector<T>::insert(int index,const T& obj) {
74  if (((int)mVector.size())==index) add(obj);
75  else {
76  int i=(int) index;
77  if (index<0) return false;
78  typename vector<T >::iterator p=mVector.begin()+i;
79  if (i>size()) return false;
80  mVector.insert(p,obj);
81  }
82  return true;
83 }
84 template<class T>
85 int CORE_Vector<T>::insertInOrder(const T& obj,const tBoolean& evenIfEqual){
86  int si=0;
87  int sf=getSize()-1;
88  if (sf<0) {
89  add(obj);
90  return 0;
91  }
92  if (obj<mVector[0]) {
93  insert(0,obj);
94  return 0;
95  } else if (obj==mVector[0]) {
96  if (evenIfEqual) return insert(0,evenIfEqual);
97  else return 0;
98  }
99  if (obj>mVector[sf]) {
100  add(obj);
101  return sf+1;
102  } else if (obj==mVector[sf]) {
103  if (evenIfEqual) return insert(sf,evenIfEqual);
104  return sf;
105  };
106  int i=0;
107  while (sf-si>1) {
108  i=(si+sf)/2;
109  if (obj>mVector[i]) si=i;
110  else if (obj==mVector[i]) {
111  if (evenIfEqual) return insert(i,evenIfEqual);
112  return i;
113  }
114  else sf=i;
115  }
116  if (obj==mVector[sf]) {
117  if (evenIfEqual) return insert(sf,evenIfEqual);
118  return sf;
119  }
120  if (obj==mVector[si]) {
121  if (evenIfEqual) return insert(si,evenIfEqual);
122  return si;
123  }
124  return insert(sf,obj);
125 }
126 
127 
128 template<class T>
129 int CORE_Vector<T>::search(const vector<T>& values,
130  const T& obj,
131  const tString& order){
132 
133  int si=0;
134  int sf=values.size()-1;
135  if (sf<0) {
136  return -1;
137  }
138  // min
139  if (compare(obj,values[0],order)) {
140  return -1;
141  }
142 
143  // max
144  if (compare(values[sf],obj,order)) {
145  return -1;
146  }
147 
148  //eq
149  if (compare(values[0],obj,"=")) {
150  return 0;
151  }
152  if (compare(values[sf],obj,"=")) {
153  return sf;
154  }
155 
156 
157 
158  int i=0;
159  while (sf-si>1) {
160  i=(si+sf)/2;
161  if (compare(values[i],obj,"=")) {
162  return i;
163  } else if (compare(values[i],obj,order)) {
164  si=i;
165  }
166  else sf=i;
167  }
168  if (compare(obj,values[sf],"=")) {
169  return sf;
170  }
171  if (compare(obj,values[si],"=")) {
172  return si;
173  }
174  return -1;
175 }
176 
177 template<class T>
178 tBoolean CORE_Vector<T>::set(int index,const T& obj) {
179  int i=(int) index;
180  int p=size();
181  if (i>=p) {
182  mVector.resize(i+1);
183  }
184  mVector[i]=obj;
185  return true;
186 }
187 template<class T>
189  int k=getSize()-1;
190  int n=mVector.size();
191  for (int i=0;i<n/2;i++) {
192  T &temp=mVector[i];
193  mVector[i]=mVector[k];
194  mVector[k]=temp;
195  k--;
196  }
197 }
198 
199 template<class T>
201  int oldSize=getSize();
202  int n=array.size();
203  setSize(oldSize+n);
204  int newSize=getSize();
205  for (int i=oldSize;i<newSize;i++) set(i,array[i]);
206 }
207 
208 template<class T>
209 tBoolean CORE_Vector<T>::exists(const T& obj) const {
210  typename vector<T>::const_iterator p=find(mVector.begin(),mVector.end(),obj);
211  return (p!=mVector.end());
212 };
213 
214 
215 template<class T>
216 void CORE_Vector<T>::addAfterIndex(const int& index,const T& v) {
217  int dim=mVector.size()+1;
218  setSize(dim);
219  for (int i=dim-1;i>index+1;i--) {
220  mVector[i]=mVector[i-1];
221  }
222  if (((int)mVector.size())>index+1)
223  mVector[index+1]=v;
224 }
225 
226 template<class T>
227 void CORE_Vector<T>::addAfterIndices(boost::shared_ptr<CORE_Vector<int> >& p_indices,
228  const CORE_SharedPointersList<CORE_Vector<T> >& values) {
229  if (p_indices.get()==null) return;
230 
231  CORE_Vector<int> &indices=*p_indices.get();
232 
233  int nIndices=indices.getSize();
234  int nValues=values.getSize();
235  int nValuesToAdd=0;
236  for (int i=0;i<nValues;i++)
237  nValuesToAdd+=values[i]->getSize();
238 
239  // resize the new dimension
240  int dim=mVector.size()+nValuesToAdd;
241  vector<T> newValues(dim);
242  // set the current cursors
243  int cur_newValues=dim-1;
244  int cur_indices=nIndices-1;
245  int cur_mVector=mVector.size()-1;
246 
247  int insertIndex=-1;
248  while (cur_indices>-1) {
249  insertIndex=indices[cur_indices];
250  //cout << "insert index:"<< insertIndex<<"\n";
251  // copy the values of mVector in newValues
252  //cout << "copy the values from "<<cur_mVector<<" to "<<(insertIndex+1)<<"\n";
253  for (int i=cur_mVector;i>insertIndex;i--) {
254  newValues[cur_newValues]=mVector[i];
255  //cout << "newValues["<<cur_newValues<<"]="<<mVector[i]<<"\n";
256  cur_newValues--;
257  }
258  cur_mVector=insertIndex;
259 
260  // insert the new values
261  //cout << "insert the new values \n";
262  const CORE_Vector<T> &vals=*values.get(cur_indices);
263  int nVals=vals.getSize();
264  for (int i=nVals-1;i>=0;i--) {
265  newValues[cur_newValues]=vals[i];
266  //cout << "newValues["<<cur_newValues<<"]="<<vals[i]<<"\n";
267  cur_newValues--;
268  }
269 
270  // insert at new index
271  cur_indices--;
272  }
273 
274  // copy the last values
275  //cout << "copy the last values from "<<cur_mVector<<" to 0 \n";
276  for (int i=cur_mVector;i>=0;i--) {
277  newValues[cur_newValues]=mVector[i];
278  //cout << "newValues["<<cur_newValues<<"]="<<mVector[i]<<"\n";
279  cur_newValues--;
280  }
281 
282  // set the new dimension
283  mVector.resize(dim);
284  for (int i=0;i<dim;i++) mVector[i]=newValues[i];
285 
286 }
287 
288 template<class T>
289 void CORE_Vector<T>::sort(vector<T>& items,
290  const tString& order) {
291 
292  int h=1;
293  int i,j;
294  int N=items.size();
295  T v;
296  do h=3*h+1;
297  while (h<=N);
298  do {
299  h/=3;
300  for (i=h;i<N;++i) {
301  if (compare(items[i],
302  items[i-h],
303  order)) {
304  v=items[i];
305  j=i;
306  do {
307  items[j]=items[j-h];
308  j=j-h;
309  }
310  while ((j>=h) && (!compare(items[j-h],
311  v,
312  order)));
313  items[j]=v;
314  }
315  }
316  }
317  while (h>1);
318 
319 }
320 #endif
tBoolean remove()
remove the last pointer
Definition: CORE_Vector.h:345
void sort(const tString &order)
sort the vector decreasing order < increasing order
Definition: CORE_Vector.h:323
this class describes an array
Definition: CORE_Vector.h:18
class CORE_SharedPointersList is a list of shared pointers
Definition: CORE_SharedPointersList.h:11
int insertInOrder(const T &obj, const tBoolean &evenIfEqual)
insert T
Definition: CORE_Vector.hpp:85
this class describes a list
Definition: CORE_List.h:12
tBoolean exists(const T &obj) const
exists
Definition: CORE_Vector.hpp:209
void reverse()
reverse the vector
Definition: CORE_Vector.hpp:188
#define tBoolean
Definition: types.h:35
tBoolean removeAtIndex(const int &i)
remove the pointer at index i
Definition: CORE_Vector.hpp:56
#define null
Definition: types.h:13
CORE_Vector()
build a vector of T
Definition: CORE_Vector.hpp:11
virtual ~CORE_Vector()
destroy an array of T*
Definition: CORE_Vector.hpp:34
int size() const
return the size of the vector
Definition: CORE_Vector.h:384
static int search(const vector< T > &values, const T &value, const tString &order)
search the value in values vector ordered in order
Definition: CORE_Vector.hpp:129
tBoolean insert(int i, const T &obj)
insert the pointer at index i the old element i become the element i+1
Definition: CORE_Vector.hpp:73
int getSize() const
return the size of the vector
Definition: CORE_Vector.h:387
tBoolean set(int i, const T &obj)
set the pointer at the index i
Definition: CORE_Vector.hpp:178
#define tString
Definition: types.h:36
void permute(const int &i, const int &j)
permute
Definition: CORE_Vector.hpp:65
const T & get(int i) const
get the pointer at index i ASSERT_IN(i>-1); ASSERT_IN(i<((int)mVector.size()));
Definition: CORE_Vector.h:367
void addAfterIndex(const int &index, const T &v)
add an element after index
Definition: CORE_Vector.hpp:216
void addAfterIndices(boost::shared_ptr< CORE_Vector< int > > &p_indices, const CORE_SharedPointersList< CORE_Vector< T > > &values)
add alements after indices
Definition: CORE_Vector.hpp:227
void merge(const CORE_Vector< T > &array)
merge the array in this
Definition: CORE_Vector.hpp:200