2014-10-17 10:37:13 -04:00
|
|
|
/**
|
2014-03-12 20:23:34 -04:00
|
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
|
|
*/
|
2014-06-21 09:37:47 -04:00
|
|
|
#ifndef OCARINA_CORE_INDEX_H
|
|
|
|
#define OCARINA_CORE_INDEX_H
|
2014-03-12 20:23:34 -04:00
|
|
|
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/database.h>
|
2014-03-12 20:23:34 -04:00
|
|
|
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
2014-10-17 10:37:13 -04:00
|
|
|
/**
|
2015-10-21 10:41:42 -04:00
|
|
|
* The index_entry struct is used to associate a specific key with a set of
|
2014-11-04 08:21:14 -05:00
|
|
|
* integer identifiers. This lets us use a Database as an inverted index.
|
2014-10-17 10:37:13 -04:00
|
|
|
*/
|
2015-10-21 10:41:42 -04:00
|
|
|
struct index_entry : public DatabaseEntry {
|
|
|
|
std::string ie_key;
|
|
|
|
std::set<unsigned int> ie_set;
|
2014-03-12 20:23:34 -04:00
|
|
|
|
2014-11-06 08:24:13 -05:00
|
|
|
/** Iterator access for our backing std::set */
|
|
|
|
typedef typename std::set<unsigned int>::iterator iterator;
|
|
|
|
/** Const iterator access for our backing std::set */
|
|
|
|
typedef typename std::set<unsigned int>::const_iterator const_iterator;
|
|
|
|
|
2015-10-21 10:41:42 -04:00
|
|
|
index_entry(); /**< Create an empty IndexEntry. */
|
2014-10-17 10:37:13 -04:00
|
|
|
|
|
|
|
/**
|
2014-11-04 08:21:14 -05:00
|
|
|
* Create an IndexEntry with a specific key.
|
|
|
|
*
|
2014-10-17 10:37:13 -04:00
|
|
|
* @param key The key associated with this IndexEntry.
|
|
|
|
*/
|
2015-10-21 10:41:42 -04:00
|
|
|
index_entry(const std::string &);
|
2014-10-17 10:37:13 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-11-04 08:21:14 -05:00
|
|
|
* Access the key stored by this IndexEntry.
|
|
|
|
*
|
|
|
|
* @return IndexEntry::_key.
|
2014-10-17 10:37:13 -04:00
|
|
|
*/
|
2014-03-25 17:51:37 -04:00
|
|
|
const std::string primary_key() const;
|
2014-10-17 10:37:13 -04:00
|
|
|
|
2014-11-06 08:24:13 -05:00
|
|
|
/**
|
|
|
|
* Called to find the number of values in our backing std::set.
|
|
|
|
*
|
|
|
|
* @return IndexEntry::_values.size()
|
|
|
|
*/
|
|
|
|
size_t size();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called to check if a specific value is stored in this IndexEntry.
|
|
|
|
*
|
|
|
|
* @param value The value to find.
|
|
|
|
* @return True if the value was found and false otherwise.
|
|
|
|
*/
|
|
|
|
bool has(unsigned int);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return An iterator pointing to the first item in IndexEntry::_values
|
|
|
|
*/
|
|
|
|
iterator begin();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return An iterator pointing past the last item in IndexEntry::_vaues
|
|
|
|
*/
|
|
|
|
iterator end();
|
|
|
|
|
2014-10-17 10:37:13 -04:00
|
|
|
|
|
|
|
/**
|
2014-11-04 08:21:14 -05:00
|
|
|
* Write an IndexEntry to file.
|
|
|
|
*
|
2014-10-17 10:37:13 -04:00
|
|
|
* @param file The file to use when writing data.
|
|
|
|
*/
|
2015-09-10 08:00:42 -04:00
|
|
|
void write(file &);
|
2014-10-17 10:37:13 -04:00
|
|
|
|
|
|
|
/**
|
2014-11-04 08:21:14 -05:00
|
|
|
* Read an IndexEntry from file.
|
|
|
|
*
|
2014-10-17 10:37:13 -04:00
|
|
|
* @param file The file read from.
|
|
|
|
*/
|
2015-09-10 08:00:42 -04:00
|
|
|
void read(file &);
|
2014-03-12 20:23:34 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-10-17 10:37:13 -04:00
|
|
|
/**
|
2014-11-07 08:12:55 -05:00
|
|
|
* An Index is a special Database for mapping std::strings to a std::set of
|
|
|
|
* integer identifiers.
|
2014-10-17 10:37:13 -04:00
|
|
|
*/
|
2015-09-29 16:25:16 -04:00
|
|
|
class Index : public database<index_entry> {};
|
2014-03-12 20:23:34 -04:00
|
|
|
|
2015-09-29 16:25:16 -04:00
|
|
|
/* Initialize an Index. */
|
|
|
|
void index_init(Index *, const gchar *, bool);
|
2015-09-29 16:11:59 -04:00
|
|
|
|
|
|
|
/* Add a value to an index item with the specified key. */
|
|
|
|
index_entry *index_insert(Index *, const gchar *, unsigned int);
|
|
|
|
|
2015-09-29 16:07:34 -04:00
|
|
|
/* Remove a value from an index item with the specified key. */
|
|
|
|
void index_remove(Index *, const gchar *, unsigned int);
|
|
|
|
|
2014-06-21 09:37:47 -04:00
|
|
|
#endif /* OCARINA_CORE_DATABASE_H */
|