core/queue: Move queue_add() out of the queue struct

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-11-25 08:24:04 -05:00
parent 4c4d0ab6ce
commit 8fe85b9782
9 changed files with 74 additions and 77 deletions

View File

@ -142,7 +142,7 @@ void audio :: load_track(struct track *track)
return;
_load_track(track, cur_driver->is_playing());
deck :: get_queue()->add(cur_track);
queue_add(deck :: get_queue(), cur_track);
}
struct track *audio :: current_track()

View File

@ -37,7 +37,7 @@ TempQueue :: TempQueue(bool random, struct queue_ops *ops)
unsigned int TempQueue :: add(struct track *track)
{
unsigned int res = queue :: add(track);
unsigned int res = queue_add(this, track);
deck :: write();
return res;
}

View File

@ -73,7 +73,7 @@ static void tag_track(struct library *library, const std::string &filepath)
{
struct track *track = track_add(library, filepath.c_str());
if (track)
library_q.add(track);
queue_add(&library_q, track);
}
static void process_path(struct library *library, const std :: string &dir,
@ -147,7 +147,7 @@ void collection :: init(struct queue_ops *ops)
db_for_each(track, next, track_db_get()) {
if (TRACK(track)->tr_library->li_enabled)
library_q.add(TRACK(track));
queue_add(&library_q, TRACK(track));
}
library_q.load();
@ -222,7 +222,7 @@ void collection :: set_enabled(struct library *library, bool enabled)
track = TRACK(dbe);
if (track->tr_library == library) {
if (enabled)
library_q.add(track);
queue_add(&library_q, track);
else
library_q.del(track);
}

View File

@ -20,7 +20,7 @@ public:
clear();
set_for_each(&ent->ie_set, &it)
add(track_get(it.it_val));
queue_add(this, track_get(it.it_val));
}
unsigned int find_average_count()
@ -41,11 +41,11 @@ public:
void dynamic_add(const std::string &name, struct track *track, unsigned int avg)
{
if ((name == "Unplayed") && (track->tr_count == 0))
add(track);
queue_add(this, track);
if ((name == "Most Played") && (track->tr_count > avg))
add(track);
queue_add(this, track);
if ((name == "Least Played") && (track->tr_count < avg))
add(track);
queue_add(this, track);
}
void dynamic_fill(const std::string &name)
@ -105,7 +105,7 @@ void playlist :: add(struct track *track, const std::string &name)
if (!has(track, name)) {
index_insert(&playlist_db, name.c_str(), track->tr_dbe.dbe_index);
if (cur_plist == name)
playlist_q.add(track);
queue_add(&playlist_q, track);
if (name == "Banned")
collection :: get_queue()->del(track);
}
@ -117,7 +117,7 @@ void playlist :: del(struct track *track, const std::string &name)
if (cur_plist == name)
playlist_q.del(track);
if (name == "Banned")
collection :: get_queue()->add(track);
queue_add(collection :: get_queue(), track);
}
void playlist :: select(const std::string &name)

View File

