96 lines
1.6 KiB
C++
96 lines
1.6 KiB
C++
/**
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
*/
|
|
#include <core/index.h>
|
|
|
|
|
|
IndexEntry :: IndexEntry() {}
|
|
IndexEntry :: IndexEntry(const std::string &key)
|
|
: _key(key)
|
|
{}
|
|
|
|
const std::string IndexEntry :: primary_key() const
|
|
{
|
|
return _key;
|
|
}
|
|
|
|
void IndexEntry :: insert(unsigned int value)
|
|
{
|
|
_values.insert(value);
|
|
}
|
|
|
|
void IndexEntry :: remove(unsigned int value)
|
|
{
|
|
_values.erase(value);
|
|
}
|
|
|
|
size_t IndexEntry :: size()
|
|
{
|
|
return _values.size();
|
|
}
|
|
|
|
bool IndexEntry :: has(unsigned int value)
|
|
{
|
|
return _values.find(value) != _values.end();
|
|
}
|
|
|
|
typename IndexEntry::iterator IndexEntry :: begin()
|
|
{
|
|
return _values.begin();
|
|
}
|
|
|
|
typename IndexEntry::iterator IndexEntry :: end()
|
|
{
|
|
return _values.end();
|
|
}
|
|
|
|
void IndexEntry :: write(file &file)
|
|
{
|
|
std::set<unsigned int>::iterator it;
|
|
file_writef(&file, "%s\n%zu ", _key.c_str(), _values.size());
|
|
for (it = _values.begin(); it != _values.end(); it++)
|
|
file_writef(&file, "%u ", *it);
|
|
}
|
|
|
|
void IndexEntry :: read(file &file)
|
|
{
|
|
unsigned int num, val;
|
|
gchar *key = file_readl(&file);
|
|
|
|
file_readf(&file, "%u", &num);
|
|
_key = key;
|
|
g_free(key);
|
|
|
|
for (unsigned int i = 0; i < num; i++) {
|
|
file_readf(&file, "%u", &val);
|
|
insert(val);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Index :: Index(const std::string &filepath, bool autosave)
|
|
: database(filepath, autosave)
|
|
{}
|
|
|
|
void Index :: insert(const std::string &key, unsigned int value)
|
|
{
|
|
IndexEntry *it = find(key);
|
|
if (it == NULL)
|
|
it = db_insert(this, new IndexEntry(key));
|
|
|
|
it->insert(value);
|
|
::db_autosave(this);
|
|
}
|
|
|
|
void Index :: remove(const std::string &key, unsigned int value)
|
|
{
|
|
IndexEntry *it = find(key);
|
|
|
|
if (it == NULL)
|
|
return;
|
|
|
|
it->remove(value);
|
|
::db_autosave(this);
|
|
}
|