sbMediacoreShuffleSequenceGenerator.cpp
Go to the documentation of this file.
1 /* vim: set sw=2 :miv */
2 /*
3 //
4 // BEGIN SONGBIRD GPL
5 //
6 // This file is part of the Songbird web player.
7 //
8 // Copyright(c) 2005-2008 POTI, Inc.
9 // http://songbirdnest.com
10 //
11 // This file may be licensed under the terms of of the
12 // GNU General Public License Version 2 (the "GPL").
13 //
14 // Software distributed under the License is distributed
15 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
16 // express or implied. See the GPL for the specific language
17 // governing rights and limitations.
18 //
19 // You should have received a copy of the GPL along with this
20 // program. If not, go to http://www.gnu.org/licenses/gpl.html
21 // or write to the Free Software Foundation, Inc.,
22 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 //
24 // END SONGBIRD GPL
25 //
26 */
27 
29 
30 #include <nsIURI.h>
31 #include <nsIURL.h>
32 
33 #include <nsAutoLock.h>
34 #include <nsArrayUtils.h>
35 #include <nsComponentManagerUtils.h>
36 #include <nsMemory.h>
37 #include <nsServiceManagerUtils.h>
38 #include <nsStringGlue.h>
39 #include <nsTArray.h>
40 
42 
43 #include <ctime>
44 #include <vector>
45 #include <algorithm>
46 
49 
50 nsresult
52 {
53  return NS_OK;
54 }
55 
56 NS_IMETHODIMP
57 sbMediacoreShuffleSequenceGenerator::OnGenerateSequence(sbIMediaListView *aView,
58  PRUint32 *aSequenceLength,
59  PRUint32 **aSequence)
60 {
61  NS_ENSURE_ARG_POINTER(aView);
62  NS_ENSURE_ARG_POINTER(aSequenceLength);
63  NS_ENSURE_ARG_POINTER(aSequence);
64 
65  *aSequenceLength = 0;
66  *aSequence = nsnull;
67 
68  PRUint32 length = 0;
69  nsresult rv = aView->GetLength(&length);
70  NS_ENSURE_SUCCESS(rv, rv);
71 
72  // Reserve space for return array
73  *aSequence = (PRUint32*)NS_Alloc(sizeof(PRUint32) * length);
74  *aSequenceLength = length;
75 
76  // Reserve space for pool and sequence.
77  std::vector<PRUint32> pool;
78  pool.reserve(length);
79 
80  // Generate pool.
81  for(PRUint32 current = 0; current < length; ++current) {
82  pool.push_back(current);
83  }
84 
85  // Seed.
86  std::srand(std::clock());
87 
88  // Randomly sample the pool to populate the sequence.
89  random_shuffle(pool.begin(), pool.end());
90 
91  // Copy into the return array
92  copy(pool.begin(), pool.end(), *aSequence);
93 
94  return NS_OK;
95 }
return NS_OK
NS_IMPL_THREADSAFE_ISUPPORTS1(sbMediacoreShuffleSequenceGenerator, sbIMediacoreSequenceGenerator) nsresult sbMediacoreShuffleSequenceGenerator
A distinct view on a given media list.
function Init()