Add array randomisation helper routine

This commit is contained in:
Danny Robson 2012-05-22 14:13:07 +10:00
parent 2783bcaa23
commit db6c603517
4 changed files with 53 additions and 1 deletions

View File

@ -76,6 +76,7 @@ UTIL_FILES = \
raii.hpp \
random.cpp \
random.hpp \
random.ipp \
range.cpp \
range.hpp \
region.cpp \

View File

@ -22,13 +22,27 @@
#include "range.hpp"
#include <type_traits>
using namespace util;
namespace util {
template <typename T>
T
random (void) {
static_assert (std::is_integral<T>::value, "random should only operate on integral types");
return range<T>::UNLIMITED.random ();
}
template <>
double
random (void)
{ return range<T>::UNLIMITED.random (); }
{ return range<double>::UNIT.random (); }
template <>
float
random (void)
{ return range<float>::UNIT.random (); }
template <typename T>
T&

View File

@ -26,6 +26,9 @@ namespace util {
template <typename T>
T& randomise (T &);
template <typename T, size_t N>
T* randomise (T(&)[N]);
template <typename T>
T random (void);
@ -49,4 +52,6 @@ namespace util {
}
}
#include "random.ipp"
#endif

32
random.ipp Normal file
View File

@ -0,0 +1,32 @@
/*
* This file is part of libgim.
*
* libgim is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2010 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_RANDOM_IPP
#define __UTIL_RANDOM_IPP
namespace util {
template <typename T, size_t N>
T* randomise (T (&array)[N]) {
for (auto &i: array)
i = random<T> ();
return array;
}
}
#endif