From 46363b14061be24fd43a8d5e20df1112adb8d0dd Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 29 Sep 2015 16:11:59 -0400 Subject: [PATCH] core/index: Move index_insert() out of the Index class And fold IndexEntry :: insert() into index_insert(). Signed-off-by: Anna Schumaker --- core/filter.cpp | 4 +++- core/index.cpp | 17 +++++++---------- core/playlist.cpp | 2 +- include/core/index.h | 21 ++++----------------- tests/core/index.cpp | 18 +++++++++++------- 5 files changed, 26 insertions(+), 36 deletions(-) diff --git a/core/filter.cpp b/core/filter.cpp index 6f1e8417..3dbe2d30 100644 --- a/core/filter.cpp +++ b/core/filter.cpp @@ -22,7 +22,9 @@ const std::string filter :: add(const std::string &text, unsigned int index) g_free(g_lc); for (end = 1; end <= lc.size(); end++) { - filter_index.insert(lc.substr(begin, end - begin), index); + index_insert(&filter_index, + lc.substr(begin, end - begin).c_str(), + index); if (lc[end] == ' ') begin = ++end; } diff --git a/core/index.cpp b/core/index.cpp index 60e5f2a0..5fc65862 100644 --- a/core/index.cpp +++ b/core/index.cpp @@ -14,11 +14,6 @@ const std::string index_entry :: primary_key() const return ie_key; } -void index_entry :: insert(unsigned int value) -{ - ie_set.insert(value); -} - void index_entry :: remove(unsigned int value) { ie_set.erase(value); @@ -63,7 +58,7 @@ void index_entry :: read(file &file) for (unsigned int i = 0; i < num; i++) { file_readf(&file, "%u", &val); - insert(val); + ie_set.insert(val); } } @@ -74,11 +69,13 @@ Index :: Index(const std::string &filepath, bool autosave) db_init(this, filepath.c_str(), autosave); } -void Index :: insert(const std::string &key, unsigned int value) +index_entry *index_insert(Index *index, const gchar *key, unsigned int value) { - index_entry *it = db_find(this, key.c_str()); - it->insert(value); - ::db_autosave(this); + index_entry *it = db_find(index, key); + + it->ie_set.insert(value); + db_autosave(index); + return it; } void Index :: remove(const std::string &key, unsigned int value) diff --git a/core/playlist.cpp b/core/playlist.cpp index 93958102..60aca2c0 100644 --- a/core/playlist.cpp +++ b/core/playlist.cpp @@ -113,7 +113,7 @@ void playlist :: add(Track *track, const std::string &name) return; if (!has(track, name)) { - playlist_db.insert(name, track->index()); + index_insert(&playlist_db, name.c_str(), track->index()); if (cur_plist == name) playlist_q.add(track); if (name == "Banned") diff --git a/include/core/index.h b/include/core/index.h index 923418f4..85d28694 100644 --- a/include/core/index.h +++ b/include/core/index.h @@ -40,13 +40,6 @@ struct index_entry : public DatabaseEntry { */ const std::string primary_key() const; - /** - * Add a new value to this IndexEntry. - * - * @param value The new value to add. - */ - void insert(unsigned int); - /** * Remove a value from this IndexEntry. * @@ -111,16 +104,6 @@ public: */ Index(const std::string &, bool); - /** - * Check if we already have an IndexEntry for key, and create one if - * we don't. Insert value into the found or created IndexEntry and - * call autosave() to save the Index. - * - * @param key String to store in the index. - * @param value Integer value associated with the key. - */ - void insert(const std::string &, unsigned int); - /** * Remove value from the IndexEntry corresponding to key. The * IndexEntry will not be removed even if it has 0 values left. @@ -131,4 +114,8 @@ public: void remove(const std::string &, unsigned int); }; + +/* Add a value to an index item with the specified key. */ +index_entry *index_insert(Index *, const gchar *, unsigned int); + #endif /* OCARINA_CORE_DATABASE_H */ diff --git a/tests/core/index.cpp b/tests/core/index.cpp index a2f27107..306fbf5d 100644 --- a/tests/core/index.cpp +++ b/tests/core/index.cpp @@ -12,9 +12,9 @@ static void test_entry() unsigned int i; struct file f; - ie->insert(0); - ie->insert(1); - ie->insert(2); + ie->ie_set.insert(0); + ie->ie_set.insert(1); + ie->ie_set.insert(2); test_equal(ie->primary_key(), "Link"); test_equal(ie->size(), 3); @@ -55,17 +55,21 @@ static void test_entry() static void test_stress(unsigned int N) { Index index("stress.idx", false); + index_entry *ie, *ie2; std::string key; - index_entry *ie; unsigned int i; char c; - /* index.insert() */ + /* index_insert() */ for (c = 'a'; c <= 'z'; c++) { key = c; - index.insert(key, 0); + ie = index_insert(&index, key.c_str(), 0); + test_loop_not_equal(ie, NULL, c - 'a'); + test_loop_equal(ie->primary_key(), key, c - 'a'); for (i = 0; i < N; i++) - index.insert(key, i); + ie2 = index_insert(&index, key.c_str(), i); + test_loop_equal(ie, ie2, c - 'a'); + test_loop_equal(ie->ie_set.size(), N, c - 'a'); test_loop_equal(index.db_size, c - 'a' + 1, c - 'a'); } test_loop_passed(); test_equal(index.db_size, 26);