3 //=============================================================================
7 * Light weight array-based map with fast iteration but linear
8 * (i.e. O(n)) search times. STL-style interface is exposed.
10 * @note This class requires the STL generic algorithms and
11 * reverse_iterator adapter.
13 * @author Ossama Othman
15 //=============================================================================
18 #ifndef ACE_ARRAY_MAP_H
19 #define ACE_ARRAY_MAP_H
21 #include /**/ "ace/pre.h"
23 #include /**/ "ace/config-lite.h"
25 #if !defined (ACE_LACKS_PRAGMA_ONCE)
27 #endif /* ACE_LACKS_PRAGMA_ONCE */
34 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
36 #if defined __SUNPRO_CC && !defined _RWSTD_ALLOCATOR
37 # define ACE_ARRAY_MAP_DEFAULT_ALLOCATOR(K, V) std::allocator_interface< \
38 std::allocator<void>, \
41 # define ACE_ARRAY_MAP_DEFAULT_ALLOCATOR(K, V) std::allocator<std::pair<K, V> >
45 * @class ACE_Array_Map
47 * @brief Light weight array-based map with fast iteration, but linear
48 * (i.e. O(n)) search times.
50 * Map implementation that focuses on small footprint and fast
51 * iteration. Search times are, however, linear (O(n)) meaning that
52 * this map isn't suitable for large data sets that will be searched
53 * in performance critical areas of code. Iteration over large data
54 * sets, however, is faster than linked list-based maps, for example,
55 * since spatial locality is maximized through the use of contiguous
56 * arrays as the underlying storage.
58 * An @c ACE_Array_Map is a unique associative container, meaning that
59 * duplicate values may not be added to the map. It is also pair
60 * associative (value_type is a std::pair<>). It is not a sorted
63 * An STL @c std::map -like interface is exposed by this class
64 * portability. Furthermore, this map's iterators are compatible with
67 * <b> Requirements and Performance Characteristics</b>
68 * - Internal Structure
70 * - Duplicates allowed?
72 * - Random access allowed?
76 * - Insert/replace speed
77 * O(n), can be longer if the map has to resize
78 * - Iterator still valid after change to container?
80 * - Frees memory for removed elements?
84 * - Requirements for key type
85 * -# Default constructor
89 * - Requirements for object type
90 * -# Default constructor
94 template<typename Key
, typename Value
, class EqualTo
= std::equal_to
<Key
>,
95 class Alloc
= ACE_ARRAY_MAP_DEFAULT_ALLOCATOR (Key
, Value
) >
99 // STL-style typedefs/traits.
100 typedef Key key_type
;
101 typedef Value mapped_type
;
102 typedef Value data_type
;
103 typedef std::pair
<key_type
, mapped_type
> value_type
;
104 typedef Alloc allocator_type
;
105 typedef value_type
& reference
;
106 typedef value_type
const & const_reference
;
107 typedef value_type
* pointer
;
108 typedef value_type
const * const_pointer
;
109 typedef value_type
* iterator
;
110 typedef value_type
const * const_iterator
;
111 typedef ptrdiff_t difference_type
;
112 typedef size_t size_type
;
113 ACE_DECLARE_STL_REVERSE_ITERATORS
115 /// Default Constructor.
117 * Create an empty map with a preallocated buffer of size @a s.
119 ACE_Array_Map (size_type s
= 0);
121 template<typename InputIterator
>
122 ACE_Array_Map (InputIterator f
, InputIterator l
);
124 ACE_Array_Map (ACE_Array_Map
const & map
);
125 ACE_Array_Map
& operator= (ACE_Array_Map
const & map
);
128 ~ACE_Array_Map (void);
131 * @name Forward Iterator Accessors
133 * Forward iterator accessors.
136 iterator
begin (void);
138 const_iterator
begin (void) const;
139 const_iterator
end (void) const;
143 * @name Reverse Iterator Accessors
145 * Reverse iterator accessors.
148 reverse_iterator
rbegin (void);
149 reverse_iterator
rend (void);
150 const_reverse_iterator
rbegin (void) const;
151 const_reverse_iterator
rend (void) const;
154 /// Return current size of map.
156 * @return The number of elements in the map.
158 size_type
size (void) const;
160 /// Maximum number of elements the map can hold.
161 size_type
max_size (void) const;
163 /// Return @c true if the map is empty, else @c false.
164 bool is_empty (void) const; // ACE style
167 * Return @c true if the map is empty, else @c false. We recommend
168 * using @c is_empty() instead since it's more consistent with the
169 * ACE container naming conventions.
171 bool empty (void) const; // STL style
173 /// Swap the contents of this map with the given @a map in an
174 /// exception-safe manner.
175 void swap (ACE_Array_Map
& map
);
177 /// Insert the value @a x into the map.
179 * STL-style map insertion method.
181 * @param x @c std::pair containing key and datum.
183 * @return @c std::pair::second will be @c false if the map already
184 * contains a value with the same key as @a x.
186 std::pair
<iterator
, bool> insert (value_type
const & x
);
188 /// Insert range of elements into map.
189 template<typename InputIterator
>
190 void insert (InputIterator f
, InputIterator l
);
192 /// Remove element at position @a pos from the map.
193 void erase (iterator pos
);
195 /// Remove element corresponding to key @a k from the map.
197 * @return Number of elements that were erased.
199 size_type
erase (key_type
const & k
);
201 /// Remove range of elements [@a first, @a last) from the map.
203 * @note [@a first, @a last) must be valid range within the map.
205 void erase (iterator first
, iterator last
);
207 /// Clear contents of map.
209 * @note This a constant time (O(1)) operation.
214 * @name Search Operations
216 * Search the map for data corresponding to key @a k.
220 * @return @c end() if data corresponding to key @a k is not in the
223 iterator
find (key_type
const & k
);
226 * @return @c end() if data corresponding to key @a k is not in the
229 const_iterator
find (key_type
const & k
) const;
232 /// Count the number of elements corresponding to key @a k.
234 * @return In the case of this map, the count will always be one if
235 * such exists in the map.
237 size_type
count (key_type
const & k
);
239 /// Convenience array index operator.
241 * Array index operator that allows insertion and retrieval of
242 * elements using an array index syntax, such as:
246 mapped_type
& operator[] (key_type
const & k
);
248 allocator_type
get_allocator() const { return alloc_
; }
251 /// Increase size of underlying buffer by @a s.
252 void grow (size_type s
);
255 allocator_type alloc_
;
257 /// Number of elements in the map.
260 /// Current size of underlying array.
262 * @note @c capacity_ is always greater than or equal to @c size_;
266 /// Underlying array containing keys and data.
270 // --------------------------------------------------------------
272 /// @c ACE_Array_Map equality operator.
273 template <typename Key
, typename Value
, class EqualTo
, class Alloc
>
274 bool operator== (ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
> const & lhs
,
275 ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
> const & rhs
);
277 /// @c ACE_Array_Map lexicographical comparison operator.
278 template <typename Key
, typename Value
, class EqualTo
, class Alloc
>
279 bool operator< (ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
> const & lhs
,
280 ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
> const & rhs
);
282 // --------------------------------------------------------------
284 ACE_END_VERSIONED_NAMESPACE_DECL
286 #ifdef __ACE_INLINE__
287 # include "ace/Array_Map.inl"
288 #endif /* __ACE_INLINE__ */
290 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
291 # include "ace/Array_Map.cpp"
292 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
294 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
295 #pragma implementation ("Array_Map.cpp")
296 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
298 #include /**/ "ace/post.h"
300 #endif /* ACE_ARRAY_MAP_H */