MetricKnn API
Fast Similarity Search using the Metric Space Approach
Public Member Functions | Static Public Member Functions | Protected Attributes | Friends | List of all members
mknn::Distance Class Reference

The Distance is the object with the definition of the method for comparing objects. More...

#include <mknn_distance.hpp>

Public Member Functions

DistanceParamsgetParameters ()
 Return the parameters used to create the distance. More...
 
std::string getIdPredefinedDistance ()
 The id of the predefined distance. More...
 
DistanceEval newDistanceEval (Domain domain_left, Domain domain_right)
 Creates a distance for computing distances between objects of the given domains. More...
 
 Distance ()
 Default constructor.
 
virtual ~Distance ()
 Default destructor.
 
 Distance (const Distance &other)
 Copy constructor.
 
Distanceoperator= (const Distance &other)
 Assignment operator.
 

Static Public Member Functions

static Distance newPredefined (DistanceParams &parameters)
 Creates a new distance for the given parameters. More...
 
static Distance newCustom (DistanceCustomFactory *factory, bool delete_factory_on_distance_release)
 Creates a new custom function. More...
 

Protected Attributes

std::unique_ptr< Impl > pimpl
 Internal opaque class.
 

Friends

class Domain
 
class Index
 

Load/Save distances

static Distance restore (std::string filename_read, DistanceParams &more_parameters)
 Loads a distance from a file. More...
 
void save (std::string filename_write)
 The distance is saved to a file. More...
 

Detailed Description

The Distance is the object with the definition of the method for comparing objects.

A distance object can be instantiated in three different ways:

1) Using one of the constructors for predefined distances in PredefinedDistance class.

2) Using function pointers and define a custom distance, see Distance::newCustom.

3) Using the generic method newPredefined, which requires a DistanceParams object. The parameters can be created by parsing a string of parameters (e.g. a command line parameter), by setting each required parameter, or by loading a configuration file. Each pre-defined distance is identified by a unique distance identifier. Some valid IDs are:

The complete list of pre-defined distances provided by MetricKnn can be printed by calling PredefDistance::helpListDistances. The parameters supported by each distance can be printed by calling PredefDistance::helpPrintDistance.

Depending on the distance, the constructor method may take some time to complete. In order to reduce the build time, a distance can be saved and then loaded.

Each predefined distance requires the objects to compare be in a given general domain, i.e., vectors, strings or multi object.

In order to compute distance values, the Distance object must be used to instantiate one or more DistanceEval objects (one for each parallel thread). The DistanceEval object is used to compute the distance value between two objects.

The DistanceEval is created by the method mknn::Distance::newDistanceEval, which requires the actual domain of the objects to be compared.

A search method uses the Distance to create as many DistanceEval as threads will be used to solve the search. During the search, each thread uses a single DistanceEval.

Member Function Documentation

std::string mknn::Distance::getIdPredefinedDistance ( )

The id of the predefined distance.

Returns
a string with an id or NULL if it is a custom distance.
DistanceParams& mknn::Distance::getParameters ( )

Return the parameters used to create the distance.

Returns
the parameters used to create the distance.
static Distance mknn::Distance::newCustom ( DistanceCustomFactory factory,
bool  delete_factory_on_distance_release 
)
static

Creates a new custom function.

When resolving a search using N parallel threads, the factory is invoked N times (thus creating a state for each thread), then each thread invokes mknn::DistanceCustomInstance::evalDistance.

Parameters
factorythe object that will be used to obtain instances of mknn::DistanceCustomInstance
delete_factory_on_distance_releasebinds the lifetime of factory to the new distance object
Returns
a new distance object
DistanceEval mknn::Distance::newDistanceEval ( Domain  domain_left,
Domain  domain_right 
)

Creates a distance for computing distances between objects of the given domains.

domain_object1 and domain_object2 are the domains of the objects that will be used in DistanceEval::eval. These two domains must be compatible with the domain of distance, i.e., they may differ only in the datatype (see Domain::testEqualExceptDatatype).

Parameters
domain_leftthe domain of the left object
domain_rightthe domain of the right object
Returns
a new distance (it must be released with delete).
static Distance mknn::Distance::newPredefined ( DistanceParams parameters)
static

Creates a new distance for the given parameters.

The list of pre-defined distances can be listed by invoking PredefDistance::helpListDistances.

Parameters
parametersthe parameters to create the distance.
delete_parameters_on_distance_releasebinds the lifetime of parameters to the new distance.
Returns
a new distance (must be released)
static Distance mknn::Distance::restore ( std::string  filename_read,
DistanceParams more_parameters 
)
static

Loads a distance from a file.

The file must have been created by save.

Parameters
filename_readFile to read. If the file does not exists an error is raised.
more_parametersany parameter that may override the stored parameters or new parameters that could not be saved.
delete_parameters_on_distance_releasebinds the lifetime of more_parameters to the new distance.
Returns
a new distance (it must be released with delete).
void mknn::Distance::save ( std::string  filename_write)

The distance is saved to a file.

It may create other files using filename_write as prefix. The created files may be in binary format for fast loading.

Remarks
A custom distance cannot be saved.
Parameters
filename_writeFile to create. If the file already exists it is overwritten.

The documentation for this class was generated from the following file:
Powered by Download MetricKnn