ocarina/core/index.cpp
Anna Schumaker 95064e4537 Rename lib/ -> core/
I plan to introduce a new lib/ that sits between the gui and the backend
files (similar to how glibc sits between the kernel and userspace).
This gets the rename out of the way before I change my mind again.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
2014-06-05 10:21:32 -04:00

72 lines
1.1 KiB
C++

/*
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/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();
}