diff --git a/core/filter.cpp b/core/filter.cpp index 928809b0..8a4edc4a 100644 --- a/core/filter.cpp +++ b/core/filter.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -91,22 +92,23 @@ std::string filter :: add(const std::string &text, unsigned int track_id) return reassemble_text(parsed); } -static void do_set_intersection(std::set &a, - std::set &b, +static void do_set_intersection(IndexEntry *entry, std::set &res) { - set_intersection(a.begin(), a.end(), b.begin(), b.end(), - std::inserter >(res, res.begin())); + std::set tmp; + + set_intersection(entry->begin(), entry->end(), res.begin(), res.end(), + std::inserter >(tmp, tmp.begin())); + res.swap(tmp); } static void find_intersection(std::string &text, std::set &res) { - std::set tmp; IndexEntry *it = filter_index.find(text); - if (it) - do_set_intersection(res, it->_values, tmp); - res.swap(tmp); + do_set_intersection(it, res); + else + res.clear(); } void filter :: search(const std::string &text, std::set &res) @@ -117,6 +119,7 @@ void filter :: search(const std::string &text, std::set &res) res.clear(); parse_text(text, parsed); + if (parsed.size() == 0) return; @@ -124,7 +127,7 @@ void filter :: search(const std::string &text, std::set &res) found = filter_index.find(*it); if (!found) return; - res = found->_values; + std::copy(found->begin(), found->end(), std::inserter(res, res.begin())); for (it++; it != parsed.end(); it++) find_intersection(*it, res); diff --git a/core/index.cpp b/core/index.cpp index e03f6161..289aa473 100644 --- a/core/index.cpp +++ b/core/index.cpp @@ -25,6 +25,26 @@ void IndexEntry :: remove(unsigned int 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) { std::set::iterator it; diff --git a/core/playlist.cpp b/core/playlist.cpp index 4405a3c2..1eef5ec8 100644 --- a/core/playlist.cpp +++ b/core/playlist.cpp @@ -24,7 +24,7 @@ public: while (size() > 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)); } @@ -46,7 +46,7 @@ void playlist :: init() if (!ent) 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)); } @@ -57,9 +57,7 @@ bool playlist :: has(Track *track, const std::string &name) if (ent == NULL) return false; - - it = ent->_values.find(track->index()); - return it != ent->_values.end(); + return ent->has(track->index()); } void playlist :: add(Track *track, const std::string &name) diff --git a/include/core/index.h b/include/core/index.h index 6fb2138a..b91a159a 100644 --- a/include/core/index.h +++ b/include/core/index.h @@ -18,11 +18,15 @@ class IndexEntry : public DatabaseEntry { private: std::string _key; /**< The term stored by this IndexEntry. */ - -public: std::set _values; /**< Integers representing strings that contain this term. */ +public: + /** Iterator access for our backing std::set */ + typedef typename std::set::iterator iterator; + /** Const iterator access for our backing std::set */ + typedef typename std::set::const_iterator const_iterator; + IndexEntry(); /**< Create an empty IndexEntry. */ /** @@ -54,6 +58,31 @@ public: */ 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. diff --git a/lib/colmgr.cpp b/lib/colmgr.cpp index e8170009..fd165547 100644 --- a/lib/colmgr.cpp +++ b/lib/colmgr.cpp @@ -57,7 +57,7 @@ static void remove_banned_tracks() if (!ent) 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)); } diff --git a/tests/core/index.cpp b/tests/core/index.cpp index cd1fffae..85aa296d 100644 --- a/tests/core/index.cpp +++ b/tests/core/index.cpp @@ -44,12 +44,12 @@ static void test_single_item() test_not_equal(it, IDX_NULL); test_equal(index.size(), (unsigned)1); - test_equal(it->_values.size(), (size_t)1); - test_equal(*(it->_values.begin()), (unsigned)0); + test_equal(it->size(), (size_t)1); + test_equal(*(it->begin()), (unsigned)0); index.remove("a", 0); 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) @@ -80,7 +80,7 @@ static void test_insertion(struct TestArgs *args) check_equal(it, IDX_NULL); else { 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(); @@ -102,7 +102,7 @@ static void test_removal(struct TestArgs *args) check_equal(it, IDX_NULL); else { check_not_equal(it, IDX_NULL); - check_equal(it->_values.size(), (size_t)0); + check_equal(it->size(), (size_t)0); } } test :: success(); @@ -135,7 +135,7 @@ static void test_saving(struct TestArgs *args) } else { check_not_equal(it1, IDX_NULL); check_not_equal(it2, IDX_NULL); - check_equal(it1->_values.size(), it2->_values.size()); + check_equal(it1->size(), it2->size()); } } test :: success(); diff --git a/tests/core/playlist.cpp b/tests/core/playlist.cpp index a1111ef1..6d83e9fd 100644 --- a/tests/core/playlist.cpp +++ b/tests/core/playlist.cpp @@ -27,10 +27,10 @@ static void test_init() playlist :: init(); 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); 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"); test_equal(ent, IDX_NULL); } @@ -54,14 +54,14 @@ static void test_add() playlist :: add(tagdb :: lookup(5), "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(l->size(), (unsigned)19); playlist :: add(tagdb :: lookup(16), "Favorites"); playlist :: add(tagdb :: lookup(5), "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); playlist :: add(tagdb :: lookup(6), "No Playlist"); @@ -76,13 +76,13 @@ static void test_delete() playlist :: del(tagdb :: lookup(5), "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(l->size(), (unsigned)20); playlist :: del(tagdb :: lookup(5), "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); playlist :: del(tagdb :: lookup(6), "No Playlist");