Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

more/math/equi_set.h

Go to the documentation of this file.
00001 //  Copyright (C) 2001--2002  Petter Urkedal
00002 //
00003 //  This file is free software; you can redistribute it and/or modify
00004 //  it under the terms of the GNU General Public License as published by
00005 //  the Free Software Foundation; either version 2 of the License, or
00006 //  (at your option) any later version.
00007 //
00008 //  This file is distributed in the hope that it will be useful,
00009 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 //  GNU General Public License for more details.
00012 //
00013 //  You should have received a copy of the GNU General Public License
00014 //  along with this program; if not, write to the Free Software
00015 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00016 //
00017 //  As a special exception, you may use this file as part of a free
00018 //  software library without restriction.  Specifically, if other files
00019 //  instantiate templates or use macros or inline functions from this
00020 //  file, or you compile this file and link it with other files to
00021 //  produce an executable, this file does not by itself cause the
00022 //  resulting executable to be covered by the GNU General Public
00023 //  License.  This exception does not however invalidate any other
00024 //  reasons why the executable file might be covered by the GNU General
00025 //  Public License.
00026 //
00027 //  $Id: equi_set.h,v 1.3 2002/09/01 12:01:45 petter_urkedal Exp $
00028 
00029 #ifndef MORE_GEN_EQUI_SET_H
00030 #define MORE_GEN_EQUI_SET_H
00031 
00032 ///\if experimental
00033 
00034 namespace more {
00035 namespace gen {
00036 
00037   /** \class equi_set equi_set.h more/math/equi_set.h
00038       A set of equidistant numbers. */
00039   template<typename Number>
00040     struct equi_set
00041     {
00042         equi_set(Number begin, Number end, Number step)
00043             : m_beg(begin), m_end(end), m_step(step) {}
00044 
00045         bool contains(Number i)
00046         {
00047             return !(i < m_beg) && i < m_end;
00048         }
00049 
00050         equi_set& operator|=(equi_set const& rhs) const;
00051 
00052         friend Number below(equi_set const& x) const {return x.m_beg-m_step;}
00053         friend Number min(equi_set const& x) const { return x.m_beg; }
00054         friend Number max(equi_set const& x) const { return x.m_end-m_step; }
00055         friend Number above(equi_set const& x) const { return x.m_end; }
00056 
00057       private:
00058         Number m_beg;
00059         Number m_end;
00060         Number m_step;
00061     };
00062 
00063   template<typename Number>
00064     equi_set<Number>&
00065     equi_set<Number>::operator|=(equi_set const& rhs)
00066     {
00067         if (m_step == rhs.m_step) {
00068             if ((m_beg - rhs.m_beg) % m_step == 0) {
00069                 if (rhs.m_beg < m_beg) {
00070                     if (rhs.m_end < m_beg)
00071                         goto fail;
00072                     m_beg = rhs.m_beg;
00073                 }
00074                 if (m_end < rhs.m_end) {
00075                     if (m_end < rhs.m_beg)
00076                         goto fail;
00077                     m_end = rhs.m_end;
00078                 }
00079                 return *this;
00080             }
00081             throw std::logic_error("more::math::equi_set::operator|=: "
00082                                    "Case is not implemented.");
00083         }
00084       fail:
00085         throw std::logic_error("more::math::equi_set::operator|=: "
00086                                "Result set can not be represented.");
00087     }
00088 
00089 }}
00090 
00091 ///\endif
00092 
00093 #endif

Generated on Sat Sep 7 19:11:18 2002 for more with Doxygen 1.2.13.1. Doxygen 1.2.13.1 is written and copyright 1997-2002 by Dimitri van Heesch.