database: Implement quick removal of items
Items will be marked "invalid" rather than directly removed from the database. This make it easier, since I won't need to reassign IDs to every database item after the removed one. Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
This commit is contained in:
parent
50a7c3dae6
commit
a5891a3338
|
@ -29,6 +29,7 @@ public:
|
|||
~Database();
|
||||
|
||||
unsigned int insert(T);
|
||||
void remove(unsigned int);
|
||||
unsigned int size();
|
||||
unsigned int num_rows();
|
||||
|
||||
|
|
|
@ -27,6 +27,13 @@ unsigned int Database<T> :: insert(T val)
|
|||
return db.size() - 1;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Database<T> :: remove(unsigned int id)
|
||||
{
|
||||
db[id].valid = false;
|
||||
_size--;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
unsigned int Database<T> :: size()
|
||||
{
|
||||
|
|
|
@ -30,14 +30,18 @@ void print_db(Database<DBTest> &db)
|
|||
|
||||
void test_insertion(Database<DBTest> &db)
|
||||
{
|
||||
for (unsigned int i = 1; i <= 10; i++)
|
||||
for (unsigned int i = 1; i <= 1000; i++)
|
||||
db.insert(DBTest(i));
|
||||
|
||||
print_db(db);
|
||||
}
|
||||
|
||||
void test_deletion(Database<DBTest> &db)
|
||||
{
|
||||
for (unsigned int i = db.first(); i < db.num_rows(); i = db.next(i)) {
|
||||
db.remove(i);
|
||||
i = db.next(i);
|
||||
}
|
||||
print_db(db);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
|
@ -46,6 +50,7 @@ int main(int argc, char **argv)
|
|||
|
||||
test_insertion(db);
|
||||
test_deletion(db);
|
||||
test_deletion(db);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue