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 #define ACE_ARRAY_MAP_DEFAULT_ALLOCATOR(K, V) std::allocator<std::pair<K, V> >
39 * @class ACE_Array_Map
41 * @brief Light weight array-based map with fast iteration, but linear
42 * (i.e. O(n)) search times.
44 * Map implementation that focuses on small footprint and fast
45 * iteration. Search times are, however, linear (O(n)) meaning that
46 * this map isn't suitable for large data sets that will be searched
47 * in performance critical areas of code. Iteration over large data
48 * sets, however, is faster than linked list-based maps, for example,
49 * since spatial locality is maximized through the use of contiguous
50 * arrays as the underlying storage.
52 * An @c ACE_Array_Map is a unique associative container, meaning that
53 * duplicate values may not be added to the map. It is also pair
54 * associative (value_type is a std::pair<>). It is not a sorted
57 * An STL @c std::map -like interface is exposed by this class
58 * portability. Furthermore, this map's iterators are compatible with
61 * <b> Requirements and Performance Characteristics</b>
62 * - Internal Structure
64 * - Duplicates allowed?
66 * - Random access allowed?
70 * - Insert/replace speed
71 * O(n), can be longer if the map has to resize
72 * - Iterator still valid after change to container?
74 * - Frees memory for removed elements?
78 * - Requirements for key type
79 * -# Default constructor
83 * - Requirements for object type
84 * -# Default constructor
88 template<typename Key
, typename Value
, class EqualTo
= std::equal_to
<Key
>,
89 class Alloc
= std::allocator
<std::pair
<Key
,Value
>>>
93 // STL-style typedefs/traits.
95 typedef Value mapped_type
;
96 typedef Value data_type
;
97 typedef std::pair
<key_type
, mapped_type
> value_type
;
98 typedef Alloc allocator_type
;
99 typedef value_type
& reference
;
100 typedef value_type
const & const_reference
;
101 typedef value_type
* pointer
;
102 typedef value_type
const * const_pointer
;
103 typedef value_type
* iterator
;
104 typedef value_type
const * const_iterator
;
105 typedef ptrdiff_t difference_type
;
106 typedef size_t size_type
;
107 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
108 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
110 /// Default Constructor.
112 * Create an empty map with a preallocated buffer of size @a s.
114 ACE_Array_Map (size_type s
= 0);
116 template<typename InputIterator
>
117 ACE_Array_Map (InputIterator f
, InputIterator l
);
119 ACE_Array_Map (ACE_Array_Map
const & map
);
120 ACE_Array_Map
& operator= (ACE_Array_Map
const & map
);
126 * @name Forward Iterator Accessors
128 * Forward iterator accessors.
133 const_iterator
begin () const;
134 const_iterator
end () const;
138 * @name Reverse Iterator Accessors
140 * Reverse iterator accessors.
143 reverse_iterator
rbegin ();
144 reverse_iterator
rend ();
145 const_reverse_iterator
rbegin () const;
146 const_reverse_iterator
rend () const;
149 /// Return current size of map.
151 * @return The number of elements in the map.
153 size_type
size () const;
155 /// Maximum number of elements the map can hold.
156 size_type
max_size () const;
158 /// Return @c true if the map is empty, else @c false.
159 bool is_empty () const; // ACE style
162 * Return @c true if the map is empty, else @c false. We recommend
163 * using @c is_empty() instead since it's more consistent with the
164 * ACE container naming conventions.
166 bool empty () const; // STL style
168 /// Swap the contents of this map with the given @a map in an
169 /// exception-safe manner.
170 void swap (ACE_Array_Map
& map
);
172 /// Insert the value @a x into the map.
174 * STL-style map insertion method.
176 * @param x @c std::pair containing key and datum.
178 * @return @c std::pair::second will be @c false if the map already
179 * contains a value with the same key as @a x.
181 std::pair
<iterator
, bool> insert (value_type
const & x
);
183 /// Insert range of elements into map.
184 template<typename InputIterator
>
185 void insert (InputIterator f
, InputIterator l
);
187 /// Remove element at position @a pos from the map.
188 void erase (iterator pos
);
190 /// Remove element corresponding to key @a k from the map.
192 * @return Number of elements that were erased.
194 size_type
erase (key_type
const & k
);
196 /// Remove range of elements [@a first, @a last) from the map.
198 * @note [@a first, @a last) must be valid range within the map.
200 void erase (iterator first
, iterator last
);
202 /// Clear contents of map.
204 * @note This a constant time (O(1)) operation.
209 * @name Search Operations
211 * Search the map for data corresponding to key @a k.
215 * @return @c end() if data corresponding to key @a k is not in the
218 iterator
find (key_type
const & k
);
221 * @return @c end() if data corresponding to key @a k is not in the
224 const_iterator
find (key_type
const & k
) const;
227 /// Count the number of elements corresponding to key @a k.
229 * @return In the case of this map, the count will always be one if
230 * such exists in the map.
232 size_type
count (key_type
const & k
);
234 /// Convenience array index operator.
236 * Array index operator that allows insertion and retrieval of
237 * elements using an array index syntax, such as:
241 mapped_type
& operator[] (key_type
const & k
);
243 allocator_type
get_allocator() const { return alloc_
; }
246 /// Increase size of underlying buffer by @a s.
247 void grow (size_type s
);
250 allocator_type alloc_
;
252 /// Number of elements in the map.
255 /// Current size of underlying array.
257 * @note @c capacity_ is always greater than or equal to @c size_;
261 /// Underlying array containing keys and data.
265 // --------------------------------------------------------------
267 /// @c ACE_Array_Map equality operator.
268 template <typename Key
, typename Value
, class EqualTo
, class Alloc
>
269 bool operator== (ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
> const & lhs
,
270 ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
> const & rhs
);
272 /// @c ACE_Array_Map lexicographical comparison 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 // --------------------------------------------------------------
279 ACE_END_VERSIONED_NAMESPACE_DECL
281 #ifdef __ACE_INLINE__
282 # include "ace/Array_Map.inl"
283 #endif /* __ACE_INLINE__ */
285 #include "ace/Array_Map.cpp"
287 #include /**/ "ace/post.h"
289 #endif /* ACE_ARRAY_MAP_H */