core/database: Move db_actual_size() out of the database struct

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-10-17 15:12:40 -04:00
parent 1dcbc0c505
commit 3a74cb0aa5
7 changed files with 20 additions and 22 deletions

View File

@ -93,5 +93,5 @@ void tags :: remove_library(Library *library)
unsigned int tags :: library_size()
{
return library_db.actual_size();
return db_actual_size(&library_db);
}

View File

@ -176,7 +176,7 @@ void tags :: remove_library_tracks(Library *library)
unsigned int tags :: track_size()
{
return track_db.actual_size();
return db_actual_size(&track_db);
}
void tags :: commit_track_db()

View File

@ -156,13 +156,6 @@ struct database {
*/
void remove(unsigned int);
/**
* Called to find the size of the backing std::vector.
*
* @return The size of Database::_db.
*/
unsigned int actual_size();
/**
* @return An iterator pointing to the first valid entry in the Database.
@ -199,6 +192,11 @@ struct database {
T *find(const std::string &);
};
/* Returns the size of the backing std::vector. */
template <class T>
unsigned int db_actual_size(const struct database<T> *);
#include "database.hpp"
#endif /* OCARINA_CORE_DATABASE_H */

View File

@ -28,7 +28,7 @@ void database<T> :: save()
if (file_open(&db_file, OPEN_WRITE) == false)
return;
file_writef(&db_file, "%u\n", actual_size());
file_writef(&db_file, "%u\n", db_actual_size(this));
for (unsigned int i = 0; i < db_entries.size(); i++) {
if (db_entries[i] == NULL)
file_writef(&db_file, "%d\n", false);
@ -89,7 +89,7 @@ T *database<T> :: insert(const T &item)
return NULL;
t = new T(item);
t->_index = actual_size();
t->_index = db_actual_size(this);
db_entries.push_back(t);
db_keys[t->primary_key()] = t->index();
@ -101,7 +101,7 @@ T *database<T> :: insert(const T &item)
template <class T>
void database<T> :: remove(unsigned int index)
{
if (index >= actual_size())
if (index >= db_actual_size(this))
return;
if (db_entries[index] == NULL)
return;
@ -113,9 +113,9 @@ void database<T> :: remove(unsigned int index)
}
template <class T>
unsigned int database<T> :: actual_size()
unsigned int db_actual_size(const struct database<T> *db)
{
return db_entries.size();
return db->db_entries.size();
}
template <class T>
@ -148,7 +148,7 @@ typename database<T>::iterator database<T> :: next(iterator &it)
template <class T>
T *database<T> :: at(unsigned int index)
{
if (index >= actual_size())
if (index >= db_actual_size(this))
return NULL;
return db_entries[index];
}

View File

@ -64,7 +64,7 @@ static void test_init()
database<struct int_entry> db("database.db", false);
/* Check initial sizes. */
test_equal(db.actual_size(), 0);
test_equal(db_actual_size(&db), 0);
test_equal(db.db_size, 0);
}
@ -88,7 +88,7 @@ static void test_stress(unsigned int N)
} test_loop_passed();
test_equal(db.db_size, N);
test_equal(db.actual_size(), N);
test_equal(db_actual_size(&db), N);
/* database.at() */
for (i = 0; i < N; i++) {
@ -124,7 +124,7 @@ static void test_stress(unsigned int N)
} test_loop_passed();
db.remove(N);
test_equal(db.db_size, N / 2);
test_equal(db.actual_size(), N);
test_equal(db_actual_size(&db), N);
/* database.first(), database.next(), database.end() */
i = 1;
@ -155,7 +155,7 @@ static void test_save_load()
db2.load();
test_equal(db2.db_size, N);
test_equal(db2.actual_size(), N);
test_equal(db_actual_size(&db2), N);
for (i = 0; i < N; i++) {
dbe = db2.at(i);
test_loop_not_equal(dbe, NULL, i);
@ -194,7 +194,7 @@ static void test_save_load()
db1.load();
test_equal(db1.db_size, N);
test_equal(db1.actual_size(), 2 * N);
test_equal(db_actual_size(&db1), 2 * N);
for (i = 1; i < (2 * N); i++) {
dbe = db1.at(i);
if ((i % 2) == 0) {

View File

@ -84,7 +84,7 @@ static void test_save_load(unsigned int n)
idx2.load();
test_equal(idx2.db_size, INDEX->db_size);
test_equal(idx2.actual_size(), INDEX->actual_size());
test_equal(db_actual_size(&idx2), db_actual_size(INDEX));
for (char c = 'a'; c <= 'z'; c++) {
it1 = INDEX->find(std::string(1, c));

View File

@ -59,7 +59,7 @@ static void test_library_tag_lookup()
test_equal(tags :: get_library(1), (Library *)NULL);
library_db.load();
test_equal(library_db.actual_size(), (unsigned)tags :: library_size());
test_equal(db_actual_size(&library_db), (unsigned)tags :: library_size());
tags :: remove_library(library);
test_equal(tags :: get_library(0), (Library *)NULL);