]> git.sesse.net Git - vlc/blobdiff - src/playlist/sort.c
Remove useless <dirent.h> check
[vlc] / src / playlist / sort.c
index ad1e861e7a36d97d06721deb38bb6cdb8ee782b9..e125b0d112cc639e5b3af8ebdf644526092cf565 100644 (file)
@@ -27,6 +27,7 @@
 #endif
 
 #include <vlc_common.h>
+#include <vlc_rand.h>
 #define  VLC_INTERNAL_PLAYLIST_SORT_FUNCTIONS
 #include "vlc_playlist.h"
 #include "playlist_internal.h"
@@ -127,33 +128,26 @@ static inline sortfn_t find_sorting_fn( unsigned i_mode, unsigned i_type )
  * Sort an array of items recursively
  * @param i_items: number of items
  * @param pp_items: the array of items
- * @param i_mode: a SORT_* enum indicating the field to sort on
- * @param i_type: ORDER_NORMAL or ORDER_REVERSE
+ * @param p_sortfn: the sorting function
  * @return nothing
  */
 static inline
 void playlist_ItemArraySort( unsigned i_items, playlist_item_t **pp_items,
-                             unsigned i_mode, unsigned i_type )
+                             sortfn_t p_sortfn )
 {
-    sortfn_t sortfn = find_sorting_fn(i_mode, i_type);
-
-    if( sortfn )
+    if( p_sortfn )
     {
-        qsort( pp_items, i_items, sizeof( pp_items[0] ), sortfn );
+        qsort( pp_items, i_items, sizeof( pp_items[0] ), p_sortfn );
     }
     else /* Randomise */
     {
-        int i_position;
+        unsigned i_position;
+        unsigned i_new;
         playlist_item_t *p_temp;
 
-        for( i_position = 0; i_position < i_items ; i_position++ )
+        for( i_position = i_items - 1; i_position > 0; i_position-- )
         {
-            int i_new;
-
-            if( i_items > 1 )
-                i_new = rand() % (i_items - 1);
-            else
-                i_new = 0;
+            i_new = ((unsigned)vlc_mrand48()) % (i_position+1);
             p_temp = pp_items[i_position];
             pp_items[i_position] = pp_items[i_new];
             pp_items[i_new] = p_temp;
@@ -167,22 +161,19 @@ void playlist_ItemArraySort( unsigned i_items, playlist_item_t **pp_items,
  * This function must be entered with the playlist lock !
  * @param p_playlist the playlist
  * @param p_node the node to sort
- * @param i_mode: SORT_ID, SORT_TITLE, SORT_ARTIST, SORT_ALBUM, SORT_RANDOM
- * @param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order)
+ * @param p_sortfn the sorting function
  * @return VLC_SUCCESS on success
  */
 static int recursiveNodeSort( playlist_t *p_playlist, playlist_item_t *p_node,
-                              int i_mode, int i_type )
+                              sortfn_t p_sortfn )
 {
     int i;
-    playlist_ItemArraySort( p_node->i_children, p_node->pp_children,
-                            i_mode, i_type );
+    playlist_ItemArraySort(p_node->i_children,p_node->pp_children,p_sortfn);
     for( i = 0 ; i< p_node->i_children; i++ )
     {
         if( p_node->pp_children[i]->i_children != -1 )
         {
-            recursiveNodeSort( p_playlist, p_node->pp_children[i],
-                               i_mode, i_type );
+            recursiveNodeSort( p_playlist, p_node->pp_children[i], p_sortfn );
         }
     }
     return VLC_SUCCESS;
@@ -195,7 +186,7 @@ static int recursiveNodeSort( playlist_t *p_playlist, playlist_item_t *p_node,
  *
  * \param p_playlist the playlist
  * \param p_node the node to sort
- * \param i_mode: SORT_ID, SORT_TITLE, SORT_ARTIST, SORT_ALBUM, SORT_RANDOM
+ * \param i_mode: a SORT_* constant indicating the field to sort on
  * \param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order)
  * \return VLC_SUCCESS on success
  */
@@ -206,7 +197,7 @@ int playlist_RecursiveNodeSort( playlist_t *p_playlist, playlist_item_t *p_node,
     pl_priv(p_playlist)->b_reset_currently_playing = true;
 
     /* Do the real job recursively */
-    return recursiveNodeSort( p_playlist, p_node, i_mode, i_type );
+    return recursiveNodeSort(p_playlist,p_node,find_sorting_fn(i_mode,i_type));
 }
 
 
@@ -214,7 +205,7 @@ int playlist_RecursiveNodeSort( playlist_t *p_playlist, playlist_item_t *p_node,
  * functions are wrapped in cmp_a_## and cmp_d_## functions that do
  * void * to const playlist_item_t * casting and dereferencing and
  * cmp_d_## inverts the result, too. proto_## are static inline,
- * cmp_[ad]_## are merely inline as they're the target of pointers.
+ * cmp_[ad]_## are merely static as they're the target of pointers.
  *
  * In any case, each SORT_## constant (except SORT_RANDOM) must have
  * a matching SORTFN( )-declared function here.
@@ -250,8 +241,11 @@ SORTFN( SORT_DESCRIPTION, first, second )
 
 SORTFN( SORT_DURATION, first, second )
 {
-    return input_item_GetDuration( first->p_input ) -
-           input_item_GetDuration( second->p_input );
+    mtime_t time1 = input_item_GetDuration( first->p_input );
+    mtime_t time2 = input_item_GetDuration( second->p_input );
+    int i_ret = time1 > time2 ? 1 :
+                    ( time1 == time2 ? 0 : -1 );
+    return i_ret;
 }
 
 SORTFN( SORT_GENRE, first, second )