mpd/src/Queue.cxx

540 lines
12 KiB
C++
Raw Normal View History

/*
2013-01-04 20:50:00 +01:00
* Copyright (C) 2003-2013 The Music Player Daemon Project
* 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.
*
* 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.
*/
#include "config.h"
2013-01-04 20:50:00 +01:00
#include "Queue.hxx"
#include "song.h"
#include <stdlib.h>
queue::queue(unsigned _max_length)
:max_length(_max_length), length(0),
version(1),
2013-01-08 16:05:10 +01:00
items(g_new(Item, max_length)),
order((unsigned *)g_malloc(sizeof(order[0]) * max_length)),
id_to_position((int *)g_malloc(sizeof(id_to_position[0]) *
2013-01-08 16:05:10 +01:00
max_length * HASH_MULT)),
repeat(false),
single(false),
consume(false),
random(false)
{
2013-01-08 16:05:10 +01:00
for (unsigned i = 0; i < max_length * HASH_MULT; ++i)
id_to_position[i] = -1;
}
queue::~queue()
{
Clear();
g_free(items);
g_free(order);
g_free(id_to_position);
}
/**
* Generate a non-existing id number.
*/
unsigned
queue::GenerateId() const
{
static unsigned cur = (unsigned)-1;
do {
cur++;
2013-01-08 16:05:10 +01:00
if (cur >= max_length * HASH_MULT)
cur = 0;
} while (id_to_position[cur] != -1);
return cur;
}
int
queue::GetNextOrder(unsigned _order) const
{
assert(_order < length);
if (single && repeat && !consume)
return _order;
else if (_order + 1 < length)
return _order + 1;
else if (repeat && (_order > 0 || !consume))
/* restart at first song */
return 0;
else
/* end of queue */
return -1;
}
void
queue::IncrementVersion()
{
static unsigned long max = ((uint32_t) 1 << 31) - 1;
version++;
if (version >= max) {
for (unsigned i = 0; i < length; i++)
items[i].version = 0;
version = 1;
}
}
void
queue::ModifyAtOrder(unsigned _order)
{
assert(_order < length);
unsigned position = order[_order];
items[position].version = version;
IncrementVersion();
}
void
queue::ModifyAll()
{
for (unsigned i = 0; i < length; i++)
items[i].version = version;
IncrementVersion();
}
unsigned
queue::Append(struct song *song, uint8_t priority)
{
const unsigned id = GenerateId();
assert(!IsFull());
auto &item = items[length];
2013-01-04 20:50:00 +01:00
item.song = song_dup_detached(song);
item.id = id;
item.version = version;
2013-01-04 20:50:00 +01:00
item.priority = priority;
order[length] = length;
id_to_position[id] = length;
++length;
return id;
}
void
queue::SwapPositions(unsigned position1, unsigned position2)
{
unsigned id1 = items[position1].id;
unsigned id2 = items[position2].id;
2013-01-06 23:44:59 +01:00
std::swap(items[position1], items[position2]);
items[position1].version = version;
items[position2].version = version;
id_to_position[id1] = position2;
id_to_position[id2] = position1;
}
void
queue::MovePostion(unsigned from, unsigned to)
{
2013-01-08 16:05:10 +01:00
const Item tmp = items[from];
/* move songs to one less in from->to */
for (unsigned i = from; i < to; i++)
MoveItemTo(i + 1, i);
/* move songs to one more in to->from */
for (unsigned i = from; i > to; i--)
MoveItemTo(i - 1, i);
/* put song at _to_ */
2013-01-08 16:05:10 +01:00
id_to_position[tmp.id] = to;
items[to] = tmp;
items[to].version = version;
/* now deal with order */
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;
}
}
}
void
queue::MoveRange(unsigned start, unsigned end, unsigned to)
{
2013-01-08 16:05:10 +01:00
Item tmp[end - start];
// Copy the original block [start,end-1]
for (unsigned i = start; i < end; i++)
tmp[i - start] = items[i];
// If to > start, we need to move to-start items to start, starting from end
for (unsigned i = end; i < end + to - start; i++)
MoveItemTo(i, start + i - end);
// 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
for (unsigned i = start - 1; i >= to && i != G_MAXUINT; i--)
MoveItemTo(i, i + end - start);
// Copy the original block back in, starting at to.
for (unsigned i = start; i< end; i++)
{
id_to_position[tmp[i-start].id] = to + i - start;
items[to + i - start] = tmp[i-start];
items[to + i - start].version = version;
}
if (random) {
// Update the positions in the queue.
// Note that the ranges for these cases are the same as the ranges of
// the loops above.
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;
}
}
}
void
queue::MoveOrder(unsigned from_order, unsigned to_order)
{
assert(from_order < length);
assert(to_order <= length);
const unsigned from_position = OrderToPosition(from_order);
if (from_order < to_order) {
for (unsigned i = from_order; i < to_order; ++i)
order[i] = order[i + 1];
} else {
for (unsigned i = from_order; i > to_order; --i)
order[i] = order[i - 1];
}
order[to_order] = from_position;
}
void
queue::DeletePosition(unsigned position)
{
assert(position < length);
struct song *song = Get(position);
assert(!song_in_database(song) || song_is_detached(song));
song_free(song);
const unsigned id = PositionToId(position);
const unsigned _order = PositionToOrder(position);
--length;
/* release the song id */
id_to_position[id] = -1;
/* delete song from songs array */
for (unsigned i = position; i < length; i++)
MoveItemTo(i + 1, i);
/* delete the entry from the order array */
for (unsigned i = _order; i < length; i++)
order[i] = order[i + 1];
/* readjust values in the order array */
for (unsigned i = 0; i < length; i++)
if (order[i] > position)
--order[i];
}
void
queue::Clear()
{
for (unsigned i = 0; i < length; i++) {
2013-01-08 16:05:10 +01:00
Item *item = &items[i];
assert(!song_in_database(item->song) ||
song_is_detached(item->song));
song_free(item->song);
id_to_position[item->id] = -1;
}
length = 0;
}
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;
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;
if (G_LIKELY(a == b))
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)
{
assert(queue != NULL);
assert(queue->random);
assert(start <= end);
assert(end <= queue->length);
g_qsort_with_data(&queue->order[start], end - start,
sizeof(queue->order[0]),
queue_item_compare_order_priority,
queue);
}
void
queue::ShuffleOrderRange(unsigned start, unsigned end)
{
assert(random);
assert(start <= end);
assert(end <= length);
rand.AutoCreate();
std::shuffle(order + start, order + end, rand);
}
/**
* Sort the "order" of items by priority, and then shuffle each
* priority group.
*/
void
queue::ShuffleOrderRangeWithPriority(unsigned start, unsigned end)
{
assert(random);
assert(start <= end);
assert(end <= length);
if (start == end)
return;
/* first group the range by priority */
queue_sort_order_by_priority(this, start, end);
/* now shuffle each priority group */
unsigned group_start = start;
uint8_t group_priority = GetOrderPriority(start);
for (unsigned i = start + 1; i < end; ++i) {
const uint8_t priority = GetOrderPriority(i);
assert(priority <= group_priority);
if (priority != group_priority) {
/* start of a new group - shuffle the one that
has just ended */
ShuffleOrderRange(group_start, i);
group_start = i;
group_priority = priority;
}
}
/* shuffle the last group */
ShuffleOrderRange(group_start, end);
}
void
queue::ShuffleOrder()
{
ShuffleOrderRangeWithPriority(0, length);
}
void
queue::ShuffleOrderFirst(unsigned start, unsigned end)
{
rand.AutoCreate();
std::uniform_int_distribution<unsigned> distribution(start, end - 1);
SwapOrders(start, distribution(rand));
}
void
queue::ShuffleOrderLast(unsigned start, unsigned end)
{
rand.AutoCreate();
std::uniform_int_distribution<unsigned> distribution(start, end - 1);
SwapOrders(end - 1, distribution(rand));
}
void
queue::ShuffleRange(unsigned start, unsigned end)
{
assert(start <= end);
assert(end <= length);
rand.AutoCreate();
for (unsigned i = start; i < end; i++) {
std::uniform_int_distribution<unsigned> distribution(start,
end - 1);
unsigned ri = distribution(rand);
SwapPositions(i, ri);
}
}
unsigned
queue::FindPriorityOrder(unsigned start_order, uint8_t priority,
unsigned exclude_order) const
{
assert(random);
assert(start_order <= length);
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];
if (item->priority <= priority && i != exclude_order)
return i;
}
return length;
}
unsigned
queue::CountSamePriority(unsigned start_order, uint8_t priority) const
{
assert(random);
assert(start_order <= length);
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];
if (item->priority != priority)
return i - start_order;
}
return length - start_order;
}
bool
queue::SetPriority(unsigned position, uint8_t priority, int after_order)
{
assert(position < length);
2013-01-08 16:05:10 +01:00
Item *item = &items[position];
uint8_t old_priority = item->priority;
if (old_priority == priority)
return false;
item->version = version;
item->priority = priority;
if (!random)
/* don't reorder if not in random mode */
return true;
unsigned _order = PositionToOrder(position);
if (after_order >= 0) {
if (_order == (unsigned)after_order)
/* don't reorder the current song */
return true;
if (_order < (unsigned)after_order) {
/* 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 =
OrderToPosition(after_order);
2013-01-08 16:05:10 +01:00
const Item *after_item =
&items[after_position];
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 =
FindPriorityOrder(after_order + 1, priority, _order);
const unsigned new_order = before_order > _order
? before_order - 1
: before_order;
MoveOrder(_order, new_order);
/* shuffle the song within that priority group */
const unsigned priority_count = CountSamePriority(new_order, priority);
assert(priority_count >= 1);
ShuffleOrderFirst(new_order, new_order + priority_count);
return true;
}
bool
queue::SetPriorityRange(unsigned start_position, unsigned end_position,
uint8_t priority, int after_order)
{
assert(start_position <= end_position);
assert(end_position <= length);
bool modified = false;
int after_position = after_order >= 0
? (int)OrderToPosition(after_order)
: -1;
for (unsigned i = start_position; i < end_position; ++i) {
after_order = after_position >= 0
? (int)PositionToOrder(after_position)
: -1;
modified |= SetPriority(i, priority, after_order);
}
return modified;
}