Queue.cxx 11.8 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 "DetachedSong.hxx"
23

24
Queue::Queue(unsigned _max_length)
25 26
	:max_length(_max_length), length(0),
	 version(1),
27 28
	 items(new Item[max_length]),
	 order(new unsigned[max_length]),
29
	 id_table(max_length * HASH_MULT),
30 31 32
	 repeat(false),
	 single(false),
	 consume(false),
33
	 random(false)
34 35 36
{
}

37
Queue::~Queue()
38 39 40
{
	Clear();

41 42
	delete[] items;
	delete[] order;
43 44 45
}

int
46
Queue::GetNextOrder(unsigned _order) const noexcept
47
{
48
	assert(_order < length);
49

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

void
63
Queue::IncrementVersion() noexcept
64 65 66
{
	static unsigned long max = ((uint32_t) 1 << 31) - 1;

67
	version++;
68

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

73
		version = 1;
74 75 76 77
	}
}

void
78
Queue::ModifyAtOrder(unsigned _order) noexcept
79
{
80
	assert(_order < length);
81

82
	unsigned position = order[_order];
83
	ModifyAtPosition(position);
84 85 86
}

unsigned
Max Kellermann's avatar
Max Kellermann committed
87
Queue::Append(DetachedSong &&song, uint8_t priority)
88
{
89
	assert(!IsFull());
90

91 92 93 94
	const unsigned position = length++;
	const unsigned id = id_table.Insert(position);

	auto &item = items[position];
95
	item.song = new DetachedSong(std::move(song));
96
	item.id = id;
97
	item.version = version;
98
	item.priority = priority;
99

100
	order[position] = position;
101 102 103 104 105

	return id;
}

void
106
Queue::SwapPositions(unsigned position1, unsigned position2) noexcept
107
{
108 109
	unsigned id1 = items[position1].id;
	unsigned id2 = items[position2].id;
110

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

113 114
	items[position1].version = version;
	items[position2].version = version;
115

116 117
	id_table.Move(id1, position2);
	id_table.Move(id2, position1);
118 119 120
}

void
121
Queue::MovePostion(unsigned from, unsigned to) noexcept
122
{
123
	const Item tmp = items[from];
124 125 126 127

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

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

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

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

	/* put song at _to_ */

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

	/* now deal with order */

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

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

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

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

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

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

198
unsigned
199
Queue::MoveOrder(unsigned from_order, unsigned to_order) noexcept
200
{
201 202
	assert(from_order < length);
	assert(to_order <= length);
203

204
	const unsigned from_position = OrderToPosition(from_order);
205 206 207

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

214
	order[to_order] = from_position;
215
	return to_order;
216 217
}

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

236
void
237
Queue::DeletePosition(unsigned position) noexcept
238
{
239
	assert(position < length);
240

241
	delete items[position].song;
242

243 244
	const unsigned id = PositionToId(position);
	const unsigned _order = PositionToOrder(position);
245

246
	--length;
247 248 249

	/* release the song id */

250
	id_table.Erase(id);
251 252 253

	/* delete song from songs array */

254
	for (unsigned i = position; i < length; i++)
255
		MoveItemTo(i + 1, i);
256 257 258

	/* delete the entry from the order array */

259 260
	for (unsigned i = _order; i < length; i++)
		order[i] = order[i + 1];
261 262 263

	/* readjust values in the order array */

264 265 266
	for (unsigned i = 0; i < length; i++)
		if (order[i] > position)
			--order[i];
267 268 269
}

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

275
		delete item->song;
276

277
		id_table.Erase(item->id);
278 279
	}

280
	length = 0;
281 282
}

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

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

		return a.priority > b.priority;
	};

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

302
void
303
Queue::ShuffleOrderRange(unsigned start, unsigned end)
304
{
305
	assert(random);
306
	assert(start <= end);
307
	assert(end <= length);
308

309 310
	rand.AutoCreate();
	std::shuffle(order + start, order + end, rand);
311 312 313 314 315 316 317
}

/**
 * Sort the "order" of items by priority, and then shuffle each
 * priority group.
 */
void
318
Queue::ShuffleOrderRangeWithPriority(unsigned start, unsigned end)
319
{
320
	assert(random);
321
	assert(start <= end);
322
	assert(end <= length);
323 324 325 326 327

	if (start == end)
		return;

	/* first group the range by priority */
328
	queue_sort_order_by_priority(this, start, end);
329 330 331

	/* now shuffle each priority group */
	unsigned group_start = start;
332
	uint8_t group_priority = GetOrderPriority(start);
333 334

	for (unsigned i = start + 1; i < end; ++i) {
335
		const uint8_t priority = GetOrderPriority(i);
336 337 338 339 340
		assert(priority <= group_priority);

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

	/* shuffle the last group */
348
	ShuffleOrderRange(group_start, end);
349 350 351
}

void
352
Queue::ShuffleOrder()
353
{
354
	ShuffleOrderRangeWithPriority(0, length);
355 356
}

357
void
358
Queue::ShuffleOrderFirst(unsigned start, unsigned end)
359
{
360 361 362 363
	rand.AutoCreate();

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

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

381 382 383 384
	rand.AutoCreate();

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

387
void
388
Queue::ShuffleRange(unsigned start, unsigned end)
389 390
{
	assert(start <= end);
391
	assert(end <= length);
392

393 394
	rand.AutoCreate();

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

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

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

417
	return length;
418 419
}

420
unsigned
421
Queue::CountSamePriority(unsigned start_order, uint8_t priority) const noexcept
422
{
423 424
	assert(random);
	assert(start_order <= length);
425

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

433
	return length - start_order;
434 435 436
}

bool
437 438
Queue::SetPriority(unsigned position, uint8_t priority, int after_order,
		   bool reorder)
439
{
440
	assert(position < length);
441

442
	Item *item = &items[position];
443 444 445 446
	uint8_t old_priority = item->priority;
	if (old_priority == priority)
		return false;

447
	item->version = version;
448 449
	item->priority = priority;

450
	if (!random || !reorder)
451 452 453
		/* don't reorder if not in random mode */
		return true;

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

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

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

	/* shuffle the song within that priority group */

489
	const unsigned priority_count = CountSamePriority(new_order, priority);
490
	assert(priority_count >= 1);
491
	ShuffleOrderFirst(new_order, new_order + priority_count);
492 493 494 495 496

	return true;
}

bool
497
Queue::SetPriorityRange(unsigned start_position, unsigned end_position,
498
			uint8_t priority, int after_order)
499 500
{
	assert(start_position <= end_position);
501
	assert(end_position <= length);
502 503 504

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

512
		modified |= SetPriority(i, priority, after_order);
513 514 515 516
	}

	return modified;
}