libsaria: Implement index::rand()
Needed to get a random song from the index when filtered.
This commit is contained in:
parent
5bf0a26584
commit
1d3baa79d5
|
@ -14,6 +14,7 @@ namespace libsaria
|
|||
bool is_visible(sid_t &);
|
||||
bool is_filtered();
|
||||
unsigned int size();
|
||||
sid_t rand();
|
||||
void print_stats();
|
||||
|
||||
}; /* Namespace: index */
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#include <libsaria/callback.h>
|
||||
#include "index.h"
|
||||
|
||||
static set<sid_t> results;
|
||||
set<sid_t> results;
|
||||
static bool filtered = false;
|
||||
|
||||
/*
|
||||
|
|
|
@ -98,4 +98,14 @@ namespace libsaria
|
|||
{
|
||||
println("Index cache hits: %u misses: %u", hits, misses);
|
||||
}
|
||||
|
||||
sid_t index::rand()
|
||||
{
|
||||
set<sid_t>::iterator it = results.begin();
|
||||
unsigned int index = rand() % results.size();
|
||||
|
||||
for (unsigned int i = 0; i < index; i++)
|
||||
it++;
|
||||
return *it;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,5 +9,6 @@ using namespace std;
|
|||
extern map<string, set<sid_t> > artist_index;
|
||||
extern map<string, set<sid_t> > album_index;
|
||||
extern map<string, set<sid_t> > title_index;
|
||||
extern set<sid_t> results;
|
||||
|
||||
#endif /* LIBSARIA_INDEX_INTERNAL_H */
|
||||
|
|
Loading…
Reference in New Issue