2014-10-17 10:37:13 -04:00
|
|
|
/**
|
2014-03-12 20:23:34 -04:00
|
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
|
|
*/
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/index.h>
|
2014-03-12 20:23:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
IndexEntry :: IndexEntry() {}
|
2014-11-04 08:21:14 -05:00
|
|
|
IndexEntry :: IndexEntry(const std::string &key)
|
|
|
|
: _key(key)
|
2014-03-12 20:23:34 -04:00
|
|
|
{}
|
|
|
|
|
2014-03-25 17:51:37 -04:00
|
|
|
const std::string IndexEntry :: primary_key() const
|
2014-03-12 20:23:34 -04:00
|
|
|
{
|
2014-11-04 08:21:14 -05:00
|
|
|
return _key;
|
2014-03-12 20:23:34 -04:00
|
|
|
}
|
|
|
|
|
2014-11-04 08:21:14 -05:00
|
|
|
void IndexEntry :: insert(unsigned int value)
|
2014-03-12 20:23:34 -04:00
|
|
|
{
|
2014-11-04 08:21:14 -05:00
|
|
|
_values.insert(value);
|
2014-03-12 20:23:34 -04:00
|
|
|
}
|
|
|
|
|
2014-11-04 08:21:14 -05:00
|
|
|
void IndexEntry :: remove(unsigned int value)
|
2014-03-12 20:23:34 -04:00
|
|
|
{
|
2014-11-04 08:21:14 -05:00
|
|
|
_values.erase(value);
|
2014-03-12 20:23:34 -04:00
|
|
|
}
|
|
|
|
|
2014-11-06 08:24:13 -05:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-09-10 08:00:42 -04:00
|
|
|
void IndexEntry :: write(file &file)
|
2014-03-12 20:23:34 -04:00
|
|
|
{
|
|
|
|
std::set<unsigned int>::iterator it;
|
2015-10-08 09:41:51 -04:00
|
|
|
file_writef(&file, "%s\n%zu ", _key.c_str(), _values.size());
|
2014-11-04 08:21:14 -05:00
|
|
|
for (it = _values.begin(); it != _values.end(); it++)
|
2015-10-08 09:41:51 -04:00
|
|
|
file_writef(&file, "%u ", *it);
|
2014-03-12 20:23:34 -04:00
|
|
|
}
|
|
|
|
|
2015-09-10 08:00:42 -04:00
|
|
|
void IndexEntry :: read(file &file)
|
2014-03-12 20:23:34 -04:00
|
|
|
{
|
|
|
|
unsigned int num, val;
|
2015-10-09 13:09:47 -04:00
|
|
|
gchar *key = file_readl(&file);
|
2014-03-12 20:23:34 -04:00
|
|
|
|
2015-10-08 11:16:38 -04:00
|
|
|
file_readf(&file, "%u", &num);
|
2015-10-09 13:09:47 -04:00
|
|
|
_key = key;
|
|
|
|
g_free(key);
|
2015-10-08 11:16:38 -04:00
|
|
|
|
2014-03-12 20:23:34 -04:00
|
|
|
for (unsigned int i = 0; i < num; i++) {
|
2015-10-08 11:16:38 -04:00
|
|
|
file_readf(&file, "%u", &val);
|
2014-03-12 20:23:34 -04:00
|
|
|
insert(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Index :: Index(const std::string &filepath, bool autosave)
|
2015-10-17 10:07:58 -04:00
|
|
|
: database(filepath, autosave)
|
2014-03-12 20:23:34 -04:00
|
|
|
{}
|
|
|
|
|
2014-11-07 08:12:55 -05:00
|
|
|
void Index :: insert(const std::string &key, unsigned int value)
|
2014-03-12 20:23:34 -04:00
|
|
|
{
|
2014-03-24 19:51:13 -04:00
|
|
|
IndexEntry *it = find(key);
|
|
|
|
if (it == NULL)
|
2015-10-18 10:59:20 -04:00
|
|
|
it = db_insert(this, new IndexEntry(key));
|
2014-03-12 20:23:34 -04:00
|
|
|
|
2014-11-07 08:12:55 -05:00
|
|
|
it->insert(value);
|
2015-10-18 10:11:24 -04:00
|
|
|
::db_autosave(this);
|
2014-03-12 20:23:34 -04:00
|
|
|
}
|
|
|
|
|
2014-11-07 08:12:55 -05:00
|
|
|
void Index :: remove(const std::string &key, unsigned int value)
|
2014-03-12 20:23:34 -04:00
|
|
|
{
|
2014-03-24 19:51:13 -04:00
|
|
|
IndexEntry *it = find(key);
|
2014-03-12 20:23:34 -04:00
|
|
|
|
2014-03-24 19:51:13 -04:00
|
|
|
if (it == NULL)
|
2014-03-12 20:23:34 -04:00
|
|
|
return;
|
|
|
|
|
2014-11-07 08:12:55 -05:00
|
|
|
it->remove(value);
|
2015-10-18 10:11:24 -04:00
|
|
|
::db_autosave(this);
|
2014-03-12 20:23:34 -04:00
|
|
|
}
|