ocarina/core/index.cpp

92 lines
1.5 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 << _key << std::endl << _values.size() << " ";
for (it = _values.begin(); it != _values.end(); it++)
file << *it << " ";
}
void IndexEntry :: read(File &file)
{
unsigned int num, val;
file >> _key >> num;
for (unsigned int i = 0; i < num; i++) {
file >> 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 = Database :: insert(IndexEntry(key));
it->insert(value);
autosave();
}
void Index :: remove(const std::string &key, unsigned int value)
{
IndexEntry *it = find(key);
if (it == NULL)
return;
it->remove(value);
autosave();
}