ocarina/lib/index.cpp

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()
{
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)
{
iterator it = find(key);
if (it == end())
it = at(Database :: insert(IndexEntry(key)));
it->insert(val);
autosave();
}
void Index :: remove(const std::string &key, unsigned int val)
{
iterator it = find(key);
if (it == end())
return;
it->remove(val);
autosave();
}