1 #ifndef ACE_ARRAY_MAP_CPP
2 #define ACE_ARRAY_MAP_CPP
4 #include "ace/Array_Map.h"
7 # include "ace/Array_Map.inl"
8 #endif /* !__ACE_INLINE__ */
12 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
14 template<typename Key
, typename Value
, class EqualTo
, class Alloc
>
15 template<typename InputIterator
>
16 ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::ACE_Array_Map (InputIterator f
,
20 , nodes_ (size_
== 0 ? 0 : this->alloc_
.allocate (size_
))
22 (void) std::uninitialized_copy (f
,
27 template<typename Key
, typename Value
, class EqualTo
, class Alloc
>
28 ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::ACE_Array_Map (
29 ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
> const & map
)
31 , capacity_ (map
.size_
)
32 , nodes_ (size_
== 0 ? 0 : this->alloc_
.allocate (size_
))
34 (void) std::uninitialized_copy (map
.begin (),
39 template<typename Key
, typename Value
, class EqualTo
, class Alloc
>
40 ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::~ACE_Array_Map ()
42 for (size_t idx
= 0; idx
!= capacity_
; ++idx
)
44 #if defined (ACE_HAS_BCC32)
46 (nodes_
+ idx
)->~pair
<key_type
, mapped_type
>();
48 (nodes_
+ idx
)->~value_type();
53 alloc_
.deallocate(this->nodes_
, capacity_
);
56 template<typename Key
, typename Value
, class EqualTo
, class Alloc
>
58 ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::swap (
59 ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
> & map
)
61 std::swap (this->size_
, map
.size_
);
62 std::swap (this->capacity_
, map
.capacity_
);
63 std::swap (this->nodes_
, map
.nodes_
);
66 template<typename Key
, typename Value
, class EqualTo
, class Alloc
>
67 std::pair
<typename ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::iterator
, bool>
68 ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::insert (
69 typename ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::value_type
const & x
)
71 // Linear insertion due to linear duplicate key search.
73 bool inserted
= false;
74 iterator i
= this->find (x
.first
);
76 if (i
== this->end ())
78 // Add the element to the array.
80 size_type
const old_size
= this->size ();
81 this->grow (1); // Increase size by at least one.
83 i
= this->begin () + old_size
;
91 return std::make_pair (i
, inserted
);
94 template<typename Key
, typename Value
, class EqualTo
, class Alloc
>
95 template<typename InputIterator
>
97 ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::insert (InputIterator f
, InputIterator l
)
99 this->grow (l
- f
); // Preallocate storage.
101 for (InputIterator i
= f
; i
!= l
; ++i
)
103 (void) this->insert (*i
);
107 template<typename Key
, typename Value
, class EqualTo
, class Alloc
>
109 ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::erase (
110 typename ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::iterator pos
)
112 iterator
const first
= this->begin ();
113 iterator
const last
= this->end ();
115 if (pos
>= first
&& pos
< last
)
119 // Relocate the tail element to the location of the erased
120 // element to prevent introduction of "holes" in the
125 // Explicitly destroy the tail element by assigning a default
126 // constructed instance to it. Note that this also works for
127 // the case of a map of size 1.
128 *(last
- 1) = value_type ();
134 template<typename Key
, typename Value
, class EqualTo
, class Alloc
>
135 typename ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::size_type
136 ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::erase (
137 typename ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::key_type
const & k
)
139 iterator pos
= this->find (k
);
141 size_type
const old_size
= this->size_
;
145 return old_size
- this->size_
;
148 template<typename Key
, typename Value
, class EqualTo
, class Alloc
>
150 ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::erase (
151 typename ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::iterator first
,
152 typename ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::iterator last
)
154 if (this->begin () <= first
&& first
< last
&& last
< this->end ())
155 for (iterator i
= first
; i
!= last
; ++i
)
159 template<typename Key
, typename Value
, class EqualTo
, class Alloc
>
161 ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::clear ()
163 this->size_
= 0; // No need to deallocate array nor destroy elements.
166 template<typename Key
, typename Value
, class EqualTo
, class Alloc
>
167 typename ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::iterator
168 ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::find (
169 typename ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::key_type
const & k
)
171 iterator
const the_end
= this->end ();
175 for (iterator i
= this->begin (); i
!= the_end
; ++i
)
176 if (eq (k
, i
->first
))
182 template<typename Key
, typename Value
, class EqualTo
, class Alloc
>
183 typename ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::const_iterator
184 ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::find (
185 typename ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::key_type
const & k
) const
187 const_iterator
const the_end
= this->end ();
191 for (const_iterator i
= this->begin (); i
!= the_end
; ++i
)
192 if (eq (k
, i
->first
))
198 template<typename Key
, typename Value
, class EqualTo
, class Alloc
>
200 ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::grow (
201 typename ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
>::size_type s
)
203 if (this->size () + s
> this->capacity_
)
205 // This implementation focuses more on static footprint than
208 // Strongly exception safe.
210 ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
> temp (this->size () + s
);
212 std::copy (this->begin (),
216 size_type
const n
= this->size (); // Do not swap out the size
217 // since we bypassed the
218 // temporary map's element
226 // ---------------------------------------------------------------
228 template <typename Key
, typename Value
, class EqualTo
, class Alloc
>
230 operator== (ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
> const & lhs
,
231 ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
> const & rhs
)
233 // Do not include Array_Map capacity in comparison. It isn't useful
236 return (lhs
.size () == rhs
.size ()
237 && std::equal (lhs
.begin (),
242 template <typename Key
, typename Value
, class EqualTo
, class Alloc
>
244 operator< (ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
> const & lhs
,
245 ACE_Array_Map
<Key
, Value
, EqualTo
, Alloc
> const & rhs
)
247 return std::lexicographical_compare (lhs
.begin (), lhs
.end (),
248 rhs
.begin (), rhs
.end ());
251 ACE_END_VERSIONED_NAMESPACE_DECL
253 #endif /* ACE_ARRAY_MAP_CPP */