00001 // -*- C++ -*- 00002 00003 //========================================================================== 00004 /** 00005 * @file Vector_T.h 00006 * 00007 * Vector_T.h,v 4.11 2005/10/28 23:55:10 ossama Exp 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<T>) 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<T>, 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 protected: 00189 00190 /** 00191 * Dynamic size (length) of the vector. 00192 */ 00193 size_t length_; 00194 00195 /** 00196 * Current capacity (buffer size) of the vector. 00197 */ 00198 size_t curr_max_size_; 00199 00200 friend class ACE_Vector_Iterator<T, DEFAULT_SIZE>; 00201 }; 00202 00203 #if 0 00204 /* 00205 * Not sure about including these functions, if for no other reason, 00206 * because they polute the global namespace! 00207 */ 00208 00209 /** 00210 * Compare two vectors in the range of [from_ndx..to_ndx]. This 00211 * template function requires class T to have the bool operator!=() 00212 * declared in the class. It is safe to define vectors of scalar data 00213 * types, like int, double, etc., including class ACE_TString. 00214 * 00215 * @param v1 The first vector (out of the two) to be compared. 00216 * @param v2 The Second vector (out of the two) to be compared. 00217 * @param from_ndx Compare vector v1 and v2, starting with the 00218 * "from_ndx" index . 00219 * @param to_ndx Compare vector v1 and v2, from "from_ndx" to 00220 * "to_ndx". 00221 * @return Returns true if v1==v2 in the specified index range, 00222 * returns false otherwise. Also, returns false in case if 00223 * v1's size is not equal to v2's size. 00224 */ 00225 template<class T> 00226 int compare (const ACE_Vector<T>& v1, 00227 const ACE_Vector<T>& v2, 00228 const size_t from_ndx, 00229 const size_t to_ndx); 00230 00231 /** 00232 * Does a partial comparison of two vectors in the range of 00233 * [from_ndx..to_ndx]. The only difference between this function and 00234 * the template compare<T> function is that this function does 00235 * not require v1 and v2 to be of equal size. 00236 * 00237 * @param v1 The first vector (out of the two) to be compared. 00238 * @param v2 The Second vector (out of the two) to be compared. 00239 * @param from_ndx Compare vector v1 and v2, starting with the 00240 * "from_ndx" index . 00241 * @param to_ndx Compare vector v1 and v2, from "from_ndx" to 00242 * "to_ndx". 00243 * @return Returns true if vector v1 and v2 are equal in the specified 00244 * index range. 00245 */ 00246 00247 template<class T> 00248 int partial_compare (const ACE_Vector<T>& v1, 00249 const ACE_Vector<T>& v2, 00250 const size_t from_ndx, 00251 const size_t to_ndx); 00252 #endif /* 0 */ 00253 // **************************************************************** 00254 00255 /** 00256 * @class ACE_Vector_Iterator 00257 * 00258 * @brief Implement an iterator over an ACE_Vector. 00259 * 00260 * This iterator is safe in the face of vector element deletions. 00261 * But it is NOT safe if the vector is resized via the assignment 00262 * operator during iteration. That would be very odd, and dangerous. 00263 */ 00264 template <class T, size_t DEFAULT_SIZE = ACE_VECTOR_DEFAULT_SIZE> 00265 class ACE_Vector_Iterator 00266 { 00267 public: 00268 // = Initialization method. 00269 ACE_Vector_Iterator (ACE_Vector<T, DEFAULT_SIZE> &); 00270 00271 // = Iteration methods. 00272 00273 /// Pass back the <next_item> that hasn't been seen in the vector. 00274 /// Returns 0 when all items have been seen, else 1. 00275 int next (T *&next_item); 00276 00277 /// Move forward by one element in the vector. Returns 0 when all the 00278 /// items in the vector have been seen, else 1. 00279 int advance (void); 00280 00281 /// Returns 1 when all items have been seen, else 0. 00282 int done (void) const; 00283 00284 /// Dump the state of an object. 00285 void dump (void) const; 00286 00287 /// Declare the dynamic allocation hooks. 00288 ACE_ALLOC_HOOK_DECLARE; 00289 00290 private: 00291 /// Pointer to the current item in the iteration. 00292 size_t current_; 00293 00294 /// Reference to the vector we're iterating over. 00295 ACE_Vector<T, DEFAULT_SIZE> &vector_; 00296 }; 00297 00298 ACE_END_VERSIONED_NAMESPACE_DECL 00299 00300 #if defined (__ACE_INLINE__) 00301 #include "ace/Vector_T.inl" 00302 #endif /* __ACE_INLINE__ */ 00303 00304 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE) 00305 #include "ace/Vector_T.cpp" 00306 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */ 00307 00308 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA) 00309 #pragma implementation ("Vector_T.cpp") 00310 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */ 00311 00312 #include /**/ "ace/post.h" 00313 00314 #endif /* ACE_VECTOR_T_H */