Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / ACE / ace / Array_Map.cpp
blobadccba57efca306eb963a2fa3faa08d2c38b9eea
1 #ifndef ACE_ARRAY_MAP_CPP
2 #define ACE_ARRAY_MAP_CPP
4 #include "ace/Array_Map.h"
6 #ifndef __ACE_INLINE__
7 # include "ace/Array_Map.inl"
8 #endif /* !__ACE_INLINE__ */
10 #include <algorithm>
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,
17 InputIterator l)
18 : size_ (l - f)
19 , capacity_ (size_)
20 , nodes_ (size_ == 0 ? 0 : this->alloc_.allocate (size_))
22 (void) std::uninitialized_copy (f,
24 this->begin ());
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)
30 : size_ (map.size_)
31 , capacity_ (map.size_)
32 , nodes_ (size_ == 0 ? 0 : this->alloc_.allocate (size_))
34 (void) std::uninitialized_copy (map.begin (),
35 map.end (),
36 this->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)
45 using std::pair;
46 (nodes_ + idx)->~pair<key_type, mapped_type>();
47 #else
48 (nodes_ + idx)->~value_type();
49 #endif
53 alloc_.deallocate(this->nodes_, capacity_);
56 template<typename Key, typename Value, class EqualTo, class Alloc>
57 void
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;
84 *i = x;
86 ++this->size_;
88 inserted = true;
91 return std::make_pair (i, inserted);
94 template<typename Key, typename Value, class EqualTo, class Alloc>
95 template<typename InputIterator>
96 void
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>
108 void
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)
117 if (pos != last - 1)
119 // Relocate the tail element to the location of the erased
120 // element to prevent introduction of "holes" in the
121 // underlying array.
122 *pos = *(last - 1);
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 ();
130 --this->size_;
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_;
143 this->erase (pos);
145 return old_size - this->size_;
148 template<typename Key, typename Value, class EqualTo, class Alloc>
149 void
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)
156 this->erase (i);
159 template<typename Key, typename Value, class EqualTo, class Alloc>
160 void
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 ();
173 EqualTo eq;
175 for (iterator i = this->begin (); i != the_end; ++i)
176 if (eq (k, i->first))
177 return i;
179 return this->end ();
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 ();
189 EqualTo eq;
191 for (const_iterator i = this->begin (); i != the_end; ++i)
192 if (eq (k, i->first))
193 return i;
195 return this->end ();
198 template<typename Key, typename Value, class EqualTo, class Alloc>
199 void
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
206 // speed.
208 // Strongly exception safe.
210 ACE_Array_Map<Key, Value, EqualTo, Alloc> temp (this->size () + s);
212 std::copy (this->begin (),
213 this->end (),
214 temp.begin ());
216 size_type const n = this->size (); // Do not swap out the size
217 // since we bypassed the
218 // temporary map's element
219 // counting code.
220 this->swap (temp);
222 this->size_ = n;
226 // ---------------------------------------------------------------
228 template <typename Key, typename Value, class EqualTo, class Alloc>
229 bool
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
234 // in this case.
236 return (lhs.size () == rhs.size ()
237 && std::equal (lhs.begin (),
238 lhs.end (),
239 rhs.begin ()));
242 template <typename Key, typename Value, class EqualTo, class Alloc>
243 bool
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 */