Commit e9595abe authored by Davis King's avatar Davis King

Cleaned up more code and added yet more specs.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403593
parent d565ca6a
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "../string.h" #include "../string.h"
#include "../rand.h" #include "../rand.h"
#include <algorithm> #include <algorithm>
#include "sample_pair.h"
namespace dlib namespace dlib
{ {
...@@ -24,8 +25,8 @@ namespace dlib ...@@ -24,8 +25,8 @@ namespace dlib
) )
/*! /*!
ensures ensures
- returns an iterator that points to the element in the given range that has the biggest - returns an iterator that points to the element in the given range
distance that has the biggest distance
!*/ !*/
{ {
float dist = begin->distance(); float dist = begin->distance();
...@@ -60,13 +61,6 @@ namespace dlib ...@@ -60,13 +61,6 @@ namespace dlib
const T& random_seed, const T& random_seed,
std::vector<sample_pair, alloc>& out std::vector<sample_pair, alloc>& out
) )
/*!
requires
- samples.size() > 1
- 0 < percent <= 1
- num > 0
- random_seed must be convertible to a string by dlib::cast_to_string()
!*/
{ {
std::vector<sample_pair, alloc> edges; std::vector<sample_pair, alloc> edges;
edges.reserve(num); edges.reserve(num);
...@@ -121,11 +115,6 @@ namespace dlib ...@@ -121,11 +115,6 @@ namespace dlib
const unsigned long k, const unsigned long k,
std::vector<sample_pair, alloc>& out std::vector<sample_pair, alloc>& out
) )
/*!
requires
- samples.size() > k
- k > 0
!*/
{ {
using namespace impl; using namespace impl;
std::vector<sample_pair> edges; std::vector<sample_pair> edges;
...@@ -188,6 +177,28 @@ namespace dlib ...@@ -188,6 +177,28 @@ namespace dlib
} }
// ----------------------------------------------------------------------------------------
template <
typename alloc
>
bool contains_duplicate_pairs (
const std::vector<sample_pair, alloc>& pairs
)
{
std::vector<sample_pair, alloc> temp(pairs);
std::sort(temp.begin(), temp.end(), &order_by_index);
for (unsigned long i = 1; i < temp.size(); ++i)
{
// if we found a duplicate
if (temp[i-1] == temp[i])
return true;
}
return false;
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
} }
......
// Copyright (C) 2010 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_GRAPH_CrEATION_ABSTRACT_H__
#ifdef DLIB_GRAPH_CrEATION_ABSTRACT_H__
#include <vector>
#include "../string.h"
#include "sample_pair_abstract.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
template <
typename vector_type,
typename distance_function_type,
typename alloc,
typename T
>
void find_percent_shortest_edges_randomly (
const vector_type& samples,
const distance_function_type& dist_funct,
const double percent,
const unsigned long num,
const T& random_seed,
std::vector<sample_pair, alloc>& out
);
/*!
requires
- samples.size() > 1
- 0 < percent <= 1
- num > 0
- random_seed must be convertible to a string by dlib::cast_to_string()
- dist_funct(samples[i], samples[j]) must be a valid expression that evaluates
to a floating point number
ensures
- This function randomly samples the space of pairs of integers between
0 and samples.size()-1 inclusive. For each of these pairs, (i,j), a
sample_pair is created as follows:
sample_pair(i, j, dist_funct(samples[i], samples[j]))
num such sample_pair objects are generated, duplicates are removed, and
then the top percent of them with the smallest distance are stored into
out.
- #out.size() <= num*percent
- contains_duplicate_pairs(#out) == false
- for all valid i:
- #out[i].distance() == dist_funct(samples[#out[i].index1()], samples[#out[i].index2()])
- random_seed is used to seed the random number generator used by this
function.
!*/
// ----------------------------------------------------------------------------------------
template <
typename vector_type,
typename distance_function_type,
typename alloc
>
void find_k_nearest_neighbors (
const vector_type& samples,
const distance_function_type& dist_funct,
const unsigned long k,
std::vector<sample_pair, alloc>& out
);
/*!
requires
- samples.size() > k
- k > 0
- dist_funct(samples[i], samples[j]) must be a valid expression that evaluates
to a floating point number
ensures
- #out == a set of sample_pair objects that represent all the k nearest
neighbors in samples according to the given distance function dist_funct.
- for all valid i:
- #out[i].distance() == dist_funct(samples[#out[i].index1()], samples[#out[i].index2()])
- contains_duplicate_pairs(#out) == false
!*/
// ----------------------------------------------------------------------------------------
template <
typename alloc
>
bool contains_duplicate_pairs (
const std::vector<sample_pair, alloc>& pairs
);
/*!
ensures
- if (pairs contains any elements that are equal according to operator==) then
- returns true
- else
- returns false
!*/
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_GRAPH_CrEATION_ABSTRACT_H__
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment