Unbounded_Set_Ex.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file Unbounded_Set_Ex.h
00006  *
00007  *  $Id: Unbounded_Set_Ex.h 81624 2008-05-06 17:14:57Z wotte $
00008  *
00009  *  @author Douglas C. Schmidt <schmidt@cs.wustl.edu>
00010  */
00011 //=============================================================================
00012 
00013 #ifndef ACE_UNBOUNDED_SET_EX_H
00014 #define ACE_UNBOUNDED_SET_EX_H
00015 #include /**/ "ace/pre.h"
00016 
00017 #include "ace/Node.h"
00018 #include "ace/os_include/os_stddef.h"
00019 
00020 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00021 # pragma once
00022 #endif /* ACE_LACKS_PRAGMA_ONCE */
00023 
00024 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00025 
00026 class ACE_Allocator;
00027 
00028 template <class T, class C>
00029 class ACE_Unbounded_Set_Ex_Iterator;
00030 
00031 template <class T, class C>
00032 class ACE_Unbounded_Set_Ex_Const_Iterator;
00033 
00034 template <class T, class C>
00035 class ACE_Unbounded_Set_Ex;
00036 
00037 /**
00038  * @class ACE_Unbounded_Set_Ex_Iterator
00039  *
00040  * @brief Implement an iterator over an unbounded set.
00041  */
00042 template <class T, class C>
00043 class ACE_Unbounded_Set_Ex_Iterator
00044 {
00045 public:
00046   // = Initialization method.
00047   ACE_Unbounded_Set_Ex_Iterator (ACE_Unbounded_Set_Ex<T, C> &s, bool end = false);
00048 
00049   // = Iteration methods.
00050 
00051   /// Pass back the <next_item> that hasn't been seen in the Set.
00052   /// Returns 0 when all items have been seen, else 1.
00053   int next (T *&next_item);
00054 
00055   /// Move forward by one element in the set.  Returns 0 when all the
00056   /// items in the set have been seen, else 1.
00057   int advance (void);
00058 
00059   /// Move to the first element in the set.  Returns 0 if the
00060   /// set is empty, else 1.
00061   int first (void);
00062 
00063   /// Returns 1 when all items have been seen, else 0.
00064   int done (void) const;
00065 
00066   /// Dump the state of an object.
00067   void dump (void) const;
00068 
00069   // = STL styled iteration, compare, and reference functions.
00070 
00071   /// Postfix advance.
00072   ACE_Unbounded_Set_Ex_Iterator<T, C> operator++ (int);
00073 
00074   /// Prefix advance.
00075   ACE_Unbounded_Set_Ex_Iterator<T, C>& operator++ (void);
00076 
00077   /// Returns a reference to the internal element @c this is pointing to.
00078   T& operator* (void);
00079 
00080   /// Check if two iterators point to the same position
00081   bool operator== (const ACE_Unbounded_Set_Ex_Iterator<T, C> &) const;
00082   bool operator!= (const ACE_Unbounded_Set_Ex_Iterator<T, C> &) const;
00083 
00084   /// Declare the dynamic allocation hooks.
00085   ACE_ALLOC_HOOK_DECLARE;
00086 
00087 private:
00088 
00089   /// Pointer to the current node in the iteration.
00090   ACE_Node<T, C> *current_;
00091 
00092   /// Pointer to the set we're iterating over.
00093   ACE_Unbounded_Set_Ex<T, C> *set_;
00094 };
00095 
00096 /**
00097  * @class ACE_Unbounded_Set_Ex_Const_Iterator
00098  *
00099  * @brief Implement an const iterator over an unbounded set.
00100  */
00101 template <class T, class C>
00102 class ACE_Unbounded_Set_Ex_Const_Iterator
00103 {
00104 public:
00105   // = Initialization method.
00106   ACE_Unbounded_Set_Ex_Const_Iterator (const ACE_Unbounded_Set_Ex<T, C> &s,
00107                                        bool end = false);
00108 
00109   // = Iteration methods.
00110 
00111   /// Pass back the @a next_item that hasn't been seen in the Set.
00112   /// @return Returns 0 when all items have been seen, else 1.
00113   int next (T *&next_item);
00114 
00115   /// Move forward by one element in the set.  Returns 0 when all the
00116   /// items in the set have been seen, else 1.
00117   int advance (void);
00118 
00119   /// Move to the first element in the set.  Returns 0 if the
00120   /// set is empty, else 1.
00121   int first (void);
00122 
00123   /// Returns 1 when all items have been seen, else 0.
00124   int done (void) const;
00125 
00126   /// Dump the state of an object.
00127   void dump (void) const;
00128 
00129   // = STL styled iteration, compare, and reference functions.
00130 
00131   /// Postfix advance.
00132   ACE_Unbounded_Set_Ex_Const_Iterator<T, C> operator++ (int);
00133 
00134   /// Prefix advance.
00135   ACE_Unbounded_Set_Ex_Const_Iterator<T, C>& operator++ (void);
00136 
00137   /// Returns a reference to the internal element @c this is pointing to.
00138   T& operator* (void);
00139 
00140   /// Check if two iterators point to the same position
00141   bool operator== (const ACE_Unbounded_Set_Ex_Const_Iterator<T, C> &) const;
00142   bool operator!= (const ACE_Unbounded_Set_Ex_Const_Iterator<T, C> &) const;
00143 
00144   /// Declare the dynamic allocation hooks.
00145   ACE_ALLOC_HOOK_DECLARE;
00146 
00147 private:
00148 
00149   /// Pointer to the current node in the iteration.
00150   ACE_Node<T, C> *current_;
00151 
00152   /// Pointer to the set we're iterating over.
00153   const ACE_Unbounded_Set_Ex<T, C> *set_;
00154 };
00155 
00156 /**
00157  * @class ACE_Unbounded_Set_Ex
00158  *
00159  * @brief Implement a simple unordered set of <T> of unbounded size.
00160  *
00161  * This implementation of an unordered set uses a circular
00162  * linked list with a dummy node.  This implementation does not
00163  * allow duplicates, but it maintains FIFO ordering of insertions.
00164  *
00165  * This implementation may also be parameterized with a comparator
00166  * functor, which must implement bool operator () (const T&, const T&) const, 
00167  * returning true if the given items are equivalent.  The default comparator
00168  * is sufficient for objects reliably compared with operator==.
00169  *
00170  * <b> Requirements and Performance Characteristics</b>
00171  *   - Internal Structure
00172  *       Circular linked list
00173  *   - Duplicates allowed?
00174  *       No
00175  *   - Random access allowed?
00176  *       No
00177  *   - Search speed
00178  *       Linear
00179  *   - Insert/replace speed
00180  *       Linear
00181  *   - Iterator still valid after change to container?
00182  *       Yes
00183  *   - Frees memory for removed elements?
00184  *       Yes
00185  *   - Items inserted by
00186  *       Value
00187  *   - Requirements for contained type
00188  *       -# Default constructor
00189  *       -# Copy constructor
00190  *       -# operator=
00191  *       -# operator== const
00192  *
00193  */
00194 template <class T, class C>
00195 class ACE_Unbounded_Set_Ex
00196 {
00197 public:
00198   friend class ACE_Unbounded_Set_Ex_Iterator<T, C>;
00199   friend class ACE_Unbounded_Set_Ex_Const_Iterator<T, C>;
00200 
00201   // Trait definition.
00202   typedef ACE_Unbounded_Set_Ex_Iterator<T, C> ITERATOR;
00203   typedef ACE_Unbounded_Set_Ex_Iterator<T, C> iterator;
00204   typedef ACE_Unbounded_Set_Ex_Const_Iterator<T, C> CONST_ITERATOR;
00205   typedef ACE_Unbounded_Set_Ex_Const_Iterator<T, C> const_iterator;
00206   typedef C COMP;
00207   typedef ACE_Node<T, C> NODE;
00208   
00209   // = Initialization and termination methods.
00210   /// Constructor.  Use user specified allocation strategy
00211   /// if specified.
00212   /**
00213    * Initialize an empty set using the allocation strategy of the user if
00214    * provided.
00215    */
00216   ACE_Unbounded_Set_Ex (ACE_Allocator *alloc = 0);
00217 
00218   /**
00219    * Initialize an empty set using the allocation strategy of the user if
00220    * provided, and a given comparator functor.
00221    */
00222   ACE_Unbounded_Set_Ex (const C &comparator, ACE_Allocator *alloc = 0);
00223 
00224   /// Copy constructor.
00225   /**
00226    * Initialize this set to be an exact copy of the set provided.
00227    */
00228   ACE_Unbounded_Set_Ex (const ACE_Unbounded_Set_Ex<T, C> &);
00229 
00230   /// Assignment operator.
00231   /**
00232    * Perform a deep copy of the rhs into the lhs.
00233    */
00234   ACE_Unbounded_Set_Ex<T, C> & operator= (const ACE_Unbounded_Set_Ex<T, C> &);
00235 
00236   /// Destructor.
00237   /**
00238    * Destroy the nodes of the set.
00239    */
00240   ~ACE_Unbounded_Set_Ex (void);
00241 
00242   // = Check boundary conditions.
00243 
00244   /// Returns @c true if the container is empty, otherwise returns @c false.
00245   /**
00246    * Constant time is_empty check.
00247    */
00248   bool is_empty (void) const;
00249 
00250   /// Returns @c false.
00251   /**
00252    * Always returns @c false since the set can never fill up.
00253    */
00254   bool is_full (void) const;
00255 
00256   // = Classic unordered set operations.
00257 
00258   /// Linear insertion of an item.
00259   /**
00260    * Insert @a new_item into the set (doesn't allow duplicates).
00261    * Returns -1 if failures occur, 1 if item is already present, else
00262    * 0.
00263    */
00264   int insert (const T &new_item);
00265 
00266   /// Insert @a item at the tail of the set (doesn't check for
00267   /// duplicates).
00268   /**
00269    * Constant time insert at the end of the set.
00270    */
00271   int insert_tail (const T &item);
00272 
00273   /// Linear remove operation.
00274   /**
00275    * Remove first occurrence of @a item from the set.  Returns 0 if
00276    * it removes the item, -1 if it can't find the item, and -1 if a
00277    * failure occurs.
00278    */
00279   int remove (const T &item);
00280 
00281   /// Finds if @a item occurs in the set.  Returns 0 if find succeeds,
00282   /// else -1.
00283   /**
00284    * Performs a linear find operation.
00285    */
00286   int find (const T &item) const;
00287 
00288   /// Size of the set.
00289   /**
00290    * Access the size of the set.
00291    */
00292   size_t size (void) const;
00293 
00294   /// Dump the state of an object.
00295   void dump (void) const;
00296 
00297   /// Reset the ACE_Unbounded_Set_Ex to be empty.
00298   /**
00299    * Delete the nodes of the set.
00300    */
00301   void reset (void);
00302 
00303   // = STL-styled unidirectional iterator factory.
00304   iterator begin (void);
00305   iterator end (void);
00306   const_iterator begin (void) const;
00307   const_iterator end (void) const;
00308 
00309   /// Declare the dynamic allocation hooks.
00310   ACE_ALLOC_HOOK_DECLARE;
00311 
00312 private:
00313   /// Delete all the nodes in the Set.
00314   void delete_nodes (void);
00315 
00316   /// Copy nodes into this set.
00317   void copy_nodes (const ACE_Unbounded_Set_Ex<T, C> &);
00318 
00319   /// Head of the linked list of Nodes.
00320   NODE *head_;
00321 
00322   /// Current size of the set.
00323   size_t cur_size_;
00324 
00325   /// Allocation strategy of the set.
00326   ACE_Allocator *allocator_;
00327   
00328   /// Comparator to be used
00329   COMP comp_;
00330 };
00331 
00332 ACE_END_VERSIONED_NAMESPACE_DECL
00333 
00334 #if defined (__ACE_INLINE__)
00335 #include "ace/Unbounded_Set_Ex.inl"
00336 #endif /* __ACE_INLINE__ */
00337 
00338 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
00339 #include "ace/Unbounded_Set_Ex.cpp"
00340 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
00341 
00342 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
00343 #pragma implementation ("Unbounded_Set_Ex.cpp")
00344 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
00345 
00346 #include /**/ "ace/post.h"
00347 #endif /* ACE_UNBOUNDED_SET_H */

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