From 4c4d0ab6ce4ab34479678f7a01fc3067908bde5a Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Thu, 3 Dec 2015 13:43:15 -0500 Subject: [PATCH] core/queue: Move queue_sort() out of the queue struct Signed-off-by: Anna Schumaker --- core/deck.cpp | 4 ++-- core/library.cpp | 10 +++++----- core/playlist.cpp | 6 +++--- core/queue.cpp | 24 +++++++++++++----------- gui/tabs.cpp | 4 ++-- include/core/queue.h | 16 +++------------- tests/core/queue.cpp | 24 ++++++++++++------------ 7 files changed, 40 insertions(+), 48 deletions(-) diff --git a/core/deck.cpp b/core/deck.cpp index f90c2e89..15c2f8c2 100644 --- a/core/deck.cpp +++ b/core/deck.cpp @@ -67,9 +67,9 @@ static void upgrade_v0() for (unsigned int i = 0; i < num; i++) { file_readf(&deck_file, "%u %d", &field, &ascending); - library->sort((compare_t)field, (i == 0) ? true : false); + queue_sort(library, (compare_t)field, (i == 0) ? true : false); if (!ascending) - library->sort((compare_t)field, false); + queue_sort(library, (compare_t)field, false); } } diff --git a/core/library.cpp b/core/library.cpp index 446b7c57..1069ac15 100644 --- a/core/library.cpp +++ b/core/library.cpp @@ -47,9 +47,9 @@ public: file_readf(&f, "%u %u", &q_flags, &n); for (unsigned int i = 0; i < n; i++) { file_readf(&f, "%u %d", &field, &ascending); - queue :: sort((compare_t)field, (i == 0) ? true : false); + queue_sort(this, (compare_t)field, (i == 0) ? true : false); if (ascending == false) - queue :: sort((compare_t)field, false); + queue_sort(this, (compare_t)field, false); } file_close(&f); } @@ -152,9 +152,9 @@ void collection :: init(struct queue_ops *ops) library_q.load(); if (library_q.q_sort.size() == 0) { - library_q.sort(COMPARE_ARTIST, true); - library_q.sort(COMPARE_YEAR, false); - library_q.sort(COMPARE_TRACK, false); + queue_sort(&library_q, COMPARE_ARTIST, true); + queue_sort(&library_q, COMPARE_YEAR, false); + queue_sort(&library_q, COMPARE_TRACK, false); } queue_set_flag(&library_q, Q_SAVE_SORT); diff --git a/core/playlist.cpp b/core/playlist.cpp index fab14a7f..67ee80bb 100644 --- a/core/playlist.cpp +++ b/core/playlist.cpp @@ -74,9 +74,9 @@ void playlist :: init() struct set_iter it; queue_init(&playlist_q, Q_ENABLED | Q_REPEAT, NULL); - playlist_q.sort(COMPARE_ARTIST, true); - playlist_q.sort(COMPARE_YEAR, false); - playlist_q.sort(COMPARE_TRACK, false); + queue_sort(&playlist_q, COMPARE_ARTIST, true); + queue_sort(&playlist_q, COMPARE_YEAR, false); + queue_sort(&playlist_q, COMPARE_TRACK, false); queue_set_flag(&playlist_q, Q_NO_SORT); index_init(&playlist_db, "playlist.db", true); diff --git a/core/queue.cpp b/core/queue.cpp index 9d65aef0..f68cfb8c 100644 --- a/core/queue.cpp +++ b/core/queue.cpp @@ -195,30 +195,32 @@ public: } }; -void queue :: sort(compare_t field, bool reset) +void queue_sort(struct queue *queue, enum compare_t field, bool reset) { bool found = false; struct sort_info info = { field, true }; - if (queue_has_flag(this, Q_NO_SORT)) + if (queue_has_flag(queue, Q_NO_SORT)) return; if (reset) - q_sort.clear(); + queue->q_sort.clear(); - for (unsigned int i = 0; i < q_sort.size(); i++) { - if (q_sort[i].field == info.field) { - q_sort[i].ascending = !q_sort[i].ascending; + for (unsigned int i = 0; i < queue->q_sort.size(); i++) { + if (queue->q_sort[i].field == info.field) { + queue->q_sort[i].ascending = !queue->q_sort[i].ascending; found = true; break; } } if (!found) - q_sort.push_back(info); + queue->q_sort.push_back(info); - std::stable_sort(q_tracks.begin(), q_tracks.end(), SortTracks(q_sort)); + std::stable_sort(queue->q_tracks.begin(), + queue->q_tracks.end(), + SortTracks(queue->q_sort)); - for (unsigned int i = 0; i < q_tracks.size(); i++) - q_notify->on_track_updated(i); - __queue_save(this, Q_SAVE_SORT); + for (unsigned int i = 0; i < queue->q_tracks.size(); i++) + queue->q_notify->on_track_updated(i); + __queue_save(queue, Q_SAVE_SORT); } diff --git a/gui/tabs.cpp b/gui/tabs.cpp index 27cd48a1..9e5add62 100644 --- a/gui/tabs.cpp +++ b/gui/tabs.cpp @@ -280,9 +280,9 @@ void Tab :: on_column_clicked(unsigned int col) { if (tab_sorting_count == 0) { tab_sorting_title = tab_window->q_treeview->get_column(col)->get_title(); - tab_pq->sort(sort_fields[col], true); + queue_sort(tab_pq, sort_fields[col], true); } else - tab_pq->sort(sort_fields[col], false); + queue_sort(tab_pq, sort_fields[col], false); tab_sorting_count++; tab_display_sorting(); diff --git a/include/core/queue.h b/include/core/queue.h index 1d1c37e3..49d7c122 100644 --- a/include/core/queue.h +++ b/include/core/queue.h @@ -134,19 +134,6 @@ struct queue { * @param track Track to remove from the queue. */ virtual void del(struct track *); - - - /** - * Add a new sort field to the queue. If the field is already in the - * _sort_order then it's ascending or descending value will be toggled. - * - * If reset is set to True then the _sort_order will be cleared before - * adding the new value. - * - * @param field Field to sort by. - * @param reset Set to true if current sort data should be discarded. - */ - virtual void sort(compare_t, bool); }; @@ -189,4 +176,7 @@ void queue_selected(struct queue *, unsigned int); /* Called to pick the next track from the queue. */ struct track *queue_next(struct queue *); +/* Called to sort the queue. */ +void queue_sort(struct queue *, enum compare_t, bool); + #endif /* OCARINA_CORE_QUEUE_H */ diff --git a/tests/core/queue.cpp b/tests/core/queue.cpp index af31995d..ffdd2fd7 100644 --- a/tests/core/queue.cpp +++ b/tests/core/queue.cpp @@ -302,7 +302,7 @@ static void test_sorting() q.add(track); } - q.sort(COMPARE_COUNT, true); + queue_sort(&q, COMPARE_COUNT, true); for (i = 0; i < 13; i++) { track = queue_at(&q, i); test_loop_not_equal(track, NULL, i); @@ -311,7 +311,7 @@ static void test_sorting() test_equal(count_sort, 1); queue_set_flag(&q, Q_NO_SORT); - q.sort(COMPARE_TITLE, true); + queue_sort(&q, COMPARE_TITLE, true); for (i = 0; i < 13; i++) { track = queue_at(&q, i); test_loop_not_equal(track, NULL, i); @@ -319,7 +319,7 @@ static void test_sorting() } test_loop_passed(); queue_unset_flag(&q, Q_NO_SORT); - q.sort(COMPARE_TITLE, true); + queue_sort(&q, COMPARE_TITLE, true); for (i = 0; i < 13; i++) { track = queue_at(&q, i); test_loop_not_equal(track, NULL, i); @@ -327,9 +327,9 @@ static void test_sorting() } test_loop_passed(); test_equal(count_sort, 2); - q.sort(COMPARE_COUNT, true); - q.sort(COMPARE_TITLE, false); - q.sort(COMPARE_COUNT, false); + queue_sort(&q, COMPARE_COUNT, true); + queue_sort(&q, COMPARE_TITLE, false); + queue_sort(&q, COMPARE_COUNT, false); for (i = 0; i < 13; i++) { track = queue_at(&q, i); test_loop_not_equal(track, NULL, i); @@ -338,10 +338,10 @@ static void test_sorting() test_equal(count_sort, 5); queue_unset_flag(&q, Q_SAVE_SORT); - q.sort(COMPARE_ARTIST, true); - q.sort(COMPARE_ALBUM, false); - q.sort(COMPARE_TRACK, false); - q.sort(COMPARE_TRACK, false); + queue_sort(&q, COMPARE_ARTIST, true); + queue_sort(&q, COMPARE_ALBUM, false); + queue_sort(&q, COMPARE_TRACK, false); + queue_sort(&q, COMPARE_TRACK, false); for (i = 0; i < 13; i++) { track = queue_at(&q, i); test_loop_not_equal(track, NULL, i); @@ -362,8 +362,8 @@ static void test_save_load() for (i = 0; i < 13; i++) q.add(track_get(i)); - q.sort(COMPARE_TRACK, true); - q.sort(COMPARE_TRACK, false); + queue_sort(&q, COMPARE_TRACK, true); + queue_sort(&q, COMPARE_TRACK, false); file_init(&f, "test.q", 0); file_open(&f, OPEN_WRITE);