diff --git a/core/database.cpp b/core/database.cpp index 2c1ecf40..fa7eb9fb 100644 --- a/core/database.cpp +++ b/core/database.cpp @@ -17,7 +17,7 @@ db_entry :: ~db_entry() {} static void __dbe_free(struct database *db, struct db_entry *dbe) { if (dbe) { - db->db_keys.erase(dbe->dbe_key); + g_hash_table_remove(db->db_keys, dbe->dbe_key); g_free(dbe->dbe_key); g_ptr_array_index(db->db_entries, dbe->dbe_index) = NULL; @@ -59,7 +59,7 @@ static void __dbe_setup(struct database *db, unsigned int index) if (dbe) { dbe->dbe_index = index; dbe->dbe_key = db->db_ops->dbe_key(dbe); - db->db_keys[dbe->dbe_key] = index; + g_hash_table_insert(db->db_keys, dbe->dbe_key, dbe); db->db_size++; if (db->db_ops->dbe_setup) db->db_ops->dbe_setup(dbe); @@ -83,6 +83,7 @@ void db_init(struct database *db, const char *filepath, bool autosave, db->db_size = 0; db->db_autosave = autosave; db->db_entries = g_ptr_array_new(); + db->db_keys = g_hash_table_new(g_str_hash, g_str_equal); file_init(&db->db_file, filepath, 0); } @@ -94,7 +95,9 @@ void db_deinit(struct database *db) __dbe_free(db, dbe); g_ptr_array_free(db->db_entries, true); + g_hash_table_destroy(db->db_keys); db->db_entries = NULL; + db->db_keys = NULL; } void db_save(struct database *db) @@ -180,11 +183,7 @@ struct db_entry *db_at(struct database *db, unsigned int index) struct db_entry *db_get(struct database *db, const gchar *key) { - std::map::iterator it; - it = db->db_keys.find(key); - if (it == db->db_keys.end()) - return NULL; - return DB_ENTRY_AT(db, it->second); + return (struct db_entry *)g_hash_table_lookup(db->db_keys, key); } struct db_entry *db_find(struct database *db, const gchar *key) diff --git a/include/core/database.h b/include/core/database.h index b88a6431..977bd3d2 100644 --- a/include/core/database.h +++ b/include/core/database.h @@ -8,8 +8,6 @@ extern "C" { #include } -#include - /** * The DatabaseEntry class is the base class for storing * generic data inside a Database. @@ -67,9 +65,9 @@ struct database { bool db_autosave; /* True if the database saves when changed. */ struct file db_file; /* The database's associated file object. */ GPtrArray *db_entries; /* The database's backing array. */ + GHashTable *db_keys; /* The database's mapping of key -> value. */ const struct db_ops *db_ops; /* The database's operations vector. */ - std::map db_keys; }; diff --git a/include/core/tags/album.h b/include/core/tags/album.h index fbfc0d35..d350a024 100644 --- a/include/core/tags/album.h +++ b/include/core/tags/album.h @@ -5,6 +5,7 @@ #define OCARINA_CORE_TAGS_ALBUM_H #include +#include /** * The Album tag is used to store the name and year of albums diff --git a/include/core/tags/artist.h b/include/core/tags/artist.h index b1052119..793455a1 100644 --- a/include/core/tags/artist.h +++ b/include/core/tags/artist.h @@ -5,6 +5,7 @@ #define OCARINA_CORE_TAGS_ARTIST_H #include +#include /** * The Artist tag is used to store the name of artists added diff --git a/include/core/tags/genre.h b/include/core/tags/genre.h index 55079918..582cce34 100644 --- a/include/core/tags/genre.h +++ b/include/core/tags/genre.h @@ -5,6 +5,7 @@ #define OCARINA_CORE_TAGS_GENRE_H #include +#include /** * The Genre tag is used to store the name of genres added diff --git a/include/core/tags/library.h b/include/core/tags/library.h index 0528c2e3..7a37802b 100644 --- a/include/core/tags/library.h +++ b/include/core/tags/library.h @@ -5,6 +5,7 @@ #define OCARINA_CORE_TAGS_LIBRARY_H #include +#include /** * The Library tag is used to store a single directory added diff --git a/tests/core/database.cpp b/tests/core/database.cpp index 39037cd9..1c74298e 100644 --- a/tests/core/database.cpp +++ b/tests/core/database.cpp @@ -108,7 +108,7 @@ static void test_init() /* Check initial sizes. */ test_equal(db.db_entries->len, 0); - test_equal(db.db_keys.size(), 0); + test_equal(g_hash_table_size(db.db_keys), 0); test_equal(db.db_size, 0); test_equal(db.db_autosave, false); test_equal(db.db_file.f_version, 0); @@ -256,6 +256,7 @@ static void test_save_load() db_deinit(&db2); db2.db_entries = g_ptr_array_new(); + db2.db_keys = g_hash_table_new(g_str_hash, g_str_equal); test_setup_count = 0; db_load(&db2); test_equal(db2.db_size, N / 2); @@ -276,12 +277,14 @@ static void test_save_load() db_deinit(&db2); db2.db_entries = g_ptr_array_new(); + db2.db_keys = g_hash_table_new(g_str_hash, g_str_equal); db_load(&db2); test_equal(db2.db_size, N / 2); db_save(&db1); db_deinit(&db2); db2.db_entries = g_ptr_array_new(); + db2.db_keys = g_hash_table_new(g_str_hash, g_str_equal); db_load(&db2); i = 1;