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

23 24
#include <algorithm>

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

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

37 38
	delete[] items;
	delete[] order;
39 40 41
}

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

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

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

63
	version++;
64

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

69
		version = 1;
70 71 72 73
	}
}

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

78
	unsigned position = order[_order];
79
	ModifyAtPosition(position);
80 81 82
}

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

87 88 89 90
	const unsigned position = length++;
	const unsigned id = id_table.Insert(position);

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

96
	order[position] = position;
97 98 99 100 101

	return id;
}

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

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

109 110
	items[position1].version = version;
	items[position2].version = version;
111

112 113
	id_table.Move(id1, position2);
	id_table.Move(id2, position1);
114 115 116
}

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

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

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

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

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

	/* put song at _to_ */

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

	/* now deal with order */

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

152
void
153
Queue::MoveRange(unsigned start, unsigned end, unsigned to) noexcept
154
{
155 156
	const auto tmp = std::make_unique<Item[]>(end - start);

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

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

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

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

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

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

201
	const unsigned from_position = OrderToPosition(from_order);
202 203 204

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

211
	order[to_order] = from_position;
212
	return to_order;
213 214
}

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

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

238
	delete items[position].song;
239

240 241
	const unsigned id = PositionToId(position);
	const unsigned _order = PositionToOrder(position);
242

243
	--length;
244 245 246

	/* release the song id */

247
	id_table.Erase(id);
248 249 250

	/* delete song from songs array */

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

	/* delete the entry from the order array */

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

	/* readjust values in the order array */

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

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

272
		delete item->song;
273

274
		id_table.Erase(item->id);
275 276
	}

277
	length = 0;
278 279
}

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

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

		return a.priority > b.priority;
	};

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

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

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

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

	if (start == end)
		return;

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

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

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

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

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

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

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

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

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

378 379 380 381
	rand.AutoCreate();

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

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

390 391
	rand.AutoCreate();

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

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

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

414
	return length;
415 416
}

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

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

430
	return length - start_order;
431 432 433
}

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

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

444
	item->version = version;
445 446
	item->priority = priority;

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

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

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

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

	/* shuffle the song within that priority group */

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

	return true;
}

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

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

509
		modified |= SetPriority(i, priority, after_order);
510 511 512 513
	}

	return modified;
}