Patch: Playlistmanager
Ralf Engels <[email protected]>
| Newsgroups | gmane.comp.audio.zinf.devel |
|---|---|
| Message-ID | <[email protected]> |
Hi,
the attached patch solves a bug in the playlist manager that getItem
returns an invalid pointer.
It also cleans up the shuffle and does a common subexpression reduction.
The patch is tested and solves the problem without producing new ones.
have fun,
Ralf
diff -ur zinf-compiled/base/src/playlist.cpp zinf/base/src/playlist.cpp
--- zinf-compiled/base/src/playlist.cpp 2003-08-09 19:48:42.000000000 +0200
+++ zinf/base/src/playlist.cpp 2003-08-20 22:32:01.000000000 +0200
@@ -448,43 +448,21 @@
if (!toBeShuffled)
toBeShuffled = &m_shuffleList;
- int max = toBeShuffled->size();
- vector<PlaylistItem *> tempShuffled;
-
srand((unsigned int)time(NULL));
+
- int i;
- vector<bool> usedList;
- for (i = 0; i < max; i++)
- {
- usedList.push_back(false);
- }
-
- bool used = true;
- int index = 0;
- int lastindex = 0;
-
- for (i = 0; i < max; i++)
- {
- while (used)
- {
- index = (int)((double)rand() / (RAND_MAX + 1.0) * max);
- if (usedList[index] == false)
- used = false;
- if (max - i > 50 && abs(index - lastindex) < 10)
- used = true;
- }
- usedList[index] = true;
- PlaylistItem *dupe = (*toBeShuffled)[index];
- tempShuffled.push_back(dupe);
- used = true;
- lastindex = index;
- }
-
- toBeShuffled->erase(toBeShuffled->begin(), toBeShuffled->end());
- vector<PlaylistItem *>::iterator iter = tempShuffled.begin();
- for (; iter != tempShuffled.end(); iter++)
- toBeShuffled->push_back(*iter);
+ // --- shuffling is quite easy if you know how to do it :)
+
+ for( uint32_t i=0; i<toBeShuffled->size()-i; i++ ) {
+ int randIndex = i + (int)((double)rand() / (RAND_MAX + 1.0)
+ * (toBeShuffled->size()-i) );
+
+ // exchange items
+ PlaylistItem* item = (*toBeShuffled)[i];
+ (*toBeShuffled)[i] = (*toBeShuffled)[randIndex];
+ (*toBeShuffled)[randIndex] = item;
+ }
+
}
Error PlaylistManager::SetCurrentItem(PlaylistItem* item)
@@ -494,7 +472,11 @@
PlaylistItem* PlaylistManager::GetCurrentItem()
{
+ if( m_current == kInvalidIndex )
+ return NULL;
+
PlaylistItem* result = NULL;
+
m_mutex.Acquire();
if(m_masterList.size())
@@ -1669,21 +1651,17 @@
else
stable_sort(m_activeList->begin(), m_activeList->end(),
not2(PlaylistItemSort(key)));
- m_sortKey = key;
- m_sortType = type;
-
- result = kError_NoErr;
}
else if(key == kPlaylistSortKey_Random)
{
ShuffleIt(m_activeList);
-
- m_sortKey = key;
- m_sortType = type;
-
- result = kError_NoErr;
}
+ m_sortKey = key;
+ m_sortType = type;
+
+ result = kError_NoErr;
+
if(IsntError(result))
{
if(kPlaylistKey_MasterPlaylist == GetActivePlaylist() &&
currentItem)
playlist.patch
(text/plain, 2.6 KB)
diff -ur zinf-compiled/base/src/playlist.cpp zinf/base/src/playlist.cpp
--- zinf-compiled/base/src/playlist.cpp 2003-08-09 19:48:42.000000000 +0200
+++ zinf/base/src/playlist.cpp 2003-08-20 22:32:01.000000000 +0200
@@ -448,43 +448,21 @@
if (!toBeShuffled)
toBeShuffled = &m_shuffleList;
- int max = toBeShuffled->size();
- vector<PlaylistItem *> tempShuffled;
-
srand((unsigned int)time(NULL));
+
- int i;
- vector<bool> usedList;
- for (i = 0; i < max; i++)
- {
- usedList.push_back(false);
- }
-
- bool used = true;
- int index = 0;
- int lastindex = 0;
-
- for (i = 0; i < max; i++)
- {
- while (used)
- {
- index = (int)((double)rand() / (RAND_MAX + 1.0) * max);
- if (usedList[index] == false)
- used = false;
- if (max - i > 50 && abs(index - lastindex) < 10)
- used = true;
- }
- usedList[index] = true;
- PlaylistItem *dupe = (*toBeShuffled)[index];
- tempShuffled.push_back(dupe);
- used = true;
- lastindex = index;
- }
-
- toBeShuffled->erase(toBeShuffled->begin(), toBeShuffled->end());
- vector<PlaylistItem *>::iterator iter = tempShuffled.begin();
- for (; iter != tempShuffled.end(); iter++)
- toBeShuffled->push_back(*iter);
+ // --- shuffling is quite easy if you know how to do it :)
+
+ for( uint32_t i=0; i<toBeShuffled->size()-i; i++ ) {
+ int randIndex = i + (int)((double)rand() / (RAND_MAX + 1.0)
+ * (toBeShuffled->size()-i) );
+
+ // exchange items
+ PlaylistItem* item = (*toBeShuffled)[i];
+ (*toBeShuffled)[i] = (*toBeShuffled)[randIndex];
+ (*toBeShuffled)[randIndex] = item;
+ }
+
}
Error PlaylistManager::SetCurrentItem(PlaylistItem* item)
@@ -494,7 +472,11 @@
PlaylistItem* PlaylistManager::GetCurrentItem()
{
+ if( m_current == kInvalidIndex )
+ return NULL;
+
PlaylistItem* result = NULL;
+
m_mutex.Acquire();
if(m_masterList.size())
@@ -1669,21 +1651,17 @@
else
stable_sort(m_activeList->begin(), m_activeList->end(), not2(PlaylistItemSort(key)));
- m_sortKey = key;
- m_sortType = type;
-
- result = kError_NoErr;
}
else if(key == kPlaylistSortKey_Random)
{
ShuffleIt(m_activeList);
-
- m_sortKey = key;
- m_sortType = type;
-
- result = kError_NoErr;
}
+ m_sortKey = key;
+ m_sortType = type;
+
+ result = kError_NoErr;
+
if(IsntError(result))
{
if(kPlaylistKey_MasterPlaylist == GetActivePlaylist() && currentItem)