source: trunk/yat/utility/concept_check.h @ 2338

Last change on this file since 2338 was 2338, checked in by Peter, 12 years ago

adding an archetype class for distance concept and use that class in KNN and NCC. Adding CopyConstructible? to requirement for Distance concept

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.0 KB
Line 
1#ifndef _theplu_yat_utility_concept_check_
2#define _theplu_yat_utility_concept_check_
3
4// $Id: concept_check.h 2338 2010-10-16 05:00:12Z peter $
5
6/*
7  Copyright (C) 2010 Peter Johansson
8
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#include "iterator_traits.h"
26
27#include <boost/concept_archetype.hpp>
28#include <boost/concept_check.hpp>
29
30namespace theplu {
31namespace yat {
32namespace utility {
33
34  /**
35     \brief Concept check for \ref concept_container_2d
36
37     This class is intended to be used in a <a
38     href="\boost_url/concept_check/using_concept_check.htm">
39     BOOST_CONCEPT_ASSERT </a>
40
41     \code
42     template<class T>
43     void some_function(const T& t)
44     {
45     BOOST_CONCEPT_ASSERT((Container2D<T>));
46     ...
47     }
48     \endcode
49
50     \since New in yat 0.7
51  */
52  template <class T>
53  class Container2D
54  {
55  public:
56    /// value_type
57    typedef typename T::value_type value_type;
58    /// const_reference
59    typedef typename T::const_reference const_reference;
60    /// const_iterator
61    typedef typename T::const_iterator const_iterator;
62    /// const_row_iterator
63    typedef typename T::const_row_iterator const_row_iterator;
64    /// const_column_iterator
65    typedef typename T::const_column_iterator const_column_iterator;
66
67    /**
68       \brief function doing the concept test
69     */
70    BOOST_CONCEPT_USAGE(Container2D)
71    {
72      const_iterator iter_ = t_.begin();
73      iter_ = t_.end();
74      const_row_iterator row_iter_ = t_.begin_row(0);
75      row_iter_ = t_.end_row(0);
76      const_column_iterator col_iter_ = t_.begin_column(0);
77      col_iter_ = t_.end_column(0);
78      const_reference r = t_(0,0);
79      value_ = r;
80      size_t n = t_.rows();
81      n = t_.columns();
82      BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<const_iterator>));
83      BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<const_row_iterator>));
84      BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<const_column_iterator>));
85    }
86
87  private:
88    T t_;
89    value_type value_;
90  };
91
92  /**
93     \brief Concept check for \ref concept_mutable_container_2d
94
95     This class is intended to be used in a <a
96     href="\boost_url/concept_check/using_concept_check.htm">
97     BOOST_CONCEPT_ASSERT </a>
98
99     \code
100     template<class T>
101     void some_function(const T& t)
102     {
103     BOOST_CONCEPT_ASSERT((Mutable_Container2D<T>));
104     ...
105     }
106     \endcode
107
108     \since New in yat 0.7
109  */
110  template <class T>
111  class Mutable_Container2D : public Container2D<T>
112  {
113  public:
114    /// reference
115    typedef typename T::reference reference;
116    /// iterator
117    typedef typename T::iterator iterator;
118    /// row_iterator
119    typedef typename T::row_iterator row_iterator;
120    /// column_iterator
121    typedef typename T::column_iterator column_iterator;
122
123    /**
124       \brief function doing the concept test
125     */
126    BOOST_CONCEPT_USAGE(Mutable_Container2D)
127    {
128      iterator iter_ = t_.begin();
129      iter_ = t_.end();
130      row_iterator row_iter_ = t_.begin_row(0);
131      row_iter_ = t_.end_row(0);
132      column_iterator col_iter_ = t_.begin_column(0);
133      col_iter_ = t_.end_column(0);
134      reference r = t_(0,0);
135      t_(0,0) = r;
136      using boost::Mutable_RandomAccessIterator; // just to avoid long lines
137      BOOST_CONCEPT_ASSERT((Mutable_RandomAccessIterator<iterator>));
138      BOOST_CONCEPT_ASSERT((Mutable_RandomAccessIterator<row_iterator>));
139      BOOST_CONCEPT_ASSERT((Mutable_RandomAccessIterator<column_iterator>));
140    }
141  private:
142    T t_;
143  };
144
145
146  /**
147     \brief Concept check for \trivial_iterator
148
149     This class is intended to be used in a <a
150     href="\boost_url/concept_check/using_concept_check.htm">
151     BOOST_CONCEPT_ASSERT </a>
152
153     \code
154     template<class Iterator>
155     void some_function(const Iterator& it)
156     {
157     BOOST_CONCEPT_ASSERT((TrivialIterator<Iterator>));
158     ...
159     }
160     \endcode
161
162     \since New in yat 0.7
163  */
164  template <class T>
165  class TrivialIterator
166    : public boost::Assignable<T>
167    , public boost::EqualityComparable<T>
168    , public boost::DefaultConstructible<T>
169
170  {
171  public:
172    /// iterator_category
173    typedef typename std::iterator_traits<T>::iterator_category iterator_category;
174    /// value_type
175    typedef typename std::iterator_traits<T>::value_type value_type;
176    /// difference_type
177    typedef typename std::iterator_traits<T>::difference_type difference_type;
178    /// pointer
179    typedef typename std::iterator_traits<T>::pointer pointer;
180    /// reference
181    typedef typename std::iterator_traits<T>::reference reference;
182
183    /**
184       \brief function doing the concept test
185     */
186    BOOST_CONCEPT_USAGE(TrivialIterator)
187    {
188      T t;
189      value_ = *t;
190    }
191  private:
192    value_type value_;
193  };
194
195  /**
196     \brief Concept check for \ref concept_data_iterator
197
198     This class is intended to be used in a <a
199     href="\boost_url/concept_check/using_concept_check.htm">
200     BOOST_CONCEPT_ASSERT </a>
201
202     \code
203     template<class Iterator>
204     void some_function(const Iterator& it)
205     {
206     BOOST_CONCEPT_ASSERT((DataIteratorConcept<Iterator>));
207     ...
208     }
209     \endcode
210
211     \since New in yat 0.7
212  */
213  template <class T>
214  class DataIteratorConcept
215  {
216  public:
217    /// tag
218    typedef typename weighted_iterator_traits<T>::type tag;
219    /// value_type
220    typedef typename std::iterator_traits<T>::value_type value_type;
221
222    /**
223       \brief function doing the concept test
224     */
225    BOOST_CONCEPT_USAGE(DataIteratorConcept)
226    {
227      BOOST_CONCEPT_ASSERT((TrivialIterator<T>));
228      tag t;
229      constraints(t);
230    }
231  private:
232    void constraints(yat::utility::unweighted_iterator_tag t) const
233    { 
234      BOOST_CONCEPT_ASSERT((boost::Convertible<value_type, double>));
235    }
236
237    void constraints(yat::utility::weighted_iterator_tag t) const
238    {
239      BOOST_CONCEPT_ASSERT((boost::Convertible<value_type, DataWeight>));
240    }
241  };
242
243
244  /**
245     \brief Concept check for a \ref concept_distance
246
247     This class is intended to be used in a <a
248     href="\boost_url/concept_check/using_concept_check.htm">
249     BOOST_CONCEPT_ASSERT </a>
250
251     \code
252     template<class Distance>
253     void some_function(double x)
254     {
255     BOOST_CONCEPT_ASSERT((DistanceConcept<Distance>));
256     ...
257     }
258     \endcode
259
260     \since New in yat 0.7
261  */
262  template <class T>
263  class DistanceConcept : boost::CopyConstructible<T>
264  {
265  public:
266    /**
267       \brief function doing the concept test
268     */
269    BOOST_CONCEPT_USAGE(DistanceConcept)
270    {
271      boost::random_access_iterator_archetype<double> unweighted;
272      boost::random_access_iterator_archetype<DataWeight> weighted;
273      double d;
274      d = distance_(unweighted, unweighted, unweighted);
275      d = distance_(unweighted, unweighted, weighted);
276      d = distance_(weighted, weighted, unweighted);
277      d = distance_(weighted, weighted, weighted);
278    }
279  private:
280    T distance_;
281  };
282
283}}} // of namespace utility, yat, and theplu
284#endif
Note: See TracBrowser for help on using the repository browser.