C++ main module for emicrom 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  mVector.resize(n);
19 }
20 template<class T>
22  copy(v);
23 }
24 
25 
26 
27 template<class T>
29  //clear();
30 }
31 
32 
33 template<class T>
35  tUIndex l=v.length();
36  if (l==0) {
37  clear();
38  }
39  if ((v[0]!='[') && (v[0]!='(')) return *this;
40  SP::CORE_String str=CORE_String::New(v.substr(1,v.length()-2));
41  str->tokenize(",");
42  str->begin();
43  tUIndex n=str->getTokensCount();
44 
45  // set the size
46  setSize(n+from);
47 
48  T value;
49  typename vector<T>::iterator iter=mVector.begin()+from;
50  while (str->hasNextToken()) {
51  //parse the value
52  CORE_String::parse(str->nextToken(),value);
53  //set it
54  *iter=value;
55  //next iter
56  iter++;
57  }
58 
59  return *this;
60 }
61 
62 
63 
64 template<class T>
66  typename vector<T >::iterator p;
67  tBoolean found=false;
68  tBoolean exists=false;
69  do {
70  p=find(mVector.begin(),mVector.end(),obj);
71  found=(p!=mVector.end());
72  if (found) {
73  exists=true;
74  mVector.erase(p);
75  }
76  } while(found);
77  return exists;
78 }
79 template<class T>
81  if (index>=getSize()) return false;
82  typename vector<T >::iterator p=mVector.begin()+index;
83  mVector.erase(p);
84  return true;
85 }
86 
87 template<class T>
88 void CORE_Vector<T>::permute(const tUIndex& i,const tUIndex& j) {
89  std::swap(mVector[i],mVector[j]);
90 }
91 
92 
93 template<class T>
94 tBoolean CORE_Vector<T>::insert(const tUIndex& index,const T& obj) {
95  if (index>getSize()) return false;
96  if (getSize()==index) add(obj);
97  else {
98 
99  typename vector<T >::iterator p=mVector.begin()+index;
100  mVector.insert(p,obj);
101  }
102  return true;
103 }
104 
105 template<class T>
107  tUIndex si=0;
108  tUIndex sf=getSize();
109  if (sf==0) {
110  add(obj);
111  return 0;
112  }
113  sf--;
114 
115  if (obj<mVector[0]) {
116  insertAtIndex(0,obj);
117  return 0;
118  } else if (obj==mVector[0]) {
119  if (evenIfEqual) {
120  insertAtIndex(0,obj);
121  }
122  return 0;
123  }
124  if (obj>mVector[sf]) {
125  add(obj);
126  return sf+1;
127  } else if (obj==mVector[sf]) {
128  if (evenIfEqual) {
129  insertAtIndex(sf,obj);
130  }
131  return sf;
132  };
133  tUIndex i=0;
134  while (sf-si>1) {
135  i=(si+sf)/2;
136  if (obj>mVector[i]) si=i;
137  else if (obj==mVector[i]) {
138  if (evenIfEqual) {
139  insertAtIndex(i,obj);
140  }
141  return i;
142  }
143  else sf=i;
144  }
145  if (obj==mVector[sf]) {
146  if (evenIfEqual) insertAtIndex(sf,obj);
147  return sf;
148  }
149  if (obj==mVector[si]) {
150  if (evenIfEqual) insertAtIndex(si,obj);
151  return si;
152  }
153 
154  insertAtIndex(sf,obj);
155  return sf;
156 }
157 
158 
159 template<class T>
160 tUIndex CORE_Vector<T>::search(const vector<T>& values,
161  const T& obj,
162  const tFlag& order){
163 
164  tUIndex si=0;
165  tUIndex sf=values.size();
166  if (sf==0) {
167  return getMaxUIndex();
168  }
169  sf--;
170 
171  // min
172  if (compare(obj,values[0],order)) {
173  return getMaxUIndex();
174  }
175 
176  // max
177  if (compare(values[sf],obj,order)) {
178  return getMaxUIndex();
179  }
180 
181  //eq
182  if (compare(values[0],obj,EQ)) {
183  return 0;
184  }
185  if (compare(values[sf],obj,EQ)) {
186  return sf;
187  }
188 
189 
190 
191  tUIndex i=0;
192  while (sf-si>1) {
193  i=(si+sf)/2;
194  if (compare(values[i],obj,EQ)) {
195  return i;
196  } else if (compare(values[i],obj,order)) {
197  si=i;
198  }
199  else sf=i;
200  }
201  if (compare(obj,values[sf],EQ)) {
202  return sf;
203  }
204  if (compare(obj,values[si],EQ)) {
205  return si;
206  }
207  return getMaxUIndex();
208 }
209 
210 template<class T>
212  typename vector<T>::iterator ei=mVector.end();
213  typename vector<T>::iterator bi=mVector.begin();
214  while (bi<ei) {
215  ei--;
216  std::swap(*ei,*bi);
217  bi++;
218  }
219 }
220 
221 template<class T>
223  tUIndex oldSize=getSize();
224  tUIndex n=array.getSize();
225  setSize(oldSize+n);
226  typename vector<T>::iterator it=mVector.begin()+oldSize;
227  typename vector<T>::iterator ia=array.begin();
228  while (it!=mVector.end()) {
229  *it=*ia;
230  it++;
231  ia++;
232  }
233 }
234 
235 template<class T>
236 tBoolean CORE_Vector<T>::exists(const T& obj) const {
237  typename vector<T>::const_iterator p=find(mVector.begin(),mVector.end(),obj);
238  return (p!=mVector.end());
239 };
240 
241 
242 template<class T>
243 void CORE_Vector<T>::addAfterIndex(const tUIndex& index,const T& v) {
244  tUIndex i,dim=mVector.size()+1;
245  setSize(dim);
246  typename vector<T>::iterator it=mVector.end();
247  it--;
248  for (i=dim-1;i>index+1;i--) {
249  *it=*(it-1);
250  it--;
251  }
252  //i=index+1
253  if (mVector.size()>index+1) *it=v;
254 }
255 
256 template<class T>
257 void CORE_Vector<T>::addAfterIndices(boost::shared_ptr<CORE_Vector<tUIndex> >& p_indices,
258  const CORE_SharedPointersList<CORE_Vector<T> >& values) {
259  if (p_indices.get()==null) return;
260 
261  CORE_Vector<tUIndex> &indices=*p_indices.get();
262 
263  tUIndex nIndices=indices.getSize();
264  tUIndex i,nValues=values.getSize();
265  tUIndex nValuesToAdd=0;
266 
267  //compute the number of values to add
268  for (i=0;i<nValues;i++)
269  nValuesToAdd+=values[i]->getSize();
270  if (nValuesToAdd==0) return;
271 
272  // resize the new dimension
273  tUIndex dim=mVector.size()+nValuesToAdd;
274  vector<T> newValues(dim);
275 
276  // set the current cursors
277  tIndex cur_newValues=dim-1;
278  tIndex cur_indices=nIndices-1;
279  tIndex cur_mVector=mVector.size()-1;
280 
281  tUIndex insertIndex=getMaxUIndex();
282  while (cur_indices>=0) {
283  insertIndex=indices[cur_indices];
284  //cout << "insert index:"<< insertIndex<<"\n";
285  // copy the values of mVector in newValues
286  //cout << "copy the values from "<<cur_mVector<<" to "<<(insertIndex+1)<<"\n";
287  for (i=cur_mVector;i>insertIndex;i--) {
288  newValues[cur_newValues]=mVector[i];
289  //cout << "newValues["<<cur_newValues<<"]="<<mVector[i]<<"\n";
290  cur_newValues--;
291  }
292  cur_mVector=insertIndex;
293 
294  // insert the new values
295  //cout << "insert the new values \n";
296  const CORE_Vector<T> &vals=*values.get(cur_indices);
297  tUIndex nVals=vals.getSize();
298  i=nVals;
299  while (i>0) {
300  i--;
301  newValues[cur_newValues]=vals[i];
302  //cout << "newValues["<<cur_newValues<<"]="<<vals[i]<<"\n";
303  cur_newValues--;
304  }
305 
306  // insert at new index
307  cur_indices--;
308  }
309 
310  // copy the last values
311  //cout << "copy the last values from "<<cur_mVector<<" to 0 \n";
312  i=cur_mVector+1;
313  while (i>0) {
314  i--;
315  newValues[cur_newValues]=mVector[i];
316  //cout << "newValues["<<cur_newValues<<"]="<<mVector[i]<<"\n";
317  cur_newValues--;
318  }
319 
320  // set the new dimension
321  copy(newValues);
322 
323 }
324 
325 template<class T>
326 void CORE_Vector<T>::sort(vector<T>& items,
327  const tFlag& order) {
328 
329  tUIndex h=1;
330  tUIndex i,j;
331  tUIndex N=items.size();
332  T v;
333  do h=3*h+1;
334  while (h<=N);
335  do {
336  h/=3;
337  for (i=h;i<N;++i) {
338  if (compare(items[i],
339  items[i-h],
340  order)) {
341  v=items[i];
342  j=i;
343  do {
344  items[j]=items[j-h];
345  j=j-h;
346  }
347  while ((j>=h) && (!compare(items[j-h],
348  v,
349  order)));
350  items[j]=v;
351  }
352  }
353  }
354  while (h>1);
355 
356 }
357 #endif
tBoolean remove()
remove the last pointer
Definition: CORE_Vector.h:787
void append(const CORE_Vector< T > &array)
append the array at the end of this
Definition: CORE_Vector.hpp:222
void permute(const tUIndex &i, const tUIndex &j)
permute
Definition: CORE_Vector.hpp:88
tBoolean exists(const T &obj) const
test if the value obj exists
Definition: CORE_Vector.hpp:236
this class describes an array
Definition: CORE_Vector.h:19
class CORE_SharedPointersList is a list of shared pointers
Definition: CORE_SharedPointersList.h:11
tUIndex insertInIncreasingOrder(const T &obj, const tBoolean &evenIfEqual)
insert The value in an increasing order
Definition: CORE_Vector.hpp:106
static void parse(const tString &str, tUChar &c)
parse unsigned char c in str
Definition: CORE_String.h:504
this class describes a list interface
Definition: CORE_List.h:12
void sort()
sort the vector decreasing order < increasing order
Definition: CORE_Vector.h:803
void addAfterIndex(const tUIndex &index, const T &v)
add an element after index
Definition: CORE_Vector.hpp:243
void reverse()
reverse the vector
Definition: CORE_Vector.hpp:211
vector< T > mVector
Definition: CORE_Vector.h:23
#define tBoolean
Definition: types.h:139
CORE_Vector< T > & initValues(const T &v)
init the value to v
Definition: CORE_Vector.h:268
static const tFlag EQ
Definition: CORE_List.h:21
#define null
Definition: types.h:144
CORE_Vector()
build a vector of T
Definition: CORE_Vector.hpp:11
#define tIndex
Definition: types.h:129
void setSize(const tUIndex &n)
set the size of the vector
Definition: CORE_Vector.h:255
tUIndex getSize() const
return the size of the vector
Definition: CORE_Vector.h:391
tBoolean insertAtIndex(const tUIndex &i, const T &obj)
insert the pointer at index i the old element i become the element i+1
Definition: CORE_Vector.h:716
virtual ~CORE_Vector()
destroy an array of T*
Definition: CORE_Vector.hpp:28
void add(const CORE_Vector< T > &u, const CORE_Vector< T > &v)
init the value to this=u-v
Definition: CORE_Vector.h:439
#define tUIndex
Definition: types.h:126
static tUIndex getMaxUIndex()
get the max value for difference the array/vector indexing type
Definition: CORE_Object.h:565
static tUIndex search(const vector< T > &values, const T &value, const tFlag &order)
search the value in values vector ordered in order If it does not existe return getMaxUIndex() ...
Definition: CORE_Vector.hpp:160
abstract base class for most classes.
Definition: CORE_Object.h:53
#define tString
Definition: types.h:135
void copy(const CORE_Vector< Q > &u)
copy
Definition: CORE_Vector.h:217
static SP::CORE_String New()
create a class String
Definition: CORE_String.h:96
const T & get(const tUIndex &i) const
get the value at index i
Definition: CORE_Vector.h:376
vector< T >::const_iterator begin() const
get iterator
Definition: CORE_Vector.h:331
void clear()
clear the array
Definition: CORE_Vector.h:248
void addAfterIndices(boost::shared_ptr< CORE_Vector< tUIndex > > &p_indices, const CORE_SharedPointersList< CORE_Vector< T > > &values)
add alements after indices
Definition: CORE_Vector.hpp:257
tBoolean removeAtIndex(const tUIndex &i)
remove the pointer at index i
Definition: CORE_Vector.hpp:80
static tBoolean compare(const Q &a, const Q &b, const tFlag &order)
compare 2 object a & b
Definition: CORE_List.h:55
tBoolean insert(const tUIndex &i, const T &obj)
insert the pointer at index i the old element i become the element i+1
Definition: CORE_Vector.hpp:94
#define tFlag
Definition: types.h:74