ocarina/include/core/index.h

138 lines
3.2 KiB
C++

/**
* Copyright 2014 (c) Anna Schumaker.
*/
#ifndef OCARINA_CORE_INDEX_H
#define OCARINA_CORE_INDEX_H
#include <core/database.h>
#include <set>
#include <string>
/**
* The IndexEntry class is used to associate a specific key with a set of
* integer identifiers. This lets us use a Database as an inverted index.
*/
class IndexEntry : public DatabaseEntry {
private:
std::string _key; /**< The term stored by this IndexEntry. */
std::set<unsigned int> _values; /**< Integers representing strings that
contain this term. */
public:
/** 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;
IndexEntry(); /**< Create an empty IndexEntry. */
/**
* Create an IndexEntry with a specific key.
*
* @param key The key associated with this IndexEntry.
*/
IndexEntry(const std::string &);
/**
* Access the key stored by this IndexEntry.
*
* @return IndexEntry::_key.
*/
const std::string primary_key() const;
/**
* Add a new value to this IndexEntry.
*
* @param value The new value to add.
*/
void insert(unsigned int);
/**
* Remove a value from this IndexEntry.
*
* @param value The value to remove.
*/
void remove(unsigned int);
/**
* 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();
/**
* Write an IndexEntry to file.
*
* @param file The file to use when writing data.
*/
void write(File &);
/**
* Read an IndexEntry from file.
*
* @param file The file read from.
*/
void read(File &);
};
/**
* An Index is a special Database for mapping std::strings to a std::set of
* integer identifiers.
*/
class Index : public Database<IndexEntry> {
public:
/**
* Index constructor. This simply passes the filepath and autosave
* parameters to the Database constructor.
*
* @param filepath Where to store the file on disk.
* @param autosave True if changes should automatically be saved.
*/
Index(const std::string &, bool);
/**
* Check if we already have an IndexEntry for key, and create one if
* we don't. Insert value into the found or created IndexEntry and
* call autosave() to save the Index.
*
* @param key String to store in the index.
* @param value Integer value associated with the key.
*/
void insert(const std::string &, unsigned int);
/**
* Remove value from the IndexEntry corresponding to key. The
* IndexEntry will not be removed even if it has 0 values left.
*
* @param key Key associated with the value to be removed.
* @param value Value to remove from the index.
*/
void remove(const std::string &, unsigned int);
};
#endif /* OCARINA_CORE_DATABASE_H */