From 025f13ffebdeb5e14d36054d35c0714a58576b5a Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 29 Sep 2015 16:40:53 -0400 Subject: [PATCH] core/index: Move index_has() out of the index_entry struct Signed-off-by: Anna Schumaker --- core/index.cpp | 14 +++++++++----- core/playlist.cpp | 7 +------ include/core/index.h | 11 +++-------- tests/core/index.cpp | 18 +++--------------- 4 files changed, 16 insertions(+), 34 deletions(-) diff --git a/core/index.cpp b/core/index.cpp index 291052ac..38d7618d 100644 --- a/core/index.cpp +++ b/core/index.cpp @@ -19,11 +19,6 @@ size_t index_entry :: size() return ie_set.size(); } -bool index_entry :: has(unsigned int value) -{ - return ie_set.find(value) != ie_set.end(); -} - typename index_entry::iterator index_entry :: begin() { return ie_set.begin(); @@ -81,3 +76,12 @@ void index_remove(Index *index, const gchar *key, unsigned int value) db_autosave(index); } } + +bool index_has(Index *index, const gchar *key, unsigned int value) +{ + index_entry *it = db_get(index, key); + + if (!it) + return false; + return it->ie_set.find(value) != it->ie_set.end(); +} diff --git a/core/playlist.cpp b/core/playlist.cpp index 8d4bcef6..212f28f5 100644 --- a/core/playlist.cpp +++ b/core/playlist.cpp @@ -100,12 +100,7 @@ void playlist :: init() bool playlist :: has(Track *track, const std::string &name) { - std::set::iterator it; - index_entry *ent = db_get(&playlist_db, name.c_str()); - - if (ent == NULL) - return false; - return ent->has(track->index()); + return index_has(&playlist_db, name.c_str(), track->index()); } void playlist :: add(Track *track, const std::string &name) diff --git a/include/core/index.h b/include/core/index.h index 47f3de3c..b1f6e99d 100644 --- a/include/core/index.h +++ b/include/core/index.h @@ -47,14 +47,6 @@ struct index_entry : public DatabaseEntry { */ 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 */ @@ -97,4 +89,7 @@ index_entry *index_insert(Index *, const gchar *, unsigned int); /* Remove a value from an index item with the specified key. */ void index_remove(Index *, const gchar *, unsigned int); +/* Called to check if the index has the specified (key, value) pair. */ +bool index_has(Index *, const gchar *, unsigned int); + #endif /* OCARINA_CORE_DATABASE_H */ diff --git a/tests/core/index.cpp b/tests/core/index.cpp index 450a32f4..dfadb318 100644 --- a/tests/core/index.cpp +++ b/tests/core/index.cpp @@ -9,7 +9,6 @@ static void test_entry() { index_entry *ie = new index_entry("Link"); - unsigned int i; struct file f; ie->ie_set.insert(0); @@ -18,10 +17,6 @@ static void test_entry() test_equal(ie->primary_key(), "Link"); test_equal(ie->size(), 3); - for (i = 0; i < 3; i++) { - test_loop_equal(ie->has(i), true, i); - } test_loop_passed(); - test_equal(ie->has(3), false); file_init(&f, "index_entry", 0); file_open(&f, OPEN_WRITE); @@ -33,7 +28,6 @@ static void test_entry() ie = new index_entry(); test_equal(ie->primary_key(), ""); test_equal(ie->size(), 0); - test_equal(ie->has(42), false); file_open(&f, OPEN_READ); ie->read(f); @@ -45,10 +39,6 @@ static void test_entry() test_equal(ie->size(), 3); file_close(&f); - for (i = 0; i < 3; i++) { - test_loop_equal(ie->has(i), true, i); - } test_loop_passed(); - delete ie; } @@ -76,14 +66,12 @@ static void test_stress(unsigned int N) } test_loop_passed(); test_equal(index.db_size, 26); - /* index.has() */ + /* index_has() */ for (c = 'a'; c <= 'z'; c += 4) { key = c; - ie = db_find(&index, key.c_str()); - test_loop_not_equal(ie, NULL, i); for (i = 0; i < N; i++) - test_loop_equal(ie->has(i), true, i); - test_loop_equal(ie->has(N), false, c - 'a'); + test_loop_equal(index_has(&index, key.c_str(), i), true, i); + test_loop_equal(index_has(&index, key.c_str(), N), false, c - 'a'); } test_loop_passed(); /* index_remove() */