MPD  0.20.6
Queue.hxx
Go to the documentation of this file.
1 /*
2  * Copyright 2003-2017 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef MPD_QUEUE_HXX
21 #define MPD_QUEUE_HXX
22 
23 #include "Compiler.h"
24 #include "IdTable.hxx"
26 
27 #include <algorithm>
28 
29 #include <assert.h>
30 #include <stdint.h>
31 
32 class DetachedSong;
33 
44 struct Queue {
49  static constexpr unsigned HASH_MULT = 4;
50 
55  struct Item {
57 
59  unsigned id;
60 
62  uint32_t version;
63 
69  uint8_t priority;
70  };
71 
73  unsigned max_length;
74 
76  unsigned length;
77 
79  uint32_t version;
80 
83 
85  unsigned *order;
86 
89 
92  bool repeat;
93 
95  bool single;
96 
98  bool consume;
99 
101  bool random;
102 
105 
106  explicit Queue(unsigned max_length);
107 
112  ~Queue();
113 
114  Queue(const Queue &) = delete;
115  Queue &operator=(const Queue &) = delete;
116 
117  unsigned GetLength() const {
118  assert(length <= max_length);
119 
120  return length;
121  }
122 
126  bool IsEmpty() const {
127  return length == 0;
128  }
129 
133  bool IsFull() const {
134  assert(length <= max_length);
135 
136  return length >= max_length;
137  }
138 
142  bool IsValidPosition(unsigned position) const {
143  return position < length;
144  }
145 
149  bool IsValidOrder(unsigned _order) const {
150  return _order < length;
151  }
152 
153  int IdToPosition(unsigned id) const {
154  return id_table.IdToPosition(id);
155  }
156 
157  int PositionToId(unsigned position) const
158  {
159  assert(position < length);
160 
161  return items[position].id;
162  }
163 
164  gcc_pure
165  unsigned OrderToPosition(unsigned _order) const {
166  assert(_order < length);
167 
168  return order[_order];
169  }
170 
171  gcc_pure
172  unsigned PositionToOrder(unsigned position) const {
173  assert(position < length);
174 
175  for (unsigned i = 0;; ++i) {
176  assert(i < length);
177 
178  if (order[i] == position)
179  return i;
180  }
181  }
182 
183  gcc_pure
184  uint8_t GetPriorityAtPosition(unsigned position) const {
185  assert(position < length);
186 
187  return items[position].priority;
188  }
189 
190  const Item &GetOrderItem(unsigned i) const {
191  assert(IsValidOrder(i));
192 
193  return items[OrderToPosition(i)];
194  }
195 
196  uint8_t GetOrderPriority(unsigned i) const {
197  return GetOrderItem(i).priority;
198  }
199 
203  DetachedSong &Get(unsigned position) const {
204  assert(position < length);
205 
206  return *items[position].song;
207  }
208 
212  DetachedSong &GetOrder(unsigned _order) const {
213  return Get(OrderToPosition(_order));
214  }
215 
220  bool IsNewerAtPosition(unsigned position, uint32_t _version) const {
221  assert(position < length);
222 
223  return _version > version ||
224  items[position].version >= _version ||
225  items[position].version == 0;
226  }
227 
234  gcc_pure
235  int GetNextOrder(unsigned order) const;
236 
241  void IncrementVersion();
242 
248  void ModifyAtPosition(unsigned position) {
249  assert(position < length);
250 
251  items[position].version = version;
252  }
253 
259  void ModifyAtOrder(unsigned order);
260 
271  unsigned Append(DetachedSong &&song, uint8_t priority);
272 
276  void SwapPositions(unsigned position1, unsigned position2);
277 
281  void SwapOrders(unsigned order1, unsigned order2) {
282  std::swap(order[order1], order[order2]);
283  }
284 
288  void MoveOrder(unsigned from_order, unsigned to_order);
289 
293  void MovePostion(unsigned from, unsigned to);
294 
298  void MoveRange(unsigned start, unsigned end, unsigned to);
299 
303  void DeletePosition(unsigned position);
304 
308  void Clear();
309 
313  void RestoreOrder() {
314  for (unsigned i = 0; i < length; ++i)
315  order[i] = i;
316  }
317 
322  void ShuffleOrderRange(unsigned start, unsigned end);
323 
328  void ShuffleOrderRangeWithPriority(unsigned start, unsigned end);
329 
334  void ShuffleOrder();
335 
336  void ShuffleOrderFirst(unsigned start, unsigned end);
337 
343  void ShuffleOrderLast(unsigned start, unsigned end);
344 
349  void ShuffleRange(unsigned start, unsigned end);
350 
351  bool SetPriority(unsigned position, uint8_t priority, int after_order,
352  bool reorder=true);
353 
354  bool SetPriorityRange(unsigned start_position, unsigned end_position,
355  uint8_t priority, int after_order);
356 
357 private:
358  void MoveItemTo(unsigned from, unsigned to) {
359  unsigned from_id = items[from].id;
360 
361  items[to] = items[from];
362  items[to].version = version;
363  id_table.Move(from_id, to);
364  }
365 
370  gcc_pure
371  unsigned FindPriorityOrder(unsigned start_order, uint8_t priority,
372  unsigned exclude_order) const;
373 
374  gcc_pure
375  unsigned CountSamePriority(unsigned start_order,
376  uint8_t priority) const;
377 };
378 
379 #endif
void SwapOrders(unsigned order1, unsigned order2)
Swaps two songs, addressed by their order number.
Definition: Queue.hxx:281
uint32_t version
when was this item last changed?
Definition: Queue.hxx:62
unsigned * order
map order numbers to positions
Definition: Queue.hxx:85
uint32_t version
the current version number
Definition: Queue.hxx:79
gcc_pure unsigned PositionToOrder(unsigned position) const
Definition: Queue.hxx:172
bool IsValidOrder(unsigned _order) const
Is that a valid order number?
Definition: Queue.hxx:149
void ShuffleOrderLast(unsigned start, unsigned end)
Shuffles the virtual order of the last song in the specified (order) range.
gcc_pure int GetNextOrder(unsigned order) const
Returns the order number following the specified one.
static constexpr unsigned HASH_MULT
reserve max_length * HASH_MULT elements in the id number space
Definition: Queue.hxx:49
uint8_t priority
The priority of this item, between 0 and 255.
Definition: Queue.hxx:69
int IdToPosition(unsigned id) const
Definition: IdTable.hxx:48
bool consume
remove each played files.
Definition: Queue.hxx:98
A table that maps id numbers to position numbers.
Definition: IdTable.hxx:32
bool repeat
repeat playback when the end of the queue has been reached?
Definition: Queue.hxx:92
bool IsEmpty() const
Determine if the queue is empty, i.e.
Definition: Queue.hxx:126
void MoveOrder(unsigned from_order, unsigned to_order)
Moves a song to a new position in the "order" list.
Item * items
all songs in "position" order
Definition: Queue.hxx:82
unsigned max_length
configured maximum length of the queue
Definition: Queue.hxx:73
LazyRandomEngine rand
random number generator for shuffle and random mode
Definition: Queue.hxx:104
void MovePostion(unsigned from, unsigned to)
Moves a song to a new position.
Queue & operator=(const Queue &)=delete
bool IsValidPosition(unsigned position) const
Is that a valid position number?
Definition: Queue.hxx:142
void MoveRange(unsigned start, unsigned end, unsigned to)
Moves a range of songs to a new position.
int PositionToId(unsigned position) const
Definition: Queue.hxx:157
void Move(unsigned id, unsigned position)
Definition: IdTable.hxx:76
void ShuffleOrder()
Shuffles the virtual order of songs, but does not move them physically.
DetachedSong & GetOrder(unsigned _order) const
Returns the song at the specified order number.
Definition: Queue.hxx:212
DetachedSong * song
Definition: Queue.hxx:56
unsigned id
the unique id of this item in the queue
Definition: Queue.hxx:59
unsigned Append(DetachedSong &&song, uint8_t priority)
Appends a song to the queue and returns its position.
DetachedSong & Get(unsigned position) const
Returns the song at the specified position.
Definition: Queue.hxx:203
A queue of songs.
Definition: Queue.hxx:44
void RestoreOrder()
Initializes the "order" array, and restores "normal" order.
Definition: Queue.hxx:313
~Queue()
Deinitializes a queue object.
void IncrementVersion()
Increments the queue's version number.
void ShuffleOrderRangeWithPriority(unsigned start, unsigned end)
Shuffle the order of items in the specified range, taking their priorities into account.
One element of the queue: basically a song plus some queue specific information attached.
Definition: Queue.hxx:55
Queue(unsigned max_length)
void ModifyAtOrder(unsigned order)
Marks the specified song as "modified".
bool IsNewerAtPosition(unsigned position, uint32_t _version) const
Is the song at the specified position newer than the specified version?
Definition: Queue.hxx:220
unsigned GetLength() const
Definition: Queue.hxx:117
void ShuffleOrderFirst(unsigned start, unsigned end)
gcc_pure unsigned OrderToPosition(unsigned _order) const
Definition: Queue.hxx:165
void ShuffleOrderRange(unsigned start, unsigned end)
Shuffle the order of items in the specified range, ignoring their priorities.
bool random
play back songs in random order?
Definition: Queue.hxx:101
bool single
play only current song.
Definition: Queue.hxx:95
int IdToPosition(unsigned id) const
Definition: Queue.hxx:153
void DeletePosition(unsigned position)
Removes a song from the playlist.
bool IsFull() const
Determine if the maximum number of songs has been reached.
Definition: Queue.hxx:133
const Item & GetOrderItem(unsigned i) const
Definition: Queue.hxx:190
#define gcc_pure
Definition: Compiler.h:116
IdTable id_table
map song ids to positions
Definition: Queue.hxx:88
void ModifyAtPosition(unsigned position)
Marks the specified song as "modified".
Definition: Queue.hxx:248
gcc_pure uint8_t GetPriorityAtPosition(unsigned position) const
Definition: Queue.hxx:184
void SwapPositions(unsigned position1, unsigned position2)
Swaps two songs, addressed by their position.
bool SetPriorityRange(unsigned start_position, unsigned end_position, uint8_t priority, int after_order)
A random engine that will be created and seeded on demand.
uint8_t GetOrderPriority(unsigned i) const
Definition: Queue.hxx:196
void Clear()
Removes all songs from the playlist.
unsigned length
number of songs in the queue
Definition: Queue.hxx:76
void ShuffleRange(unsigned start, unsigned end)
Shuffles a (position) range in the queue.
bool SetPriority(unsigned position, uint8_t priority, int after_order, bool reorder=true)