libsaria: Sort list using comparison operator

It makes sense to define a function for sorting tracks as part of the
track class, rather than having to implement it externally.
This commit is contained in:
Bryan Schumaker 2011-12-28 22:02:10 -05:00
parent 1e5628d059
commit 336f188686
4 changed files with 50 additions and 39 deletions

View File

@ -56,8 +56,7 @@ namespace libsaria
int get_channels();
sid_t get_songid();
string *get_artist_lc();
string *get_album_lc();
bool operator<(Track &);
};
} /* Namespace: libsaria */

View File

@ -14,33 +14,10 @@ using namespace std;
static list<libsaria::Track *> play_list;
static list<libsaria::Track *>::iterator cur_track = play_list.end();
static inline bool compare_tracks(libsaria::Track *one, libsaria::Track *two)
static bool compare_tracks(libsaria::Track *one, libsaria::Track *two)
{
return one->get_track() < two->get_track();
}
static inline bool compare_albums(libsaria::Track *one, libsaria::Track *two)
{
string *s_one = one->get_album_lc();
string *s_two = two->get_album_lc();
if (*s_one < *s_two)
return true;
else if (*s_one == *s_two)
return compare_tracks(one, two);
return false;
}
static bool compare_tracktags(libsaria::Track *one, libsaria::Track *two)
{
string *s_one = one->get_artist_lc();
string *s_two = two->get_artist_lc();
if (*s_one == "")
return false;
if ( (*s_one < *s_two) || (*s_two == "") )
return true;
else if (*s_one == *s_two)
return compare_albums(one, two);
return false;
/* I want to compare tracks and not pointers... */
return *one < *two;
}
static bool find_id(sid_t &inode, list<libsaria::Track *>::iterator &it)
@ -115,7 +92,7 @@ namespace libsaria
play_list.clear();
for (it = path_list->begin(); it != path_list->end(); it++)
insert_list(it->get_list());
play_list.sort(compare_tracktags);
play_list.sort(compare_tracks);
}
void library::for_each(SourceModel *model)

View File

@ -69,14 +69,4 @@ namespace libsaria
return songid;
}
string *Track::get_artist_lc()
{
return artist_lc;
}
string *Track::get_album_lc()
{
return album_lc;
}
} /* Namespace: libsaria */

View File

@ -0,0 +1,45 @@
#include <libsaria/track.h>
/*
* Return positive value if s1 > s2
* Return negative value if s1 < s2
* Return 0 s1 == s2
*/
static inline int compare_strings(string *s1, string *s2)
{
/* List empty strings last */
if (*s1 == "")
return 1;
if (*s2 == "")
return -1;
return s1->compare(*s2);
}
namespace libsaria
{
/* Return true if this < cmp_track */
bool Track::operator<(Track &cmp_track)
{
int cmp;
/* Compare artists */
cmp = compare_strings(artist_lc, cmp_track.artist_lc);
if (cmp < 0)
return true;
else if (cmp > 0)
return false;
/* Compare albums */
cmp = compare_strings(album_lc, cmp_track.album_lc);
if (cmp < 0)
return true;
else if (cmp > 0)
return false;
/* Compare tracks */
return track < cmp_track.track;
}
}