setunion.h

Go to the documentation of this file.
00001 
00002 
00003 
00017 
00018 // Copyright (C) 1997-2010 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 
00060 template <class set1type, class set2type>
00061 class setunion
00062 {
00063 public:
00065         typedef typename set1type::value_type value_type;
00066 
00068         setunion (const set1type &_set1, const set2type &_set2);
00069 
00071         setunion (const setunion<set1type,set2type> &s);
00072 
00074         setunion &operator = (const setunion<set1type,set2type> &s);
00075 
00077         ~setunion ();
00078 
00080         const set1type &get1 () const;
00081 
00083         const set2type &get2 () const;
00084 
00087         int_t getnumber (const typename set1type::value_type &e) const;
00088 
00093         bool checknum (int_t n) const;
00094 
00097         bool check (const typename set1type::value_type &e) const;
00098 
00100         const typename setunion<set1type,set2type>::value_type &
00101                 operator [] (int_t n) const;
00102 
00104         const typename setunion<set1type,set2type>::value_type &get (int_t n)
00105                 const;
00106 
00108         int_t size () const;
00109 
00111         bool empty () const;
00112 
00113 private:
00115         const set1type *set1;
00116 
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 
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 
00250