Queue: use C++ random instead of GLib's GRand

This commit is contained in:
Max Kellermann 2013-01-07 21:07:30 +01:00
parent 5b8b7d1412
commit c6281b2680
3 changed files with 20 additions and 10 deletions

View File

@ -1337,8 +1337,10 @@ test_test_pcm_LDADD = \
test_TestQueuePriority_SOURCES = \ test_TestQueuePriority_SOURCES = \
src/Queue.cxx \ src/Queue.cxx \
src/fd_util.c \
test/TestQueuePriority.cxx test/TestQueuePriority.cxx
test_TestQueuePriority_LDADD = \ test_TestQueuePriority_LDADD = \
libutil.a \
$(GLIB_LIBS) $(GLIB_LIBS)
noinst_PROGRAMS += src/dsd2pcm/dsd2pcm noinst_PROGRAMS += src/dsd2pcm/dsd2pcm

View File

@ -33,8 +33,7 @@ queue::queue(unsigned _max_length)
repeat(false), repeat(false),
single(false), single(false),
consume(false), consume(false),
random(false), random(false)
rand(g_rand_new())
{ {
for (unsigned i = 0; i < max_length * QUEUE_HASH_MULT; ++i) for (unsigned i = 0; i < max_length * QUEUE_HASH_MULT; ++i)
id_to_position[i] = -1; id_to_position[i] = -1;
@ -47,8 +46,6 @@ queue::~queue()
g_free(items); g_free(items);
g_free(order); g_free(order);
g_free(id_to_position); g_free(id_to_position);
g_rand_free(rand);
} }
/** /**
@ -375,8 +372,8 @@ queue::ShuffleOrderRange(unsigned start, unsigned end)
assert(start <= end); assert(start <= end);
assert(end <= length); assert(end <= length);
for (unsigned i = start; i < end; ++i) rand.AutoCreate();
SwapOrders(i, g_rand_int_range(rand, i, end)); std::shuffle(order + start, order + end, rand);
} }
/** /**
@ -426,13 +423,19 @@ queue::ShuffleOrder()
void void
queue::ShuffleOrderFirst(unsigned start, unsigned end) 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 void
queue::ShuffleOrderLast(unsigned start, unsigned end) 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 void
@ -441,8 +444,12 @@ queue::ShuffleRange(unsigned start, unsigned end)
assert(start <= end); assert(start <= end);
assert(end <= length); assert(end <= length);
rand.AutoCreate();
for (unsigned i = start; i < end; i++) { 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); SwapPositions(i, ri);
} }
} }

View File

@ -21,6 +21,7 @@
#define MPD_QUEUE_HXX #define MPD_QUEUE_HXX
#include "gcc.h" #include "gcc.h"
#include "util/LazyRandomEngine.hxx"
#include <glib.h> #include <glib.h>
@ -101,7 +102,7 @@ struct queue {
bool random; bool random;
/** random number generator for shuffle and random mode */ /** random number generator for shuffle and random mode */
GRand *rand; LazyRandomEngine rand;
queue(unsigned max_length); queue(unsigned max_length);