Queue.cxx 11.9 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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 154
	const auto tmp = std::make_unique<Item[]>(end - start);

155 156
	// Copy the original block [start,end-1]
	for (unsigned i = start; i < end; i++)
157
		tmp[i - start] = items[i];
158 159 160

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

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

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

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

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

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

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

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

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
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));
}

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

236
	delete items[position].song;
237

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

241
	--length;
242 243 244

	/* release the song id */

245
	id_table.Erase(id);
246 247 248

	/* delete song from songs array */

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

	/* delete the entry from the order array */

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

	/* readjust values in the order array */

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

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

270
		delete item->song;
271

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

275
	length = 0;
276 277
}

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

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

		return a.priority > b.priority;
	};

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

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

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

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

	if (start == end)
		return;

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

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

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

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

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

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

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

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

361
void
362
Queue::ShuffleOrderLastWithPriority(unsigned start, unsigned end) noexcept
363
{
364 365 366 367 368 369 370 371 372 373 374 375
	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);
	}

376 377 378 379
	rand.AutoCreate();

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

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

388 389
	rand.AutoCreate();

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

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

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

412
	return length;
413 414
}

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

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

428
	return length - start_order;
429 430 431
}

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

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

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

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

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

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

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

	/* shuffle the song within that priority group */

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

	return true;
}

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

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

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

	return modified;
}