summaryrefslogtreecommitdiff
path: root/ace/Vector_T.h
blob: b82ea62b90ef0e28b9cd2e8585ca737f19b2d965 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// -*- C++ -*-

//==========================================================================
/**
 *  @file    Vector_T.h
 *
 *  $Id$
 *
 *  @author Craig L. Ching <cching@mqsoftware.com>
 *  @author Gonzalo Diethelm <gonzalo.diethelm@aditiva.com>
 */
//==========================================================================

#ifndef ACE_VECTOR_T_H
#define ACE_VECTOR_T_H

#include /**/ "ace/pre.h"

#include "ace/Array.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

/*
 * Default size for an ACE_Vector.
 */
static const size_t ACE_VECTOR_DEFAULT_SIZE = 32;

// Forward declaration.
template <class T, size_t DEFAULT_SIZE> class ACE_Vector_Iterator;

/**
 * @class ACE_Vector
 *
 * @brief Defines an STL-like vector container.
 *
 * This is an STL-like template vector container, a wrapper around
 * ACE_Array.  It provides at least the basic std::vector look and
 * feel: push_back(), clear(), resize(), capacity().  This template
 * class uses the copy semantic paradigm, though it is okay to use
 * reference counted smart pointers (see ACE_Ptr&lt;T&gt;) with this
 * template class.
 *
 * <b> Requirements and Performance Characteristics</b>
 *   - Internal Structure
 *       ACE_Array
 *   - Duplicates allowed?
 *       Yes
 *   - Random access allowed?
 *       No
 *   - Search speed
 *       N/A
 *   - Insert/replace speed
 *       Linear
 *   - Iterator still valid after change to container?
 *       Yes
 *   - Frees memory for removed elements?
 *       No
 *   - Items inserted by
 *       Value
 *   - Requirements for contained type
 *       -# Default constructor
 *       -# Copy constructor
 *       -# operator=
 */
template<class T, size_t DEFAULT_SIZE = ACE_VECTOR_DEFAULT_SIZE>
class ACE_Vector : public ACE_Array<T>
{
public:
  /**
   * A short name for iterator for ACE_Vector.
   */
  typedef ACE_Vector_Iterator<T, DEFAULT_SIZE> Iterator;


  /**
   * General constructor.
   *
   * @param init_size Initial size of the vector with the default
   *                  value of DEFAULT_SIZE
   * @param alloc Pointer to an ACE allocator.  If it is NULL then the
   *              default ACE allocator is used
   */
  ACE_Vector (const size_t init_size = DEFAULT_SIZE,
              ACE_Allocator* alloc = 0);

  /**
   * Destructor.
   */
  ~ACE_Vector ();

  /**
   * Returns the current vector capacity, that is, the currently
   * allocated buffer size.
   *
   * @return Current buffer size of the vector
   */
  size_t capacity (void) const;

  /**
   * Returns the vector's dynamic size / actual current size of the
   * vector.  Do not confuse it with ACE_Array::size(), which returns
   * the array's capacity.  Unfortunately, ACE is not very consistent
   * with the function names.
   *
   * @return Dynamic size / actual current size of the vector.
   */
  size_t size (void) const;

  /**
   * Clears out the vector.  It does not reallocate the vector's
   * buffer, it is just sets the vector's dynamic size to 0.
   */
  void clear (void);

  /**
   * Resizes the vector to the new capacity.  If the vector's current
   * capacity is smaller than the size to be specified, then the
   * buffer gets reallocated.  If the new capacity is less than the
   * current capacity of the vector, the buffer size stays the same.
   *
   * @param new_size New capacity of the vector
   * @param t A filler value (of the class T) for initializing the
   *          elements of the vector with.  By default, if this
   *          parameter is not specified, the default value of the
   *          class T will be used (for more detail, see the
   *          initialization clause for this parameter).
   */
  void resize (const size_t new_size,
               const T& t);

  /**
   * Appends a new element to the vector ("push back").  If the
   * dynamic size of the vector is equal to the capacity of the vector
   * (vector is at capacity), the vector automatically doubles its
   * capacity.
   *
   * @param elem A reference to the new element to be appended.  By
   *             default, this parameters gets initialized with the
   *             default value of the class T.
   */
  void push_back (const T& elem);

  /**
   * Deletes the last element from the vector ("pop back").  What this
   * function really does is decrement the dynamic size of the
   * vector.  The vector's buffer does not get reallocated for
   * performance.
   */
  void pop_back (void);

  /**
   * This function dumps the content of the vector.  TO BE MOVED out
   * of this class.  It needs to be implemented as a global template
   * function that accepts a const ACE_Vector&lt;T&gt;, in order to
   * make instances of this class compile on Linux, AIX.  G++ and xlC
   * have template instantiation algoriths, which are different from
   * the one in Visual C++.  The algorithms try to instantiate ALL
   * methods declared in the template class, regardless of whether the
   * functions are used or not.  That is, all of the classes, that are
   * used as elements in ACE_Vector's, have to have the dump() methods
   * defined in them (seems to be overkill).
   *
   * This function calls T::dump() for each element of the vector.
   */
  void dump (void) const;

