2013-12-06 22:29:25 +01:00
|
|
|
#ifndef UTILS_HDR
|
|
|
|
#define UTILS_HDR
|
|
|
|
|
|
|
|
#include <boost/shared_ptr.hpp>
|
2014-05-15 22:20:31 +02:00
|
|
|
#include <boost/foreach.hpp>
|
2013-12-06 22:29:25 +01:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "concordia/common/config.hpp"
|
|
|
|
#include "concordia/concordia_exception.hpp"
|
2015-04-21 15:14:48 +02:00
|
|
|
#include "concordia/interval.hpp"
|
|
|
|
#include "concordia/matched_pattern_fragment.hpp"
|
2014-04-29 14:46:04 +02:00
|
|
|
#include <divsufsort.h>
|
2013-12-06 22:29:25 +01:00
|
|
|
|
2015-04-30 22:22:54 +02:00
|
|
|
/*!
|
|
|
|
Class offering a range of simple utility methods.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2013-12-06 22:29:25 +01:00
|
|
|
class Utils {
|
|
|
|
public:
|
2015-04-30 22:22:54 +02:00
|
|
|
/*! Constructor
|
|
|
|
*/
|
2013-12-06 22:29:25 +01:00
|
|
|
explicit Utils();
|
|
|
|
|
|
|
|
/*! Destructor.
|
|
|
|
*/
|
|
|
|
virtual ~Utils();
|
|
|
|
|
2015-04-30 22:22:54 +02:00
|
|
|
/*! Writes an index character to a binary file.
|
|
|
|
The character is written to the file as bytes, not necessarily in
|
|
|
|
the order in which they come in the character.
|
|
|
|
\param file file to write the character to
|
|
|
|
\param character character to write
|
|
|
|
*/
|
2015-04-15 14:14:10 +02:00
|
|
|
static void writeIndexCharacter(std::ofstream & file,
|
2013-12-06 22:29:25 +01:00
|
|
|
INDEX_CHARACTER_TYPE character);
|
|
|
|
|
2015-04-30 22:22:54 +02:00
|
|
|
/*! Writes a marker to a binary file.
|
|
|
|
The marker is written to the file as bytes, not necessarily in
|
|
|
|
the order in which they come in the marker.
|
|
|
|
\param file file to write the marker to
|
|
|
|
\param marker marker to write
|
|
|
|
*/
|
2015-04-15 14:14:10 +02:00
|
|
|
static void writeMarker(std::ofstream & file,
|
2014-02-20 10:49:17 +01:00
|
|
|
SUFFIX_MARKER_TYPE marker);
|
|
|
|
|
2015-04-30 22:22:54 +02:00
|
|
|
/*! Reads an index character from a binary file.
|
|
|
|
Reading restores the order of the bytes in the original
|
|
|
|
character, which was written to the file.
|
|
|
|
\param file file to read the character from
|
|
|
|
\returns read character
|
|
|
|
*/
|
2015-04-15 14:14:10 +02:00
|
|
|
static INDEX_CHARACTER_TYPE readIndexCharacter(std::ifstream & file);
|
2013-12-06 22:29:25 +01:00
|
|
|
|
2015-04-30 22:22:54 +02:00
|
|
|
/*! Reads a marker from a binary file.
|
|
|
|
Reading restores the order of the bytes in the original
|
|
|
|
marker, which was written to the file.
|
|
|
|
\param file file to read the marker from
|
|
|
|
\returns read marker
|
|
|
|
*/
|
2015-04-15 14:14:10 +02:00
|
|
|
static SUFFIX_MARKER_TYPE readMarker(std::ifstream & file);
|
2014-02-20 10:49:17 +01:00
|
|
|
|
2015-04-30 22:22:54 +02:00
|
|
|
/*! Converts a vector of index characters to higher resolution array.
|
|
|
|
\param input vector of index characters
|
|
|
|
\returns array of smaller parts of characters
|
|
|
|
*/
|
2013-12-06 22:29:25 +01:00
|
|
|
static sauchar_t * indexVectorToSaucharArray(
|
2015-04-15 14:14:10 +02:00
|
|
|
const std::vector<INDEX_CHARACTER_TYPE> & input);
|
2013-12-06 22:29:25 +01:00
|
|
|
|
2015-04-30 22:22:54 +02:00
|
|
|
/*! Converts a vector of index characters to higher resolution vector.
|
|
|
|
\param input vector of index characters
|
|
|
|
\returns vector of smaller parts of characters
|
|
|
|
*/
|
2015-04-15 10:55:26 +02:00
|
|
|
static std::vector<sauchar_t> indexVectorToSaucharVector(
|
2015-04-15 14:14:10 +02:00
|
|
|
const std::vector<INDEX_CHARACTER_TYPE> & input);
|
2014-06-24 18:23:46 +02:00
|
|
|
|
2015-04-30 22:22:54 +02:00
|
|
|
/*! Appends an index character to a high resolution vector.
|
|
|
|
\param vector vector to be appended to
|
|
|
|
\param character character to append
|
|
|
|
*/
|
2013-12-14 15:23:17 +01:00
|
|
|
static void appendCharToSaucharVector(
|
|
|
|
boost::shared_ptr<std::vector<sauchar_t> > vector,
|
|
|
|
INDEX_CHARACTER_TYPE character);
|
2014-02-20 10:49:17 +01:00
|
|
|
|
2015-04-30 22:22:54 +02:00
|
|
|
/*! Appends an index character to a high resolution vector.
|
|
|
|
\param vector vector to be appended to
|
|
|
|
\param character character to append
|
|
|
|
*/
|
2015-04-15 10:55:26 +02:00
|
|
|
static void appendCharToSaucharVector(
|
|
|
|
std::vector<sauchar_t> & vector,
|
|
|
|
INDEX_CHARACTER_TYPE character);
|
|
|
|
|
2015-04-30 22:22:54 +02:00
|
|
|
/*! Prints a given vector to standard output.
|
|
|
|
\param vector vector to be printed
|
|
|
|
*/
|
2014-05-15 22:20:31 +02:00
|
|
|
template <typename T>
|
2015-04-15 10:55:26 +02:00
|
|
|
static void printVector(const std::vector<T> & vector);
|
2014-05-15 22:20:31 +02:00
|
|
|
|
2015-04-30 22:22:54 +02:00
|
|
|
/*! Retrieves sentence id from a marker.
|
|
|
|
\param marker input marker
|
|
|
|
\returns sentence id
|
|
|
|
*/
|
2015-04-09 22:17:19 +02:00
|
|
|
static SUFFIX_MARKER_TYPE getIdFromMarker(SUFFIX_MARKER_TYPE marker);
|
|
|
|
|
2015-04-30 22:22:54 +02:00
|
|
|
/*! Retrieves offset from a marker.
|
|
|
|
\param marker input marker
|
|
|
|
\returns offset
|
|
|
|
*/
|
2015-04-09 22:17:19 +02:00
|
|
|
static SUFFIX_MARKER_TYPE getOffsetFromMarker(SUFFIX_MARKER_TYPE marker);
|
|
|
|
|
2015-04-30 22:22:54 +02:00
|
|
|
/*! Retrieves example length from a marker.
|
|
|
|
\param marker input marker
|
|
|
|
\returns example length
|
|
|
|
*/
|
2015-04-09 22:17:19 +02:00
|
|
|
static SUFFIX_MARKER_TYPE getLengthFromMarker(SUFFIX_MARKER_TYPE marker);
|
|
|
|
|
2015-04-30 22:22:54 +02:00
|
|
|
/*! Creates a marker from given data.
|
|
|
|
\param id sentence id
|
|
|
|
\param offset offset
|
|
|
|
\param length example length
|
|
|
|
\returns generated marker
|
|
|
|
*/
|
2015-04-09 22:17:19 +02:00
|
|
|
static SUFFIX_MARKER_TYPE createMarker(SUFFIX_MARKER_TYPE id,
|
|
|
|
SUFFIX_MARKER_TYPE offset,
|
|
|
|
SUFFIX_MARKER_TYPE length);
|
|
|
|
|
2015-04-30 22:22:54 +02:00
|
|
|
/*! Computes overlay score based on a list of non-intersecting intervals.
|
|
|
|
\param intervalList list of the intervals
|
|
|
|
\param sentenceSize the total size of the sentence (or pattern)
|
|
|
|
\param k significance factor. When set to 1, the significance is neutral.
|
|
|
|
\returns score
|
|
|
|
*/
|
2015-04-21 15:14:48 +02:00
|
|
|
static double getLogarithmicOverlay(
|
|
|
|
const std::vector<Interval> & intervalList,
|
|
|
|
SUFFIX_MARKER_TYPE sentenceSize,
|
|
|
|
double k);
|
|
|
|
|
2015-04-30 22:22:54 +02:00
|
|
|
/*! Computes overlay score based on a list of non-intersecting fragments.
|
|
|
|
\param intervalList list of the intervals
|
|
|
|
\param sentenceSize the total size of the sentence (or pattern)
|
|
|
|
\param k significance factor. When set to 1, the significance is neutral.
|
|
|
|
\returns score
|
|
|
|
*/
|
2015-04-21 15:14:48 +02:00
|
|
|
static double getLogarithmicOverlay(
|
|
|
|
const std::vector<MatchedPatternFragment> & fragmentList,
|
|
|
|
SUFFIX_MARKER_TYPE patternSize,
|
|
|
|
double k);
|
|
|
|
|
2015-04-30 22:22:54 +02:00
|
|
|
/*! Field holding the maximum sentence size allowed in the index.
|
|
|
|
*/
|
2015-04-09 22:17:19 +02:00
|
|
|
static SUFFIX_MARKER_TYPE maxSentenceSize;
|
|
|
|
|
2013-12-06 22:29:25 +01:00
|
|
|
private:
|
2013-12-14 15:23:17 +01:00
|
|
|
static void _insertCharToSaucharArray(sauchar_t * array,
|
|
|
|
INDEX_CHARACTER_TYPE character, int pos);
|
2015-04-09 22:17:19 +02:00
|
|
|
|
|
|
|
static int _idBytes;
|
2013-12-06 22:29:25 +01:00
|
|
|
};
|
|
|
|
|
2014-05-15 22:20:31 +02:00
|
|
|
template <typename T>
|
2015-04-15 10:55:26 +02:00
|
|
|
void Utils::printVector(const std::vector<T> & vector) {
|
|
|
|
for (int i = 0; i < vector.size(); i++) {
|
2015-04-15 14:14:10 +02:00
|
|
|
std::cout << static_cast<int>(vector.at(i)) << " ";
|
2014-05-15 22:20:31 +02:00
|
|
|
}
|
2015-04-15 14:14:10 +02:00
|
|
|
std::cout << std::endl;
|
2014-05-15 22:20:31 +02:00
|
|
|
}
|
2013-12-06 22:29:25 +01:00
|
|
|
#endif
|