File size: 4,125 Bytes
9375c9a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
// Copyright (C) 2010 Davis E. King ([email protected])
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_SAMPLE_PaIR_Hh_
#define DLIB_SAMPLE_PaIR_Hh_
#include "sample_pair_abstract.h"
#include <limits>
#include "../serialize.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
class sample_pair
{
public:
sample_pair(
) :
_index1(0),
_index2(0)
{
_distance = 1;
}
sample_pair (
const unsigned long idx1,
const unsigned long idx2
)
{
_distance = 1;
if (idx1 < idx2)
{
_index1 = idx1;
_index2 = idx2;
}
else
{
_index1 = idx2;
_index2 = idx1;
}
}
sample_pair (
const unsigned long idx1,
const unsigned long idx2,
const double dist
)
{
_distance = dist;
if (idx1 < idx2)
{
_index1 = idx1;
_index2 = idx2;
}
else
{
_index1 = idx2;
_index2 = idx1;
}
}
const unsigned long& index1 (
) const { return _index1; }
const unsigned long& index2 (
) const { return _index2; }
const double& distance (
) const { return _distance; }
private:
unsigned long _index1;
unsigned long _index2;
double _distance;
};
// ----------------------------------------------------------------------------------------
template <typename T>
inline bool order_by_index (
const T& a,
const T& b
)
{
return a.index1() < b.index1() || (a.index1() == b.index1() && a.index2() < b.index2());
}
template <typename T>
inline bool order_by_distance (
const T& a,
const T& b
)
{
return a.distance() < b.distance();
}
template <typename T>
inline bool order_by_descending_distance (
const T& a,
const T& b
)
{
return a.distance() > b.distance();
}
template <typename T>
bool order_by_distance_and_index (
const T& a,
const T& b
)
{
return a.distance() < b.distance() || (a.distance() == b.distance() && order_by_index(a,b));
}
// ----------------------------------------------------------------------------------------
inline bool operator == (
const sample_pair& a,
const sample_pair& b
)
{
return a.index1() == b.index1() && a.index2() == b.index2();
}
inline bool operator != (
const sample_pair& a,
const sample_pair& b
)
{
return !(a == b);
}
// ----------------------------------------------------------------------------------------
inline void serialize (
const sample_pair& item,
std::ostream& out
)
{
try
{
serialize(item.index1(),out);
serialize(item.index2(),out);
serialize(item.distance(),out);
}
catch (serialization_error& e)
{
throw serialization_error(e.info + "\n while serializing object of type sample_pair");
}
}
inline void deserialize (
sample_pair& item,
std::istream& in
)
{
try
{
unsigned long idx1, idx2;
double dist;
deserialize(idx1,in);
deserialize(idx2,in);
deserialize(dist,in);
item = sample_pair(idx1, idx2, dist);
}
catch (serialization_error& e)
{
throw serialization_error(e.info + "\n while deserializing object of type sample_pair");
}
}
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_SAMPLE_PaIR_Hh_
|