core/index: Move index_insert() out of the Index class

And fold IndexEntry :: insert() into index_insert().

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-09-29 16:11:59 -04:00
parent a91326551f
commit 46363b1406
5 changed files with 26 additions and 36 deletions

View File

@ -22,7 +22,9 @@ const std::string filter :: add(const std::string &text, unsigned int index)
g_free(g_lc); g_free(g_lc);
for (end = 1; end <= lc.size(); end++) { 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] == ' ') if (lc[end] == ' ')
begin = ++end; begin = ++end;
} }

View File

@ -14,11 +14,6 @@ const std::string index_entry :: primary_key() const
return ie_key; return ie_key;
} }
void index_entry :: insert(unsigned int value)
{
ie_set.insert(value);
}
void index_entry :: remove(unsigned int value) void index_entry :: remove(unsigned int value)
{ {
ie_set.erase(value); ie_set.erase(value);
@ -63,7 +58,7 @@ void index_entry :: read(file &file)
for (unsigned int i = 0; i < num; i++) { for (unsigned int i = 0; i < num; i++) {
file_readf(&file, "%u", &val); 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); 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()); index_entry *it = db_find(index, key);
it->insert(value);
::db_autosave(this); it->ie_set.insert(value);
db_autosave(index);
return it;
} }
void Index :: remove(const std::string &key, unsigned int value) void Index :: remove(const std::string &key, unsigned int value)

View File

@ -113,7 +113,7 @@ void playlist :: add(Track *track, const std::string &name)
return; return;
if (!has(track, name)) { if (!has(track, name)) {
playlist_db.insert(name, track->index()); index_insert(&playlist_db, name.c_str(), track->index());
if (cur_plist == name) if (cur_plist == name)
playlist_q.add(track); playlist_q.add(track);
if (name == "Banned") if (name == "Banned")

View File

@ -40,13 +40,6 @@ struct index_entry : public DatabaseEntry {
*/ */
const std::string primary_key() const; 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. * Remove a value from this IndexEntry.
* *
@ -111,16 +104,6 @@ public:
*/ */
Index(const std::string &, bool); 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 * Remove value from the IndexEntry corresponding to key. The
* IndexEntry will not be removed even if it has 0 values left. * IndexEntry will not be removed even if it has 0 values left.
@ -131,4 +114,8 @@ public:
void remove(const std::string &, unsigned int); 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 */ #endif /* OCARINA_CORE_DATABASE_H */

View File

@ -12,9 +12,9 @@ static void test_entry()
unsigned int i; unsigned int i;
struct file f; struct file f;
ie->insert(0); ie->ie_set.insert(0);
ie->insert(1); ie->ie_set.insert(1);
ie->insert(2); ie->ie_set.insert(2);
test_equal(ie->primary_key(), "Link"); test_equal(ie->primary_key(), "Link");
test_equal(ie->size(), 3); test_equal(ie->size(), 3);
@ -55,17 +55,21 @@ static void test_entry()
static void test_stress(unsigned int N) static void test_stress(unsigned int N)
{ {
Index index("stress.idx", false); Index index("stress.idx", false);
index_entry *ie, *ie2;
std::string key; std::string key;
index_entry *ie;
unsigned int i; unsigned int i;
char c; char c;
/* index.insert() */ /* index_insert() */
for (c = 'a'; c <= 'z'; c++) { for (c = 'a'; c <= 'z'; c++) {
key = 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++) 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_equal(index.db_size, c - 'a' + 1, c - 'a');
} test_loop_passed(); } test_loop_passed();
test_equal(index.db_size, 26); test_equal(index.db_size, 26);