Queue.cxx 11.9 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "Queue.hxx"
21
#include "song/DetachedSong.hxx"
22

Max Kellermann's avatar
Max Kellermann committed
23
Queue::Queue(unsigned _max_length) noexcept
24
	:max_length(_max_length),
25 26
	 items(new Item[max_length]),
	 order(new unsigned[max_length]),
27
	 id_table(max_length * HASH_MULT)
28 29 30
{
}

Max Kellermann's avatar
Max Kellermann committed
31
Queue::~Queue() noexcept
32 33 34
{
	Clear();

35 36
	delete[] items;
	delete[] order;
37 38 39
}

int
40
Queue::GetNextOrder(unsigned _order) const noexcept
41
{
42
	assert(_order < length);
43

44
	if (single != SingleMode::OFF && repeat && !consume)
45 46 47 48
		return _order;
	else if (_order + 1 < length)
		return _order + 1;
	else if (repeat && (_order > 0 || !consume))
49 50 51 52 53 54 55 56
		/* restart at first song */
		return 0;
	else
		/* end of queue */
		return -1;
}

void
57
Queue::IncrementVersion() noexcept
58 59 60
{
	static unsigned long max = ((uint32_t) 1 << 31) - 1;

61
	version++;
62

63 64 65
	if (version >= max) {
		for (unsigned i = 0; i < length; i++)
			items[i].version = 0;
66

67
		version = 1;
68 69 70 71
	}
}

void
72
Queue::ModifyAtOrder(unsigned _order) noexcept
73
{
74
	assert(_order < length);
75

76
	unsigned position = order[_order];
77
	ModifyAtPosition(position);
78 79 80
}

unsigned
Max Kellermann's avatar
Max Kellermann committed
81
Queue::Append(DetachedSong &&song, uint8_t priority) noexcept
82
{
83
	assert(!IsFull());
84

85 86 87 88
	const unsigned position = length++;
	const unsigned id = id_table.Insert(position);

	auto &item = items[position];
89
	item.song = new DetachedSong(std::move(song));
90
	item.id = id;
91
	item.version = version;
92
	item.priority = priority;
93

94
	order[position] = position;
95 96 97 98 99

	return id;
}

void
100
Queue::SwapPositions(unsigned position1, unsigned position2) noexcept
101
{
102 103
	unsigned id1 = items[position1].id;
	unsigned id2 = items[position2].id;
104

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

107 108
	items[position1].version = version;
	items[position2].version = version;
109

110 111
	id_table.Move(id1, position2);
	id_table.Move(id2, position1);
112 113 114
}

void
115
Queue::MovePostion(unsigned from, unsigned to) noexcept
116
{
117
	const Item tmp = items[from];
118 119 120 121

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

	for (unsigned i = from; i < to; i++)
122
		MoveItemTo(i + 1, i);
123 124 125 126

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

	for (unsigned i = from; i > to; i--)
127
		MoveItemTo(i - 1, i);
128 129 130

	/* put song at _to_ */

131
	id_table.Move(tmp.id, to);
132
	items[to] = tmp;
133
	items[to].version = version;
134 135 136

	/* now deal with order */

137 138 139 140 141 142 143 144 145
	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;
146 147
		}
	}
148 149
}

150
void
151
Queue::MoveRange(unsigned start, unsigned end, unsigned to) noexcept
152
{
153
	Item tmp[end - start];
154 155
	// Copy the original block [start,end-1]
	for (unsigned i = start; i < end; i++)
156
		tmp[i - start] = items[i];
157 158 159

	// If to > start, we need to move to-start items to start, starting from end
	for (unsigned i = end; i < end + to - start; i++)
160
		MoveItemTo(i, start + i - end);
161 162 163 164

	// 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
165
	for (int i = start - 1; i >= int(to); i--)
166
		MoveItemTo(i, i + end - start);
167 168 169 170

	// Copy the original block back in, starting at to.
	for (unsigned i = start; i< end; i++)
	{
171
		id_table.Move(tmp[i - start].id, to + i - start);
172 173
		items[to + i - start] = tmp[i-start];
		items[to + i - start].version = version;
174 175
	}

176
	if (random) {
177 178 179
		// Update the positions in the queue.
		// Note that the ranges for these cases are the same as the ranges of
		// the loops above.
180 181 182 183 184 185 186 187
		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;
188 189 190 191
		}
	}
}

