IndexEntry: Make _values private
I don't want anybody outside of the IndexEntry class to modify the values in this container. This patch makes it private and provides iterator access instead. Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
dcb79dceed
commit
2b71ecbeca
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
#include <core/filter.h>
|
#include <core/filter.h>
|
||||||
#include <core/index.h>
|
#include <core/index.h>
|
||||||
|
#include <core/print.h>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <list>
|
#include <list>
|
||||||
|
@ -91,22 +92,23 @@ std::string filter :: add(const std::string &text, unsigned int track_id)
|
||||||
return reassemble_text(parsed);
|
return reassemble_text(parsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void do_set_intersection(std::set<unsigned int> &a,
|
static void do_set_intersection(IndexEntry *entry,
|
||||||
std::set<unsigned int> &b,
|
|
||||||
std::set<unsigned int> &res)
|
std::set<unsigned int> &res)
|
||||||
{
|
{
|
||||||
set_intersection(a.begin(), a.end(), b.begin(), b.end(),
|
std::set<unsigned int> tmp;
|
||||||
std::inserter<std::set<unsigned int> >(res, res.begin()));
|
|
||||||
|
set_intersection(entry->begin(), entry->end(), res.begin(), res.end(),
|
||||||
|
std::inserter<std::set<unsigned int> >(tmp, tmp.begin()));
|
||||||
|
res.swap(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void find_intersection(std::string &text, std::set<unsigned int> &res)
|
static void find_intersection(std::string &text, std::set<unsigned int> &res)
|
||||||
{
|
{
|
||||||
std::set<unsigned int> tmp;
|
|
||||||
IndexEntry *it = filter_index.find(text);
|
IndexEntry *it = filter_index.find(text);
|
||||||
|
|
||||||
if (it)
|
if (it)
|
||||||
do_set_intersection(res, it->_values, tmp);
|
do_set_intersection(it, res);
|
||||||
res.swap(tmp);
|
else
|
||||||
|
res.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void filter :: search(const std::string &text, std::set<unsigned int> &res)
|
void filter :: search(const std::string &text, std::set<unsigned int> &res)
|
||||||
|
@ -117,6 +119,7 @@ void filter :: search(const std::string &text, std::set<unsigned int> &res)
|
||||||
|
|
||||||
res.clear();
|
res.clear();
|
||||||
parse_text(text, parsed);
|
parse_text(text, parsed);
|
||||||
|
|
||||||
if (parsed.size() == 0)
|
if (parsed.size() == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -124,7 +127,7 @@ void filter :: search(const std::string &text, std::set<unsigned int> &res)
|
||||||
found = filter_index.find(*it);
|
found = filter_index.find(*it);
|
||||||
if (!found)
|
if (!found)
|
||||||
return;
|
return;
|
||||||
res = found->_values;
|
std::copy(found->begin(), found->end(), std::inserter(res, res.begin()));
|
||||||
|
|
||||||
for (it++; it != parsed.end(); it++)
|
for (it++; it != parsed.end(); it++)
|
||||||
find_intersection(*it, res);
|
find_intersection(*it, res);
|
||||||
|
|
|
@ -25,6 +25,26 @@ void IndexEntry :: remove(unsigned int value)
|
||||||
_values.erase(value);
|
_values.erase(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t IndexEntry :: size()
|
||||||
|
{
|
||||||
|
return _values.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IndexEntry :: has(unsigned int value)
|
||||||
|
{
|
||||||
|
return _values.find(value) != _values.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
typename IndexEntry::iterator IndexEntry :: begin()
|
||||||
|
{
|
||||||
|
return _values.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
typename IndexEntry::iterator IndexEntry :: end()
|
||||||
|
{
|
||||||
|
return _values.end();
|
||||||
|
}
|
||||||
|
|
||||||
void IndexEntry :: write(File &file)
|
void IndexEntry :: write(File &file)
|
||||||
{
|
{
|
||||||
std::set<unsigned int>::iterator it;
|
std::set<unsigned int>::iterator it;
|
||||||
|
|
|
@ -24,7 +24,7 @@ public:
|
||||||
while (size() > 0)
|
while (size() > 0)
|
||||||
del((unsigned)0);
|
del((unsigned)0);
|
||||||
|
|
||||||
for (it = ent->_values.begin(); it != ent->_values.end(); it++)
|
for (it = ent->begin(); it != ent->end(); it++)
|
||||||
add(tagdb :: lookup(*it));
|
add(tagdb :: lookup(*it));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ void playlist :: init()
|
||||||
if (!ent)
|
if (!ent)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (it = ent->_values.begin(); it != ent->_values.end(); it++)
|
for (it = ent->begin(); it != ent->end(); it++)
|
||||||
library :: get_queue()->del(tagdb :: lookup(*it));
|
library :: get_queue()->del(tagdb :: lookup(*it));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,9 +57,7 @@ bool playlist :: has(Track *track, const std::string &name)
|
||||||
|
|
||||||
if (ent == NULL)
|
if (ent == NULL)
|
||||||
return false;
|
return false;
|
||||||
|
return ent->has(track->index());
|
||||||
it = ent->_values.find(track->index());
|
|
||||||
return it != ent->_values.end();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void playlist :: add(Track *track, const std::string &name)
|
void playlist :: add(Track *track, const std::string &name)
|
||||||
|
|
|
@ -18,11 +18,15 @@
|
||||||
class IndexEntry : public DatabaseEntry {
|
class IndexEntry : public DatabaseEntry {
|
||||||
private:
|
private:
|
||||||
std::string _key; /**< The term stored by this IndexEntry. */
|
std::string _key; /**< The term stored by this IndexEntry. */
|
||||||
|
|
||||||
public:
|
|
||||||
std::set<unsigned int> _values; /**< Integers representing strings that
|
std::set<unsigned int> _values; /**< Integers representing strings that
|
||||||
contain this term. */
|
contain this term. */
|
||||||
|
|
||||||
|
public:
|
||||||
|
/** Iterator access for our backing std::set */
|
||||||
|
typedef typename std::set<unsigned int>::iterator iterator;
|
||||||
|
/** Const iterator access for our backing std::set */
|
||||||
|
typedef typename std::set<unsigned int>::const_iterator const_iterator;
|
||||||
|
|
||||||
IndexEntry(); /**< Create an empty IndexEntry. */
|
IndexEntry(); /**< Create an empty IndexEntry. */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -54,6 +58,31 @@ public:
|
||||||
*/
|
*/
|
||||||
void remove(unsigned int);
|
void remove(unsigned int);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called to find the number of values in our backing std::set.
|
||||||
|
*
|
||||||
|
* @return IndexEntry::_values.size()
|
||||||
|
*/
|
||||||
|
size_t size();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called to check if a specific value is stored in this IndexEntry.
|
||||||
|
*
|
||||||
|
* @param value The value to find.
|
||||||
|
* @return True if the value was found and false otherwise.
|
||||||
|
*/
|
||||||
|
bool has(unsigned int);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return An iterator pointing to the first item in IndexEntry::_values
|
||||||
|
*/
|
||||||
|
iterator begin();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return An iterator pointing past the last item in IndexEntry::_vaues
|
||||||
|
*/
|
||||||
|
iterator end();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write an IndexEntry to file.
|
* Write an IndexEntry to file.
|
||||||
|
|
|
@ -57,7 +57,7 @@ static void remove_banned_tracks()
|
||||||
if (!ent)
|
if (!ent)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (it = ent->_values.begin(); it != ent->_values.end(); it++)
|
for (it = ent->begin(); it != ent->end(); it++)
|
||||||
library :: get_queue()->del(tagdb :: lookup(*it));
|
library :: get_queue()->del(tagdb :: lookup(*it));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,12 +44,12 @@ static void test_single_item()
|
||||||
|
|
||||||
test_not_equal(it, IDX_NULL);
|
test_not_equal(it, IDX_NULL);
|
||||||
test_equal(index.size(), (unsigned)1);
|
test_equal(index.size(), (unsigned)1);
|
||||||
test_equal(it->_values.size(), (size_t)1);
|
test_equal(it->size(), (size_t)1);
|
||||||
test_equal(*(it->_values.begin()), (unsigned)0);
|
test_equal(*(it->begin()), (unsigned)0);
|
||||||
|
|
||||||
index.remove("a", 0);
|
index.remove("a", 0);
|
||||||
test_equal(index.size(), (unsigned)1);
|
test_equal(index.size(), (unsigned)1);
|
||||||
test_equal(it->_values.size(), (size_t)0);
|
test_equal(it->size(), (size_t)0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_insertion(struct TestArgs *args)
|
static void test_insertion(struct TestArgs *args)
|
||||||
|
@ -80,7 +80,7 @@ static void test_insertion(struct TestArgs *args)
|
||||||
check_equal(it, IDX_NULL);
|
check_equal(it, IDX_NULL);
|
||||||
else {
|
else {
|
||||||
check_not_equal(it, IDX_NULL);
|
check_not_equal(it, IDX_NULL);
|
||||||
check_equal(it->_values.size(), (size_t)args->n);
|
check_equal(it->size(), (size_t)args->n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
test :: success();
|
test :: success();
|
||||||
|
@ -102,7 +102,7 @@ static void test_removal(struct TestArgs *args)
|
||||||
check_equal(it, IDX_NULL);
|
check_equal(it, IDX_NULL);
|
||||||
else {
|
else {
|
||||||
check_not_equal(it, IDX_NULL);
|
check_not_equal(it, IDX_NULL);
|
||||||
check_equal(it->_values.size(), (size_t)0);
|
check_equal(it->size(), (size_t)0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
test :: success();
|
test :: success();
|
||||||
|
@ -135,7 +135,7 @@ static void test_saving(struct TestArgs *args)
|
||||||
} else {
|
} else {
|
||||||
check_not_equal(it1, IDX_NULL);
|
check_not_equal(it1, IDX_NULL);
|
||||||
check_not_equal(it2, IDX_NULL);
|
check_not_equal(it2, IDX_NULL);
|
||||||
check_equal(it1->_values.size(), it2->_values.size());
|
check_equal(it1->size(), it2->size());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
test :: success();
|
test :: success();
|
||||||
|
|
|
@ -27,10 +27,10 @@ static void test_init()
|
||||||
playlist :: init();
|
playlist :: init();
|
||||||
|
|
||||||
ent = playlist :: get_tracks("Banned");
|
ent = playlist :: get_tracks("Banned");
|
||||||
test_equal(ent->_values.size(), (size_t)4);
|
test_equal(ent->size(), (size_t)4);
|
||||||
test_equal(library :: get_queue()->size(), (unsigned)20);
|
test_equal(library :: get_queue()->size(), (unsigned)20);
|
||||||
ent = playlist :: get_tracks("Favorites");
|
ent = playlist :: get_tracks("Favorites");
|
||||||
test_equal(ent->_values.size(), (size_t)8);
|
test_equal(ent->size(), (size_t)8);
|
||||||
ent = playlist :: get_tracks("No Such Playlist");
|
ent = playlist :: get_tracks("No Such Playlist");
|
||||||
test_equal(ent, IDX_NULL);
|
test_equal(ent, IDX_NULL);
|
||||||
}
|
}
|
||||||
|
@ -54,14 +54,14 @@ static void test_add()
|
||||||
|
|
||||||
playlist :: add(tagdb :: lookup(5), "Banned");
|
playlist :: add(tagdb :: lookup(5), "Banned");
|
||||||
ent = playlist :: get_tracks("Banned");
|
ent = playlist :: get_tracks("Banned");
|
||||||
test_equal(ent->_values.size(), (size_t)5);
|
test_equal(ent->size(), (size_t)5);
|
||||||
test_equal(q->size(), (unsigned)8);
|
test_equal(q->size(), (unsigned)8);
|
||||||
test_equal(l->size(), (unsigned)19);
|
test_equal(l->size(), (unsigned)19);
|
||||||
|
|
||||||
playlist :: add(tagdb :: lookup(16), "Favorites");
|
playlist :: add(tagdb :: lookup(16), "Favorites");
|
||||||
playlist :: add(tagdb :: lookup(5), "Favorites");
|
playlist :: add(tagdb :: lookup(5), "Favorites");
|
||||||
ent = playlist :: get_tracks("Favorites");
|
ent = playlist :: get_tracks("Favorites");
|
||||||
test_equal(ent->_values.size(), (size_t)9);
|
test_equal(ent->size(), (size_t)9);
|
||||||
test_equal(q->size(), (unsigned)9);
|
test_equal(q->size(), (unsigned)9);
|
||||||
|
|
||||||
playlist :: add(tagdb :: lookup(6), "No Playlist");
|
playlist :: add(tagdb :: lookup(6), "No Playlist");
|
||||||
|
@ -76,13 +76,13 @@ static void test_delete()
|
||||||
|
|
||||||
playlist :: del(tagdb :: lookup(5), "Banned");
|
playlist :: del(tagdb :: lookup(5), "Banned");
|
||||||
ent = playlist :: get_tracks("Banned");
|
ent = playlist :: get_tracks("Banned");
|
||||||
test_equal(ent->_values.size(), (size_t)4);
|
test_equal(ent->size(), (size_t)4);
|
||||||
test_equal(q->size(), (unsigned)9);
|
test_equal(q->size(), (unsigned)9);
|
||||||
test_equal(l->size(), (unsigned)20);
|
test_equal(l->size(), (unsigned)20);
|
||||||
|
|
||||||
playlist :: del(tagdb :: lookup(5), "Favorites");
|
playlist :: del(tagdb :: lookup(5), "Favorites");
|
||||||
ent = playlist :: get_tracks("Favorites");
|
ent = playlist :: get_tracks("Favorites");
|
||||||
test_equal(ent->_values.size(), (size_t)8);
|
test_equal(ent->size(), (size_t)8);
|
||||||
test_equal(q->size(), (unsigned)8);
|
test_equal(q->size(), (unsigned)8);
|
||||||
|
|
||||||
playlist :: del(tagdb :: lookup(6), "No Playlist");
|
playlist :: del(tagdb :: lookup(6), "No Playlist");
|
||||||
|
|
Loading…
Reference in New Issue