Vector_T.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //==========================================================================
00004 /**
00005  *  @file    Vector_T.h
00006  *
00007  *  $Id: Vector_T.h 80826 2008-03-04 14:51:23Z wotte $
00008  *
00009  *  @author Craig L. Ching <cching@mqsoftware.com>
00010  *  @author Gonzalo Diethelm <gonzalo.diethelm@aditiva.com>
00011  */
00012 //==========================================================================
00013 
00014 #ifndef ACE_VECTOR_T_H
00015 #define ACE_VECTOR_T_H
00016 
00017 #include /**/ "ace/pre.h"
00018 
00019 #include "ace/Array.h"
00020 
00021 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00022 # pragma once
00023 #endif /* ACE_LACKS_PRAGMA_ONCE */
00024 
00025 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00026 
00027 /*
00028  * Default size for an ACE_Vector.
00029  */
00030 static const size_t ACE_VECTOR_DEFAULT_SIZE = 32;
00031 
00032 // Forward declaration.
00033 template <class T, size_t DEFAULT_SIZE> class ACE_Vector_Iterator;
00034 
00035 /**
00036  * @class ACE_Vector
00037  *
00038  * @brief Defines an STL-like vector container.
00039  *
00040  * This is an STL-like template vector container, a wrapper around
00041  * ACE_Array.  It provides at least the basic std::vector look and
00042  * feel: push_back(), clear(), resize(), capacity().  This template
00043  * class uses the copy semantic paradigm, though it is okay to use
00044  * reference counted smart pointers (see ACE_Ptr&lt;T&gt;) with this
00045  * template class.
00046  *
00047  * <b> Requirements and Performance Characteristics</b>
00048  *   - Internal Structure
00049  *       ACE_Array
00050  *   - Duplicates allowed?
00051  *       Yes
00052  *   - Random access allowed?
00053  *       No
00054  *   - Search speed
00055  *       N/A
00056  *   - Insert/replace speed
00057  *       Linear
00058  *   - Iterator still valid after change to container?
00059  *       Yes
00060  *   - Frees memory for removed elements?
00061  *       No
00062  *   - Items inserted by
00063  *       Value
00064  *   - Requirements for contained type
00065  *       -# Default constructor
00066  *       -# Copy constructor
00067  *       -# operator=
00068  */
00069 template<class T, size_t DEFAULT_SIZE = ACE_VECTOR_DEFAULT_SIZE>
00070 class ACE_Vector : public ACE_Array<T>
00071 {
00072 public:
00073   /**
00074    * A short name for iterator for ACE_Vector.
00075    */
00076   typedef ACE_Vector_Iterator<T, DEFAULT_SIZE> Iterator;
00077 
00078 
00079   /**
00080    * General constructor.
00081    *
00082    * @param init_size Initial size of the vector with the default
00083    *                  value of DEFAULT_SIZE
00084    * @param alloc Pointer to an ACE allocator.  If it is NULL then the
00085    *              default ACE allocator is used
00086    */
00087   ACE_Vector (const size_t init_size = DEFAULT_SIZE,
00088               ACE_Allocator* alloc = 0);
00089 
00090   /**
00091    * Destructor.
00092    */
00093   ~ACE_Vector ();
00094 
00095   /**
00096    * Returns the current vector capacity, that is, the currently
00097    * allocated buffer size.
00098    *
00099    * @return Current buffer size of the vector
00100    */
00101   size_t capacity (void) const;
00102 
00103   /**
00104    * Returns the vector's dynamic size / actual current size of the
00105    * vector.  Do not confuse it with ACE_Array::size(), which returns
00106    * the array's capacity.  Unfortunately, ACE is not very consistent
00107    * with the function names.
00108    *
00109    * @return Dynamic size / actual current size of the vector.
00110    */
00111   size_t size (void) const;
00112 
00113   /**
00114    * Clears out the vector.  It does not reallocate the vector's
00115    * buffer, it is just sets the vector's dynamic size to 0.
00116    */
00117   void clear (void);
00118 
00119   /**
00120    * Resizes the vector to the new capacity.  If the vector's current
00121    * capacity is smaller than the size to be specified, then the
00122    * buffer gets reallocated.  If the new capacity is less than the
00123    * current capacity of the vector, the buffer size stays the same.
00124    *
00125    * @param new_size New capacity of the vector
00126    * @param t A filler value (of the class T) for initializing the
00127    *          elements of the vector with.  By default, if this
00128    *          parameter is not specified, the default value of the
00129    *          class T will be used (for more detail, see the
00130    *          initialization clause for this parameter).
00131    */
00132   void resize (const size_t new_size,
00133                const T& t);
00134 
00135   /**
00136    * Appends a new element to the vector ("push back").  If the
00137    * dynamic size of the vector is equal to the capacity of the vector
00138    * (vector is at capacity), the vector automatically doubles its
00139    * capacity.
00140    *
00141    * @param elem A reference to the new element to be appended.  By
00142    *             default, this parameters gets initialized with the
00143    *             default value of the class T.
00144    */
00145   void push_back (const T& elem);
00146 
00147   /**
00148    * Deletes the last element from the vector ("pop back").  What this
00149    * function really does is decrement the dynamic size of the
00150    * vector.  The vector's buffer does not get reallocated for
00151    * performance.
00152    */
00153   void pop_back (void);
00154 
00155   /**
00156    * This function dumps the content of the vector.  TO BE MOVED out
00157    * of this class.  It needs to be implemented as a global template
00158    * function that accepts a const ACE_Vector&lt;T&gt;, in order to
00159    * make instances of this class compile on Linux, AIX.  G++ and xlC
00160    * have template instantiation algoriths, which are different from
00161    * the one in Visual C++.  The algorithms try to instantiate ALL
00162    * methods declared in the template class, regardless of whether the
00163    * functions are used or not.  That is, all of the classes, that are
00164    * used as elements in ACE_Vector's, have to have the dump() methods
00165    * defined in them (seems to be overkill).
00166    *
00167    * This function calls T::dump() for each element of the vector.
00168    */
00169   void dump (void) const;
00170 
00171   // = Compare operators
00172 
00173   ///Equality comparison operator.
00174   /**
00175    * Compare this vector with @arg s for equality.  Two vectors are equal
00176    * if their sizes are equal and all the elements are equal.
00177    */
00178   bool operator== (const ACE_Vector<T, DEFAULT_SIZE> &s) const;
00179 
00180   ///Inequality comparison operator.
00181   /**
00182    * Compare this vector with @arg s for inequality such that @c *this !=
00183    * @arg s is always the complement of the boolean return value of
00184    * @c *this == @arg s.
00185    */
00186   bool operator!= (const ACE_Vector<T, DEFAULT_SIZE> &s) const;
00187 
00188   void swap (ACE_Vector &rhs);
00189 
00190 protected:
00191 
00192   /**
00193    * Dynamic size (length) of the vector.
00194    */
00195   size_t length_;
00196 
00197   /**
00198    * Current capacity (buffer size) of the vector.
00199    */
00200   size_t curr_max_size_;
00201 
00202   friend class ACE_Vector_Iterator<T, DEFAULT_SIZE>;
00203 };
00204 
00205 #if 0
00206 /*
00207  * Not sure about including these functions, if for no other reason,
00208  * because they polute the global namespace!
00209  */
00210 
00211 /**
00212  * Compare two vectors in the range of [from_ndx..to_ndx].  This
00213  * template function requires class T to have the bool operator!=()
00214  * declared in the class.  It is safe to define vectors of scalar data
00215  * types, like int, double, etc., including class ACE_TString.
00216  *
00217  * @param v1 The first vector (out of the two) to be compared.
00218  * @param v2 The Second vector (out of the two) to be compared.
00219  * @param from_ndx Compare vector v1 and v2, starting with the
00220  *                 "from_ndx" index .
00221  * @param to_ndx Compare vector v1 and v2, from "from_ndx" to
00222  *               "to_ndx".
00223  * @return Returns true if v1==v2 in the specified index range,
00224  *          returns false otherwise.  Also, returns false in case if
00225  *          v1's size is not equal to v2's size.
00226  */
00227 template<class T>
00228 int compare (const ACE_Vector<T>& v1,
00229               const ACE_Vector<T>& v2,
00230               const size_t from_ndx,
00231               const size_t to_ndx);
00232 
00233 /**
00234  * Does a partial comparison of two vectors in the range of
00235  * [from_ndx..to_ndx].  The only difference between this function and
00236  * the template compare&lt;T&gt; function is that this function does
00237  * not require v1 and v2 to be of equal size.
00238  *
00239  * @param v1 The first vector (out of the two) to be compared.
00240  * @param v2 The Second vector (out of the two) to be compared.
00241  * @param from_ndx Compare vector v1 and v2, starting with the
00242  *                 "from_ndx" index .
00243  * @param to_ndx Compare vector v1 and v2, from "from_ndx" to
00244  *               "to_ndx".
00245  * @return Returns true if vector v1 and v2 are equal in the specified
00246  *         index range.
00247  */
00248 
00249 template<class T>
00250 int partial_compare (const ACE_Vector<T>& v1,
00251                       const ACE_Vector<T>& v2,
00252                       const size_t from_ndx,
00253                       const size_t to_ndx);
00254 #endif /* 0 */
00255 // ****************************************************************
00256 
00257 /**
00258  * @class ACE_Vector_Iterator
00259  *
00260  * @brief Implement an iterator over an ACE_Vector.
00261  *
00262  * This iterator is safe in the face of vector element deletions.
00263  * But it is NOT safe if the vector is resized via the assignment
00264  * operator during iteration.  That would be very odd, and dangerous.
00265  */
00266 template <class T, size_t DEFAULT_SIZE = ACE_VECTOR_DEFAULT_SIZE>
00267 class ACE_Vector_Iterator
00268 {
00269 public:
00270   // = Initialization method.
00271   ACE_Vector_Iterator (ACE_Vector<T, DEFAULT_SIZE> &);
00272 
00273   // = Iteration methods.
00274 
00275   /// Pass back the <next_item> that hasn't been seen in the vector.
00276   /// Returns 0 when all items have been seen, else 1.
00277   int next (T *&next_item);
00278 
00279   /// Move forward by one element in the vector.  Returns 0 when all the
00280   /// items in the vector have been seen, else 1.
00281   int advance (void);
00282 
00283   /// Returns 1 when all items have been seen, else 0.
00284   int done (void) const;
00285 
00286   /// Dump the state of an object.
00287   void dump (void) const;
00288 
00289   /// Declare the dynamic allocation hooks.
00290   ACE_ALLOC_HOOK_DECLARE;
00291 
00292 private:
00293   /// Pointer to the current item in the iteration.
00294   size_t current_;
00295 
00296   /// Reference to the vector we're iterating over.
00297   ACE_Vector<T, DEFAULT_SIZE> &vector_;
00298 };
00299 
00300 ACE_END_VERSIONED_NAMESPACE_DECL
00301 
00302 #if defined (__ACE_INLINE__)
00303 #include "ace/Vector_T.inl"
00304 #endif /* __ACE_INLINE__ */
00305 
00306 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
00307 #include "ace/Vector_T.cpp"
00308 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
00309 
00310 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
00311 #pragma implementation ("Vector_T.cpp")
00312 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
00313 
00314 #include /**/ "ace/post.h"
00315 
00316 #endif /* ACE_VECTOR_T_H */

Generated on Tue Feb 2 17:18:44 2010 for ACE by  doxygen 1.4.7