• Main Page
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

chomp/struct/setunion.h

Go to the documentation of this file.
00001 /// @addtogroup struct
00002 /// @{
00003 
00004 /////////////////////////////////////////////////////////////////////////////
00005 ///
00006 /// @file setunion.h
00007 ///
00008 /// This file contains the definition of the container "setunion".
00009 /// The purpose of this container is to temporarily represent a union
00010 /// of two hashed sets of elements of the same type without actually
00011 /// merging the two hashed sets. Both sets cannot be modified through
00012 /// their union, they can only be accessed in the 'const' mode.
00013 ///
00014 /// @author Pawel Pilarczyk
00015 ///
00016 /////////////////////////////////////////////////////////////////////////////
00017 
00018 // Copyright (C) 1997-2013 by Pawel Pilarczyk.
00019 //
00020 // This file is part of the Homology Library.  This library is free software;
00021 // you can redistribute it and/or modify it under the terms of the GNU
00022 // General Public License as published by the Free Software Foundation;
00023 // either version 2 of the License, or (at your option) any later version.
00024 //
00025 // This library is distributed in the hope that it will be useful,
00026 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00027 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00028 // GNU General Public License for more details.
00029 //
00030 // You should have received a copy of the GNU General Public License along
00031 // with this software; see the file "license.txt".  If not, write to the
00032 // Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00033 // MA 02111-1307, USA.
00034 
00035 // Started on April 22, 2008. Last revision: April 23, 2008.
00036 
00037 
00038 #ifndef _CHOMP_STRUCT_SETUNION_H_
00039 #define _CHOMP_STRUCT_SETUNION_H_
00040 
00041 #include "chomp/system/config.h"
00042 
00043 namespace chomp {
00044 namespace homology {
00045 
00046 
00047 // class templates defined within this header file (in this order):
00048 template <class set1type, class set2type>
00049 class setunion;
00050 
00051 
00052 // --------------------------------------------------
00053 // ------------------- set union --------------------
00054 // --------------------------------------------------
00055 
00056 /// A union of two hashed sets. Thanks to the template style definition,
00057 /// it can be used to define a union of unions of sets etc., although
00058 /// the efficiency of this solution decreases with the increasing
00059 /// recursion level.
00060 template <class set1type, class set2type>
00061 class setunion
00062 {
00063 public:
00064         /// The type of the element of each of the sets.
00065         typedef typename set1type::value_type value_type;
00066 
00067         /// The only allowed constructor.
00068         setunion (const set1type &_set1, const set2type &_set2);
00069 
00070         /// The copy constructor.
00071         setunion (const setunion<set1type,set2type> &s);
00072 
00073         /// The assignment operator.
00074         setunion &operator = (const setunion<set1type,set2type> &s);
00075 
00076         /// The destructor.
00077         ~setunion ();
00078 
00079         /// Returns a const reference to the first set in the union.
00080         const set1type &get1 () const;
00081 
00082         /// Returns a const reference to the second set in the union.
00083         const set2type &get2 () const;
00084 
00085         /// Finds the given element and returns its number.
00086         /// Returns -1 if the element is not in the union of the sets.
00087         int_t getnumber (const typename set1type::value_type &e) const;
00088 
00089         /// Checks if the given number is an index of some element in the
00090         /// set union. That is, checks if the number is non-negative and
00091         /// strictly smaller than the number of elements in the set union.
00092         /// Returns true if yes, false if no.
00093         bool checknum (int_t n) const;
00094 
00095         /// Checks if the given element is in the set union.
00096         /// Returns true if yes, false if no.
00097         bool check (const typename set1type::value_type &e) const;
00098 
00099         /// Returns the element with the given number from the set union.
00100         const typename setunion<set1type,set2type>::value_type &
00101                 operator [] (int_t n) const;
00102 
00103         /// Returns the element with the given number from the set union.
00104         const typename setunion<set1type,set2type>::value_type &get (int_t n)
00105                 const;
00106 
00107         /// Returns the number of elements in the set union.
00108         int_t size () const;
00109 
00110         /// Returns true if both sets are empty. Otherwise returns false.
00111         bool empty () const;
00112 
00113 private:
00114         /// Reference to the first set.
00115         const set1type *set1;
00116 
00117         /// Reference to the second set.
00118         const set2type *set2;
00119 
00120 }; /* class setunion */
00121 
00122 // --------------------------------------------------
00123 
00124 template <class set1type, class set2type>
00125 inline setunion<set1type,set2type>::setunion (const set1type &_set1,
00126         const set2type &_set2): set1 (&_set1), set2 (&_set2)
00127 {
00128         return;
00129 } /* setunion::setunion */
00130 
00131 template <class set1type, class set2type>
00132 inline setunion<set1type,set2type>::~setunion ()
00133 {
00134         return;
00135 } /* setunion::~setunion */
00136 
00137 template <class set1type, class set2type>
00138 inline setunion<set1type,set2type>::setunion
00139         (const setunion<set1type,set2type> &)
00140 {
00141         throw "Trying to use the copy constructor of a set union.";
00142         return;
00143 } /* setunion::setunion */
00144 
00145 template <class set1type, class set2type>
00146 inline setunion<set1type,set2type> &setunion<set1type,set2type>::
00147 	operator = (const setunion<set1type,set2type> &)
00148 {
00149         throw "Trying to use the assignment operator of a set union.";
00150         return;
00151 } /* setunion::setunion */
00152 
00153 template <class set1type, class set2type>
00154 inline const set1type &setunion<set1type,set2type>::get1 () const
00155 {
00156         return *set1;
00157 } /* setunion::get1 */
00158 
00159 template <class set1type, class set2type>
00160 inline const set2type &setunion<set1type,set2type>::get2 () const
00161 {
00162         return *set2;
00163 } /* setunion::get2 */
00164 
00165 template <class set1type, class set2type>
00166 inline int_t setunion<set1type,set2type>::getnumber
00167         (const typename set1type::value_type &e) const
00168 {
00169         int_t n = set1 -> getnumber (e);
00170         if (n >= 0)
00171                 return n;
00172         n = set2 -> getnumber (e);
00173         if (n >= 0)
00174                 return set1 -> size () + n;
00175         else
00176                 return n;
00177 } /* setunion::getnumber */
00178 
00179 template <class set1type, class set2type>
00180 inline bool setunion<set1type,set2type>::checknum (int_t n) const
00181 {
00182         return ((n >= 0) && (n < set1 -> size () + set2 -> size ()));
00183 } /* setunion::checknum */
00184 
00185 template <class set1type, class set2type>
00186 inline bool setunion<set1type,set2type>::check
00187         (const typename set1type::value_type &e) const
00188 {
00189         return (set1 -> check (e) || set2 -> check (e));
00190 } /* setunion::check */
00191 
00192 template <class set1type, class set2type>
00193 inline const typename setunion<set1type,set2type>::value_type &
00194         setunion<set1type,set2type>::get (int_t n) const
00195 {
00196         int_t size1 = set1 -> size ();
00197         if (n < size1)
00198                 return set1 -> get (n);
00199         else
00200                 return set2 -> get (n - size1);
00201 } /* setunion::get */
00202 
00203 template <class set1type, class set2type>
00204 inline const typename setunion<set1type,set2type>::value_type &
00205         setunion<set1type,set2type>::operator [] (int_t n) const
00206 {
00207         return get (n);
00208 } /* setunion::operator [] */
00209 
00210 template <class set1type, class set2type>
00211 inline int_t setunion<set1type,set2type>::size () const
00212 {
00213         return (set1 -> size () + set2 -> size ());
00214 } /* setunion::size */
00215 
00216 template <class set1type, class set2type>
00217 inline bool setunion<set1type,set2type>::empty () const
00218 {
00219         return (set1 -> empty () && set2 -> empty ());
00220 } /* setunion::empty */
00221 
00222 // --------------------------------------------------
00223 
00224 /// Creates an object which represents the union of two sets.
00225 template <class set1type, class set2type>
00226 inline setunion<set1type,set2type> makesetunion (const set1type &set1,
00227         const set2type &set2)
00228 {
00229         return setunion<set1type,set2type> (set1, set2);
00230 } /* makesetunion */
00231 
00232 // Assigns the union of two sets to a single set using the assignment
00233 // operator and the "add" function.
00234 //template <class set1type, class set2type>
00235 //inline set1type &operator = (set1type &set1,
00236 //      const setunion<set1type,set2type> &set2)
00237 //{
00238 //      set1 = set2. get1 ();
00239 //      set1. add (set2. get2 ());
00240 //      return set1;
00241 //} /* operator = */
00242 
00243 
00244 } // namespace homology
00245 } // namespace chomp
00246 
00247 #endif // _CHOMP_STRUCT_SETUNION_H_
00248 
00249 /// @}
00250 

Generated on Sun Feb 3 2013 12:40:31 for The Uniform Expansion Software by  doxygen 1.7.2