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

parttype.h

Go to the documentation of this file.
00001 /// @addtogroup unifexp
00002 /// @{
00003 
00004 /////////////////////////////////////////////////////////////////////////////
00005 ///
00006 /// @file parttype.h
00007 ///
00008 /// This file contains the definition of an abstract partition type.
00009 ///
00010 /// @author Pawel Pilarczyk
00011 ///
00012 /////////////////////////////////////////////////////////////////////////////
00013 
00014 // Copyright (C) 2007 by Pawel Pilarczyk.
00015 //
00016 // This file is part of my research program package.  This is free software;
00017 // you can redistribute it and/or modify it under the terms of the GNU
00018 // General Public License as published by the Free Software Foundation;
00019 // either version 2 of the License, or (at your option) any later version.
00020 //
00021 // This software is distributed in the hope that it will be useful,
00022 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00023 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024 // GNU General Public License for more details.
00025 //
00026 // You should have received a copy of the GNU General Public License along
00027 // with this software; see the file "license.txt".  If not, write to the
00028 // Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00029 // MA 02111-1307, USA.
00030 
00031 // Started on March 8, 2007. Last revision: August 22, 2007.
00032 
00033 #ifndef _parttype_h_
00034 #define _parttype_h_
00035 
00036 #include <vector>
00037 #include <string>
00038 #include <algorithm>
00039 #include "chomp/struct/bitfield.h"
00040 #include "maptype.h"
00041 
00042 
00043 namespace unifexp {
00044 
00045 // --------------------------------------------------
00046 // ----------------- partition type -----------------
00047 // --------------------------------------------------
00048 
00049 /// An abstract partition type.
00050 /// All the other partition types should inherit from this one.
00051 template <class numType>
00052 class partType
00053 {
00054 public:
00055         /// The constructor.
00056         partType ();
00057 
00058         /// The virtual destructor.
00059         virtual ~partType ();
00060 
00061         /// Returns the name of the object.
00062         virtual std::string name () const = 0;
00063 
00064         /// Creates a partition based on the given map, the requested
00065         /// number of elements in the partition, and the width of the
00066         /// critical neighborhood.
00067         virtual void create (const mapType<numType> &theMap, int partCount,
00068                 const numType &delta) = 0;
00069 
00070         /// Runs the bisection algorithm to find the maximal element in the
00071         /// partition table which is less than or equal to the given number.
00072         /// Uses operator < for comparision, operator [] to extract from
00073         /// the table. Returns the number of that element or -1 if the number
00074         /// is below the leftmost limit.
00075         int find (const numType &number) const;
00076 
00077         /// Returns the given interval bound from the partition: the left end
00078         /// of the n-th interval, or right end of the (n-1)-th one.
00079         /// Does not verify the correctness of the index.
00080         /// Note that the operator [] which accesses a non-constant object
00081         /// is protected, so it may be necessary to use a const reference
00082         /// to access the data of a non-const object with operator [].
00083         /// In case of any problems, use the function "at" instead (it is
00084         /// also safer, because it verifies the correctness of the index).
00085         const numType &operator [] (int n) const;
00086 
00087         /// Returns the given interval bound from the partition: the left end
00088         /// of the n-th interval, or right end of the (n-1)-th one.
00089         /// Verifies the correctness of the index and throws an error message
00090         /// if n is out of range.
00091         const numType &at (int n) const;
00092 
00093         /// Returns true if the given interval is part of a neighborhood
00094         /// of some critical point. Returns false otherwise.
00095         /// Note that this function does not verify the correctness of n,
00096         /// so it must be used with caution.
00097         bool isCritical (int n) const;
00098 
00099         /// Returns the number of the interval which is a neighborhood
00100         /// of a critical point, or throws an exception if 'n' out of scope.
00101         /// See the map class for the actual number of critical points.
00102         int getCritical (int n = 0) const;
00103 
00104 protected:
00105         /// Allocates the table to store the given number of intervals.
00106         /// It is automatically deallocated by the destructor.
00107         void allocate (int n);
00108 
00109         /// Adds a critical interval to the list of critical intervals.
00110         void addCritical (int n);
00111 
00112         /// Returns the given interval bound from the partition: the left end
00113         /// of the n-th interval, or right end of the (n-1)-th one.
00114         /// Does not verify the correctness of the index.
00115         numType &operator [] (int n);
00116 
00117 private:
00118         /// The table with the bounds of partition intervals.
00119         /// Initially this address is set to 0.
00120         /// Use the function "allocate" to (re)allocate this table,
00121         /// and the operator [] and function "at" to access its elements.
00122         numType *intBounds;
00123 
00124         /// The number of intervals in the partition.
00125         /// Initialized to 0, set by the function "allocate".
00126         int intCount;
00127 
00128         /// The list of critical intervals.
00129         std::vector<int> critical;
00130 
00131         /// A bit field which stores the information about intervals
00132         /// if they are critical or not.
00133         chomp::homology::BitField b;
00134 
00135         /// Copy constructor is not implemented.
00136         partType (const partType<numType> &);
00137 
00138         /// Assignment operator is not implemented.
00139         partType<numType> &operator = (const partType<numType> &);
00140 
00141 }; /* class partType */
00142 
00143 // --------------------------------------------------
00144 
00145 template <class numType>
00146 inline partType<numType>::partType (): intBounds (0), intCount (0)
00147 {
00148         return;
00149 } /* partType::partType */
00150 
00151 template <class numType>
00152 inline partType<numType>::partType (const partType<numType> &)
00153 {
00154         throw "Copy constructor not implemented for partitions.";
00155         return;
00156 } /* partType::partType */
00157 
00158 template <class numType>
00159 inline partType<numType> &partType<numType>::operator =
00160         (const partType<numType> &)
00161 {
00162         throw "Assignment operator not implemented for partitions.";
00163         return *this;
00164 } /* partType::operator = */
00165 
00166 template <class numType>
00167 inline partType<numType>::~partType ()
00168 {
00169         if (intBounds)
00170         {
00171                 delete [] intBounds;
00172                 b. free ();
00173         }
00174         return;
00175 } /* partType::~partType */
00176 
00177 template <class numType>
00178 inline const numType &partType<numType>::operator [] (int n) const
00179 {
00180         return intBounds [n];
00181 } /* partType::operator [] */
00182 
00183 template <class numType>
00184 inline numType &partType<numType>::operator [] (int n)
00185 {
00186         return intBounds [n];
00187 } /* partType::operator [] */
00188 
00189 template <class numType>
00190 inline const numType &partType<numType>::at (int n) const
00191 {
00192         if (!intBounds || (n < 0) || (n > intCount))
00193                 throw "Partition element out of range requested.";
00194         return intBounds [n];
00195 } /* partType::at */
00196 
00197 template <class numType>
00198 inline bool partType<numType>::isCritical (int n) const
00199 {
00200         return !!b. test (n);
00201 //      return (std::find (critical. begin (), critical. end (), n) !=
00202 //              critical. end ());
00203 } /* partType::isCritical */
00204 
00205 template <class numType>
00206 inline int partType<numType>::getCritical (int n) const
00207 {
00208         if ((n < 0) || (n >= static_cast<int> (critical. size ())))
00209                 throw "Critical interval number out of range.";
00210         return critical [n];
00211 } /* partType::getCritical */
00212 
00213 // --------------------------------------------------
00214 
00215 template <class numType>
00216 inline int partType<numType>::find (const numType &number) const
00217 {
00218         if (!intBounds)
00219                 throw "Partition not allocated.";
00220         if (number < intBounds [0])
00221                 return -1;
00222         if (number >= intBounds [intCount])
00223                 return intCount;
00224         int lowerbound = 0, upperbound = intCount + 1;
00225         while (upperbound - lowerbound > 1)
00226         {
00227                 int middle = (lowerbound + upperbound) >> 1;
00228                 if (number < intBounds [middle])
00229                         upperbound = middle;
00230                 else
00231                         lowerbound = middle;
00232         }
00233         return lowerbound;
00234 } /* partType::find */
00235 
00236 template <class numType>
00237 inline void partType<numType>::addCritical (int n)
00238 {
00239         critical. push_back (n);
00240         b. set (n);
00241         return;
00242 } /* partType::addCritical */
00243 
00244 template <class numType>
00245 inline void partType<numType>::allocate (int n)
00246 {
00247         if (n <= 0)
00248         {
00249                 throw "A non-positive number of partition intervals "
00250                         "requested.";
00251         }
00252         if (intBounds)
00253         {
00254                 delete [] intBounds;
00255                 b. free ();
00256         }
00257         intBounds = new numType [n + 1];
00258         intCount = n;
00259         b. allocate (n);
00260         b. clearall (n);
00261         critical. clear ();
00262         return;
00263 } /* partType::allocate */
00264 
00265 
00266 } // namespace unifexp
00267 
00268 #endif // _parttype_h_
00269 
00270 /// @}
00271 

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