2014-10-17 10:37:13 -04:00
|
|
|
/**
|
|
|
|
* @file
|
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
|
|
|
/**
|
2014-11-04 08:21:14 -05:00
|
|
|
* 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.
|
2014-10-17 10:37:13 -04:00
|
|
|
*/
|
2014-03-12 20:23:34 -04:00
|
|
|
class IndexEntry : public DatabaseEntry {
|
2014-11-04 08:27:36 -05:00
|
|
|
private:
|
2014-11-04 08:21:14 -05:00
|
|
|
std::string _key; /**< The term stored by this IndexEntry. */
|
|
|
|
std::set<unsigned int> _values; /**< Integers representing strings that
|
|
|
|
contain this term. */
|
2014-03-12 20:23:34 -04:00
|
|
|
|
2014-11-06 08:24:13 -05:00
|
|
|
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;
|
|
|
|
|
2014-11-04 08:21:14 -05:00
|
|
|
IndexEntry(); /**< 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.
|
|
|
|
*/
|
2014-03-12 20:23:34 -04:00
|
|
|
IndexEntry(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-04 08:21:14 -05:00
|
|
|
* Add a new value to this IndexEntry.
|
|
|
|
*
|
2014-10-17 10:37:13 -04:00
|
|
|
* @param value The new value to add.
|
|
|
|
*/
|
2014-03-12 20:23:34 -04:00
|
|
|
void insert(unsigned int);
|
2014-10-17 10:37:13 -04:00
|
|
|
|
|
|
|
/**
|
2014-11-04 08:21:14 -05:00
|
|
|
* Remove a value from this IndexEntry.
|
|
|
|
*
|
2014-10-17 10:37:13 -04:00
|
|
|
* @param value The value to remove.
|
|
|
|
*/
|
2014-03-12 20:23:34 -04:00
|
|
|
void remove(unsigned int);
|
|
|
|
|
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.
|
|
|
|
*/
|
2014-03-12 20:23:34 -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.
|
|
|
|
*/
|
2014-03-12 20:23:34 -04:00
|
|
|
void read(File &);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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
|
|
|
*/
|
2014-03-12 20:23:34 -04:00
|
|
|
class Index : public Database<IndexEntry> {
|
|
|
|
public:
|
2014-10-17 10:37:13 -04:00
|
|
|
/**
|
2014-11-07 08:12:55 -05:00
|
|
|
* Index constructor. This simply passes the filepath and autosave
|
|
|
|
* parameters to the Database constructor.
|
|
|
|
*
|
2014-10-17 10:37:13 -04:00
|
|
|
* @param filepath Where to store the file on disk.
|
|
|
|
* @param autosave True if changes should automatically be saved.
|
|
|
|
*/
|
2014-03-12 20:23:34 -04:00
|
|
|
Index(const std::string &, bool);
|
|
|
|
|
2014-10-17 10:37:13 -04:00
|
|
|
/**
|
2014-11-07 08:12:55 -05:00
|
|
|
* 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.
|
2014-10-17 10:37:13 -04:00
|
|
|
* @param value Integer value associated with the key.
|
|
|
|
*/
|
2014-03-12 20:23:34 -04:00
|
|
|
void insert(const std::string &, unsigned int);
|
2014-10-17 10:37:13 -04:00
|
|
|
|
|
|
|
/**
|
2014-11-07 08:12:55 -05:00
|
|
|
* 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.
|
2014-10-17 10:37:13 -04:00
|
|
|
* @param value Value to remove from the index.
|
|
|
|
*/
|
2014-03-12 20:23:34 -04:00
|
|
|
void remove(const std::string &, unsigned int);
|
|
|
|
};
|
|
|
|
|
2014-06-21 09:37:47 -04:00
|
|
|
#endif /* OCARINA_CORE_DATABASE_H */
|