192
unsigned
193
Queue::MoveOrder(unsigned from_order, unsigned to_order) noexcept
194
{
195 196
	assert(from_order < length);
	assert(to_order <= length);
197

198
	const unsigned from_position = OrderToPosition(from_order);
199 200 201

	if (from_order < to_order) {
		for (unsigned i = from_order; i < to_order; ++i)
202
			order[i] = order[i + 1];
203 204
	} else {
		for (unsigned i = from_order; i > to_order; --i)
205
			order[i] = order[i - 1];
206 207
	}

208
	order[to_order] = from_position;
209
	return to_order;
210 211
}

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
unsigned
Queue::MoveOrderBefore(unsigned from_order, unsigned to_order) noexcept
{
	/* if "from_order" comes before "to_order", then the new
	   position is "to_order-1"; otherwise the "to_order" song is
	   moved one ahead */
	return MoveOrder(from_order, to_order - (from_order < to_order));
}

unsigned
Queue::MoveOrderAfter(unsigned from_order, unsigned to_order) noexcept
{
	/* if "from_order" comes after "to_order", then the new
	   position is "to_order+1"; otherwise the "to_order" song is
	   moved one back */
	return MoveOrder(from_order, to_order + (from_order > to_order));
}

230
void
231
Queue::DeletePosition(unsigned position) noexcept
232
{
233
	assert(position < length);
234

235
	delete items[position].song;
236

237 238
	const unsigned id = PositionToId(position);
	const unsigned _order = PositionToOrder(position);
239

240
	--length;
241 242 243

	/* release the song id */

244
	id_table.Erase(id);
245 246 247

	/* delete song from songs array */

248
	for (unsigned i = position; i < length; i++)
249
		MoveItemTo(i + 1, i);
250 251 252

	/* delete the entry from the order array */

253 254
	for (unsigned i = _order; i < length; i++)
		order[i] = order[i + 1];
255 256 257

	/* readjust values in the order array */

258 259 260
	for (unsigned i = 0; i < length; i++)
		if (order[i] > position)
			--order[i];
261 262 263
}

void
264
Queue::Clear() noexcept
265
{
266
	for (unsigned i = 0; i < length; i++) {
267
		Item *item = &items[i];
268

269
		delete item->song;
270

271
		id_table.Erase(item->id);
272 273
	}

274
	length = 0;
275 276
}

277
static void
278 279
queue_sort_order_by_priority(Queue *queue,
			     unsigned start, unsigned end) noexcept
