1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
/*
* Copyright (C) 2003-2009 The Music Player Daemon Project
* 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.
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "queue.h"
#include "song.h"
unsigned
queue_generate_id(const struct queue *queue)
{
static unsigned cur = (unsigned)-1;
do {
cur++;
if (cur >= queue->max_length * QUEUE_HASH_MULT)
cur = 0;
} while (queue->idToPosition[cur] != -1);
return cur;
}
int
queue_next_order(const struct queue *queue, unsigned order)
{
assert(order < queue->length);
if (order + 1 < queue->length)
return order + 1;
else if (queue->repeat)
/* restart at first song */
return 0;
else
/* end of queue */
return -1;
}
void
queue_increment_version(struct queue *queue)
{
static unsigned long max = ((uint32_t) 1 << 31) - 1;
queue->version++;
if (queue->version >= max) {
for (unsigned i = 0; i < queue->length; i++)
queue->songMod[i] = 0;
queue->version = 1;
}
}
void
queue_modify(struct queue *queue, unsigned order)
{
unsigned position;
assert(order < queue->length);
position = queue->order[order];
queue->songMod[position] = queue->version;
queue_increment_version(queue);
}
void
queue_modify_all(struct queue *queue)
{
for (unsigned i = 0; i < queue->length; i++)
queue->songMod[i] = queue->version;
queue_increment_version(queue);
}
unsigned
queue_append(struct queue *queue, struct song *song)
{
unsigned id = queue_generate_id(queue);
assert(!queue_is_full(queue));
queue->songs[queue->length] = song;
queue->songMod[queue->length] = queue->version;
queue->order[queue->length] = queue->length;
queue->positionToId[queue->length] = id;
queue->idToPosition[queue->positionToId[queue->length]] =
queue->length;
++queue->length;
return id;
}
void
queue_swap(struct queue *queue, unsigned position1, unsigned position2)
{
struct song *sTemp;
unsigned iTemp;
sTemp = queue->songs[position1];
queue->songs[position1] = queue->songs[position2];
queue->songs[position2] = sTemp;
queue->songMod[position1] = queue->version;
queue->songMod[position2] = queue->version;
queue->idToPosition[queue->positionToId[position1]] = position2;
queue->idToPosition[queue->positionToId[position2]] = position1;
iTemp = queue->positionToId[position1];
queue->positionToId[position1] = queue->positionToId[position2];
queue->positionToId[position2] = iTemp;
}
static void
queue_move_song_to(struct queue *queue, unsigned from, unsigned to)
{
unsigned from_id = queue->positionToId[from];
queue->idToPosition[from_id] = to;
queue->positionToId[to] = from_id;
queue->songs[to] = queue->songs[from];
queue->songMod[to] = queue->version;
}
void
queue_move(struct queue *queue, unsigned from, unsigned to)
{
struct song *song;
unsigned id;
song = queue_get(queue, from);
id = queue_position_to_id(queue, from);
/* move songs to one less in from->to */
for (unsigned i = from; i < to; i++)
queue_move_song_to(queue, i + 1, i);
/* move songs to one more in to->from */
for (unsigned i = from; i > to; i--)
queue_move_song_to(queue, i - 1, i);
/* put song at _to_ */
queue->idToPosition[id] = to;
queue->positionToId[to] = id;
queue->songs[to] = song;
queue->songMod[to] = queue->version;
}
void
queue_delete(struct queue *queue, unsigned position)
{
struct song *song;
unsigned id, order;
assert(position < queue->length);
song = queue_get(queue, position);
if (!song_in_database(song))
song_free(song);
id = queue_position_to_id(queue, position);
order = queue_position_to_order(queue, position);
--queue->length;
/* release the song id */
queue->idToPosition[id] = -1;
/* delete song from songs array */
for (unsigned i = position; i < queue->length; i++)
queue_move_song_to(queue, i + 1, i);
/* delete the entry from the order array */
for (unsigned i = order; i < queue->length; i++)
queue->order[i] = queue->order[i + 1];
/* readjust values in the order array */
for (unsigned i = 0; i < queue->length; i++)
if (queue->order[i] > position)
--queue->order[i];
}
void
queue_clear(struct queue *queue)
{
for (unsigned i = 0; i < queue->length; i++) {
if (!song_in_database(queue->songs[i]))
song_free(queue->songs[i]);
queue->idToPosition[queue->positionToId[i]] = -1;
}
queue->length = 0;
}
void
queue_init(struct queue *queue, unsigned max_length)
{
queue->max_length = max_length;
queue->length = 0;
queue->version = 1;
queue->repeat = false;
queue->random = false;
queue->songs = g_malloc(sizeof(queue->songs[0]) * max_length);
queue->songMod = g_malloc(sizeof(queue->songMod[0]) *
max_length);
queue->order = g_malloc(sizeof(queue->order[0]) *
max_length);
queue->idToPosition = g_malloc(sizeof(queue->idToPosition[0]) *
max_length * QUEUE_HASH_MULT);
queue->positionToId = g_malloc(sizeof(queue->positionToId[0]) *
max_length);
for (unsigned i = 0; i < max_length * QUEUE_HASH_MULT; ++i)
queue->idToPosition[i] = -1;
queue->rand = g_rand_new();
}
void
queue_finish(struct queue *queue)
{
queue_clear(queue);
g_free(queue->songs);
g_free(queue->songMod);
g_free(queue->order);
g_free(queue->idToPosition);
g_free(queue->positionToId);
g_rand_free(queue->rand);
}
void
queue_shuffle_range(struct queue *queue, unsigned start, unsigned end)
{
assert(start <= end);
assert(end <= queue->length);
for (unsigned i = start; i < end; i++) {
unsigned ri = g_rand_int_range(queue->rand, i, end);
queue_swap(queue, i, ri);
}
}
|