@ -20,12 +20,58 @@ public:
} def_notify;
static bool track_less_than(struct track *lhs, struct track *rhs,
std::vector<struct sort_info> &order)
{
int res;
for (unsigned int i = 0; i < order.size(); i++) {
if (order[i].ascending == true)
res = track_compare(lhs, rhs, order[i].field);
else
res = track_compare(rhs, lhs, order[i].field);
if (res == 0)
continue;
break;
}
return res < 0;
}
static inline void __queue_save(struct queue *queue, enum queue_flags flag)
{
if (queue->q_ops && queue_has_flag(queue, flag))
queue->q_ops->qop_save(queue, flag);
}
static unsigned int __queue_find_sorted_id(struct queue *queue, struct track *rhs)
{
struct track *lhs;
unsigned int mid, begin = 0, end = queue_size(queue) - 1;
if (queue_size(queue) == 0)
return 0;
while (end > begin) {
mid = begin + ((end - begin) / 2);
lhs = queue->q_tracks[mid];
if (track_less_than(lhs, rhs, queue->q_sort))
begin = mid + 1;
else {
if (mid == begin)
return begin;
else
end = mid - 1;
}
}
lhs = queue->q_tracks[begin];
if (track_less_than(lhs, rhs, queue->q_sort))
return begin + 1;
return begin;
}
void queue_init(struct queue *queue, unsigned int flags,
const struct queue_ops *ops)
{
@ -70,50 +116,6 @@ void queue_unset_flag(struct queue *queue, enum queue_flags flag)
__queue_save(queue, Q_SAVE_FLAGS);
}
static bool track_less_than(struct track *lhs, struct track *rhs,
std::vector<struct sort_info> &order)
{
int res;
for (unsigned int i = 0; i < order.size(); i++) {
if (order[i].ascending == true)
res = track_compare(lhs, rhs, order[i].field);
else
res = track_compare(rhs, lhs, order[i].field);
if (res == 0)
continue;
break;
}
return res < 0;
}
unsigned int queue :: find_sorted_id(struct track *rhs)
{
struct track *lhs;
unsigned int begin = 0, end = (q_tracks.size() - 1), mid;
if (q_tracks.size() == 0)
return 0;
while (end > begin) {
mid = begin + ((end - begin) / 2);
lhs = q_tracks[mid];
if (track_less_than(lhs, rhs, q_sort))
begin = mid + 1;
else {
if (mid == begin)
return begin;
else
end = mid - 1;
}
}
lhs = q_tracks[begin];
if (track_less_than(lhs, rhs, q_sort))
return begin + 1;
return begin;
}
unsigned int queue :: _add_at(struct track *track, unsigned int pos)
{
q_tracks.insert(q_tracks.begin() + pos, track);
@ -122,12 +124,14 @@ unsigned int queue :: _add_at(struct track *track, unsigned int pos)
return pos;
}
unsigned int queue :: add(struct track *track)
unsigned int queue_add(struct queue *queue, struct track *track)
{
unsigned int id = q_tracks.size();
if (q_sort.size() > 0)
id = find_sorted_id(track);
return _add_at(track, id);
unsigned int id = queue_size(queue);
if (queue->q_sort.size() > 0)
id = __queue_find_sorted_id(queue, track);
return queue->_add_at(track, id);
}
void queue :: del(unsigned int index)

View File

@ -164,7 +164,7 @@ void Tab :: tab_queue_add(queue *pq)
tab_selected_ids(ids);
for (unsigned int i = 0; i < ids.size(); i++)
pq->add(track_get(ids[i]));
queue_add(pq, track_get(ids[i]));
}
bool Tab :: tab_queue_selected(bool random)

View File

@ -91,7 +91,6 @@ struct queue {
const struct queue_ops *q_ops; /* The queue's operations vector. */
unsigned int find_sorted_id(struct track *);
unsigned int _add_at(struct track *, unsigned int);
@ -112,15 +111,6 @@ struct queue {
void read(file &);
/**
* Add a track to the queue, possibly matching the
* current sort order.
*
* @param track Track to add to the queue.
* @return The index of the track in the queue.
*/
virtual unsigned int add(struct track *);
/**
* Remove a track based on its queue index.
*
@ -166,6 +156,9 @@ static inline struct track *queue_at(struct queue *queue, unsigned int index)
}
/* Called to add a track to the queue. */
unsigned int queue_add(struct queue *, struct track *);
/* Called to tell the queue that a track has been updated. */
void queue_updated(struct queue *, struct track *);

View File

@ -119,7 +119,7 @@ static void test_next_prev()
q0->q_notify = &test_notifier;
queue_unset_flag(q0, Q_RANDOM);
for (unsigned int i = 0; i < 4; i++)
q0->add(track_get(i));
queue_add(q0, track_get(i));
test_not_equal(q, Q_NULL);
test_equal(queue_size(q), (unsigned)0);

View File

@ -152,11 +152,11 @@ static void test_stress(unsigned int N)
queue_init(&q, 0, &test_ops);
q.q_notify = &test_notifier;
/* Queue :: add() */
/* queue_add() */
for (i = 0; i < N; i++) {
track = track_get(i % 13);
ex_length += track->tr_length;
test_loop_equal(q.add(track), i, i);
test_loop_equal(queue_add(&q, track), i, i);
test_loop_equal(count_added, i + 1, i);
} test_loop_passed();
test_equal(q.q_length, ex_length);
@ -228,7 +228,7 @@ static void test_rand_select()
} test_loop_passed();
for (i = 0; i < 13; i++)
q.add(track_get(i));
queue_add(&q, track_get(i));
/*
* The comments below use the following notation:
@ -299,7 +299,7 @@ static void test_sorting()
for (i = 0; i < 13; i++) {
track = track_get(i);
track->tr_count = (i < 6) ? 4 : 2;
q.add(track);
queue_add(&q, track);
}
queue_sort(&q, COMPARE_COUNT, true);
@ -361,7 +361,7 @@ static void test_save_load()
queue_init(&r, 0, &test_ops);
for (i = 0; i < 13; i++)
q.add(track_get(i));
queue_add(&q, track_get(i));
queue_sort(&q, COMPARE_TRACK, true);
queue_sort(&q, COMPARE_TRACK, false);