diff --git a/core/audio.cpp b/core/audio.cpp index fee81ff5..550692c5 100644 --- a/core/audio.cpp +++ b/core/audio.cpp @@ -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() diff --git a/core/deck.cpp b/core/deck.cpp index 15c2f8c2..adad464d 100644 --- a/core/deck.cpp +++ b/core/deck.cpp @@ -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; } diff --git a/core/library.cpp b/core/library.cpp index 1069ac15..bbf04e5e 100644 --- a/core/library.cpp +++ b/core/library.cpp @@ -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); } diff --git a/core/playlist.cpp b/core/playlist.cpp index 67ee80bb..efb9e223 100644 --- a/core/playlist.cpp +++ b/core/playlist.cpp @@ -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) diff --git a/core/queue.cpp b/core/queue.cpp index f68cfb8c..21746ba4 100644 --- a/core/queue.cpp +++ b/core/queue.cpp @@ -20,12 +20,58 @@ public: } def_notify; +static bool track_less_than(struct track *lhs, struct track *rhs, + std::vector &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 &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) diff --git a/gui/tabs.cpp b/gui/tabs.cpp index 9e5add62..fb1e0cb3 100644 --- a/gui/tabs.cpp +++ b/gui/tabs.cpp @@ -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) diff --git a/include/core/queue.h b/include/core/queue.h index 49d7c122..9cc3a612 100644 --- a/include/core/queue.h +++ b/include/core/queue.h @@ -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 *); diff --git a/tests/core/deck.cpp b/tests/core/deck.cpp index 8fdddb2b..c7e730ab 100644 --- a/tests/core/deck.cpp +++ b/tests/core/deck.cpp @@ -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); diff --git a/tests/core/queue.cpp b/tests/core/queue.cpp index ffdd2fd7..450e855c 100644 --- a/tests/core/queue.cpp +++ b/tests/core/queue.cpp @@ -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);