Queue.cxx 10.9 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "config.h"
21
#include "Queue.hxx"
22
#include "Song.hxx"
23

24 25
#include <stdlib.h>

26 27 28
queue::queue(unsigned _max_length)
	:max_length(_max_length), length(0),
	 version(1),
29 30
	 items(new Item[max_length]),
	 order(new unsigned[max_length]),
31
	 id_table(max_length * HASH_MULT),
32 33 34
	 repeat(false),
	 single(false),
	 consume(false),
35
	 random(false)
36 37 38 39 40 41 42
{
}

queue::~queue()
{
	Clear();

43 44
	delete[] items;
	delete[] order;
45 46 47
}

int
48
queue::GetNextOrder(unsigned _order) const
49
{
50
	assert(_order < length);
51

52 53 54 55 56
	if (single && repeat && !consume)
		return _order;
	else if (_order + 1 < length)
		return _order + 1;
	else if (repeat && (_order > 0 || !consume))
57 58 59 60 61 62 63 64
		/* restart at first song */
		return 0;
	else
		/* end of queue */
		return -1;
}

void
65
queue::IncrementVersion()
66 67 68
{
	static unsigned long max = ((uint32_t) 1 << 31) - 1;

69
	version++;
70

71 72 73
	if (version >= max) {
		for (unsigned i = 0; i < length; i++)
			items[i].version = 0;
74

75
		version = 1;
76 77 78 79
	}
}

void
80
queue::ModifyAtOrder(unsigned _order)
81
{
82
	assert(_order < length);
83

84
	unsigned position = order[_order];
85
	ModifyAtPosition(position);
86 87 88
}

unsigned
89
queue::Append(Song *song, uint8_t priority)
90
{
91
	assert(!IsFull());
92

93 94 95 96
	const unsigned position = length++;
	const unsigned id = id_table.Insert(position);

	auto &item = items[position];
97
	item.song = song->DupDetached();
98
	item.id = id;
99
	item.version = version;
100
	item.priority = priority;
101

102
	order[position] = position;
103 104 105 106 107

	return id;
}

void
108
queue::SwapPositions(unsigned position1, unsigned position2)
109
{
110 111
	unsigned id1 = items[position1].id;
	unsigned id2 = items[position2].id;
112

Max Kellermann's avatar
Max Kellermann committed
113
	std::swap(items[position1], items[position2]);
114

115 116
	items[position1].version = version;
	items[position2].version = version;
117

118 119
	id_table.Move(id1, position2);
	id_table.Move(id2, position1);
120 121 122
}

void
123
queue::MovePostion(unsigned from, unsigned to)
124
{
125
	const Item tmp = items[from];
126 127 128 129

	/* move songs to one less in from->to */

	for (unsigned i = from; i < to; i++)
130
		MoveItemTo(i + 1, i);
131 132 133 134

	/* move songs to one more in to->from */

	for (unsigned i = from; i > to; i--)
135
		MoveItemTo(i - 1, i);
136 137 138

	/* put song at _to_ */

139
	id_table.Move(tmp.id, to);
140
	items[to] = tmp;
141
	items[to].version = version;
142 143 144

	/* now deal with order */

145 146 147 148 149 150 151 152 153
	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;
154 155
		}
	}
156 157
}

158
void
159
queue::MoveRange(unsigned start, unsigned end, unsigned to)
160
{
161
	Item tmp[end - start];
162 163
	// Copy the original block [start,end-1]
	for (unsigned i = start; i < end; i++)
164
		tmp[i - start] = items[i];
165 166 167

	// If to > start, we need to move to-start items to start, starting from end
	for (unsigned i = end; i < end + to - start; i++)
168
		MoveItemTo(i, start + i - end);
169 170 171 172

	// 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
173
	for (int i = start - 1; i >= int(to); i--)
174
		MoveItemTo(i, i + end - start);
175 176 177 178

	// Copy the original block back in, starting at to.
	for (unsigned i = start; i< end; i++)
	{
179
		id_table.Move(tmp[i - start].id, to + i - start);
180 181
		items[to + i - start] = tmp[i-start];
		items[to + i - start].version = version;
182 183
	}

184
	if (random) {
185 186 187
		// Update the positions in the queue.
		// Note that the ranges for these cases are the same as the ranges of
		// the loops above.
188 189 190 191 192 193 194 195
		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;
196 197 198 199
		}
	}
}

