core/queue: Add Q_ADD_FRONT flag
This tells the queue to always add tracks at the front. Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
8fe85b9782
commit
d5e34b76d4
|
@ -6,20 +6,8 @@
|
|||
#include <core/library.h>
|
||||
|
||||
|
||||
class RecentQueue : public queue
|
||||
{
|
||||
public:
|
||||
unsigned int add(struct track *track)
|
||||
{
|
||||
del(track);
|
||||
q_cur = 0;
|
||||
return _add_at(track, 0);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
static std::list<TempQueue> queue_deck;
|
||||
static RecentQueue recent_queue;
|
||||
static struct queue recent_queue;
|
||||
static struct file deck_file;
|
||||
|
||||
|
||||
|
@ -79,7 +67,7 @@ void deck :: init(struct queue_ops *ops)
|
|||
bool upgraded = false;
|
||||
std::list<TempQueue>::iterator it;
|
||||
|
||||
queue_init(&recent_queue, Q_ENABLED | Q_REPEAT | Q_NO_SORT, NULL);
|
||||
queue_init(&recent_queue, Q_ENABLED | Q_REPEAT | Q_NO_SORT | Q_ADD_FRONT, NULL);
|
||||
file_init(&deck_file, "deck", 1);
|
||||
|
||||
if (!file_open(&deck_file, OPEN_READ))
|
||||
|
@ -212,8 +200,11 @@ struct track *deck :: next()
|
|||
|
||||
if (!track)
|
||||
track = queue_next(collection :: get_queue());
|
||||
if (track)
|
||||
recent_queue.add(track);
|
||||
if (track) {
|
||||
recent_queue.del(track);
|
||||
queue_add(&recent_queue, track);
|
||||
recent_queue.q_cur = 0;
|
||||
}
|
||||
return track;
|
||||
}
|
||||
|
||||
|
|
|
@ -143,7 +143,7 @@ void collection :: init(struct queue_ops *ops)
|
|||
{
|
||||
struct db_entry *track, *next;
|
||||
|
||||
queue_init(&library_q, Q_ENABLED | Q_REPEAT, ops);
|
||||
queue_init(&library_q, Q_ENABLED | Q_REPEAT | Q_ADD_FRONT, ops);
|
||||
|
||||
db_for_each(track, next, track_db_get()) {
|
||||
if (TRACK(track)->tr_library->li_enabled)
|
||||
|
@ -157,6 +157,7 @@ void collection :: init(struct queue_ops *ops)
|
|||
queue_sort(&library_q, COMPARE_TRACK, false);
|
||||
}
|
||||
|
||||
queue_unset_flag(&library_q, Q_ADD_FRONT);
|
||||
queue_set_flag(&library_q, Q_SAVE_SORT);
|
||||
queue_set_flag(&library_q, Q_SAVE_FLAGS);
|
||||
}
|
||||
|
|
|
@ -116,22 +116,19 @@ void queue_unset_flag(struct queue *queue, enum queue_flags flag)
|
|||
__queue_save(queue, Q_SAVE_FLAGS);
|
||||
}
|
||||
|
||||
unsigned int queue :: _add_at(struct track *track, unsigned int pos)
|
||||
{
|
||||
q_tracks.insert(q_tracks.begin() + pos, track);
|
||||
q_length += track->tr_length;
|
||||
q_notify->on_track_added(pos);
|
||||
return pos;
|
||||
}
|
||||
|
||||
unsigned int queue_add(struct queue *queue, struct track *track)
|
||||
{
|
||||
unsigned int id = queue_size(queue);
|
||||
unsigned int pos = queue_size(queue);
|
||||
|
||||
if (queue->q_sort.size() > 0)
|
||||
id = __queue_find_sorted_id(queue, track);
|
||||
if (queue_has_flag(queue, Q_ADD_FRONT))
|
||||
pos = 0;
|
||||
else if (queue->q_sort.size() > 0)
|
||||
pos = __queue_find_sorted_id(queue, track);
|
||||
|
||||
return queue->_add_at(track, id);
|
||||
queue->q_tracks.insert(queue->q_tracks.begin() + pos, track);
|
||||
queue->q_length += track->tr_length;
|
||||
queue->q_notify->on_track_added(pos);
|
||||
return pos;
|
||||
}
|
||||
|
||||
void queue :: del(unsigned int index)
|
||||
|
|
|
@ -23,6 +23,7 @@ enum queue_flags {
|
|||
Q_NO_SORT = (1 << 3), /* Queue will not be sorted. */
|
||||
Q_SAVE_FLAGS = (1 << 4), /* Queue will be saved when flags change. */
|
||||
Q_SAVE_SORT = (1 << 5), /* Queue will be saved when sorted. */
|
||||
Q_ADD_FRONT = (1 << 6), /* Queue will add new tracks at the front. */
|
||||
};
|
||||
|
||||
|
||||
|
@ -91,8 +92,6 @@ struct queue {
|
|||
|
||||
const struct queue_ops *q_ops; /* The queue's operations vector. */
|
||||
|
||||
unsigned int _add_at(struct track *, unsigned int);
|
||||
|
||||
|
||||
virtual ~queue(); /**< Queue destructor. */
|
||||
|
||||
|
|
|
@ -109,6 +109,7 @@ static void test_flags()
|
|||
test_equal(queue_has_flag(&q, Q_NO_SORT), false);
|
||||
test_equal(queue_has_flag(&q, Q_SAVE_FLAGS), false);
|
||||
test_equal(queue_has_flag(&q, Q_SAVE_SORT), false);
|
||||
test_equal(queue_has_flag(&q, Q_ADD_FRONT), false);
|
||||
|
||||
queue_set_flag(&q, Q_ENABLED);
|
||||
test_equal(q.q_flags, Q_ENABLED);
|
||||
|
@ -123,18 +124,23 @@ static void test_flags()
|
|||
queue_set_flag(&q, Q_RANDOM);
|
||||
queue_set_flag(&q, Q_REPEAT);
|
||||
queue_set_flag(&q, Q_NO_SORT);
|
||||
queue_set_flag(&q, Q_ADD_FRONT);
|
||||
test_equal(queue_has_flag(&q, Q_ENABLED), true);
|
||||
test_equal(queue_has_flag(&q, Q_RANDOM), true);
|
||||
test_equal(queue_has_flag(&q, Q_REPEAT), true);
|
||||
test_equal(queue_has_flag(&q, Q_NO_SORT), true);
|
||||
test_equal(queue_has_flag(&q, Q_SAVE_FLAGS), true);
|
||||
test_equal(count_flags, 5);
|
||||
test_equal(queue_has_flag(&q, Q_ADD_FRONT), true);
|
||||
test_equal(count_flags, 6);
|
||||
|
||||
queue_unset_flag(&q, Q_ENABLED);
|
||||
queue_unset_flag(&q, Q_RANDOM);
|
||||
queue_unset_flag(&q, Q_REPEAT);
|
||||
queue_unset_flag(&q, Q_NO_SORT);
|
||||
test_equal(count_flags, 9);
|
||||
queue_unset_flag(&q, Q_ADD_FRONT);
|
||||
queue_unset_flag(&q, Q_SAVE_FLAGS);
|
||||
test_equal(q.q_flags, 0);
|
||||
test_equal(count_flags, 11);
|
||||
}
|
||||
|
||||
static void test_stress(unsigned int N)
|
||||
|
@ -295,20 +301,35 @@ static void test_sorting()
|
|||
unsigned int i;
|
||||
struct queue q;
|
||||
|
||||
queue_init(&q, Q_SAVE_SORT, &test_ops);
|
||||
queue_init(&q, Q_SAVE_SORT | Q_ADD_FRONT, &test_ops);
|
||||
for (i = 0; i < 13; i++) {
|
||||
track = track_get(i);
|
||||
track->tr_count = (i < 6) ? 4 : 2;
|
||||
queue_add(&q, track);
|
||||
}
|
||||
|
||||
for (i = 0; i < 13; i++) {
|
||||
track = queue_at(&q, i);
|
||||
test_loop_not_equal(track, NULL, i);
|
||||
test_loop_equal(track->tr_dbe.dbe_index, 12 - i, i);
|
||||
} test_loop_passed();
|
||||
test_equal(count_sort, 0);
|
||||
|
||||
queue_sort(&q, COMPARE_TRACK, true);
|
||||
for (i = 0; i < 13; i++) {
|
||||
track = queue_at(&q, i);
|
||||
test_loop_not_equal(track, NULL, i);
|
||||
test_loop_equal(track->tr_dbe.dbe_index, i, i);
|
||||
} test_loop_passed();
|
||||
test_equal(count_sort, 1);
|
||||
|
||||
queue_sort(&q, COMPARE_COUNT, true);
|
||||
for (i = 0; i < 13; i++) {
|
||||
track = queue_at(&q, i);
|
||||
test_loop_not_equal(track, NULL, i);
|
||||
test_loop_equal(track->tr_dbe.dbe_index, ex_count[i], i);
|
||||
} test_loop_passed();
|
||||
test_equal(count_sort, 1);
|
||||
test_equal(count_sort, 2);
|
||||
|
||||
queue_set_flag(&q, Q_NO_SORT);
|
||||
queue_sort(&q, COMPARE_TITLE, true);
|
||||
|
@ -317,6 +338,7 @@ static void test_sorting()
|
|||
test_loop_not_equal(track, NULL, i);
|
||||
test_loop_equal(track->tr_dbe.dbe_index, ex_count[i], i);
|
||||
} test_loop_passed();
|
||||
test_equal(count_sort, 2);
|
||||
|
||||
queue_unset_flag(&q, Q_NO_SORT);
|
||||
queue_sort(&q, COMPARE_TITLE, true);
|
||||
|
@ -325,7 +347,7 @@ static void test_sorting()
|
|||
test_loop_not_equal(track, NULL, i);
|
||||
test_loop_equal(track->tr_dbe.dbe_index, ex_title[i], i);
|
||||
} test_loop_passed();
|
||||
test_equal(count_sort, 2);
|
||||
test_equal(count_sort, 3);
|
||||
|
||||
queue_sort(&q, COMPARE_COUNT, true);
|
||||
queue_sort(&q, COMPARE_TITLE, false);
|
||||
|
@ -335,7 +357,7 @@ static void test_sorting()
|
|||
test_loop_not_equal(track, NULL, i);
|
||||
test_loop_equal(track->tr_dbe.dbe_index, ex_co_ti[i], i);
|
||||
} test_loop_passed();
|
||||
test_equal(count_sort, 5);
|
||||
test_equal(count_sort, 6);
|
||||
|
||||
queue_unset_flag(&q, Q_SAVE_SORT);
|
||||
queue_sort(&q, COMPARE_ARTIST, true);
|
||||
|
@ -347,7 +369,7 @@ static void test_sorting()
|
|||
test_loop_not_equal(track, NULL, i);
|
||||
test_loop_equal(track->tr_dbe.dbe_index, 12 - i, i);
|
||||
} test_loop_passed();
|
||||
test_equal(count_sort, 5);
|
||||
test_equal(count_sort, 6);
|
||||
}
|
||||
|
||||
static void test_save_load()
|
||||
|
|
Loading…
Reference in New Issue