Array_Map.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  * @file    Array_Map.h
00006  *
00007  * $Id: Array_Map.h 80826 2008-03-04 14:51:23Z wotte $
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   ACE_DECLARE_STL_REVERSE_ITERATORS
00106 
00107   /// Default Constructor.
00108   /**
00109    * Create an empty map with a preallocated buffer of size @a s.
00110    */
00111   ACE_Array_Map (size_type s = 0);
00112 
00113 #ifndef ACE_LACKS_MEMBER_TEMPLATES
00114   template<typename InputIterator>
00115   ACE_Array_Map (InputIterator f, InputIterator l);
00116 #else
00117   ACE_Array_Map (const_iterator f, const_iterator l);
00118 #endif  /* !ACE_LACKS_MEMBER_TEMPLATES */
00119 
00120   ACE_Array_Map (ACE_Array_Map const & map);
00121   ACE_Array_Map & operator= (ACE_Array_Map const & map);
00122 
00123   /// Destructor.
00124   ~ACE_Array_Map (void);
00125 
00126   /**
00127    * @name Forward Iterator Accessors
00128    *
00129    * Forward iterator accessors.
00130    */
00131   //@{
00132   iterator begin (void);
00133   iterator end   (void);
00134   const_iterator begin (void) const;
00135   const_iterator end   (void) const;
00136   //@}
00137 
00138   /**
00139    * @name Reverse Iterator Accessors
00140    *
00141    * Reverse iterator accessors.
00142    */
00143   //@{
00144   reverse_iterator rbegin (void);
00145   reverse_iterator rend   (void);
00146   const_reverse_iterator rbegin (void) const;
00147   const_reverse_iterator rend   (void) const;
00148   //@}
00149 
00150   /// Return current size of map.
00151   /**
00152    * @return The number of elements in the map.
00153    */
00154   size_type size (void) const;
00155 
00156   /// Maximum number of elements the map can hold.
00157   size_type max_size (void) const;
00158 
00159   /// Return @c true if the map is empty, else @c false.
00160   bool is_empty (void) const;  // ACE style
00161 
00162   /** 
00163    * Return @c true if the map is empty, else @c false.  We recommend
00164    * using @c is_empty() instead since it's more consistent with the
00165    * ACE container naming conventions.
00166    */
00167   bool empty (void) const;  // STL style
00168 
00169   /// Swap the contents of this map with the given @a map in an
00170   /// exception-safe manner.
00171   void swap (ACE_Array_Map & map);
00172 
00173   /// Insert the value @a x into the map.
00174   /**
00175    * STL-style map insertion method.
00176    *
00177    * @param x @c std::pair containing key and datum.
00178    *
00179    * @return @c std::pair::second will be @c false if the map already
00180    *         contains a value with the same key as @a x.
00181    */
00182   std::pair<iterator, bool> insert (value_type const & x);
00183 
00184 #ifndef ACE_LACKS_MEMBER_TEMPLATES
00185   /// Insert range of elements into map.
00186   template<typename InputIterator>
00187   void insert (InputIterator f, InputIterator l);
00188 #else
00189   /// Insert range of elements into map.
00190   void insert (const_iterator f, const_iterator l);
00191 #endif  /* ACE_LACKS_MEMBER_TEMPLATES */
00192 
00193   /// Remove element at position @a pos from the map.
00194   void erase (iterator pos);
00195 
00196   /// Remove element corresponding to key @a k from the map.
00197   /**
00198    * @return Number of elements that were erased.
00199    */
00200   size_type erase (key_type const & k);
00201 
00202   /// Remove range of elements [@a first, @a last) from the map.
00203   /**
00204    * @note [@a first, @a last) must be valid range within the map.
00205    */
00206   void erase (iterator first, iterator last);
00207 
00208   /// Clear contents of map.
00209   /**
00210    * @note This a constant time (O(1)) operation.
00211    */
00212   void clear (void);
00213 
00214   /**
00215    * @name Search Operations
00216    *
00217    * Search the map for data corresponding to key @a k.
00218    */
00219   //@{
00220   /**
00221    * @return @c end() if data corresponding to key @a k is not in the
00222    *         map.
00223    */
00224   iterator find (key_type const & k);
00225 
00226   /**
00227    * @return @c end() if data corresponding to key @a k is not in the
00228    *         map.
00229    */
00230   const_iterator find (key_type const & k) const;
00231   //@}
00232 
00233   /// Count the number of elements corresponding to key @a k.
00234   /**
00235    * @return In the case of this map, the count will always be one if
00236    *         such exists in the map.
00237    */
00238   size_type count (key_type const & k);
00239 
00240   /// Convenience array index operator.
00241   /**
00242    * Array index operator that allows insertion and retrieval of
00243    * elements using an array index syntax, such as:
00244    * @par
00245    * map["Foo"] = 12;
00246    */
00247   data_type & operator[] (key_type const & k);
00248 
00249 private:
00250 
00251   /// Increase size of underlying buffer by @a s.
00252   void grow (size_type s);
00253 
00254 private:
00255 
00256   /// Number of elements in the map.
00257   size_type size_;
00258 
00259   /// Current size of underlying array.
00260   /**
00261    * @note @c capacity_ is always greater than or equal to @c size_;
00262    */
00263   size_type capacity_;
00264 
00265   /// Underlying array containing keys and data.
00266   value_type * nodes_;
00267 
00268 };
00269 
00270 // --------------------------------------------------------------
00271 
00272 /// @c ACE_Array_Map equality operator.
00273 template <typename Key, typename Value, class EqualTo>
00274 bool operator== (ACE_Array_Map<Key, Value, EqualTo> const & lhs,
00275                  ACE_Array_Map<Key, Value, EqualTo> const & rhs);
00276 
00277 /// @c ACE_Array_Map lexicographical comparison operator.
00278 template <typename Key, typename Value, class EqualTo>
00279 bool operator<  (ACE_Array_Map<Key, Value, EqualTo> const & lhs,
00280                  ACE_Array_Map<Key, Value, EqualTo> const & rhs);
00281 
00282 // --------------------------------------------------------------
00283 
00284 ACE_END_VERSIONED_NAMESPACE_DECL
00285 
00286 #ifdef __ACE_INLINE__
00287 # include "ace/Array_Map.inl"
00288 #endif /* __ACE_INLINE__ */
00289 
00290 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
00291 # include "ace/Array_Map.cpp"
00292 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
00293 
00294 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
00295 #pragma implementation ("Array_Map.cpp")
00296 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
00297 
00298 #include /**/ "ace/post.h"
00299 
00300 #endif  /* ACE_ARRAY_MAP_H */

Generated on Tue Feb 2 17:18:38 2010 for ACE by  doxygen 1.4.7