200 201
void
queue::MoveOrder(unsigned from_order, unsigned to_order)
202
{
203 204
	assert(from_order < length);
	assert(to_order <= length);
205

206
	const unsigned from_position = OrderToPosition(from_order);
207 208 209

	if (from_order < to_order) {
		for (unsigned i = from_order; i < to_order; ++i)
210
			order[i] = order[i + 1];
211 212
	} else {
		for (unsigned i = from_order; i > to_order; --i)
213
			order[i] = order[i - 1];
214 215
	}

216
	order[to_order] = from_position;
217 218
}

219
void
220
queue::DeletePosition(unsigned position)
221
{
222
	assert(position < length);
223

224 225 226 227 228
	{
		Song &song = Get(position);
		assert(!song.IsInDatabase() || song.IsDetached());
		song.Free();
	}
229

230 231
	const unsigned id = PositionToId(position);
	const unsigned _order = PositionToOrder(position);
232

233
	--length;
234 235 236

	/* release the song id */

237
	id_table.Erase(id);
238 239 240

	/* delete song from songs array */

241
	for (unsigned i = position; i < length; i++)
242
		MoveItemTo(i + 1, i);
243 244 245

	/* delete the entry from the order array */

246 247
	for (unsigned i = _order; i < length; i++)
		order[i] = order[i + 1];
248 249 250

	/* readjust values in the order array */

251 252 253
	for (unsigned i = 0; i < length; i++)
		if (order[i] > position)
			--order[i];
254 255 256
}

void
257
queue::Clear()
258
{
259
	for (unsigned i = 0; i < length; i++) {
260
		Item *item = &items[i];
261

262 263 264
		assert(!item->song->IsInDatabase() ||
		       item->song->IsDetached());
		item->song->Free();
265

266
		id_table.Erase(item->id);
267 268
	}

269
	length = 0;
270 271
}

272 273
static void
queue_sort_order_by_priority(struct queue *queue, unsigned start, unsigned end)
274
{
275
	assert(queue != nullptr);
276
	assert(queue->random);
277 278
	assert(start <= end);
	assert(end <= queue->length);
279

280 281 282 283 284 285 286 287
	auto cmp = [queue](unsigned a_pos, unsigned b_pos){
		const queue::Item &a = queue->items[a_pos];
		const queue::Item &b = queue->items[b_pos];

		return a.priority > b.priority;
	};

	std::stable_sort(queue->order + start, queue->order + end, cmp);
288 289
}

290 291
void
queue::ShuffleOrderRange(unsigned start, unsigned end)
292
{
293
	assert(random);
294
	assert(start <= end);
295
	assert(end <= length);
296

297 298
	rand.AutoCreate();
	std::shuffle(order + start, order + end, rand);
299 300 301 302 303 304 305
}

/**
 * Sort the "order" of items by priority, and then shuffle each
 * priority group.
 */
void
306
queue::ShuffleOrderRangeWithPriority(unsigned start, unsigned end)
307
{
308
	assert(random);
309
	assert(start <= end);
310
	assert(end <= length);
311 312 313 314 315

	if (start == end)
		return;

	/* first group the range by priority */
316
	queue_sort_order_by_priority(this, start, end);
317 318 319

	/* now shuffle each priority group */
	unsigned group_start = start;
320
	uint8_t group_priority = GetOrderPriority(start);
321 322

	for (unsigned i = start + 1; i < end; ++i) {
323
		const uint8_t priority = GetOrderPriority(i);
324 325 326 327 328
		assert(priority <= group_priority);

		if (priority != group_priority) {
			/* start of a new group - shuffle the one that
			   has just ended */
329
			ShuffleOrderRange(group_start, i);
330 331 332 333 334 335
			group_start = i;
			group_priority = priority;
		}
	}

	/* shuffle the last group */
336
	ShuffleOrderRange(group_start, end);
337 338 339
}

