Array_Map.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  * @file    Array_Map.h
00006  *
00007  * Array_Map.h,v 1.14 2006/06/08 17:27:13 schmidt Exp
00008  *
00009  * Light weight array-based map with fast iteration but linear
00010  * (i.e. O(n)) search times.  STL-style interface is exposed.
00011  *
00012  * @note This class requires the STL generic algorithms and
00013  *       reverse_iterator adapter.
00014  *
00015  * @author  Ossama Othman
00016  */
00017 //=============================================================================
00018 
00019 
00020 #ifndef ACE_ARRAY_MAP_H
00021 #define ACE_ARRAY_MAP_H
00022 
00023 #include /**/ "ace/pre.h"
00024 
00025 #include "ace/config-lite.h"
00026 
00027 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00028 # pragma once
00029 #endif /* ACE_LACKS_PRAGMA_ONCE */
00030 
00031 #include <utility>
00032 #include <iterator>
00033 #include <functional>
00034 
00035 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00036 
00037 /**
00038  * @class ACE_Array_Map
00039  *
00040  * @brief Light weight array-based map with fast iteration, but linear
00041  *        (i.e. O(n)) search times.
00042  *
00043  * Map implementation that focuses on small footprint and fast
00044  * iteration.  Search times are, however, linear (O(n)) meaning that
00045  * this map isn't suitable for large data sets that will be searched
00046  * in performance critical areas of code.  Iteration over large data
00047  * sets, however, is faster than linked list-based maps, for example,
00048  * since spatial locality is maximized through the use of contiguous
00049  * arrays as the underlying storage.
00050  * @par
00051  * An @c ACE_Array_Map is a unique associative container, meaning that
00052  * duplicate values may not be added to the map.  It is also pair
00053  * associative (value_type is a std::pair<>).  It is not a sorted
00054  * container.
00055  * @par
00056  * An STL @c std::map -like interface is exposed by this class
00057  * portability.  Furthermore, this map's iterators are compatible with
00058  * STL algorithms.
00059  * @par
00060  * <b> Requirements and Performance Characteristics</b>
00061  *   - Internal Structure
00062  *       Array
00063  *   - Duplicates allowed?
00064  *       No
00065  *   - Random access allowed?
00066  *       Yes
00067  *   - Search speed
00068  *       O(n)
00069  *   - Insert/replace speed
00070  *       O(n), can be longer if the map has to resize
00071  *   - Iterator still valid after change to container?
00072  *       No
00073  *   - Frees memory for removed elements?
00074  *       Yes
00075  *   - Items inserted by
00076  *       Value
00077  *   - Requirements for key type
00078  *       -# Default constructor
00079  *       -# Copy constructor
00080  *       -# operator=
00081  *       -# operator==
00082  *   - Requirements for object type
00083  *       -# Default constructor
00084  *       -# Copy constructor
00085  *       -# operator=
00086  */
00087 template<typename Key, typename Value, class EqualTo = std::equal_to<Key> >
00088 class ACE_Array_Map
00089 {
00090 public:
00091 
00092   // STL-style typedefs/traits.
00093   typedef Key                            key_type;
00094   typedef Value                          data_type;
00095   typedef std::pair<key_type, data_type> value_type;
00096   typedef value_type *                   iterator;
00097   typedef value_type const *             const_iterator;
00098   typedef value_type &                   reference;
00099   typedef value_type const &             const_reference;
00100   typedef value_type *                   pointer;
00101   typedef value_type const *             const_pointer;
00102   typedef ptrdiff_t                      difference_type;
00103   typedef size_t                         size_type;
00104 
00105 #if ((defined (_MSC_VER) && !defined (_CPPLIB_VER)))
00106   // the latest Platform SDK's doesn't define a standard's compliant
00107   // reverse_iterator,
00108   // It seems when there is no _CPPLIB_VER defined, then we can assume
00109   // also that the SDK is old.
00110   typedef std::reverse_iterator<iterator, value_type> reverse_iterator;
00111   typedef std::reverse_iterator<const_iterator,
00112                                 value_type const>     const_reverse_iterator;
00113 #elif defined (__SUNPRO_CC) && __SUNPRO_CC <= 0x580 \
00114       && defined (_RWSTD_NO_CLASS_PARTIAL_SPEC)
00115   typedef std::reverse_iterator<iterator,
00116                                 std::input_iterator_tag,
00117                                 value_type,
00118                                 reference,
00119                                 pointer,
00120                                 difference_type> reverse_iterator;
00121   typedef std::reverse_iterator<const_iterator,
00122                                 std::input_iterator_tag,
00123                                 value_type const,
00124                                 const_reference,
00125                                 const_pointer,
00126                                 difference_type> const_reverse_iterator;
00127 #else
00128   typedef std::reverse_iterator<iterator>       reverse_iterator;
00129   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00130 #endif  /* _MSC_VER */
00131 
00132   /// Default Constructor.
00133   /**
00134    * Create an empty map with a preallocated buffer of size @a s.
00135    */
00136   ACE_Array_Map (size_type s = 0);
00137 
00138 #ifndef ACE_LACKS_MEMBER_TEMPLATES
00139   template<typename InputIterator>
00140   ACE_Array_Map (InputIterator f, InputIterator l);
00141 #else
00142   ACE_Array_Map (const_iterator f, const_iterator l);
00143 #endif  /* !ACE_LACKS_MEMBER_TEMPLATES */
00144 
00145   ACE_Array_Map (ACE_Array_Map const & map);
00146   ACE_Array_Map & operator= (ACE_Array_Map const & map);
00147 
00148   /// Destructor.
00149   ~ACE_Array_Map (void);
00150 
00151   /**
00152    * @name Forward Iterator Accessors
00153    *
00154    * Forward iterator accessors.
00155    */
00156   //@{
00157   iterator begin (void);
00158   iterator end   (void);
00159   const_iterator begin (void) const;
00160   const_iterator end   (void) const;
00161   //@}
00162 
00163   /**
00164    * @name Reverse Iterator Accessors
00165    *
00166    * Reverse iterator accessors.
00167    */
00168   //@{
00169   reverse_iterator rbegin (void);
00170   reverse_iterator rend   (void);
00171   const_reverse_iterator rbegin (void) const;
00172   const_reverse_iterator rend   (void) const;
00173   //@}
00174 
00175   /// Return current size of map.
00176   /**
00177    * @return The number of elements in the map.
00178    */
00179   size_type size (void) const;
00180 
00181   /// Maximum number of elements the map can hold.
00182   size_type max_size (void) const;
00183 
00184   /// Return @c true if the map is empty, else @c false.
00185   bool is_empty (void) const;
00186 
00187   /** 
00188    * Return @c true if the map is empty, else @c false.  We recommend
00189    * using @c is_empty() instead since it's more consistent with the
00190    * ACE container naming conventions.
00191    */
00192   bool empty (void) const;
00193 
00194   /// Swap the contents of this map with the given @a map in an
00195   /// exception-safe manner.
00196   void swap (ACE_Array_Map & map);
00197 
00198   /// Insert the value @a x into the map.
00199   /**
00200    * STL-style map insertion method.
00201    *
00202    * @param x @c std::pair containing key and datum.
00203    *
00204    * @return @c std::pair::second will be @c false if the map already
00205    *         contains a value with the same key as @a x.
00206    */
00207   std::pair<iterator, bool> insert (value_type const & x);
00208 
00209 #ifndef ACE_LACKS_MEMBER_TEMPLATES
00210   /// Insert range of elements into map.
00211   template<typename InputIterator>
00212   void insert (InputIterator f, InputIterator l);
00213 #else
00214   /// Insert range of elements into map.
00215   void insert (const_iterator f, const_iterator l);
00216 #endif  /* ACE_LACKS_MEMBER_TEMPLATES */
00217 
00218   /// Remove element at position @a pos from the map.
00219   void erase (iterator pos);
00220 
00221   /// Remove element corresponding to key @a k from the map.
00222   /**
00223    * @return Number of elements that were erased.
00224    */
00225   size_type erase (key_type const & k);
00226 
00227   /// Remove range of elements [@a first, @a last) from the map.
00228   /**
00229    * @note [@a first, @a last) must be valid range within the map.
00230    */
00231   void erase (iterator first, iterator last);
00232 
00233   /// Clear contents of map.
00234   /**
00235    * @note This a constant time (O(1)) operation.
00236    */
00237   void clear (void);
00238 
00239   /**
00240    * @name Search Operations
00241    *
00242    * Search the map for data corresponding to key @a k.
00243    */
00244   //@{
00245   /**
00246    * @return @c end() if data corresponding to key @a k is not in the
00247    *         map.
00248    */
00249   iterator find (key_type const & k);
00250 
00251   /**
00252    * @return @c end() if data corresponding to key @a k is not in the
00253    *         map.
00254    */
00255   const_iterator find (key_type const & k) const;
00256   //@}
00257 
00258   /// Count the number of elements corresponding to key @a k.
00259   /**
00260    * @return In the case of this map, the count will always be one if
00261    *         such exists in the map.
00262    */
00263   size_type count (key_type const & k);
00264 
00265   /// Convenience array index operator.
00266   /**
00267    * Array index operator that allows insertion and retrieval of
00268    * elements using an array index syntax, such as:
00269    * @par
00270    * map["Foo"] = 12;
00271    */
00272   data_type & operator[] (key_type const & k);
00273 
00274 private:
00275 
00276   /// Increase size of underlying buffer by @a s.
00277   void grow (size_type s);
00278 
00279 private:
00280 
00281   /// Number of elements in the map.
00282   size_type size_;
00283 
00284   /// Current size of underlying array.
00285   /**
00286    * @note @c capacity_ is always greater than or equal to @c size_;
00287    */
00288   size_type capacity_;
00289 
00290   /// Underlying array containing keys and data.
00291   value_type * nodes_;
00292 
00293 };
00294 
00295 // --------------------------------------------------------------
00296 
00297 /// @c ACE_Array_Map equality operator.
00298 template <typename Key, typename Value, class EqualTo>
00299 bool operator== (ACE_Array_Map<Key, Value, EqualTo> const & lhs,
00300                  ACE_Array_Map<Key, Value, EqualTo> const & rhs);
00301 
00302 /// @c ACE_Array_Map lexicographical comparison operator.
00303 template <typename Key, typename Value, class EqualTo>
00304 bool operator<  (ACE_Array_Map<Key, Value, EqualTo> const & lhs,
00305                  ACE_Array_Map<Key, Value, EqualTo> const & rhs);
00306 
00307 // --------------------------------------------------------------
00308 
00309 ACE_END_VERSIONED_NAMESPACE_DECL
00310 
00311 #ifdef __ACE_INLINE__
00312 # include "ace/Array_Map.inl"
00313 #endif /* __ACE_INLINE__ */
00314 
00315 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
00316 # include "ace/Array_Map.cpp"
00317 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
00318 
00319 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
00320 #pragma implementation ("Array_Map.cpp")
00321 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
00322 
00323 #include /**/ "ace/post.h"
00324 
00325 #endif  /* ACE_ARRAY_MAP_H */

Generated on Thu Nov 9 09:41:45 2006 for ACE by doxygen 1.3.6