Track: Move less_than() function into the queue code

The track provides ways of accessing these fields, but I want the queue
to decide how to sort.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2014-11-28 10:28:15 -05:00
parent b5a529795d
commit 50a627412f
4 changed files with 58 additions and 95 deletions

View File

@ -61,6 +61,37 @@ bool Queue :: has_flag(queue_flags f)
return (_flags & f) == (unsigned int)f;
}
/*
* Returns:
* 0: lhs == rhs
* < 0: lhs < rhs, or rhs is empty
* > 0: lhs > rhs, or lhs is empty
*/
static inline int track_compare(Track *lhs, Track *rhs, sort_t field)
{
switch (field) {
case SORT_ARTIST:
return lhs->artist()->compare(rhs->artist());
case SORT_ALBUM:
return lhs->album()->compare(rhs->album());
case SORT_COUNT:
return lhs->count() - rhs->count();
case SORT_GENRE:
return lhs->genre()->compare(rhs->genre());
case SORT_LENGTH:
return lhs->length() - rhs->length();
case SORT_PLAYED:
return rhs->compare_date(rhs);
case SORT_TITLE:
return lhs->compare(rhs);
case SORT_TRACK:
return lhs->track() - rhs->track();
case SORT_YEAR:
return lhs->album()->year() - rhs->album()->year();
}
return 0;
}
static bool track_less_than(Track *lhs, Track *rhs,
std::vector<struct sort_info> &order)
{
@ -68,9 +99,9 @@ static bool track_less_than(Track *lhs, Track *rhs,
for (unsigned int i = 0; i < order.size(); i++) {
if (order[i].ascending == true)
res = lhs->less_than(rhs, order[i].field);
res = track_compare(lhs, rhs, order[i].field);
else
res = rhs->less_than(lhs, order[i].field);
res = track_compare(rhs, lhs, order[i].field);
if (res == 0)
continue;
break;

View File

@ -53,64 +53,6 @@ void Track :: write(File &f)
}
/*
* Returns:
* 0: lhs == rhs
* < 0: lhs < rhs, or rhs is empty
* > 0: lhs > rhs, or lhs is empty
*/
static inline int compare_string(const std::string &a, const std::string &b)
{
if (a.size() == 0)
return 1;
else if (b.size() == 0)
return -1;
return a.compare(b);
}
static inline int compare_uint(unsigned int a, unsigned int b)
{
if (a == b)
return 0;
if (a < b)
return -1;
return 1;
}
int Track :: less_than(Track *rhs, sort_t field)
{
int ret;
switch (field) {
case SORT_ARTIST:
return _artist->compare(rhs->artist());
case SORT_ALBUM:
return _album->compare(rhs->album());
case SORT_COUNT:
return compare_uint(_count, rhs->_count);
case SORT_GENRE:
return _genre->compare(rhs->genre());
case SORT_LENGTH:
return compare_uint(_length, rhs->_length);
case SORT_PLAYED:
ret = compare_uint(_date.year, rhs->_date.year);
if (ret == 0) {
ret = compare_uint(_date.month, rhs->_date.month);
if (ret == 0)
ret = compare_uint(_date.day, rhs->_date.day);
}
return ret;
case SORT_TITLE:
return compare(rhs);
case SORT_TRACK:
return compare_uint(_track, rhs->_track);
case SORT_YEAR:
return compare_uint(_album->year(), rhs->album()->year());
}
return 0;
}
/**
*
* Tagdb functions

View File

@ -35,6 +35,31 @@ enum queue_flags {
static const unsigned int Q_FLAG_MASK = Q_ENABLED | Q_RANDOM | Q_REPEAT | Q_NO_SORT;
/**
* Track fields that can be compared.
*/
enum sort_t {
/** Artist name */
SORT_ARTIST,
/** Album name */
SORT_ALBUM,
/** Play count */
SORT_COUNT,
/** Genre */
SORT_GENRE,
/** Track length */
SORT_LENGTH,
/** Date the track was last played */
SORT_PLAYED,
/** Track title */
SORT_TITLE,
/** Track number */
SORT_TRACK,
/** Track year */
SORT_YEAR,
};
/**
* Struct for passing sort parameters.
*/

View File

@ -12,31 +12,6 @@
#include <core/tags/library.h>
/**
* Track fields that can be compared.
*/
enum sort_t {
/** Artist name */
SORT_ARTIST,
/** Album name */
SORT_ALBUM,
/** Play count */
SORT_COUNT,
/** Genre */
SORT_GENRE,
/** Track length */
SORT_LENGTH,
/** Date the track was last played */
SORT_PLAYED,
/** Track title */
SORT_TITLE,
/** Track number */
SORT_TRACK,
/** Track year */
SORT_YEAR,
};
/** Structure used to represent dates. */
struct date {
unsigned int day; /**< Day of the month (1 - 31). */
@ -139,16 +114,6 @@ public:
* @param file The file to write to
*/
void write(File &);
/**
* Compare two tracks based on a specific field.
* @param rhs The other track to compare.
* @param field The field to compare.
* @return 0 if lhs == rhs
* @return < 0 if lhs < rhs, or rhs is NULL
* @return > 0 if lhs > rhs
*/
int less_than(Track *, sort_t);
};
#endif /* OCARINA_CORE_TAGS_TRACK_H */