Generated on Thu Apr 11 13:59:15 2019 for Gecode by doxygen 1.6.3

rnd.hpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2008
00008  *
00009  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 #include <ctime>
00035 
00036 namespace Gecode {
00037 
00042   class Rnd : public SharedHandle {
00043   private:
00045     class IMP : public SharedHandle::Object {
00046     protected:
00048       GECODE_KERNEL_EXPORT static Support::Mutex m;
00050       Support::RandomGenerator rg;
00051     public:
00053       IMP(unsigned int s);
00055       unsigned int seed(void) const;
00057       void seed(unsigned int s);
00059       unsigned int operator ()(unsigned int n);
00061       virtual ~IMP(void);
00062     };
00064     void _seed(unsigned int s);
00065   public:
00067     GECODE_KERNEL_EXPORT
00068     Rnd(void);
00070     GECODE_KERNEL_EXPORT
00071     Rnd(const Rnd& r);
00073     GECODE_KERNEL_EXPORT
00074     Rnd& operator =(const Rnd& r);
00076     GECODE_KERNEL_EXPORT
00077     ~Rnd(void);
00079     GECODE_KERNEL_EXPORT
00080     Rnd(unsigned int s);
00082     GECODE_KERNEL_EXPORT
00083     void seed(unsigned int s);
00085     GECODE_KERNEL_EXPORT
00086     void time(void);
00088     GECODE_KERNEL_EXPORT
00089     void hw(void);
00091     unsigned int seed(void) const;
00093     unsigned int operator ()(unsigned int n);
00094   };
00095 
00096   forceinline unsigned int
00097   Rnd::IMP::seed(void) const {
00098     unsigned int s;
00099     const_cast<Rnd::IMP&>(*this).m.acquire();
00100     s = rg.seed();
00101     const_cast<Rnd::IMP&>(*this).m.release();
00102     return s;
00103   }
00104   forceinline void
00105   Rnd::IMP::seed(unsigned int s) {
00106     m.acquire();
00107     rg.seed(s);
00108     m.release();
00109   }
00110   forceinline unsigned int
00111   Rnd::IMP::operator ()(unsigned int n) {
00112     unsigned int r;
00113     m.acquire();
00114     r=rg(n);
00115     m.release();
00116     return r;
00117   }
00118 
00119   forceinline unsigned int
00120   Rnd::seed(void) const {
00121     const IMP* i = static_cast<const IMP*>(object());
00122     return i->seed();
00123   }
00124   forceinline unsigned int
00125   Rnd::operator ()(unsigned int n) {
00126     IMP* i = static_cast<IMP*>(object());
00127     return (*i)(n);
00128   }
00129 
00130 }
00131 
00132 // STATISTICS: kernel-other