cac0aee2e6
This should offer a performance increase since the item to insert will only be copied if it is not already in the database. Signed-off-by: Anna Schumaker <anna@ocarinaproject.net>
72 lines
1.1 KiB
C++
72 lines
1.1 KiB
C++
/*
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
*/
|
|
#include <index.h>
|
|
|
|
|
|
IndexEntry :: IndexEntry() {}
|
|
IndexEntry :: IndexEntry(const std::string &k)
|
|
: key(k)
|
|
{}
|
|
|
|
const std::string IndexEntry :: primary_key() const
|
|
{
|
|
return key;
|
|
}
|
|
|
|
void IndexEntry :: insert(unsigned int val)
|
|
{
|
|
values.insert(val);
|
|
}
|
|
|
|
void IndexEntry :: remove(unsigned int val)
|
|
{
|
|
values.erase(val);
|
|
}
|
|
|
|
void IndexEntry :: write(File &f)
|
|
{
|
|
std::set<unsigned int>::iterator it;
|
|
f << key << std::endl << values.size() << " ";
|
|
for (it = values.begin(); it != values.end(); it++)
|
|
f << *it << " ";
|
|
}
|
|
|
|
void IndexEntry :: read(File &f)
|
|
{
|
|
unsigned int num, val;
|
|
|
|
f >> key >> num;
|
|
for (unsigned int i = 0; i < num; i++) {
|
|
f >> val;
|
|
insert(val);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Index :: Index(const std::string &filepath, bool autosave)
|
|
: Database(filepath, autosave)
|
|
{}
|
|
|
|
void Index :: insert(const std::string &key, unsigned int val)
|
|
{
|
|
IndexEntry *it = find(key);
|
|
if (it == NULL)
|
|
it = Database :: insert(IndexEntry(key));
|
|
|
|
it->insert(val);
|
|
autosave();
|
|
}
|
|
|
|
void Index :: remove(const std::string &key, unsigned int val)
|
|
{
|
|
IndexEntry *it = find(key);
|
|
|
|
if (it == NULL)
|
|
return;
|
|
|
|
it->remove(val);
|
|
autosave();
|
|
}
|