280
{
281
	assert(queue != nullptr);
282
	assert(queue->random);
283 284
	assert(start <= end);
	assert(end <= queue->length);
285

286
	auto cmp = [queue](unsigned a_pos, unsigned b_pos){
287 288
		const Queue::Item &a = queue->items[a_pos];
		const Queue::Item &b = queue->items[b_pos];
289 290 291 292 293

		return a.priority > b.priority;
	};

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

296
void
Max Kellermann's avatar
Max Kellermann committed
297
Queue::ShuffleOrderRange(unsigned start, unsigned end) noexcept
298
{
299
	assert(random);
300
	assert(start <= end);
301
	assert(end <= length);
302

303 304
	rand.AutoCreate();
	std::shuffle(order + start, order + end, rand);
305 306 307 308 309 310 311
}

/**
 * Sort the "order" of items by priority, and then shuffle each
 * priority group.
 */
void
Max Kellermann's avatar
Max Kellermann committed
312
Queue::ShuffleOrderRangeWithPriority(unsigned start, unsigned end) noexcept
313
{
314
	assert(random);
315
	assert(start <= end);
316
	assert(end <= length);
317 318 319 320 321

	if (start == end)
		return;

	/* first group the range by priority */
322
	queue_sort_order_by_priority(this, start, end);
323 324 325

	/* now shuffle each priority group */
	unsigned group_start = start;
326
	uint8_t group_priority = GetOrderPriority(start);
327 328

	for (unsigned i = start + 1; i < end; ++i) {
329
		const uint8_t priority = GetOrderPriority(i);
330 331 332 333 334
		assert(priority <= group_priority);

		if (priority != group_priority) {
			/* start of a new group - shuffle the one that
			   has just ended */
335
			ShuffleOrderRange(group_start, i);
336 337 338 339 340 341
			group_start = i;
			group_priority = priority;
		}
	}

	/* shuffle the last group */
342
	ShuffleOrderRange(group_start, end);
343 344 345
}

void
Max Kellermann's avatar
Max Kellermann committed
346
Queue::ShuffleOrder() noexcept
347
{
348
	ShuffleOrderRangeWithPriority(0, length);
349 350
}

351
void
Max Kellermann's avatar
Max Kellermann committed
352
Queue::ShuffleOrderFirst(unsigned start, unsigned end) noexcept
353
{
354 355 356 357
	rand.AutoCreate();

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

360
void
361
Queue::ShuffleOrderLastWithPriority(unsigned start, unsigned end) noexcept
362
{
363 364 365 366 367 368 369 370 371 372 373 374
	assert(end <= length);
	assert(start < end);

	/* skip all items at the start which have a higher priority,
	   because the last item shall only be shuffled within its
	   priority group */
	const auto last_priority = items[OrderToPosition(end - 1)].priority;
	while (items[OrderToPosition(start)].priority != last_priority) {
		++start;
		assert(start < end);
	}

375 376 377 378
	rand.AutoCreate();

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

381
void
Max Kellermann's avatar
Max Kellermann committed
382
Queue::ShuffleRange(unsigned start, unsigned end) noexcept
383 384
{
	assert(start <= end);
385
	assert(end <= length);
386

387 388
	rand.AutoCreate();

389
	for (unsigned i = start; i < end; i++) {
390 391 392
		std::uniform_int_distribution<unsigned> distribution(start,
								     end - 1);
		unsigned ri = distribution(rand);
393
		SwapPositions(i, ri);
394 395
	}
}
396

397
unsigned
398
Queue::FindPriorityOrder(unsigned start_order, uint8_t priority,
399
			 unsigned exclude_order) const noexcept
400
{
401 402
	assert(random);
	assert(start_order <= length);
403

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

411
	return length;
412 413
}

414
unsigned
415
Queue::CountSamePriority(unsigned start_order, uint8_t priority) const noexcept
416
{
417 418
	assert(random);
	assert(start_order <= length);
419

420 421
	for (unsigned i = start_order; i < length; ++i) {
		const unsigned position = OrderToPosition(i);
422
		const Item *item = &items[position];
423
		if (item->priority != priority)
424
			return i - start_order;
425 426
	}

427
	return length - start_order;
428 429 430
}

bool
431
Queue::SetPriority(unsigned position, uint8_t priority, int after_order,
Max Kellermann's avatar
Max Kellermann committed
432
		   bool reorder) noexcept
433
{
434
	assert(position < length);
435

436
	Item *item = &items[position];
437 438 439 440
	uint8_t old_priority = item->priority;
	if (old_priority == priority)
		return false;

441
	item->version = version;
442 443
	item->priority = priority;

444
	if (!random || !reorder)
445 446 447
		/* don't reorder if not in random mode */
		return true;

448
	unsigned _order = PositionToOrder(position);
449
	if (after_order >= 0) {
450
		if (_order == (unsigned)after_order)
451 452 453
			/* don't reorder the current song */
			return true;

454
		if (_order < (unsigned)after_order) {
455
			/* the specified song has been played already
456 457 458
			   - enqueue it only if its priority has been
			   increased and is now bigger than the
			   current one's */
459 460

			const unsigned after_position =
461
				OrderToPosition(after_order);
462
			const Item *after_item =
463
				&items[after_position];
464
			if (priority <= old_priority ||
465 466 467 468 469 470 471 472 473 474
			    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 =
475
		FindPriorityOrder(after_order + 1, priority, _order);
476
	const unsigned new_order = before_order > _order
477 478
		? before_order - 1
		: before_order;
479
	MoveOrder(_order, new_order);
480 481 482

	/* shuffle the song within that priority group */

483
	const unsigned priority_count = CountSamePriority(new_order, priority);
484
	assert(priority_count >= 1);
485
	ShuffleOrderFirst(new_order, new_order + priority_count);
486 487 488 489 490

	return true;
}

bool
491
Queue::SetPriorityRange(unsigned start_position, unsigned end_position,
Max Kellermann's avatar
Max Kellermann committed
492
			uint8_t priority, int after_order) noexcept
493 494
{
	assert(start_position <= end_position);
495
	assert(end_position <= length);
496 497 498

	bool modified = false;
	int after_position = after_order >= 0
499
		? (int)OrderToPosition(after_order)
500 501 502
		: -1;
	for (unsigned i = start_position; i < end_position; ++i) {
		after_order = after_position >= 0
503
			? (int)PositionToOrder(after_position)
504 505
			: -1;

506
		modified |= SetPriority(i, priority, after_order);
507 508 509 510
	}

	return modified;
}