void
340
queue::ShuffleOrder()
341
{
342
	ShuffleOrderRangeWithPriority(0, length);
343 344
}

345 346
void
queue::ShuffleOrderFirst(unsigned start, unsigned end)
347
{
348 349 350 351
	rand.AutoCreate();

	std::uniform_int_distribution<unsigned> distribution(start, end - 1);
	SwapOrders(start, distribution(rand));
352 353
}

354
void
355
queue::ShuffleOrderLast(unsigned start, unsigned end)
356
{
357 358 359 360
	rand.AutoCreate();

	std::uniform_int_distribution<unsigned> distribution(start, end - 1);
	SwapOrders(end - 1, distribution(rand));
361 362
}

363
void
364
queue::ShuffleRange(unsigned start, unsigned end)
365 366
{
	assert(start <= end);
367
	assert(end <= length);
368

369 370
	rand.AutoCreate();

371
	for (unsigned i = start; i < end; i++) {
372 373 374
		std::uniform_int_distribution<unsigned> distribution(start,
								     end - 1);
		unsigned ri = distribution(rand);
375
		SwapPositions(i, ri);
376 377
	}
}
378

379 380 381
unsigned
queue::FindPriorityOrder(unsigned start_order, uint8_t priority,
			 unsigned exclude_order) const
382
{
383 384
	assert(random);
	assert(start_order <= length);
385

386 387
	for (unsigned i = start_order; i < length; ++i) {
		const unsigned position = OrderToPosition(i);
388
		const Item *item = &items[position];
389 390
		if (item->priority <= priority && i != exclude_order)
			return i;
391 392
	}

393
	return length;
394 395
}

396 397
unsigned
queue::CountSamePriority(unsigned start_order, uint8_t priority) const
398
{
399 400
	assert(random);
	assert(start_order <= length);
401

402 403
	for (unsigned i = start_order; i < length; ++i) {
		const unsigned position = OrderToPosition(i);
404
		const Item *item = &items[position];
405
		if (item->priority != priority)
406
			return i - start_order;
407 408
	}

409
	return length - start_order;
410 411 412
}

bool
413
queue::SetPriority(unsigned position, uint8_t priority, int after_order)
414
{
415
	assert(position < length);
416

417
	Item *item = &items[position];
418 419 420 421
	uint8_t old_priority = item->priority;
	if (old_priority == priority)
		return false;

422
	item->version = version;
423 424
	item->priority = priority;

425
	if (!random)
426 427 428
		/* don't reorder if not in random mode */
		return true;

429
	unsigned _order = PositionToOrder(position);
430
	if (after_order >= 0) {
431
		if (_order == (unsigned)after_order)
432 433 434
			/* don't reorder the current song */
			return true;

435
		if (_order < (unsigned)after_order) {
436 437 438 439 440
			/* 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 =
441
				OrderToPosition(after_order);
442
			const Item *after_item =
443
				&items[after_position];
444 445 446 447 448 449 450 451 452 453 454
			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 =
455
		FindPriorityOrder(after_order + 1, priority, _order);
456
	const unsigned new_order = before_order > _order
457 458
		? before_order - 1
		: before_order;
459
	MoveOrder(_order, new_order);
460 461 462

	/* shuffle the song within that priority group */

463
	const unsigned priority_count = CountSamePriority(new_order, priority);
464
	assert(priority_count >= 1);
465
	ShuffleOrderFirst(new_order, new_order + priority_count);
466 467 468 469 470

	return true;
}

bool
471 472
queue::SetPriorityRange(unsigned start_position, unsigned end_position,
			uint8_t priority, int after_order)
473 474
{
	assert(start_position <= end_position);
475
	assert(end_position <= length);
476 477 478

	bool modified = false;
	int after_position = after_order >= 0
479
		? (int)OrderToPosition(after_order)
480 481 482
		: -1;
	for (unsigned i = start_position; i < end_position; ++i) {
		after_order = after_position >= 0
483
			? (int)PositionToOrder(after_position)
484 485
			: -1;

486
		modified |= SetPriority(i, priority, after_order);
487 488 489 490
	}

	return modified;
}