libsaria: Implement index::rand()

Needed to get a random song from the index when filtered.
This commit is contained in:
Bryan Schumaker 2011-12-27 22:41:54 -05:00
parent 5bf0a26584
commit 1d3baa79d5
4 changed files with 13 additions and 1 deletions

View File

@ -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 */

View File

@ -4,7 +4,7 @@
#include <libsaria/callback.h>
#include "index.h"
static set<sid_t> results;
set<sid_t> results;
static bool filtered = false;
/*

View File

@ -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;
}
}

View File

@ -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 */