libcrn  3.9.5
A document image processing library
•All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNLazyDistanceMatrix.h
Go to the documentation of this file.
1 /* Copyright 2015 Université Paris Descartes
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNLazyDistanceMatrix.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNLazyDistanceMatrix_HEADER
23 #define CRNLazyDistanceMatrix_HEADER
24 
26 #include <vector>
27 
28 namespace crn
29 {
38  template<typename DATA, typename DISTANCE> class LazyDistanceMatrix
39  {
40  public:
45  LazyDistanceMatrix(const std::vector<DATA> &d, DISTANCE &&dist):
46  data(d),
47  distance(std::forward<DISTANCE>(dist)),
48  distmat(d.size()),
49  cache(d.size(), std::vector<bool>(d.size(), false))
50  { }
51  LazyDistanceMatrix(const LazyDistanceMatrix&) = default;
55 
59  inline size_t GetRows() const noexcept { return data.size(); }
60 
64  inline size_t GetCols() const noexcept { return data.size(); }
65 
72  inline double At(size_t i, size_t j)
73  {
74  if (!cache[i][j])
75  {
76  distmat[i][j] = distmat[j][i] = distance(data[i], data[j]);
77  cache[i][j] = cache[j][i] = true;
78  }
79  return distmat[i][j];
80  }
81 
86  {
87  for (size_t i = 0; i < data.size(); ++i)
88  for (size_t j = i + 1; j < data.size(); ++j)
89  if (!cache[i][j])
90  At(i, j);
91  return distmat;
92  }
93 
94  private:
95  SquareMatrixDouble distmat;
96  const std::vector<DATA> &data;
97  DISTANCE distance;
98  std::vector<std::vector<bool>> cache;
99  };
100 
107  template<typename DATA, typename DISTANCE> inline LazyDistanceMatrix<DATA, DISTANCE> MakeLazyDistanceMatrix(const std::vector<DATA> &d, DISTANCE &&dist) { return LazyDistanceMatrix<DATA, DISTANCE>{d, std::forward<DISTANCE>(dist)}; }
108 }
109 
110 #endif
111 
double At(size_t i, size_t j)
Distance between two elements.
const SquareMatrixDouble & GetDistanceMatrix()
Access to the whole distance matrix.
Lazy computation of a distance matrix.
LazyDistanceMatrix & operator=(const LazyDistanceMatrix &)=delete
size_t GetCols() const noexcept
Returns the number of columns.
LazyDistanceMatrix(const std::vector< DATA > &d, DISTANCE &&dist)
#define false
Definition: ConvertUTF.cpp:56
size_t GetRows() const noexcept
Returns the number of rows.
LazyDistanceMatrix< DATA, DISTANCE > MakeLazyDistanceMatrix(const std::vector< DATA > &d, DISTANCE &&dist)
Helper to avoid typing long type names.
Square double matrix class.