  // = Compare operators

  ///Equality comparison operator.
  /**
   * Compare this vector with @arg s for equality.  Two vectors are equal
   * if their sizes are equal and all the elements are equal.
   */
  bool operator== (const ACE_Vector<T, DEFAULT_SIZE> &s) const;

  ///Inequality comparison operator.
  /**
   * Compare this vector with @arg s for inequality such that @c *this !=
   * @arg s is always the complement of the boolean return value of
   * @c *this == @arg s.
   */
  bool operator!= (const ACE_Vector<T, DEFAULT_SIZE> &s) const;

protected:

  /**
   * Dynamic size (length) of the vector.
   */
  size_t length_;

  /**
   * Current capacity (buffer size) of the vector.
   */
  size_t curr_max_size_;

  friend class ACE_Vector_Iterator<T, DEFAULT_SIZE>;
};

#if 0
/*
 * Not sure about including these functions, if for no other reason,
 * because they polute the global namespace!
 */

/**
 * Compare two vectors in the range of [from_ndx..to_ndx].  This
 * template function requires class T to have the bool operator!=()
 * declared in the class.  It is safe to define vectors of scalar data
 * types, like int, double, etc., including class ACE_TString.
 *
 * @param v1 The first vector (out of the two) to be compared.
 * @param v2 The Second vector (out of the two) to be compared.
 * @param from_ndx Compare vector v1 and v2, starting with the
 *                 "from_ndx" index .
 * @param to_ndx Compare vector v1 and v2, from "from_ndx" to
 *               "to_ndx".
 * @return Returns true if v1==v2 in the specified index range,
 *          returns false otherwise.  Also, returns false in case if
 *          v1's size is not equal to v2's size.
 */
template<class T>
int compare (const ACE_Vector<T>& v1,
              const ACE_Vector<T>& v2,
              const size_t from_ndx,
              const size_t to_ndx);

/**
 * Does a partial comparison of two vectors in the range of
 * [from_ndx..to_ndx].  The only difference between this function and
 * the template compare&lt;T&gt; function is that this function does
 * not require v1 and v2 to be of equal size.
 *
 * @param v1 The first vector (out of the two) to be compared.
 * @param v2 The Second vector (out of the two) to be compared.
 * @param from_ndx Compare vector v1 and v2, starting with the
 *                 "from_ndx" index .
 * @param to_ndx Compare vector v1 and v2, from "from_ndx" to
 *               "to_ndx".
 * @return Returns true if vector v1 and v2 are equal in the specified
 *         index range.
 */

template<class T>
int partial_compare (const ACE_Vector<T>& v1,
                      const ACE_Vector<T>& v2,
                      const size_t from_ndx,
                      const size_t to_ndx);
#endif /* 0 */
// ****************************************************************

/**
 * @class ACE_Vector_Iterator
 *
 * @brief Implement an iterator over an ACE_Vector.
 *
 * This iterator is safe in the face of vector element deletions.
 * But it is NOT safe if the vector is resized via the assignment
 * operator during iteration.  That would be very odd, and dangerous.
 */
template <class T, size_t DEFAULT_SIZE = ACE_VECTOR_DEFAULT_SIZE>
class ACE_Vector_Iterator
{
public:
  // = Initialization method.
  ACE_Vector_Iterator (ACE_Vector<T, DEFAULT_SIZE> &);

  // = Iteration methods.

  /// Pass back the <next_item> that hasn't been seen in the vector.
  /// Returns 0 when all items have been seen, else 1.
  int next (T *&next_item);

  /// Move forward by one element in the vector.  Returns 0 when all the
  /// items in the vector have been seen, else 1.
  int advance (void);

  /// Returns 1 when all items have been seen, else 0.
  int done (void) const;

  /// Dump the state of an object.
  void dump (void) const;

  /// Declare the dynamic allocation hooks.
  ACE_ALLOC_HOOK_DECLARE;

private:
  /// Pointer to the current item in the iteration.
  size_t current_;

  /// Reference to the vector we're iterating over.
  ACE_Vector<T, DEFAULT_SIZE> &vector_;
};

ACE_END_VERSIONED_NAMESPACE_DECL

#if defined (__ACE_INLINE__)
#include "ace/Vector_T.inl"
#endif /* __ACE_INLINE__ */

#if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
#include "ace/Vector_T.cpp"
#endif /* ACE_TEMPLATES_REQUIRE_SOURCE */

#if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
#pragma implementation ("Vector_T.cpp")
#endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */

#include /**/ "ace/post.h"

#endif /* ACE_VECTOR_T_H */