latest version v1.9 - last update 10 Apr 2010 |
00001 /* 00002 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 00003 * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany 00004 * 00005 * This file is part of the LTI-Computer Vision Library (LTI-Lib) 00006 * 00007 * The LTI-Lib is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public License (LGPL) 00009 * as published by the Free Software Foundation; either version 2.1 of 00010 * the License, or (at your option) any later version. 00011 * 00012 * The LTI-Lib is distributed in the hope that it will be 00013 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty 00014 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with the LTI-Lib; see the file LICENSE. If 00019 * not, write to the Free Software Foundation, Inc., 59 Temple Place - 00020 * Suite 330, Boston, MA 02111-1307, USA. 00021 */ 00022 00023 00024 /*---------------------------------------------------------------- 00025 * project ....: LTI Digital Image/Signal Processing Library 00026 * file .......: ltiSequence.h 00027 * authors ....: Pablo Alvarado 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 28.06.00 00030 * revisions ..: $Id: ltiSequence.h,v 1.4 2006/02/08 12:42:52 ltilib Exp $ 00031 */ 00032 00033 #ifndef _LTI_SEQUENCE_H_ 00034 #define _LTI_SEQUENCE_H_ 00035 00036 #include "ltiObject.h" 00037 #include <vector> 00038 #include "ltiTypes.h" 00039 #include "ltiIoObject.h" 00040 #include "ltiMath.h" 00041 #include "ltiException.h" 00042 #include "ltiAssert.h" 00043 00044 namespace lti { 00045 /** 00046 * sequence container class. 00047 * The %lti::sequence class allows the representation of sequences of other 00048 * lti objects. It can be used as a sort of vector of other objects. 00049 * 00050 * The elements of the sequence will be indexed between 0 an n-1. 00051 * 00052 * An example of a sequence of lti::vectors: 00053 * 00054 * \code 00055 * // creates a sequence of 256 vectors 00056 * lti::sequence< lti::vector<float> > mySeq(256) 00057 * 00058 * \endcode 00059 * 00060 * To access the vector elements use the access operators at() (or the 00061 * overloaded operator[]()). For example: 00062 * 00063 * \code 00064 * for (int i = 0; i < mySeq.size(); i++) { 00065 * // each element of the sequence is a vector filled with i/2 00066 * mySeq.at(i).resize(10,float(i)/2.0f); 00067 * } 00068 * \endcode 00069 * 00070 * The sequence has following methods: 00071 * - Constructors Constructors 00072 * - You can construct an empty sequence with the default constructor 00073 * (sequence()). 00074 * - If you know the number of elements use 00075 * sequence(const int& size) 00076 * - You can create the copy of another sequence with the copy 00077 * constructor (sequence(const sequence<T>& otherSequence)) 00078 * - Access members 00079 * - at(), operator[]() 00080 * - The size() member returns the number of elements of the vector. 00081 * - The firstIdx() will return in a sequence always 0 and the lastIdx() 00082 * will be always size()-1; 00083 * - Fill and Copy members 00084 * - With the fill() members you can fill the sequence with a given 00085 * constant value T or with values taken from other sequences. 00086 * - With the copy() member you can copy another sequence. 00087 * - Iterators 00088 * - It is possible to iterate within the sequence by making use of 00089 * the sequence iterators. (see begin() for more information) 00090 */ 00091 template<class T> 00092 class sequence : public ioObject { 00093 public: 00094 typedef T value_type; 00095 00096 /** 00097 * iterator type (allows read and write operations) 00098 * The use of the iterator classes is similar to the iterators of 00099 * the STL (Standard Template Library). See lti::sequence::begin() 00100 * for an example 00101 */ 00102 typedef typename std::vector<T>::iterator iterator; 00103 00104 /** 00105 * const iterator type (allows read-only operations) 00106 * The use of the iterator classes is similar to the iterators of 00107 * the STL (Standard Template Library). See lti::sequence::begin() 00108 * for an example. 00109 */ 00110 typedef typename std::vector<T>::const_iterator const_iterator; 00111 00112 /** 00113 * default constructor creates an empty sequence; 00114 */ 00115 sequence(); 00116 00117 /** 00118 * create a sequence of the given size. 00119 * 00120 * The member will not be initialized 00121 * @param theSize number of elements of the sequence 00122 */ 00123 sequence(const int& theSize); 00124 00125 /** 00126 * create this sequence as a copy of another sequence 00127 * @param other the sequence to be copied. 00128 */ 00129 sequence(const sequence<T>& other); 00130 00131 /** 00132 * destructor 00133 */ 00134 virtual ~sequence(); 00135 00136 /** 00137 * returns the name of this class: "sequence" 00138 */ 00139 const char* getTypeName() const {return "sequence";}; 00140 00141 /** 00142 * returns the number of elements of the sequence 00143 */ 00144 virtual int size() const; 00145 00146 /** 00147 * returns first index (normally 0) 00148 */ 00149 virtual int firstIdx() const; 00150 00151 /** 00152 * returns first element as a const_iterator. 00153 * Note that you can not change the values of the sequence 00154 * elements when you use a const_iterator. See also begin() 00155 */ 00156 virtual const_iterator begin() const; 00157 00158 /** 00159 * returns an iterator pointing to the first element of the sequence 00160 * The use of the interators is similar to the iterators of the 00161 * Standard Template Library (STL). 00162 * If you need to iterate on all elements of the sequence, you can 00163 * use following code: 00164 * \code 00165 * int accu = 1; 00166 * lti::sequence<vector<int> > mySeq(10); // a sequence with 10 elements 00167 * lti::sequence<vector<int> >::iterator it; // an iterator 00168 * 00169 * for (it=mySeq.begin();it!=mySeq.end();it++) { 00170 * vector<int>& tmp = *it; // tmp is a reference to the vector 00171 * // pointed by the iterator. 00172 * accu++; 00173 * tmp.resize(accu,accu); // resize and initialize the vector 00174 * } 00175 * \endcode 00176 */ 00177 virtual iterator begin(); 00178 00179 /** 00180 * returns last index (in a sequence this is always size()-1) 00181 */ 00182 virtual int lastIdx() const; 00183 00184 /** 00185 * returns last index as a const iterator. 00186 * For an example see begin() 00187 */ 00188 virtual const_iterator end() const; 00189 00190 /** 00191 * returns last index as an iterator 00192 * For an example see begin() 00193 */ 00194 virtual iterator end(); 00195 00196 /** 00197 * append element to sequence. 00198 * @param theElement will be appended to the end of the sequence. 00199 * 00200 * Automatically resizes the vector. To remove Elements from end of sequence 00201 * use <code>resize()</code> 00202 */ 00203 virtual void append(const T& theElement = T()); 00204 00205 /** 00206 * change dimension of the sequence. 00207 * @param newSize the new size of the sequence 00208 * @param iniValue the initialization value. 00209 * @param copyData if this parameter is true, the old data of the 00210 * sequence will be copied. If it is false, the old data 00211 * will be lost. 00212 * @param initNew if this parameter is true, then all new elements (if 00213 * they exist) will be initialized with 00214 * <code>iniValue</code>. 00215 * if <code>initNew</code> is false, then the new 00216 * elements are left uninitialized. 00217 * 00218 * For example: 00219 * \code 00220 * lti::sequence<int> myVct; // creates empty sequence 00221 * myVct.resize(5,0); // sequence with 5 elements initialized 00222 * // with 0 00223 * myVct.resize(10,2); // sequence has now 10 elements: the 00224 * // first five are still 0 and the 00225 * // rest have a 2 00226 * myVct.resize(20,3,false,false); // now the sequence has 20 00227 * // elements but their values 00228 * // are unknown. 00229 * myVct.resize(5,1,false,true); // the sequence has now 5 00230 * // elements initialized with 1 00231 * 00232 * // note that the last line could also be written: 00233 * 00234 * myVct.resize(5,1,false); // the sequence has now 5 00235 * // elements initialized with 1 00236 * 00237 * \endcode 00238 * 00239 * If the resize is possible (see useExternData()), this %object 00240 * will always owns the data! 00241 */ 00242 virtual void resize(const int& newSize, 00243 const T& iniValue = T(), 00244 const bool& copyData = true, 00245 const bool& initNew = true); 00246 00247 /** 00248 * fills the sequence elements with <code>iniValue</code> between 00249 * <code>from</code> and <code>to</code>. 00250 * @param iniValue the elements will be initialized with this 00251 * value. 00252 * @param from first element index 00253 * @param to last element index 00254 * 00255 * If <code>from</code> or <code>to</code> are out of bounds, 00256 * they will be (internaly) adjusted to to correct value. 00257 * 00258 * Example: 00259 * \code 00260 * lti::sequence<double> myVct(10,0); // sequence with 10 elements 00261 * // with 0 00262 * myVct.fill(9,1,3); // myVct=[0,9,9,9,0,0,0,0,0,0] 00263 * \endcode 00264 */ 00265 virtual void fill(const T& iniValue,const int& from = 0, 00266 const int& to = MaxInt32); 00267 00268 /** 00269 * fills the sequence elements from <code>from</code> to 00270 * <code>to</code> with the elements of <code>vct</code> starting 00271 * at <code>startAt</code>. 00272 * @param vct sequence with the elements to be copied 00273 * @param from first element index of the actual sequence 00274 * @param to last element index of the actual sequence 00275 * @param startAt start index of the source sequence <code>vct</code>. 00276 * 00277 * Example: if a = [0 0 0 0 0] and b = [1 2 3], after a.fill(b,3,4,1) 00278 * results a = [0 0 0 2 3] 00279 */ 00280 virtual void fill(const sequence<T>& vct,const int& from = 0, 00281 const int& to = MaxInt32, 00282 const int& startAt = 0); 00283 00284 /** 00285 * access element x of the sequence 00286 * @param x index of the sequence element to be accessed. It should 00287 * be between firstIdx() and lastIdx() 00288 */ 00289 virtual T& at(const int& x); 00290 00291 /** 00292 * access element x of the sequence in a read-only modus 00293 * @param x index of the sequence element to be accessed. It should 00294 * be between firstIdx() and lastIdx() 00295 */ 00296 virtual const T& at(const int& x) const; 00297 00298 /** 00299 * access operator (alias for at(const int& x)). 00300 */ 00301 virtual T& operator[](const int& x); 00302 00303 /** 00304 * const access operator (alias for at(const int& x) const). 00305 */ 00306 virtual const T& operator[](const int& x) const; 00307 00308 /** 00309 * assigment operator. 00310 * copy the contents of <code>other</code> in this %object. 00311 * @param other the source sequence to be copied. 00312 */ 00313 virtual sequence<T>& copy(const sequence<T>& other); 00314 00315 /** 00316 * create a clone of this sequence 00317 * @return a pointer to a copy of this sequence 00318 */ 00319 virtual object* clone() const; 00320 00321 /** 00322 * compare this sequence with other 00323 * @param other the other sequence to be compared with 00324 * @return true if both sequences have the same elements and same size 00325 */ 00326 virtual bool equals(const sequence<T>& other) const; 00327 00328 /** 00329 * compare this sequence with other 00330 * @param other the other sequence to be compared with 00331 * @return true if both sequences have the same elements and same size 00332 */ 00333 virtual bool operator==(const sequence<T>& other) const; 00334 00335 /** 00336 * assigment operator (alias for copy(other)). 00337 * @param other the sequence to be copied 00338 * @return a reference to the actual sequence 00339 */ 00340 virtual sequence<T>& 00341 operator=(const sequence<T>& other) {return copy(other);}; 00342 00343 /** 00344 * concatenate another sequence to this one 00345 * the elements of the other sequence will be concatenated 00346 * @param other the sequence to be concatenated at the end of this one 00347 * @return a reference to this sequence 00348 */ 00349 virtual sequence<T>& concatenate(const sequence<T>& other); 00350 00351 /** 00352 * write the sequence in the given ioHandler 00353 */ 00354 virtual bool write(ioHandler& handler,const bool complete = true) const; 00355 00356 /** 00357 * read the sequence from the given ioHandler 00358 */ 00359 virtual bool read(ioHandler& handler,const bool complete = true); 00360 00361 protected: 00362 00363 /// this sequence class is implemented with a std::vector instance 00364 std::vector<T> theSequence; 00365 00366 }; 00367 00368 /** @name Storable interface 00369 * Members for the storable interface 00370 */ 00371 00372 //@{ 00373 /** 00374 * read the sequence from the given ioHandler. The complete flag indicates 00375 * if the enclosing begin and end should be also be readed 00376 */ 00377 template<class T> 00378 bool read(ioHandler& handler,sequence<T>& seq,const bool complete=true) { 00379 return seq.read(handler,complete); 00380 } 00381 00382 /** 00383 * write the sequence in the given ioHandler. The complete flag indicates 00384 * if the enclosing begin and end should be also be written or not 00385 */ 00386 template<class T> 00387 bool write(ioHandler& handler, const sequence<T>& seq,const bool complete=true) { 00388 return seq.write(handler,complete); 00389 } 00390 00391 //@} 00392 00393 } // namespace lti 00394 00395 /* 00396 // outputs the elements of the vector on a stream 00397 template <class T> 00398 std::ostream& operator<<(std::ostream& s,const lti::sequence<T>& v); 00399 */ 00400 00401 #include "ltiSequence_template.h" 00402 00403 #endif