2009-01-22 23:40:11 +01:00
|
|
|
/*
|
2013-01-04 20:50:00 +01:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-01-22 23:40:11 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program 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 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2009-01-22 23:40:11 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-04 20:50:00 +01:00
|
|
|
#include "Queue.hxx"
|
2009-01-22 23:40:11 +01:00
|
|
|
#include "song.h"
|
|
|
|
|
2013-01-08 16:38:00 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
2011-07-19 00:34:33 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::queue(unsigned _max_length)
|
|
|
|
:max_length(_max_length), length(0),
|
|
|
|
version(1),
|
2013-01-08 16:35:09 +01:00
|
|
|
items(new Item[max_length]),
|
|
|
|
order(new unsigned[max_length]),
|
2013-01-08 16:11:25 +01:00
|
|
|
id_table(max_length * HASH_MULT),
|
2013-01-06 21:33:58 +01:00
|
|
|
repeat(false),
|
|
|
|
single(false),
|
|
|
|
consume(false),
|
2013-01-07 21:07:30 +01:00
|
|
|
random(false)
|
2013-01-06 21:33:58 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
queue::~queue()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
|
2013-01-08 16:35:09 +01:00
|
|
|
delete[] items;
|
|
|
|
delete[] order;
|
2009-01-22 23:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::GetNextOrder(unsigned _order) const
|
2009-01-22 23:40:11 +01:00
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(_order < length);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
if (single && repeat && !consume)
|
|
|
|
return _order;
|
|
|
|
else if (_order + 1 < length)
|
|
|
|
return _order + 1;
|
|
|
|
else if (repeat && (_order > 0 || !consume))
|
2009-01-22 23:40:11 +01:00
|
|
|
/* restart at first song */
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
/* end of queue */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::IncrementVersion()
|
2009-01-22 23:40:11 +01:00
|
|
|
{
|
|
|
|
static unsigned long max = ((uint32_t) 1 << 31) - 1;
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
version++;
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
if (version >= max) {
|
|
|
|
for (unsigned i = 0; i < length; i++)
|
|
|
|
items[i].version = 0;
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
version = 1;
|
2009-01-22 23:40:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::ModifyAtOrder(unsigned _order)
|
2009-01-22 23:40:11 +01:00
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(_order < length);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
unsigned position = order[_order];
|
|
|
|
items[position].version = version;
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
IncrementVersion();
|
2009-01-22 23:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::ModifyAll()
|
2009-01-22 23:40:11 +01:00
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
for (unsigned i = 0; i < length; i++)
|
|
|
|
items[i].version = version;
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
IncrementVersion();
|
2009-01-22 23:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::Append(struct song *song, uint8_t priority)
|
2009-01-22 23:40:11 +01:00
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(!IsFull());
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-08 16:11:25 +01:00
|
|
|
const unsigned position = length++;
|
|
|
|
const unsigned id = id_table.Insert(position);
|
|
|
|
|
|
|
|
auto &item = items[position];
|
2013-01-04 20:50:00 +01:00
|
|
|
item.song = song_dup_detached(song);
|
|
|
|
item.id = id;
|
2013-01-06 21:33:58 +01:00
|
|
|
item.version = version;
|
2013-01-04 20:50:00 +01:00
|
|
|
item.priority = priority;
|
2009-01-23 00:06:38 +01:00
|
|
|
|
2013-01-08 16:11:25 +01:00
|
|
|
order[position] = position;
|
2009-01-22 23:40:11 +01:00
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::SwapPositions(unsigned position1, unsigned position2)
|
2009-01-22 23:40:11 +01:00
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
unsigned id1 = items[position1].id;
|
|
|
|
unsigned id2 = items[position2].id;
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 23:44:59 +01:00
|
|
|
std::swap(items[position1], items[position2]);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
items[position1].version = version;
|
|
|
|
items[position2].version = version;
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-08 16:11:25 +01:00
|
|
|
id_table.Move(id1, position2);
|
|
|
|
id_table.Move(id2, position1);
|
2009-01-22 23:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::MovePostion(unsigned from, unsigned to)
|
2009-01-22 23:40:11 +01:00
|
|
|
{
|
2013-01-08 16:05:10 +01:00
|
|
|
const Item tmp = items[from];
|
2009-01-22 23:40:11 +01:00
|
|
|
|
|
|
|
/* move songs to one less in from->to */
|
|
|
|
|
|
|
|
for (unsigned i = from; i < to; i++)
|
2013-01-08 15:31:39 +01:00
|
|
|
MoveItemTo(i + 1, i);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
|
|
|
/* move songs to one more in to->from */
|
|
|
|
|
|
|
|
for (unsigned i = from; i > to; i--)
|
2013-01-08 15:31:39 +01:00
|
|
|
MoveItemTo(i - 1, i);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
|
|
|
/* put song at _to_ */
|
|
|
|
|
2013-01-08 16:11:25 +01:00
|
|
|
id_table.Move(tmp.id, to);
|
2013-01-08 16:05:10 +01:00
|
|
|
items[to] = tmp;
|
2013-01-06 21:33:58 +01:00
|
|
|
items[to].version = version;
|
2009-02-09 22:47:23 +01:00
|
|
|
|
|
|
|
/* now deal with order */
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
if (random) {
|
|
|
|
for (unsigned i = 0; i < length; i++) {
|
|
|
|
if (order[i] > from && order[i] <= to)
|
|
|
|
order[i]--;
|
|
|
|
else if (order[i] < from &&
|
|
|
|
order[i] >= to)
|
|
|
|
order[i]++;
|
|
|
|
else if (from == order[i])
|
|
|
|
order[i] = to;
|
2009-02-09 22:47:23 +01:00
|
|
|
}
|
|
|
|
}
|
2009-01-22 23:40:11 +01:00
|
|
|
}
|
|
|
|
|
2009-03-26 22:02:56 +01:00
|
|
|
void
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::MoveRange(unsigned start, unsigned end, unsigned to)
|
2009-03-26 22:02:56 +01:00
|
|
|
{
|
2013-01-08 16:05:10 +01:00
|
|
|
Item tmp[end - start];
|
2009-03-26 22:02:56 +01:00
|
|
|
// Copy the original block [start,end-1]
|
|
|
|
for (unsigned i = start; i < end; i++)
|
2013-01-06 21:33:58 +01:00
|
|
|
tmp[i - start] = items[i];
|
2009-03-26 22:02:56 +01:00
|
|
|
|
|
|
|
// If to > start, we need to move to-start items to start, starting from end
|
|
|
|
for (unsigned i = end; i < end + to - start; i++)
|
2013-01-08 15:31:39 +01:00
|
|
|
MoveItemTo(i, start + i - end);
|
2009-03-26 22:02:56 +01:00
|
|
|
|
|
|
|
// If to < start, we need to move start-to items to newend (= end + to - start), starting from to
|
|
|
|
// This is the same as moving items from start-1 to to (decreasing), with start-1 going to end-1
|
|
|
|
// We have to iterate in this order to avoid writing over something we haven't yet moved
|
2013-01-08 16:33:33 +01:00
|
|
|
for (int i = start - 1; i >= int(to); i--)
|
2013-01-08 15:31:39 +01:00
|
|
|
MoveItemTo(i, i + end - start);
|
2009-03-26 22:02:56 +01:00
|
|
|
|
|
|
|
// Copy the original block back in, starting at to.
|
|
|
|
for (unsigned i = start; i< end; i++)
|
|
|
|
{
|
2013-01-08 16:11:25 +01:00
|
|
|
id_table.Move(tmp[i - start].id, to + i - start);
|
2013-01-06 21:33:58 +01:00
|
|
|
items[to + i - start] = tmp[i-start];
|
|
|
|
items[to + i - start].version = version;
|
2009-03-26 22:02:56 +01:00
|
|
|
}
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
if (random) {
|
2009-03-26 22:02:56 +01:00
|
|
|
// Update the positions in the queue.
|
|
|
|
// Note that the ranges for these cases are the same as the ranges of
|
|
|
|
// the loops above.
|
2013-01-06 21:33:58 +01:00
|
|
|
for (unsigned i = 0; i < length; i++) {
|
|
|
|
if (order[i] >= end && order[i] < to + end - start)
|
|
|
|
order[i] -= end - start;
|
|
|
|
else if (order[i] < start &&
|
|
|
|
order[i] >= to)
|
|
|
|
order[i] += end - start;
|
|
|
|
else if (start <= order[i] && order[i] < end)
|
|
|
|
order[i] += to - start;
|
2009-03-26 22:02:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-08 15:31:39 +01:00
|
|
|
void
|
|
|
|
queue::MoveOrder(unsigned from_order, unsigned to_order)
|
2011-07-19 00:34:33 +02:00
|
|
|
{
|
2013-01-08 15:31:39 +01:00
|
|
|
assert(from_order < length);
|
|
|
|
assert(to_order <= length);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
2013-01-08 15:31:39 +01:00
|
|
|
const unsigned from_position = OrderToPosition(from_order);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
|
|
|
if (from_order < to_order) {
|
|
|
|
for (unsigned i = from_order; i < to_order; ++i)
|
2013-01-08 15:31:39 +01:00
|
|
|
order[i] = order[i + 1];
|
2011-07-19 00:34:33 +02:00
|
|
|
} else {
|
|
|
|
for (unsigned i = from_order; i > to_order; --i)
|
2013-01-08 15:31:39 +01:00
|
|
|
order[i] = order[i - 1];
|
2011-07-19 00:34:33 +02:00
|
|
|
}
|
|
|
|
|
2013-01-08 15:31:39 +01:00
|
|
|
order[to_order] = from_position;
|
2011-07-19 00:34:33 +02:00
|
|
|
}
|
|
|
|
|
2009-01-22 23:40:11 +01:00
|
|
|
void
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::DeletePosition(unsigned position)
|
2009-01-22 23:40:11 +01:00
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(position < length);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
struct song *song = Get(position);
|
2012-08-15 19:52:32 +02:00
|
|
|
assert(!song_in_database(song) || song_is_detached(song));
|
|
|
|
song_free(song);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
const unsigned id = PositionToId(position);
|
|
|
|
const unsigned _order = PositionToOrder(position);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
--length;
|
2009-01-22 23:40:11 +01:00
|
|
|
|
|
|
|
/* release the song id */
|
|
|
|
|
2013-01-08 16:11:25 +01:00
|
|
|
id_table.Erase(id);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
|
|
|
/* delete song from songs array */
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
for (unsigned i = position; i < length; i++)
|
2013-01-08 15:31:39 +01:00
|
|
|
MoveItemTo(i + 1, i);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
|
|
|
/* delete the entry from the order array */
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
for (unsigned i = _order; i < length; i++)
|
|
|
|
order[i] = order[i + 1];
|
2009-01-22 23:40:11 +01:00
|
|
|
|
|
|
|
/* readjust values in the order array */
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
for (unsigned i = 0; i < length; i++)
|
|
|
|
if (order[i] > position)
|
|
|
|
--order[i];
|
2009-01-22 23:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::Clear()
|
2009-01-22 23:40:11 +01:00
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
for (unsigned i = 0; i < length; i++) {
|
2013-01-08 16:05:10 +01:00
|
|
|
Item *item = &items[i];
|
2009-01-23 00:06:38 +01:00
|
|
|
|
2012-08-15 19:52:32 +02:00
|
|
|
assert(!song_in_database(item->song) ||
|
|
|
|
song_is_detached(item->song));
|
|
|
|
song_free(item->song);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-08 16:11:25 +01:00
|
|
|
id_table.Erase(item->id);
|
2009-01-22 23:40:11 +01:00
|
|
|
}
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
length = 0;
|
2009-01-22 23:40:11 +01:00
|
|
|
}
|
|
|
|
|
2011-07-19 00:34:33 +02:00
|
|
|
static gint
|
|
|
|
queue_item_compare_order_priority(gconstpointer av, gconstpointer bv,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
2013-01-04 20:50:00 +01:00
|
|
|
const struct queue *queue = (const struct queue *)user_data;
|
|
|
|
const unsigned *const ap = (const unsigned *)av;
|
|
|
|
const unsigned *const bp = (const unsigned *)bv;
|
2011-07-19 00:34:33 +02:00
|
|
|
assert(ap >= queue->order && ap < queue->order + queue->length);
|
|
|
|
assert(bp >= queue->order && bp < queue->order + queue->length);
|
|
|
|
uint8_t a = queue->items[*ap].priority;
|
|
|
|
uint8_t b = queue->items[*bp].priority;
|
|
|
|
|
2013-01-08 16:35:09 +01:00
|
|
|
if (gcc_likely(a == b))
|
2011-07-19 00:34:33 +02:00
|
|
|
return 0;
|
|
|
|
else if (a > b)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
queue_sort_order_by_priority(struct queue *queue, unsigned start, unsigned end)
|
2009-01-23 00:08:40 +01:00
|
|
|
{
|
2011-07-19 00:34:33 +02:00
|
|
|
assert(queue != NULL);
|
2009-01-23 00:08:40 +01:00
|
|
|
assert(queue->random);
|
2011-07-19 00:34:33 +02:00
|
|
|
assert(start <= end);
|
|
|
|
assert(end <= queue->length);
|
2009-01-23 00:08:40 +01:00
|
|
|
|
2011-07-19 00:34:33 +02:00
|
|
|
g_qsort_with_data(&queue->order[start], end - start,
|
|
|
|
sizeof(queue->order[0]),
|
|
|
|
queue_item_compare_order_priority,
|
|
|
|
queue);
|
|
|
|
}
|
|
|
|
|
2013-01-07 21:17:01 +01:00
|
|
|
void
|
|
|
|
queue::ShuffleOrderRange(unsigned start, unsigned end)
|
2011-07-19 00:34:33 +02:00
|
|
|
{
|
2013-01-07 21:17:01 +01:00
|
|
|
assert(random);
|
2011-07-19 00:34:33 +02:00
|
|
|
assert(start <= end);
|
2013-01-07 21:17:01 +01:00
|
|
|
assert(end <= length);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
2013-01-07 21:07:30 +01:00
|
|
|
rand.AutoCreate();
|
|
|
|
std::shuffle(order + start, order + end, rand);
|
2011-07-19 00:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort the "order" of items by priority, and then shuffle each
|
|
|
|
* priority group.
|
|
|
|
*/
|
|
|
|
void
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::ShuffleOrderRangeWithPriority(unsigned start, unsigned end)
|
2011-07-19 00:34:33 +02:00
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(random);
|
2011-07-19 00:34:33 +02:00
|
|
|
assert(start <= end);
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(end <= length);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
|
|
|
if (start == end)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* first group the range by priority */
|
2013-01-06 21:33:58 +01:00
|
|
|
queue_sort_order_by_priority(this, start, end);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
|
|
|
/* now shuffle each priority group */
|
|
|
|
unsigned group_start = start;
|
2013-01-08 15:31:39 +01:00
|
|
|
uint8_t group_priority = GetOrderPriority(start);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
|
|
|
for (unsigned i = start + 1; i < end; ++i) {
|
2013-01-08 15:31:39 +01:00
|
|
|
const uint8_t priority = GetOrderPriority(i);
|
2011-07-19 00:34:33 +02:00
|
|
|
assert(priority <= group_priority);
|
|
|
|
|
|
|
|
if (priority != group_priority) {
|
|
|
|
/* start of a new group - shuffle the one that
|
|
|
|
has just ended */
|
2013-01-07 21:17:01 +01:00
|
|
|
ShuffleOrderRange(group_start, i);
|
2011-07-19 00:34:33 +02:00
|
|
|
group_start = i;
|
|
|
|
group_priority = priority;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* shuffle the last group */
|
2013-01-07 21:17:01 +01:00
|
|
|
ShuffleOrderRange(group_start, end);
|
2011-07-19 00:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::ShuffleOrder()
|
2011-07-19 00:34:33 +02:00
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
ShuffleOrderRangeWithPriority(0, length);
|
2011-07-19 00:34:33 +02:00
|
|
|
}
|
|
|
|
|
2013-01-07 21:26:22 +01:00
|
|
|
void
|
|
|
|
queue::ShuffleOrderFirst(unsigned start, unsigned end)
|
2011-07-19 00:34:33 +02:00
|
|
|
{
|
2013-01-07 21:07:30 +01:00
|
|
|
rand.AutoCreate();
|
|
|
|
|
|
|
|
std::uniform_int_distribution<unsigned> distribution(start, end - 1);
|
|
|
|
SwapOrders(start, distribution(rand));
|
2009-01-23 00:08:40 +01:00
|
|
|
}
|
|
|
|
|
2009-01-25 14:00:51 +01:00
|
|
|
void
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::ShuffleOrderLast(unsigned start, unsigned end)
|
2009-01-25 14:00:51 +01:00
|
|
|
{
|
2013-01-07 21:07:30 +01:00
|
|
|
rand.AutoCreate();
|
|
|
|
|
|
|
|
std::uniform_int_distribution<unsigned> distribution(start, end - 1);
|
|
|
|
SwapOrders(end - 1, distribution(rand));
|
2009-01-25 14:00:51 +01:00
|
|
|
}
|
|
|
|
|
2009-01-22 23:40:11 +01:00
|
|
|
void
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::ShuffleRange(unsigned start, unsigned end)
|
2009-01-22 23:40:11 +01:00
|
|
|
{
|
|
|
|
assert(start <= end);
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(end <= length);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-07 21:07:30 +01:00
|
|
|
rand.AutoCreate();
|
|
|
|
|
2009-01-22 23:40:11 +01:00
|
|
|
for (unsigned i = start; i < end; i++) {
|
2013-01-07 21:07:30 +01:00
|
|
|
std::uniform_int_distribution<unsigned> distribution(start,
|
|
|
|
end - 1);
|
|
|
|
unsigned ri = distribution(rand);
|
2013-01-06 21:33:58 +01:00
|
|
|
SwapPositions(i, ri);
|
2009-01-22 23:40:11 +01:00
|
|
|
}
|
|
|
|
}
|
2011-07-19 00:34:33 +02:00
|
|
|
|
2013-01-08 15:31:39 +01:00
|
|
|
unsigned
|
|
|
|
queue::FindPriorityOrder(unsigned start_order, uint8_t priority,
|
|
|
|
unsigned exclude_order) const
|
2011-07-19 00:34:33 +02:00
|
|
|
{
|
2013-01-08 15:31:39 +01:00
|
|
|
assert(random);
|
|
|
|
assert(start_order <= length);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
2013-01-08 15:31:39 +01:00
|
|
|
for (unsigned i = start_order; i < length; ++i) {
|
|
|
|
const unsigned position = OrderToPosition(i);
|
2013-01-08 16:05:10 +01:00
|
|
|
const Item *item = &items[position];
|
2013-01-08 15:31:39 +01:00
|
|
|
if (item->priority <= priority && i != exclude_order)
|
|
|
|
return i;
|
2011-07-19 00:34:33 +02:00
|
|
|
}
|
|
|
|
|
2013-01-08 15:31:39 +01:00
|
|
|
return length;
|
2011-07-19 00:34:33 +02:00
|
|
|
}
|
|
|
|
|
2013-01-08 15:31:39 +01:00
|
|
|
unsigned
|
|
|
|
queue::CountSamePriority(unsigned start_order, uint8_t priority) const
|
2011-07-19 00:34:33 +02:00
|
|
|
{
|
2013-01-08 15:31:39 +01:00
|
|
|
assert(random);
|
|
|
|
assert(start_order <= length);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
2013-01-08 15:31:39 +01:00
|
|
|
for (unsigned i = start_order; i < length; ++i) {
|
|
|
|
const unsigned position = OrderToPosition(i);
|
2013-01-08 16:05:10 +01:00
|
|
|
const Item *item = &items[position];
|
2011-07-19 00:34:33 +02:00
|
|
|
if (item->priority != priority)
|
2013-01-08 15:31:39 +01:00
|
|
|
return i - start_order;
|
2011-07-19 00:34:33 +02:00
|
|
|
}
|
|
|
|
|
2013-01-08 15:31:39 +01:00
|
|
|
return length - start_order;
|
2011-07-19 00:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::SetPriority(unsigned position, uint8_t priority, int after_order)
|
2011-07-19 00:34:33 +02:00
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(position < length);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
2013-01-08 16:05:10 +01:00
|
|
|
Item *item = &items[position];
|
2011-07-19 00:34:33 +02:00
|
|
|
uint8_t old_priority = item->priority;
|
|
|
|
if (old_priority == priority)
|
|
|
|
return false;
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
item->version = version;
|
2011-07-19 00:34:33 +02:00
|
|
|
item->priority = priority;
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
if (!random)
|
2011-07-19 00:34:33 +02:00
|
|
|
/* don't reorder if not in random mode */
|
|
|
|
return true;
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
unsigned _order = PositionToOrder(position);
|
2011-07-19 00:34:33 +02:00
|
|
|
if (after_order >= 0) {
|
2013-01-06 21:33:58 +01:00
|
|
|
if (_order == (unsigned)after_order)
|
2011-07-19 00:34:33 +02:00
|
|
|
/* don't reorder the current song */
|
|
|
|
return true;
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
if (_order < (unsigned)after_order) {
|
2011-07-19 00:34:33 +02:00
|
|
|
/* the specified song has been played already
|
|
|
|
- enqueue it only if its priority has just
|
|
|
|
become bigger than the current one's */
|
|
|
|
|
|
|
|
const unsigned after_position =
|
2013-01-06 21:33:58 +01:00
|
|
|
OrderToPosition(after_order);
|
2013-01-08 16:05:10 +01:00
|
|
|
const Item *after_item =
|
2013-01-06 21:33:58 +01:00
|
|
|
&items[after_position];
|
2011-07-19 00:34:33 +02:00
|
|
|
if (old_priority > after_item->priority ||
|
|
|
|
priority <= after_item->priority)
|
|
|
|
/* priority hasn't become bigger */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* move the item to the beginning of the priority group (or
|
|
|
|
create a new priority group) */
|
|
|
|
|
|
|
|
const unsigned before_order =
|
2013-01-08 15:31:39 +01:00
|
|
|
FindPriorityOrder(after_order + 1, priority, _order);
|
2013-01-06 21:33:58 +01:00
|
|
|
const unsigned new_order = before_order > _order
|
2011-07-19 00:34:33 +02:00
|
|
|
? before_order - 1
|
|
|
|
: before_order;
|
2013-01-08 15:31:39 +01:00
|
|
|
MoveOrder(_order, new_order);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
|
|
|
/* shuffle the song within that priority group */
|
|
|
|
|
2013-01-08 15:31:39 +01:00
|
|
|
const unsigned priority_count = CountSamePriority(new_order, priority);
|
2011-07-19 00:34:33 +02:00
|
|
|
assert(priority_count >= 1);
|
2013-01-07 21:26:22 +01:00
|
|
|
ShuffleOrderFirst(new_order, new_order + priority_count);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::SetPriorityRange(unsigned start_position, unsigned end_position,
|
|
|
|
uint8_t priority, int after_order)
|
2011-07-19 00:34:33 +02:00
|
|
|
{
|
|
|
|
assert(start_position <= end_position);
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(end_position <= length);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
|
|
|
bool modified = false;
|
|
|
|
int after_position = after_order >= 0
|
2013-01-06 21:33:58 +01:00
|
|
|
? (int)OrderToPosition(after_order)
|
2011-07-19 00:34:33 +02:00
|
|
|
: -1;
|
|
|
|
for (unsigned i = start_position; i < end_position; ++i) {
|
|
|
|
after_order = after_position >= 0
|
2013-01-06 21:33:58 +01:00
|
|
|
? (int)PositionToOrder(after_position)
|
2011-07-19 00:34:33 +02:00
|
|
|
: -1;
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
modified |= SetPriority(i, priority, after_order);
|
2011-07-19 00:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return modified;
|
|
|
|
}
|