last update 20 Sep 2009 |
00001 /* 00002 * Copyright (C) 1998-2004 00003 * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany 00004 * 00005 * 00006 * This file is part of the Computer Vision and Robotics Library (CVR-Lib) 00007 * 00008 * The CVR-Lib is free software; you can redistribute it and/or 00009 * modify it under the terms of the BSD License. 00010 * 00011 * All rights reserved. 00012 * 00013 * Redistribution and use in source and binary forms, with or without 00014 * modification, are permitted provided that the following conditions are met: 00015 * 00016 * 1. Redistributions of source code must retain the above copyright notice, 00017 * this list of conditions and the following disclaimer. 00018 * 00019 * 2. Redistributions in binary form must reproduce the above copyright notice, 00020 * this list of conditions and the following disclaimer in the documentation 00021 * and/or other materials provided with the distribution. 00022 * 00023 * 3. Neither the name of the authors nor the names of its contributors may be 00024 * used to endorse or promote products derived from this software without 00025 * specific prior written permission. 00026 * 00027 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00028 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00029 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00030 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00031 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00032 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00033 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00034 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00035 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00036 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00037 * POSSIBILITY OF SUCH DAMAGE. 00038 */ 00039 00040 00041 00042 /** 00043 * \file cvrGenericVector.h 00044 * Contains a replacement for std::vector, which has been optimized 00045 * for the storage of static types, (i.e. types that do not manage 00046 * pointers to objects of any kind) and speeds up the access to the 00047 * vector size and other internal properties. 00048 * \author Pablo Alvarado 00049 * \date 09.04.1999 00050 * 00051 * $Id: cvrGenericVector.h,v 1.16 2007/09/15 03:36:57 alvarado Exp $ 00052 */ 00053 00054 #ifndef _CVR_GENERIC_VECTOR_H_ 00055 #define _CVR_GENERIC_VECTOR_H_ 00056 00057 #include "cvrContainer.h" 00058 #include "cvrConstantReferenceType.h" 00059 #include "cvrConstReferenceException.h" 00060 #include "cvrAllocException.h" 00061 #include "cvrTypes.h" 00062 #include "cvrAssert.h" 00063 #include "cvrResizeType.h" 00064 #include "cvrDebugIterator.h" 00065 00066 #include <vector> 00067 00068 namespace cvr { 00069 /** 00070 * Vector container class. 00071 * 00072 * The cvr::genericVector class allows the representation of 00073 * n-dimensional vectors. The elements of the vector will be 00074 * indexed between 0 an n-1. 00075 * 00076 * Note that this class is NOT intended to be a substitute for std::vector. 00077 * If you need a vector of elements which use some sort of dynamic memory 00078 * allocation then you have to use the std::vector class of the Standard 00079 * Template Library (STL). This means, you should not try to make a 00080 * cvr::genericVector of other vectors or matrices, which administrate some 00081 * memory. If you do so, the behaviour of copy or fill operations will be 00082 * unpredictable. 00083 * 00084 * Most of times you will want to use cvr::vector instead. 00085 * 00086 * The difference between cvr::genericVector and its most used inherited 00087 * class cvr::vector is the support for arithmetical operations. The 00088 * generic vector is more a "pure" container than a multi-dimensional point 00089 * representation. It inherits its memory management versatility to 00090 * cvr::vector. Again, it is usually employed as a container of any \e 00091 * static type, which do not do by itself any memory managment. 00092 * 00093 * The genericVector class is a generic container class implemented as 00094 * template, where the template type T is the type of the elements in the 00095 * vector. 00096 * 00097 * If you need to create a vector of floats with 256 elements, all of them 00098 * initialized with a value of 4.27 just create it with 00099 * 00100 * \code 00101 * cvr::genericVector<float> myVct(256,4.27f) // creates vector with 256 00102 * // elements all initialized 00103 * // with 4.27f 00104 * \endcode 00105 * 00106 * To access the vector elements use the access operators at() (or the 00107 * overloaded operator[]()). For example: 00108 * 00109 * \code 00110 * float accu = 0; // initialize accumulator 00111 * 00112 * for (int i = 0; i < myVct.size(); i++) { 00113 * tmp += myVct.at(i); // access each element of the vector 00114 * } 00115 * \endcode 00116 * 00117 * Here is also a difference to the STL std::vector. In the STL there is 00118 * always a boundary check for at() and the boundary is never checked with 00119 * operator[](). In the CVR-Lib, the debug-mode library always makes a 00120 * boundary check for both at() and operator[](), and in the release-mode 00121 * there is no check for any of both methods. 00122 * 00123 * @ingroup gAggregate 00124 */ 00125 template<typename T> 00126 class genericVector : public container { 00127 public: 00128 00129 /** 00130 * \name Internal types and classes 00131 */ 00132 //@{ 00133 00134 /** 00135 * Exception thrown when a constant reference is violated. 00136 */ 00137 typedef cvr::constReferenceException constReferenceException; 00138 00139 /** 00140 * Type of the genericVector elements. 00141 */ 00142 typedef T value_type; 00143 00144 /** 00145 * Type used for indices and size of the vector. 00146 * 00147 * Note that this type is in the CVR-Lib usually signed integer. 00148 */ 00149 typedef int size_type; 00150 00151 /** 00152 * Pointer to value_type 00153 */ 00154 typedef T* pointer; 00155 00156 /** 00157 * Const pointer to value_type 00158 */ 00159 typedef const T* const_pointer; 00160 00161 /** 00162 * Reference to value_type 00163 */ 00164 typedef T& reference; 00165 00166 /** 00167 * Const reference to value_type 00168 */ 00169 typedef const T& const_reference; 00170 00171 # ifdef NDEBUG 00172 /** 00173 * \c iterator type (allows read and write operations). 00174 * 00175 * The use of the iterator classes is similar to the iterators of the STL 00176 * (Standard Template Library). See cvr::genericVector::begin() and 00177 * cvr::genericVector::inverseBegin() for examples. 00178 * 00179 * For the debugging version of the iterators, boundary check will be 00180 * done! This explains the low speed of the iterators of the debug 00181 * version. In the release version, no boundary check will be done, 00182 * and the iterators are sometimes a factor 10 faster than the 00183 * debug iterators. 00184 * 00185 * The use of the access operator at() is faster than the iterators in 00186 * the debug version only. If you need to iterate on a genericVector use 00187 * iterators instead (in the release version iterators are approximately a 00188 * factor 3 faster than at()). 00189 * 00190 * \warning Try to use the prefix incremental operator (i.e. ++it) instead 00191 * of the postfix operator (i.e. it++) to allow efficient code also in 00192 * debug-modus! 00193 * 00194 * @see genericVector<T>::const_iterator 00195 */ 00196 typedef pointer iterator; 00197 00198 /** 00199 * \c const_iterator type (allows read-only operations). 00200 * 00201 * The use of the iterator classes is similar to the iterators of the STL 00202 * (Standard Template Library). See cvr::genericVector::begin() for an 00203 * example. 00204 * 00205 * For the debugging version of the iterators, boundary check will be 00206 * done! This explains the low speed of the iterators of the debug 00207 * version. In the release version, no boundary check will be done, and 00208 * the iterators are sometimes a factor 10 faster than the debug 00209 * iterators. 00210 * 00211 * The use of the access operator at() is faster than the iterators in the 00212 * debug version only. If you need to iterate on a genericVector use 00213 * iterators instead (in the release version iterators are approximately a 00214 * factor 3 faster than at()). 00215 * 00216 * \warning Try to use the prefix incremental operator (i.e. ++it) instead 00217 * of the postfix operator (i.e. it++) to allow efficient code also in 00218 * debug-modus! 00219 * 00220 * @see genericVector<T>::iterator 00221 */ 00222 typedef const_pointer const_iterator; 00223 00224 # else 00225 00226 /** 00227 * \c iterator type (allows read and write operations). 00228 * 00229 * The use of the iterator classes is similar to the iterators of the STL 00230 * (Standard Template Library). See cvr::genericVector::begin() and 00231 * cvr::genericVector::inverseBegin() for examples. 00232 * 00233 * For the debugging version of the iterators, boundary check will be 00234 * done! This explains the low speed of the iterators of the debug 00235 * version. In the release version, no boundary check will be done, 00236 * and the iterators are sometimes a factor 10 faster than the 00237 * debug iterators. 00238 * 00239 * The use of the access operator at() is faster than the iterators in 00240 * the debug version only. If you need to iterate on a genericVector use 00241 * iterators instead (in the release version iterators are approximately a 00242 * factor 3 faster than at()). 00243 * 00244 * \warning Try to use the prefix incremental operator (i.e. ++it) instead 00245 * of the postfix operator (i.e. it++) to allow efficient code also in 00246 * debug-modus! 00247 * 00248 * @see genericVector::const_iterator 00249 */ 00250 typedef internal::debugIterator<genericVector<T>, false> iterator; 00251 00252 /** 00253 * \c const_iterator type (allows read-only operations). 00254 * 00255 * The use of the iterator classes is similar to the iterators of the STL 00256 * (Standard Template Library). See cvr::genericVector::begin() for an 00257 * example. 00258 * 00259 * For the debugging version of the iterators, boundary check will be 00260 * done! This explains the low speed of the iterators of the debug 00261 * version. In the release version, no boundary check will be done, and 00262 * the iterators are sometimes a factor 10 faster than the debug 00263 * iterators. 00264 * 00265 * The use of the access operator at() is faster than the iterators in the 00266 * debug version only. If you need to iterate on a genericVector use 00267 * iterators instead (in the release version iterators are approximately a 00268 * factor 3 faster than at()). 00269 * 00270 * \warning Try to use the prefix incremental operator (i.e. ++it) instead 00271 * of the postfix operator (i.e. it++) to allow efficient code also in 00272 * debug-modus! 00273 * 00274 * @see genericVector<T>::iterator 00275 */ 00276 typedef internal::debugIterator<genericVector<T>,true> const_iterator; 00277 00278 # endif 00279 //@} 00280 00281 /** 00282 * Default constructor creates an empty genericVector 00283 */ 00284 genericVector(); 00285 00286 /** 00287 * Create a genericVector of the given size but do \b not initialize its 00288 * elements. 00289 * 00290 * \warning This is an interface change with the previous library. It has 00291 * been done to be consistent with the more basic features of the C++ 00292 * language. If you write for example "int c;", the content of \c c is 00293 * not defined, and in the same way, if you want a vector with initialized 00294 * data, you have to specify explicitely the value with which the elements 00295 * have to be initialized. 00296 * 00297 * @param theSize number of elements of the genericVector. 00298 */ 00299 explicit genericVector(const size_type theSize); 00300 00301 /** 00302 * Create a genericVector of the given size and initialize it with the 00303 * given value. 00304 * 00305 * @param theSize number of elements of the genericVector. 00306 * @param iniValue all elements will be initialized with this value. 00307 */ 00308 explicit genericVector(const size_type theSize,const_reference iniValue); 00309 00310 /** 00311 * Create a genericVector of the given size and initialize it with the 00312 * given data. The \a data will be copied. 00313 * 00314 * If you need to just use the memory block without copying it, then you 00315 * can wether use a third parameter of eConstantReference type or later 00316 * call the useExternData() method. 00317 * 00318 * @see useExternData() 00319 * 00320 * @param theSize number of elements of the genericVector. 00321 * @param data a pointer to the data that will be copied. 00322 */ 00323 genericVector(const size_type theSize,const value_type data[]); 00324 00325 /** 00326 * Create a genericVector of the given size and initialize it with the 00327 * given data, the same way "useExternData" does, i.e. tThe \a data will 00328 * \b not be copied!. 00329 * 00330 * @see useExternData() 00331 * 00332 * @param theSize number of elements of the genericVector. 00333 * @param data a pointer to the data that will be used. 00334 * @param constRef if this parameter is \c ConstantReference it will not be 00335 * possible to change the pointer to the external memory 00336 * block nor to resize the genericVector. Despite this, 00337 * the value of each element can be changed by the access 00338 * operators. 00339 */ 00340 genericVector(const size_type theSize,value_type data[], 00341 const eConstantReference constRef); 00342 00343 /** 00344 * Create this genericVector as a copy of another genericVector 00345 * @param other the genericVector to be copied. 00346 */ 00347 genericVector(const genericVector<T>& other); 00348 00349 /** 00350 * Create this genericVector as a copy of specified interval of 00351 * elements of another genericVector. Indices below zero are set 00352 * to zero, indices greater than the size of the %genericVector to 00353 * the size-1. 00354 * @param other the genericVector to be copied. 00355 * @param from starting point included 00356 * @param to end point included. 00357 */ 00358 genericVector(const genericVector<T>& other, 00359 const size_type from, const size_type to=MaxIndex); 00360 00361 /** 00362 * Create this genericVector as a copy of specified elements of 00363 * another genericVector. \a idx can contain the same index more 00364 * than once. 00365 * @param other the genericVector to be copied. 00366 * @param idx indices of the elements to be copied 00367 */ 00368 genericVector(const genericVector<T>& other, 00369 const genericVector<size_type>& idx); 00370 00371 /** 00372 * Create this genericVector as a copy of another std::genericVector 00373 * @param other the genericVector to be copied. 00374 */ 00375 genericVector(const std::vector<T>& other); 00376 00377 /** 00378 * Destructor 00379 */ 00380 virtual ~genericVector(); 00381 00382 /** 00383 * Check whether this %object owns the data. 00384 * 00385 * @return \a false if this genericVector contains a reference to extern 00386 * data. 00387 */ 00388 inline bool ownsData() const; 00389 00390 /** 00391 * Restore ownership. 00392 * 00393 * If this object does not own its data, this member will create a new 00394 * memory buffer with the same data and will make this vector its owner. 00395 * 00396 * If this genericVector already owns its data nothing happens. 00397 */ 00398 void restoreOwnership(); 00399 00400 /** 00401 * Reference to extern data. 00402 * 00403 * This member allows the use of this %object as an wrapper-%object to 00404 * access some memory block as a genericVector. 00405 * The user must take care for memory allocation and deallocation of 00406 * the block. This %object will never delete the external data!. 00407 * @param theSize number of \e elements in the external block. Note that 00408 * this is NOT the number of bytes of the external block. 00409 * @param data pointer to the external memory block. 00410 * @param constRef if this parameter is true, it will not be possible to 00411 * change the pointer to the external memory block nor 00412 * to resize the genericVector. Despite this, the value of 00413 * each element can be changed by the access operators. 00414 * For Example: 00415 * \code 00416 * int i; 00417 * double tmp; 00418 * double a[10]; // memory block! 00419 * 00420 * for (i=0;i<10;i++) { 00421 * a[i]=2*i; // initialize the memory block 00422 * } 00423 * 00424 * cvr::genericVector<double> myVct; // an empty genericVector 00425 * 00426 * myVct.resize(5,0); // resize the genericVector: now 5 elements 00427 * // initialized with 0 00428 * 00429 * myVct.useExternData(10,a,true); // use the genericVector as wrapper 00430 * // for the memory block 00431 * 00432 * tmp = myVct.at(5); // tmp is now 10 00433 * 00434 * myVct.at(9) = 3; // the last element of myVct 00435 * // has now the value 3 00436 * 00437 * myVct.resize(5); // INVALID!! this will throw an exception 00438 * // constReferenceException() 00439 * \endcode 00440 * 00441 * 00442 * If \a theSize is greater than the allocated memory, the behaviour could 00443 * be unpredictible. 00444 */ 00445 void useExternData(const size_type theSize, 00446 pointer data, 00447 const eConstantReference constRef=VariableReference); 00448 00449 /* 00450 * Undocumented version of the useExternData method used to overwrite 00451 * the constness in very special situations (like resizing a matrix of 00452 * vectors which are in principle constant in memory reference) 00453 */ 00454 void useExternData(const size_type theSize, 00455 pointer data, 00456 const eConstantReference constRef, 00457 const bool forced); 00458 00459 00460 /** 00461 * Attach extern data to the vector. 00462 * 00463 * This member allows the use of this %object as an access-functor for 00464 * the 'data'. An access to the element at(x) is equivalent to 00465 * data[x]. 00466 * If \a theSize is an invalid dimension, the behaviour will be 00467 * unpredictible. 00468 * 00469 * The memory will be administrated by this genericVector 00470 * instance, and may be deleted if required (e.g. genericVector deleted 00471 * or resized!). The user should not try to manipulate the memory 00472 * allocation of the data after the attachment! See also 00473 * useExternData(). 00474 * 00475 * @param theSize number of elements of the genericVector 00476 * @param data a pointer to the memory block to be used 00477 * 00478 * Example: 00479 * \code 00480 * cvr::genericVector<int> myVct; 00481 * int block1[25]; 00482 * int* block2; 00483 * block2 = new int[25]; 00484 * 00485 * myVct.useExternData(25,block1); // ok 00486 * myVct.attach(25,block1); // wrong!!! matrix will try to manipulate 00487 * // stack memory: DO NOT DO THIS!!!!! 00488 * myVct.attach(25,block2); // ok! but do not try to delete the memory 00489 * // block2!! 00490 * \endcode 00491 */ 00492 void attach(const size_type theSize,pointer data); 00493 00494 /** 00495 * Free the data of this object and hand it over to the 00496 * "receiver". The value of ownsData is also transfered to the 00497 * receiver. (see Note). 00498 * 00499 * This function makes a "memory block transfusion" to another 00500 * genericVector. It is a very efficient way to make a copy of 00501 * this genericVector, if you don't need the source data anymore! 00502 * 00503 * \b Note: Take care that if the attach() or useExternData() 00504 * methods of this genericVector have been called before detachment, the 00505 * same rules for memory management apply now for the receiver. 00506 * 00507 * At the end of the detachment, this genericVector will be empty. 00508 * @param receiver the genericVector which will receive the memory 00509 * block. All data of that genericVector will be first deleted! 00510 */ 00511 void detach(genericVector<T>& receiver); 00512 00513 /** 00514 * Exchange (in a fast way) the data between this and the other 00515 * genericVector. Similar to detach(), this method will exchange the 00516 * complete memory blocks, avoiding an element-wise copy. 00517 * 00518 * @param other the genericVector with which the data will be 00519 * exchanged. 00520 */ 00521 void swap(genericVector<T>& other); 00522 00523 /** 00524 * Returns the number of elements of the genericVector 00525 */ 00526 inline size_type size() const; 00527 00528 /** 00529 * Returns first element as a const_iterator. 00530 * 00531 * Note that you can not change the values of the genericVector 00532 * elements when you use a const_iterator. See also begin() 00533 */ 00534 inline const_iterator begin() const; 00535 00536 /** 00537 * Returns iterator pointing to the first element. 00538 * 00539 * The use of the iterators is similar to the iterators of the 00540 * Standard Template Library (STL). 00541 * If you need to iterate on all elements of the genericVector, you can 00542 * use following code: 00543 * 00544 * \code 00545 * int tmp,accu; // a temporal variable 00546 * cvr::genericVector<int> myVct(10,1); // genericVector with 10 elements 00547 * // an iterator set to the beginning of myVct 00548 * cvr::genericVector<int>::iterator it=myVct.begin(); 00549 * // an iterator set to the end of myVct 00550 * cvr::genericVector<int>::iterator eit=myVct.begin(); 00551 * 00552 * for (; it!=eit ; ++it) { 00553 * tmp = *it; // tmp has value of element pointed 00554 * // by the iterator. 00555 * accu += tmp; 00556 * (*it) = accu; // change the value in the genericVector. 00557 * } 00558 * \endcode 00559 * 00560 * \note It is significantly faster in debug builds to use a pre-increment 00561 * with iterators (++it) than a post-increment (it++). 00562 * 00563 * Please note that if you define \a it as a const_iterator, 00564 * you can not do something like \a *it=accu. 00565 */ 00566 inline iterator begin(); 00567 00568 /** 00569 * Returns last index (in a genericVector this is always size()-1) 00570 */ 00571 inline size_type firstIndex() const; 00572 00573 /** 00574 * Returns last index (in a genericVector this is always size()-1) 00575 */ 00576 inline size_type lastIndex() const; 00577 00578 /** 00579 * Returns last index as a const_iterator. 00580 * 00581 * For an example see begin() 00582 */ 00583 inline const_iterator end() const; 00584 00585 /** 00586 * Returns last index as an iterator 00587 * 00588 * For an example see begin() 00589 */ 00590 inline iterator end(); 00591 00592 /** 00593 * This method returns an iterator that points to the \b last valid element 00594 * of the genericVector. It is used for inverse order iteration through the 00595 * genericVector using normal iterators (as opposed to reverse_iterators 00596 * used in the STL). This has the advantage that iterators going from front 00597 * to end and in the inverse direction are the same and can thus be 00598 * compared, copied etc. Further the implementation of reverse_iterators is 00599 * not as fast as that of iterators and thus not desired in the CVR-Lib. 00600 * 00601 * \code 00602 * igenericVector v(false,10); 00603 * int i,tmp; 00604 * for (i=0; i<10; i++) { 00605 * v.at(i)=i; 00606 * } 00607 * igenericVector::iterator forwardIt=v.begin(); 00608 * igenericVector::iterator backIt=v.inverseBegin(); 00609 * 00610 * while (forwardIt<=backIt) { 00611 * tmp = (*forwardIt); (*forwardIt)=(*backIt); (*backIt)=tmp; 00612 * ++forwardIt; ++backIt; 00613 * } 00614 * \endcode 00615 */ 00616 inline iterator inverseBegin(); 00617 00618 /** 00619 * Return an iterator that points to the \b last valid element of the 00620 * genericVector. See inverseBegin() for more details. 00621 */ 00622 inline const_iterator inverseBegin() const; 00623 00624 /** 00625 * Return an iterator that points to the element \b before the \b first 00626 * valid element of the genericVector. It is used to mark the end for 00627 * inverse order iteration through the genericVector using normal iterators 00628 * (as opposed to reverse_iterators as used in the STL). This has the 00629 * advantage that iterators going from front to end and in the inverse 00630 * direction are the same and can thus be compared, copied etc.Further the 00631 * implementation of reverse_iterators is not as fast as that of iterators 00632 * and thus not desired in the CVR-Lib. 00633 */ 00634 inline iterator inverseEnd(); 00635 00636 /** 00637 * Return an iterator that points to the element \b before the \b first 00638 * valid element of the genericVector. 00639 */ 00640 inline const_iterator inverseEnd() const; 00641 00642 /** 00643 * Change dimension and if desired the contents of the genericVector. 00644 * 00645 * @param newSize the new size of the genericVector 00646 * @param iniValue the initialization value. 00647 * @param resizeType specifies what should happen with the data of the 00648 * resized vector. 00649 * 00650 * For example: 00651 * \code 00652 * cvr::genericVector<int> myVct; // creates empty genericVector 00653 * myVct.resize(5,0); // genericVector with 5 elements initialized 00654 * // with 0 00655 * myVct.resize(10,2); // genericVector has now 10 elements: the 00656 * // first five are still 0 and the 00657 * // rest have a 2 00658 * myVct.resize(20,3,cvr::AllocateOnly); // now the genericVector has 20 00659 * // elements but their values 00660 * // are unknown. 00661 * myVct.resize(5,1,cvr::Init); // the genericVector has now 5 00662 * // elements initialized with 1 00663 * 00664 * \endcode 00665 * 00666 * If the resize is possible (see useExternData()), after the resize, this 00667 * %object will always own the data! 00668 * 00669 * @see eResizeType, resize(const size_type), allocate(), assign() 00670 */ 00671 void resize(const size_type newSize, 00672 const_reference iniValue, 00673 const eResizeType resizeType=CopyAndInit); 00674 00675 /** 00676 * Resize the vector keeping all the old elements, but without initializing 00677 * the new ones. 00678 * 00679 * This is an alias for resize(newSize,T(),Copy); 00680 * 00681 * @see resize(const int,const_reference,const eResizeType), 00682 * allocate(), 00683 * assign() 00684 */ 00685 inline void resize(const size_type newSize); 00686 00687 /** 00688 * Change the vector to contain exactltly the given number of elements. In 00689 * opposition to resize, allocate() does \b not copy the previous data but 00690 * only modifies the size of the vector, discarding all contained data and 00691 * leaving the new data uninitialized. 00692 * 00693 * This is in principle an alias to resize(newSize,T(),AllocateOnly). 00694 * 00695 * @param newSize new vector size. All current data will be discarded. 00696 * 00697 * @see eResizeType, 00698 * @see resize(const size_type,const_reference,const eResizeType) 00699 * @see resize(const size_type),allocate(), assign() 00700 */ 00701 inline void allocate(const size_type newSize); 00702 00703 /** 00704 * Assign \a newSize copies of \a initValue to the genericVector. 00705 * 00706 * Change the size and contents of this vector to be exactly \a newSize 00707 * elements long, with all the elements initialized to the value indicated 00708 * in \a initValue. In opposition to resize, this method does \b not copy 00709 * the previous data but changes the size of the vector, discarding all 00710 * contained data, replacing it with \a newSize copies of \a initValue. 00711 * 00712 * This is in principle an alias to resize(newSize,T(),Init). 00713 * 00714 * @param newSize new vector size. 00715 * @param initValue all elements of the vector will be initialized with 00716 * this value. 00717 */ 00718 inline void assign(const size_type newSize,const_reference initValue); 00719 00720 /** 00721 * Removes all elements from the genericVector (Set dimensions to 0) 00722 */ 00723 void clear(); 00724 00725 /** 00726 * Returns true if the genericVector is empty 00727 */ 00728 inline bool empty() const; 00729 00730 /** 00731 * Fills the genericVector elements with \a iniValue between 00732 * \a from and \a to. 00733 * @param iniValue the elements will be initialized with this 00734 * value. 00735 * @param from first element index 00736 * @param to last element index 00737 * 00738 * If \a from or \a to are out of bounds, 00739 * they will be (internaly) adjusted to to correct value. 00740 * 00741 * Example: 00742 * \code 00743 * cvr::genericVector<double> myVct(10,0); // genericVector with 10 00744 * // elements with 0 00745 * myVct.fill(9,1,3); // myVct=[0,9,9,9,0,0,0,0,0,0] 00746 * \endcode 00747 */ 00748 void fill(const_reference iniValue,const size_type from = 0, 00749 const size_type to = MaxIndex); 00750 00751 /** 00752 * Fills the genericVector elements with data pointed by \a data 00753 * between \a from and \a to. 00754 * @param data the data to by copied into this genericVector 00755 * @param from first element index 00756 * @param to last element index 00757 * 00758 * If \a from or \a to are out of bounds, 00759 * they will be (internaly) adjusted to to correct value. 00760 * 00761 * Example: 00762 * \code 00763 * double* data = {2,4,8,16}; 00764 * cvr::genericVector<double> myVct(10,0); // genericVector with 10 00765 * // elements with 0 00766 * myVct.fill(data,1,3); // myVct=[0,2,4,8,0,0,0,0,0,0] 00767 * \endcode 00768 */ 00769 void fill(const value_type data[],const size_type from = 0, 00770 const size_type to = MaxIndex); 00771 00772 /** 00773 * Fills the genericVector elements from \a from to \a to with the 00774 * elements of \a vct starting at \a startAt. 00775 * 00776 * @param vct genericVector with the elements to be copied 00777 * @param from first element index of the actual genericVector 00778 * @param to last element index of the actual genericVector 00779 * @param startAt start index of the source genericVector \a vct. 00780 * 00781 * Example: if a = [0 0 0 0 0] and b = [1 2 3], after a.fill(b,3,4,1) 00782 * results a = [0 0 0 2 3] 00783 */ 00784 void fill(const genericVector<T>& vct,const size_type from = 0, 00785 const size_type to = MaxIndex, 00786 const size_type startAt = 0); 00787 00788 /** 00789 * Access element x of the genericVector 00790 * 00791 * @param x index of the genericVector element to be accessed. It should 00792 * be between firstIndex() and lastIndex() 00793 */ 00794 inline reference at(const size_type x); 00795 00796 /** 00797 * Access element x of the genericVector in a read-only modus. 00798 * 00799 * @param x index of the genericVector element to be accessed. It should 00800 * be between firstIndex() and lastIndex() 00801 */ 00802 inline const_reference at(const size_type x) const; 00803 00804 /** 00805 * Access operator (alias for at(const size_type x)). 00806 */ 00807 inline reference operator[](const size_type x); 00808 00809 /** 00810 * Constant access operator (alias for at(const size_type x) const). 00811 */ 00812 inline const_reference operator[](const size_type x) const; 00813 00814 /** 00815 * Access element \p n of the genericVector. 00816 * (alias for at(const size_type x)) 00817 * 00818 * This member function is needed for generic programming with different 00819 * container types. 00820 */ 00821 inline reference elem(const size_type n); 00822 00823 /** 00824 * Constant access to element \p n of the genericVector. (alias for 00825 * at(const size_type x) const) 00826 * 00827 * This member function is needed for generic programming with different 00828 * container types. 00829 */ 00830 inline const_reference elem(const size_type n) const; 00831 00832 /** 00833 * Assigment operator. 00834 * 00835 * Copy the contents of \a other in this %object. 00836 * 00837 * If this instance has a constReference, then only the contents are 00838 * copied. 00839 * 00840 * @param other the source genericVector to be copied. 00841 * 00842 * @return Reference to the current object. 00843 */ 00844 genericVector<T>& copy(const genericVector<T>& other); 00845 00846 /** 00847 * Assignment operator. 00848 * 00849 * Copy a specified interval of elements of another genericVector. At the 00850 * end this vector will contain t-f+1 elements, where f=max(0,from) and 00851 * t=min(other.lastIndex(),to). 00852 * 00853 * @param other the genericVector to be copied. 00854 * @param from starting point included 00855 * @param to end point included. 00856 * 00857 * @return Reference to the current object. 00858 */ 00859 genericVector<T>& copy(const genericVector<T>& other, 00860 const size_type from, 00861 const size_type to=MaxIndex); 00862 00863 /** 00864 * Assignment operator. 00865 * 00866 * Copy the specified elements of \a other into this %object. \a idx can 00867 * contain the same index more than once. 00868 * 00869 * @param other the genericVector to be copied. 00870 * @param idx indices of the elements to be copied 00871 * 00872 * @return Reference to the current object. 00873 */ 00874 genericVector<T>& copy(const genericVector<T>& other, 00875 const genericVector<size_type>& idx); 00876 00877 /** 00878 * Assigment operator (alias for copy(other)). 00879 * 00880 * @param other the genericVector to be copied 00881 * 00882 * @return a reference to the actual genericVector 00883 */ 00884 genericVector<T>& operator=(const genericVector<T>& other); 00885 00886 /** 00887 * Returns the name of this type 00888 */ 00889 virtual const std::string& name() const; 00890 00891 /** 00892 * Create a clone of this genericVector. 00893 * 00894 * @return A pointer to a copy of this genericVector. 00895 */ 00896 virtual genericVector<T>* clone() const; 00897 00898 /** 00899 * Create a clone of this genericVector. 00900 * 00901 * @return a pointer to a copy of this genericVector 00902 */ 00903 virtual genericVector<T>* newInstance() const; 00904 00905 /** 00906 * Compare this genericVector with other. 00907 * 00908 * @param other the other genericVector to be compared with 00909 * 00910 * @return true if both genericVectors have the same elements and same size 00911 */ 00912 bool equals(const genericVector<T>& other) const; 00913 00914 /** 00915 * Compare this genericVector with other. 00916 * 00917 * @param other the other genericVector to be compared with 00918 * 00919 * @return true if both genericVectors have the same elements and same size 00920 */ 00921 inline bool operator==(const genericVector<T>& other) const; 00922 00923 /** 00924 * Compare this genericVector with other. 00925 * 00926 * @param other the other genericVector to be compared with. 00927 * 00928 * @return true if both genericVectors different dimensions or elements 00929 */ 00930 inline bool operator!=(const genericVector<T>& other) const; 00931 00932 /** 00933 * Copy the \a other genericVector by casting each of its elements. 00934 * 00935 * @param other The genericVector to be copied. 00936 * 00937 * For Example: 00938 * \code 00939 * cvr::genericVector<int> vctA(10,1); // a genericVector of integers 00940 * cvr::genericVector<double> vctB; // a genericVector of doubles 00941 * 00942 * vctB.castFrom(vctA); // this will copy vctA in vctB!! 00943 * \endcode 00944 */ 00945 template<typename U> 00946 genericVector<T>& castFrom(const genericVector<U>& other); 00947 00948 /** 00949 * This is just an alias for copy(const genericVector<T>&) to facilitate 00950 * generic programming. 00951 * 00952 * @param other The genericVector to be copied. 00953 */ 00954 genericVector<T>& castFrom(const genericVector<T>& other); 00955 00956 /** 00957 * Copy a subvector of the \a other genericVector by casting each of its 00958 * elements. 00959 * 00960 * @param other The genericVector to be copied. 00961 * @param from starting point included 00962 * @param to end point included. 00963 * 00964 * @return A reference to the current casted subvector 00965 */ 00966 template<typename U> 00967 genericVector<T>& castFrom(const genericVector<U>& other, 00968 const size_type from, 00969 const size_type to=MaxIndex); 00970 00971 00972 /** 00973 * This is just an alias for copy(const genericVector<T>&, const size_type 00974 * from, const size_type to) to facilitate generic programming. 00975 * 00976 * @param other The genericVector to be copied. 00977 * @param from starting point included 00978 * @param to end point included 00979 */ 00980 genericVector<T>& castFrom(const genericVector<T>& other, 00981 const size_type from, 00982 const size_type to=MaxIndex); 00983 00984 /** 00985 * Cast from a std::genericVector of the same type 00986 */ 00987 template<typename U> 00988 genericVector<T>& castFrom(const std::vector<U>& other); 00989 00990 /** 00991 * @name Apply Methods 00992 */ 00993 //@{ 00994 00995 /** 00996 * Applies a C-function to each element of the genericVector. 00997 * 00998 * In the following example, %genericVector \a vct is initialized with 00999 * 4.0. After applying \a sqrt(), all elements of \a vct are 2.0. 01000 * \code 01001 * genericVector<float> vct(4,4.0); 01002 * vct.apply(sqrt); 01003 * \endcode 01004 * 01005 * @param function a pointer to a C-function 01006 * 01007 * @return A reference to the actual genericVector 01008 */ 01009 genericVector<T>& apply(T (*function)(T)); 01010 01011 /** 01012 * Applies a C-function to each element of the other genericVector and 01013 * leaves the result here. 01014 * 01015 * @param other the source genericVector 01016 * @param function a pointer to a C-function 01017 * 01018 * @return A reference to the actual genericVector 01019 */ 01020 genericVector<T>& apply(const genericVector<T>& other,T (*function)(T)); 01021 01022 /** 01023 * Applies a C-function to each element of the genericVector. 01024 * 01025 * @param function a pointer to a C-function 01026 * 01027 * @return A reference to the actual genericVector 01028 */ 01029 genericVector<T>& apply(T (*function)(const T&)); 01030 01031 /** 01032 * Applies a C-function to each element the other genericVector and 01033 * leaves the result here. 01034 * 01035 * @param other the genericVector with the source data 01036 * @param function a pointer to a C-function 01037 * 01038 * @return A reference to the actual genericVector 01039 */ 01040 genericVector<T>& apply(const genericVector<T>& other, 01041 T (*function)(const T&)); 01042 01043 /** 01044 * A two-parameter C-function receives the i-th elements of this 01045 * and the given genericVector and the result will be left in this 01046 * genericVector. Note that both genericVectors MUST have the same size! 01047 * If both genericVectors have different size, the function will throw an 01048 * assertion without changing anything! 01049 * 01050 * @param other the second genericVector to be considered (the first 01051 * genericVector will be this object!) 01052 * @param function a pointer to a two parameters C-function 01053 * 01054 * @return A reference to the actual genericVector. 01055 */ 01056 genericVector<T>& apply(const genericVector<T>& other, 01057 T (*function)(const T&,const T&)); 01058 01059 /** 01060 * A two-parameter C-function receives the i-th elements of this 01061 * and the given genericVector and the result will be left in this 01062 * genericVector. Note that both genericVectors MUST have the same size! 01063 * If both genericVectors have different size, the function will throw an 01064 * assertion without changing anything! 01065 * 01066 * @param other the second genericVector to be considered (the first 01067 * genericVector will be this object!) 01068 * @param function a pointer to a two parameters C-function 01069 * 01070 * @return A reference to the actual genericVector. 01071 */ 01072 genericVector<T>& apply(const genericVector<T>& other,T (*function)(T,T)); 01073 01074 /** 01075 * A two-parameter C-function receives the i-th elements of the 01076 * given genericVectors and leaves the result here. 01077 * Note that both genericVectors MUST have the same size! 01078 * If both genericVectors have different size, the function will throw an 01079 * assertion without changing anything! 01080 * 01081 * The following example uses cvr::min as function. The genericVectors \a 01082 * a and \a b contain the values [1,2,3,4] and [4,3,2,1], 01083 * respectively. After applying the function, %genericVector \a c 01084 * contains the values [1,2,2,1]. 01085 * \code 01086 * igenericVector a,b,c; 01087 * int i=0; 01088 * for (i=0; i<4; ++i) { 01089 * a.at(i)=i+1; 01090 * b.at(i)=4-i; 01091 * } 01092 * c.apply(a,b,cvr::min); 01093 * \endcode 01094 * 01095 * @param a the first genericVector 01096 * @param b the second genericVector 01097 * @param function a pointer to a two parameters C-function 01098 * 01099 * @return A reference to the actual genericVector 01100 */ 01101 genericVector<T>& apply(const genericVector<T>& a, 01102 const genericVector<T>& b, 01103 T (*function)(const T&,const T&)); 01104 01105 /** 01106 * A two-parameter C-function receives the i-th elements of the 01107 * given genericVectors and leaves the result here. 01108 * 01109 * Note that both genericVectors MUST have the same size! 01110 * If both genericVectors have different size, the function will throw an 01111 * assertion without changing anything! 01112 * 01113 * @param a the first genericVector 01114 * @param b the second genericVector 01115 * @param function a pointer to a two parameters C-function 01116 * 01117 * @return a reference to the actual genericVector 01118 */ 01119 genericVector<T>& apply(const genericVector<T>& a, 01120 const genericVector<T>& b, 01121 T (*function)(T,T)); 01122 01123 //@} 01124 01125 /** 01126 * @name Input and Output 01127 */ 01128 //@{ 01129 /** 01130 * Write the object in the given ioHandler 01131 */ 01132 virtual bool write(ioHandler& handler,const bool complete = true) const; 01133 01134 /** 01135 * Read the object from the given ioHandler 01136 */ 01137 virtual bool read(ioHandler& handler,const bool complete = true); 01138 //@} 01139 01140 protected: 01141 /** 01142 * Dimension of the genericVector. 01143 */ 01144 size_type vectorSize_; 01145 01146 /** 01147 * Index of the last element of the genericVector (always 01148 * vectorSize-1) 01149 * 01150 * We sacrifice this sizeof(int) to improve speed in many operations that 01151 * require this last index. 01152 */ 01153 size_type idxLastElement_; 01154 01155 /** 01156 * Pointer to the first element of the genericVector. 01157 */ 01158 pointer theElements_; 01159 01160 /** 01161 * Reference to extern data. 01162 * 01163 * If this value ist \a false, then the data pointed by 01164 * \a theElements will never be deleted in this 01165 * %object! (see useExternData()) 01166 */ 01167 bool ownData_; 01168 01169 /** 01170 * If constReference=true, is not possible to resize or change 01171 * the reference of this genericVector. 01172 * 01173 * Very important for an efficient matrix class!! 01174 * (see useExternData()) 01175 */ 01176 eConstantReference constReference_; 01177 01178 /** 01179 * @name Memory allocation and deallocation 01180 * 01181 * Restrict all memory allocation and deallocation to these functions. 01182 */ 01183 //@{ 01184 /** 01185 * Reserve a memory block of the given size, and ensure ownership and 01186 * release previous memory. 01187 */ 01188 void reserveMem(const size_type size); 01189 01190 /** 01191 * Reserve a memory block of the given size, but leave this vector 01192 * untouched, since some other checks have to be done first 01193 */ 01194 pointer reserveNewMem(const size_type size) const; 01195 01196 /** 01197 * Release all reserved memory. 01198 * 01199 * Clean all memory and leave the vector in a clean, empty state. 01200 */ 01201 void releaseMem(); 01202 01203 /** 01204 * Release the block pointed, but leave this vector alone. 01205 */ 01206 void releaseMem(pointer& block) const; 01207 //@} 01208 01209 }; 01210 01211 } // namespace cvr 01212 01213 namespace std { 01214 /* 01215 * outputs the elements of the genericVector on a std::stream 01216 */ 01217 template <typename T> 01218 ostream& operator<<(ostream& s,const cvr::genericVector<T>& v); 01219 } 01220 01221 #include "cvrGenericVector_inline.h" 01222 #include "cvrGenericVector_template.h" 01223 01224 #else 01225 01226 #include "cvrGenericVector_template.h" 01227 01228 #endif 01229