MPD  0.20.18
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 noexcept {
166  assert(_order < length);
167 
168  return order[_order];
169  }
170 
171  gcc_pure
172  unsigned PositionToOrder(unsigned position) const noexcept {
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 noexcept {
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 noexcept;
236 
241  void IncrementVersion() noexcept;
242 
248  void ModifyAtPosition(unsigned position) {
249  assert(position < length);
250 
251  items[position].version = version;
252  }
253 
259  void ModifyAtOrder(unsigned order) noexcept;
260 
271  unsigned Append(DetachedSong &&song, uint8_t priority);
272 
276  void SwapPositions(unsigned position1, unsigned position2) noexcept;
277 
281  void SwapOrders(unsigned order1, unsigned order2) {
282  std::swap(order[order1], order[order2]);
283  }
284 
290  unsigned MoveOrder(unsigned from_order, unsigned to_order) noexcept;
291 
298  unsigned MoveOrderBefore(unsigned from_order,
299  unsigned to_order) noexcept;
300 
307  unsigned MoveOrderAfter(unsigned from_order,
308  unsigned to_order) noexcept;
309 
313  void MovePostion(unsigned from, unsigned to) noexcept;
314 
318  void MoveRange(unsigned start, unsigned end, unsigned to) noexcept;
319 
323  void DeletePosition(unsigned position) noexcept;
324 
328  void Clear() noexcept;
329 
333  void RestoreOrder() {
334  for (unsigned i = 0; i < length; ++i)
335  order[i] = i;
336  }
337 
342  void ShuffleOrderRange(unsigned start, unsigned end);
343 
348  void ShuffleOrderRangeWithPriority(unsigned start, unsigned end);
349 
354  void ShuffleOrder();
355 
356  void ShuffleOrderFirst(unsigned start, unsigned end);
357 
364  void ShuffleOrderLastWithPriority(unsigned start, unsigned end);
365 
370  void ShuffleRange(unsigned start, unsigned end);
371 
372  bool SetPriority(unsigned position, uint8_t priority, int after_order,
373  bool reorder=true);
374 
375  bool SetPriorityRange(unsigned start_position, unsigned end_position,
376  uint8_t priority, int after_order);
377 
378 private:
379  void MoveItemTo(unsigned from, unsigned to) {
380  unsigned from_id = items[from].id;
381 
382  items[to] = items[from];
383  items[to].version = version;
384  id_table.Move(from_id, to);
385  }
386 
391  gcc_pure
392  unsigned FindPriorityOrder(unsigned start_order, uint8_t priority,
393  unsigned exclude_order) const noexcept;
394 
395  gcc_pure
396  unsigned CountSamePriority(unsigned start_order,
397  uint8_t priority) const noexcept;
398 };
399 
400 #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
void MoveRange(unsigned start, unsigned end, unsigned to) noexcept
Moves a range of songs to a new position.
uint32_t version
the current version number
Definition: Queue.hxx:79
int PositionToId(unsigned position) const
Definition: Queue.hxx:157
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
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
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
bool IsNewerAtPosition(unsigned position, uint32_t _version) const
Is the song at the specified position newer than the specified version?
Definition: Queue.hxx:220
Queue & operator=(const Queue &)=delete
void Move(unsigned id, unsigned position)
Definition: IdTable.hxx:76
void ShuffleOrder()
Shuffles the virtual order of songs, but does not move them physically.
int IdToPosition(unsigned id) const
Definition: Queue.hxx:153
void Clear() noexcept
Removes all songs from the playlist.
void SwapPositions(unsigned position1, unsigned position2) noexcept
Swaps two songs, addressed by their position.
void ModifyAtOrder(unsigned order) noexcept
Marks the specified song as "modified".
DetachedSong * song
Definition: Queue.hxx:56
int IdToPosition(unsigned id) const
Definition: IdTable.hxx:48
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.
A queue of songs.
Definition: Queue.hxx:44
gcc_pure unsigned PositionToOrder(unsigned position) const noexcept
Definition: Queue.hxx:172
gcc_pure unsigned OrderToPosition(unsigned _order) const noexcept
Definition: Queue.hxx:165
void RestoreOrder()
Initializes the "order" array, and restores "normal" order.
Definition: Queue.hxx:333
~Queue()
Deinitializes a queue object.
void ShuffleOrderRangeWithPriority(unsigned start, unsigned end)
Shuffle the order of items in the specified range, taking their priorities into account.
bool IsValidOrder(unsigned _order) const
Is that a valid order number?
Definition: Queue.hxx:149
One element of the queue: basically a song plus some queue specific information attached.
Definition: Queue.hxx:55
Queue(unsigned max_length)
gcc_pure int GetNextOrder(unsigned order) const noexcept
Returns the order number following the specified one.
bool IsValidPosition(unsigned position) const
Is that a valid position number?
Definition: Queue.hxx:142
void ShuffleOrderFirst(unsigned start, unsigned end)
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
unsigned GetLength() const
Definition: Queue.hxx:117
bool single
play only current song.
Definition: Queue.hxx:95
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
gcc_pure uint8_t GetPriorityAtPosition(unsigned position) const noexcept
Definition: Queue.hxx:184
DetachedSong & GetOrder(unsigned _order) const
Returns the song at the specified order number.
Definition: Queue.hxx:212
uint8_t GetOrderPriority(unsigned i) const
Definition: Queue.hxx:196
unsigned MoveOrder(unsigned from_order, unsigned to_order) noexcept
Moves a song to a new position in the "order" list.
void IncrementVersion() noexcept
Increments the queue&#39;s version number.
#define gcc_pure
Definition: Compiler.h:116
IdTable id_table
map song ids to positions
Definition: Queue.hxx:88
unsigned MoveOrderBefore(unsigned from_order, unsigned to_order) noexcept
Moves a song to a new position in the "order" list before the given one.
bool IsEmpty() const
Determine if the queue is empty, i.e.
Definition: Queue.hxx:126
void ModifyAtPosition(unsigned position)
Marks the specified song as "modified".
Definition: Queue.hxx:248
void ShuffleOrderLastWithPriority(unsigned start, unsigned end)
Shuffles the virtual order of the last song in the specified (order) range; only songs which match th...
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.
void MovePostion(unsigned from, unsigned to) noexcept
Moves a song to a new position.
unsigned MoveOrderAfter(unsigned from_order, unsigned to_order) noexcept
Moves a song to a new position in the "order" list after the given one.
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.
void DeletePosition(unsigned position) noexcept
Removes a song from the playlist.
bool SetPriority(unsigned position, uint8_t priority, int after_order, bool reorder=true)
DetachedSong & Get(unsigned position) const
Returns the song at the specified position.
Definition: Queue.hxx:203