diff options
author | waker <wakeroid@gmail.com> | 2012-04-06 10:32:38 +0200 |
---|---|---|
committer | waker <wakeroid@gmail.com> | 2012-04-06 10:32:38 +0200 |
commit | 26293f4882d7476459a0aa90e992ad67a4585375 (patch) | |
tree | 4c2e65559e5af2145ad58d7ca7e0909760e67357 /playlist.c | |
parent | 721aae59d8d10a242850033f66ac5685ddb9e769 (diff) |
added random sort (based on patch from defusix)
Diffstat (limited to 'playlist.c')
-rw-r--r-- | playlist.c | 58 |
1 files changed, 57 insertions, 1 deletions
@@ -3174,7 +3174,13 @@ qsort_cmp_func (const void *a, const void *b) { } void -plt_sort (playlist_t *playlist, int iter, int id, const char *format, int ascending) { +plt_sort (playlist_t *playlist, int iter, int id, const char *format, int order) { + if (order == DDB_SORT_RANDOM) { + plt_sort_random (playlist, iter); + return; + } + int ascending = order == DDB_SORT_DESCENDING ? 0 : 1; + if (id == DB_COLUMN_FILENUMBER || !playlist->head[iter] || !playlist->head[iter]->next[iter]) { return; } @@ -3234,6 +3240,56 @@ plt_sort (playlist_t *playlist, int iter, int id, const char *format, int ascend } void +plt_sort_random (playlist_t *playlist, int iter) { + if (!playlist->head[iter] || !playlist->head[iter]->next[iter]) { + return; + } + + LOCK; + + const int playlist_count = playlist->count[iter]; + playItem_t **array = malloc (playlist_count * sizeof (playItem_t *)); + int idx = 0; + for (playItem_t *it = playlist->head[iter]; it; it = it->next[iter], idx++) { + array[idx] = it; + } + + //randomize array + for (int swap_a = 0; swap_a < playlist_count - 1; swap_a++) { + //select random item above swap_a-1 + const int swap_b = swap_a + (rand() / (float)RAND_MAX * (playlist_count - swap_a)); + + //swap a with b + playItem_t* const swap_temp = array[swap_a]; + array[swap_a] = array[swap_b]; + array[swap_b] = swap_temp; + + } + + playItem_t *prev = NULL; + playlist->head[iter] = 0; + for (idx = 0; idx < playlist->count[iter]; idx++) { + playItem_t *it = array[idx]; + it->prev[iter] = prev; + it->next[iter] = NULL; + if (!prev) { + playlist->head[iter] = it; + } + else { + prev->next[iter] = it; + } + prev = it; + } + playlist->tail[iter] = array[playlist->count[iter]-1]; + + free (array); + + plt_modified (playlist); + + UNLOCK; +} + +void plt_reset_cursor (playlist_t *playlist) { int i; LOCK; |