All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
basic_metrics.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2013 Piotr Wygocki
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //=======================================================================
15 #ifndef PAAL_BASIC_METRICS_HPP
16 #define PAAL_BASIC_METRICS_HPP
17 
18 #include "metric_traits.hpp"
19 
20 #include <boost/multi_array.hpp>
21 #include <boost/range/iterator_range.hpp>
22 
23 #include <array>
24 
25 namespace paal {
26 namespace data_structures {
27 
38 template <typename DistanceTypeParam> class rectangle_array_metric {
39  public:
40  typedef DistanceTypeParam DistanceType;
41  typedef int VertexType;
48  rectangle_array_metric(int N = 0, int M = 0)
49  : m_matrix(boost::extents[N][M]) {}
50 
59  DistanceType operator()(const VertexType &v, const VertexType &w) const {
60  return m_matrix[v][w];
61  }
62 
71  DistanceType &operator()(const VertexType &v, const VertexType &w) {
72  return m_matrix[v][w];
73  }
74 
83  template <typename OtherMetrics, typename XRange, typename YRange>
84  rectangle_array_metric(const OtherMetrics &other, XRange && xrange
85  , YRange && yrange)
86  : rectangle_array_metric(boost::distance(xrange),
87  boost::distance(yrange)) {
88  int i = 0;
89  for (auto && v : xrange) {
90  int j = 0;
91  for (auto && w : yrange) {
92  m_matrix[i][j] = other(v, w);
93  ++j;
94  }
95  ++i;
96  }
97  }
98 
107  auto shape = am.m_matrix.shape();
108  std::vector<std::size_t> dim(shape, shape + DIM_NR);
109  m_matrix.resize(dim);
110  m_matrix = am.m_matrix;
111  return *this;
112  }
113 
115  bool operator==(const rectangle_array_metric & other) const {
116  return m_matrix == other.m_matrix;
117  }
118 
119  protected:
123  static const int DIM_NR = 2;
124  typedef boost::multi_array<DistanceType, DIM_NR> matrix_type;
126  matrix_type m_matrix;
127 };
128 
129 
130 
136 template <typename DistanceTypeParam>
137 class array_metric : public rectangle_array_metric<DistanceTypeParam> {
139 
140  public:
146  array_metric(int N = 0) : base(N, N) {}
147 
153  int size() const { return this->m_matrix.size(); }
154 
163  template <typename OtherMetrics, typename Items>
164  array_metric(const OtherMetrics &other, Items && items)
165  : base(other, items, items) {}
166 };
167 }
168 }
169 #endif // PAAL_BASIC_METRICS_HPP
array_metric(const OtherMetrics &other, Items &&items)
constructor from another metric
rectangle_array_metric(const OtherMetrics &other, XRange &&xrange, YRange &&yrange)
constructor from another metric
static const int DIM_NR
dimention of multi array
Metric implementation on 2 dimensional array distance calls on this metric are valid opnly when x &lt; N...
DistanceType & operator()(const VertexType &v, const VertexType &w)
operator(), valid only when v &lt; N and w &lt; M, nonconst version
DistanceType operator()(const VertexType &v, const VertexType &w) const
operator(), valid only when v &lt; N and w &lt; M
rectangle_array_metric(int N=0, int M=0)
constructor
this metric is rectangle_array_metric with N == M.
rectangle_array_metric & operator=(const rectangle_array_metric &am)
operator=
bool operator==(const rectangle_array_metric &other) const
operator==