Commit 6f38e0fd authored by Scott Wheeler's avatar Scott Wheeler

Some minor cleanups and some rather significant tuning in

PlaylistItem::compare() which has been a significant bottleneck.

svn path=/trunk/kdemultimedia/juk/; revision=202899
parent 48e75218
......@@ -46,21 +46,15 @@
// public members
////////////////////////////////////////////////////////////////////////////////
Playlist::Playlist(PlaylistSplitter *s, QWidget *parent, const QString &name) : KListView(parent, name.latin1())
Playlist::Playlist(PlaylistSplitter *s, QWidget *parent, const QString &name) : KListView(parent, name.latin1()),
boxItem(0), playlistName(name), internalFile(true), splitter(s)
{
boxItem = 0;
playlistName = name;
internalFile = true;
playlistFileName = QString::null;
splitter = s;
setup();
}
Playlist::Playlist(PlaylistSplitter *s, const QFileInfo &playlistFile, QWidget *parent, const char *name) : KListView(parent, name)
Playlist::Playlist(PlaylistSplitter *s, const QFileInfo &playlistFile, QWidget *parent, const char *name) : KListView(parent, name), internalFile(false), splitter(s)
{
internalFile = false;
playlistFileName = playlistFile.absFilePath();
splitter = s;
setup();
QFile file(playlistFileName);
......
......@@ -133,55 +133,83 @@ int PlaylistItem::compare(QListViewItem *item, int column, bool ascending) const
{
// reimplemented from QListViewItem
// This is pretty ugly. This needs to be a const method to match the
// signature from QListViewItem::compare(), but for our purposes, we need
// to be able to call non-const methods, so we're casting this to a
// non-const pointer. Yuck.
if(!item)
return(0);
PlaylistItem *playlistItem = dynamic_cast<PlaylistItem *>(item);
PlaylistItem *playlistItem = static_cast<PlaylistItem *>(item);
// The following statments first check to see if you can sort based on the
// specified column. If the values for the two PlaylistItems are the same
// in that column it then trys to sort based on columns 1, 2, 3 and 0,
// (artist, album, track number, track name) in that order.
if(playlistItem) {
if(compare(this, playlistItem, column, ascending) != 0)
return(compare(this, playlistItem, column, ascending));
else {
for(int i = ArtistColumn; i <= TrackNumberColumn; i++) {
if(compare(this, playlistItem, i, ascending) != 0)
return(compare(this, playlistItem, i, ascending));
}
if(compare(this, playlistItem, TrackColumn, ascending) != 0)
return(compare(this, playlistItem, TrackColumn, ascending));
return(0);
}
int c = compare(this, playlistItem, column, ascending);
if(c != 0)
return(c);
else {
// Loop through the columns doing comparisons until something is differnt.
// If all else is the same, compare the track name.
for(int i = ArtistColumn; i <= TrackNumberColumn; i++) {
c = compare(this, playlistItem, i, ascending);
if(c != 0)
return(c);
}
return(compare(this, playlistItem, TrackColumn, ascending));
}
else
return(0); // cast failed, something is wrong
}
int PlaylistItem::compare(const PlaylistItem *firstItem, const PlaylistItem *secondItem, int column, bool ascending) const
{
// Try some very basic caching for "two in a row" searches. From what I've
// seen this is ~15% of all calls.
static const PlaylistItem *previousFirstItem = 0;
static const PlaylistItem *previousSecondItem = 0;
static int previousColumn = 0;
static int previousResult = 0;
if(firstItem == previousFirstItem && secondItem == previousSecondItem && column == previousColumn)
return(previousResult);
previousFirstItem = firstItem;
previousSecondItem = secondItem;
previousColumn = column;
if(column == TrackNumberColumn) {
if(firstItem->tag()->trackNumber() > secondItem->tag()->trackNumber())
if(firstItem->tag()->trackNumber() > secondItem->tag()->trackNumber()) {
previousResult = 1;
return(1);
else if(firstItem->tag()->trackNumber() < secondItem->tag()->trackNumber())
}
else if(firstItem->tag()->trackNumber() < secondItem->tag()->trackNumber()) {
previousResult = -1;
return(-1);
else
}
else {
previousResult = 0;
return(0);
}
}
else if(column == LengthColumn) {
if(firstItem->tag()->seconds() > secondItem->tag()->seconds())
if(firstItem->tag()->seconds() > secondItem->tag()->seconds()) {
previousResult = 1;
return(1);
else if(firstItem->tag()->seconds() < secondItem->tag()->seconds())
}
else if(firstItem->tag()->seconds() < secondItem->tag()->seconds()) {
previousResult = -1;
return(-1);
else
}
else {
previousResult = 0;
return(0);
}
}
else {
previousResult = firstItem->key(column, ascending).lower().localeAwareCompare(secondItem->key(column, ascending).lower());
return(previousResult);
}
else
return(firstItem->key(column, ascending).lower().localeAwareCompare(secondItem->key(column, ascending).lower()));
}
////////////////////////////////////////////////////////////////////////////////
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment