diff options
author | Max Kellermann <max@duempel.org> | 2013-01-07 21:07:30 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2013-01-07 22:00:57 +0100 |
commit | c6281b2680ec10d31d00869091a0f918041a0554 (patch) | |
tree | f6d7bcb97c393aacd1ff287160ecda3d43aa5e21 | |
parent | 5b8b7d14127a5ea9ce8f0301c92267f86eeab85a (diff) |
Queue: use C++ random instead of GLib's GRand
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | src/Queue.cxx | 25 | ||||
-rw-r--r-- | src/Queue.hxx | 3 |
3 files changed, 20 insertions, 10 deletions
diff --git a/Makefile.am b/Makefile.am index e86be73d9..fb500ed47 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1337,8 +1337,10 @@ test_test_pcm_LDADD = \ test_TestQueuePriority_SOURCES = \ src/Queue.cxx \ + src/fd_util.c \ test/TestQueuePriority.cxx test_TestQueuePriority_LDADD = \ + libutil.a \ $(GLIB_LIBS) noinst_PROGRAMS += src/dsd2pcm/dsd2pcm diff --git a/src/Queue.cxx b/src/Queue.cxx index 5b3e00d90..8dcb96aa0 100644 --- a/src/Queue.cxx +++ b/src/Queue.cxx @@ -33,8 +33,7 @@ queue::queue(unsigned _max_length) repeat(false), single(false), consume(false), - random(false), - rand(g_rand_new()) + random(false) { for (unsigned i = 0; i < max_length * QUEUE_HASH_MULT; ++i) id_to_position[i] = -1; @@ -47,8 +46,6 @@ queue::~queue() g_free(items); g_free(order); g_free(id_to_position); - - g_rand_free(rand); } /** @@ -375,8 +372,8 @@ queue::ShuffleOrderRange(unsigned start, unsigned end) assert(start <= end); assert(end <= length); - for (unsigned i = start; i < end; ++i) - SwapOrders(i, g_rand_int_range(rand, i, end)); + rand.AutoCreate(); + std::shuffle(order + start, order + end, rand); } /** @@ -426,13 +423,19 @@ queue::ShuffleOrder() void queue::ShuffleOrderFirst(unsigned start, unsigned end) { - SwapOrders(start, g_rand_int_range(rand, start, end)); + rand.AutoCreate(); + + std::uniform_int_distribution<unsigned> distribution(start, end - 1); + SwapOrders(start, distribution(rand)); } void queue::ShuffleOrderLast(unsigned start, unsigned end) { - SwapOrders(end - 1, g_rand_int_range(rand, start, end)); + rand.AutoCreate(); + + std::uniform_int_distribution<unsigned> distribution(start, end - 1); + SwapOrders(end - 1, distribution(rand)); } void @@ -441,8 +444,12 @@ queue::ShuffleRange(unsigned start, unsigned end) assert(start <= end); assert(end <= length); + rand.AutoCreate(); + for (unsigned i = start; i < end; i++) { - unsigned ri = g_rand_int_range(rand, i, end); + std::uniform_int_distribution<unsigned> distribution(start, + end - 1); + unsigned ri = distribution(rand); SwapPositions(i, ri); } } diff --git a/src/Queue.hxx b/src/Queue.hxx index b887810b0..00af9b666 100644 --- a/src/Queue.hxx +++ b/src/Queue.hxx @@ -21,6 +21,7 @@ #define MPD_QUEUE_HXX #include "gcc.h" +#include "util/LazyRandomEngine.hxx" #include <glib.h> @@ -101,7 +102,7 @@ struct queue { bool random; /** random number generator for shuffle and random mode */ - GRand *rand; + LazyRandomEngine rand; queue(unsigned max_length); |