rnd.cpp
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 * Mikael Lagerkvist <lagerkvist@gecode.org> 00006 * 00007 * Copyright: 00008 * Christian Schulte, 2008 00009 * Mikael Lagerkvist, 2008 00010 * 00011 * Last modified: 00012 * $Date: 2016-06-19 22:08:39 +0200 (Sun, 19 Jun 2016) $ by $Author: schulte $ 00013 * $Revision: 15119 $ 00014 * 00015 * This file is part of Gecode, the generic constraint 00016 * development environment: 00017 * http://www.gecode.org 00018 * 00019 * Permission is hereby granted, free of charge, to any person obtaining 00020 * a copy of this software and associated documentation files (the 00021 * "Software"), to deal in the Software without restriction, including 00022 * without limitation the rights to use, copy, modify, merge, publish, 00023 * distribute, sublicense, and/or sell copies of the Software, and to 00024 * permit persons to whom the Software is furnished to do so, subject to 00025 * the following conditions: 00026 * 00027 * The above copyright notice and this permission notice shall be 00028 * included in all copies or substantial portions of the Software. 00029 * 00030 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00031 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00032 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00033 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00034 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00035 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00036 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00037 * 00038 */ 00039 00040 #include <gecode/kernel.hh> 00041 00042 namespace Gecode { 00043 00044 forceinline 00045 Rnd::IMP::IMP(unsigned int s) 00046 : rg(s) {} 00047 00048 Rnd::IMP::~IMP(void) {} 00049 00050 SharedHandle::Object* 00051 Rnd::IMP::copy(void) const { 00052 return new IMP(rg.seed()); 00053 } 00054 00055 forceinline void 00056 Rnd::_seed(unsigned int s) { 00057 if (object() == NULL) { 00058 object(new IMP(s)); 00059 } else { 00060 static_cast<IMP*>(object())->rg.seed(s); 00061 } 00062 } 00063 00064 Rnd::Rnd(void) {} 00065 Rnd::Rnd(unsigned int s) { 00066 object(new IMP(s)); 00067 } 00068 Rnd::Rnd(const Rnd& r) 00069 : SharedHandle(r) {} 00070 Rnd& 00071 Rnd::operator =(const Rnd& r) { 00072 (void) SharedHandle::operator =(r); 00073 return *this; 00074 } 00075 Rnd::~Rnd(void) {} 00076 00077 void 00078 Rnd::seed(unsigned int s) { 00079 _seed(s); 00080 } 00081 void 00082 Rnd::time(void) { 00083 _seed(static_cast<unsigned int>(::time(NULL))); 00084 } 00085 void 00086 Rnd::hw(void) { 00087 seed(Support::hwrnd()); 00088 } 00089 } 00090 00091 // STATISTICS: kernel-other