Spatial C++ Library
Generic Multi-Dimensional Containers and Spatial Operations
spatial_manhattan_neighbor.hpp
1 // -*- C++ -*-
2 //
3 // Copyright Sylvain Bougerel 2009 - 2013.
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file COPYING or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 
17 #ifndef SPATIAL_MANHATTAN_NEIGHBOR_HPP
18 #define SPATIAL_MANHATTAN_NEIGHBOR_HPP
19 
20 #include "spatial_neighbor.hpp"
21 
22 namespace spatial
23 {
39  template <typename Ct, typename DistanceType, typename Diff
41  = typename details::with_builtin_difference<Ct>::type>
43  : public neighbor_iterator<Ct, manhattan<Ct, DistanceType, Diff> >
44  {
45  // Check that DistanceType is a fundamental arithmetic type
46  typedef typename enable_if<import::is_arithmetic<DistanceType> >::type
48 
49  public:
51 
52  template <typename AnyDistanceType>
57  (other.rank(), other.key_comp(), other.metric(),
58  other.target_key(), other.node_dim, other.node,
59  static_cast<AnyDistanceType>(other.distance())) { }
60  };
61 
62  template <typename Ct, typename DistanceType, typename Diff>
63  class manhattan_neighbor_iterator<const Ct, DistanceType, Diff>
64  : public neighbor_iterator<const Ct, manhattan<Ct, DistanceType, Diff> >
65  {
66  // Some concept checking performed here
69 
70  public:
72 
73  template <typename AnyDistanceType>
76  <const Ct, manhattan<Ct, AnyDistanceType, Diff> >& other)
78  (other.rank(), other.key_comp(), other.metric(),
79  other.target_key(), other.node_dim, other.node,
80  static_cast<AnyDistanceType>(other.distance())) { }
81 
82  template <typename AnyDistanceType>
87  (other.rank(), other.key_comp(), other.metric(),
88  other.target_key(), other.node_dim, other.node,
89  static_cast<AnyDistanceType>(other.distance())) { }
90  };
92 
108  template <typename Ct, typename DistanceType, typename Diff
110  = typename details::with_builtin_difference<Ct>::type>
112  : public neighbor_iterator_pair<Ct, manhattan<Ct, DistanceType, Diff> >
113  {
114  // Some concept checking performed here
117 
118  public:
120 
125  (a, b) { }
126 
127  template <typename AnyDistanceType>
133  (other.first, other.second)) { }
134  };
135 
136  template <typename Ct, typename DistanceType, typename Diff>
137  class manhattan_neighbor_iterator_pair<const Ct, DistanceType, Diff>
138  : public neighbor_iterator_pair
139  <const Ct, manhattan<Ct, DistanceType, Diff> >
140  {
141  // Some concept checking performed here
144 
145  public:
147 
152  (a, b) { }
153 
154  template <typename AnyDistanceType>
157  <const Ct, manhattan<Ct, AnyDistanceType, Diff> >& other)
160  (other.first, other.second)) { }
161 
162  template <typename AnyDistanceType>
168  (other.first, other.second)) { }
169  };
171 
190  template <typename Ct, typename Diff>
192  inline manhattan_neighbor_iterator<Ct, double, Diff>
194  (Ct& container, const Diff& diff,
195  const typename container_traits<Ct>::key_type& target)
196  {
197  return neighbor_begin
198  (container, manhattan<Ct, double, Diff>(diff), target);
199  }
200 
201  template <typename Ct, typename Diff>
202  inline manhattan_neighbor_iterator<const Ct, double, Diff>
204  (const Ct& container, const Diff& diff,
205  const typename container_traits<Ct>::key_type& target)
206  {
207  return neighbor_begin
208  (container, manhattan<Ct, double, Diff>(diff), target);
209  }
210 
211  template <typename Ct, typename Diff>
212  inline manhattan_neighbor_iterator<const Ct, double, Diff>
214  (const Ct& container, const Diff& diff,
215  const typename container_traits<Ct>::key_type& target)
216  {
217  return neighbor_begin
218  (container, manhattan<Ct, double, Diff>(diff), target);
219  }
221 
239  template <typename Ct>
241  inline typename
242  enable_if<details::is_compare_builtin<Ct>,
243  manhattan_neighbor_iterator<Ct, double> >::type
245  (Ct& container,
246  const typename container_traits<Ct>::key_type& target)
247  {
248  return neighbor_begin
249  (container,
250  manhattan<Ct, double,
253  target);
254  }
255 
256  template <typename Ct>
257  inline typename
258  enable_if<details::is_compare_builtin<Ct>,
259  manhattan_neighbor_iterator<const Ct, double> >::type
261  (const Ct& container,
262  const typename container_traits<Ct>::key_type& target)
263  {
264  return neighbor_begin
265  (container,
266  manhattan<Ct, double,
269  target);
270  }
271 
272  template <typename Ct>
273  inline typename
274  enable_if<details::is_compare_builtin<Ct>,
275  manhattan_neighbor_iterator<const Ct, double> >::type
277  (const Ct& container,
278  const typename container_traits<Ct>::key_type& target)
279  {
280  return neighbor_begin
281  (container,
282  manhattan<Ct, double,
285  target);
286  }
288 
307  template <typename Ct, typename Diff>
309  inline manhattan_neighbor_iterator<Ct, double, Diff>
311  (Ct& container, const Diff& diff,
312  const typename container_traits<Ct>::key_type& target)
313  {
314  return neighbor_end
315  (container, manhattan<Ct, double, Diff>(diff), target);
316  }
317 
318  template <typename Ct, typename Diff>
319  inline manhattan_neighbor_iterator<const Ct, double, Diff>
321  (const Ct& container, const Diff& diff,
322  const typename container_traits<Ct>::key_type& target)
323  {
324  return neighbor_end
325  (container, manhattan<Ct, double, Diff>(diff), target);
326  }
327 
328  template <typename Ct, typename Diff>
329  inline manhattan_neighbor_iterator<const Ct, double, Diff>
331  (const Ct& container, const Diff& diff,
332  const typename container_traits<Ct>::key_type& target)
333  {
334  return neighbor_end
335  (container, manhattan<Ct, double, Diff>(diff), target);
336  }
338 
356  template <typename Ct>
358  inline typename
359  enable_if<details::is_compare_builtin<Ct>,
360  manhattan_neighbor_iterator<Ct, double> >::type
362  (Ct& container,
363  const typename container_traits<Ct>::key_type& target)
364  {
365  return neighbor_end
366  (container,
367  manhattan<Ct, double,
370  target);
371  }
372 
373  template <typename Ct>
374  inline typename
375  enable_if<details::is_compare_builtin<Ct>,
376  manhattan_neighbor_iterator<const Ct, double> >::type
378  (const Ct& container,
379  const typename container_traits<Ct>::key_type& target)
380  {
381  return neighbor_end
382  (container,
383  manhattan<Ct, double,
386  target);
387  }
388 
389  template <typename Ct>
390  inline typename
391  enable_if<details::is_compare_builtin<Ct>,
392  manhattan_neighbor_iterator<const Ct, double> >::type
394  (const Ct& container,
395  const typename container_traits<Ct>::key_type& target)
396  {
397  return neighbor_end
398  (container,
399  manhattan<Ct, double,
402  target);
403  }
405 
415  template <typename Ct, typename Diff, typename DistanceType>
417  inline typename
418  enable_if<import::is_arithmetic<DistanceType>,
419  manhattan_neighbor_iterator<Ct, DistanceType, Diff> >::type
421  (Ct& container, const Diff& diff,
422  const typename container_traits<Ct>::key_type& target,
423  DistanceType bound)
424  {
425  return neighbor_lower_bound
426  (container, manhattan<Ct, DistanceType, Diff>(diff), target, bound);
427  }
428 
429  template <typename Ct, typename Diff, typename DistanceType>
430  inline typename
431  enable_if<import::is_arithmetic<DistanceType>,
432  manhattan_neighbor_iterator<const Ct, DistanceType, Diff> >::type
434  (const Ct& container, const Diff& diff,
435  const typename container_traits<Ct>::key_type& target,
436  DistanceType bound)
437  {
438  return neighbor_lower_bound
439  (container, manhattan<Ct, DistanceType, Diff>(diff), target, bound);
440  }
441 
442  template <typename Ct, typename Diff, typename DistanceType>
443  inline typename
444  enable_if<import::is_arithmetic<DistanceType>,
445  manhattan_neighbor_iterator<const Ct, DistanceType, Diff> >::type
447  (const Ct& container, const Diff& diff,
448  const typename container_traits<Ct>::key_type& target,
449  DistanceType bound)
450  {
451  return neighbor_lower_bound
452  (container, manhattan<Ct, DistanceType, Diff>(diff), target, bound);
453  }
455 
464  template <typename Ct, typename DistanceType>
466  inline typename
467  enable_if_c<details::is_compare_builtin<Ct>::value
468  && import::is_arithmetic<DistanceType>::value,
469  manhattan_neighbor_iterator<Ct, DistanceType> >::type
471  (Ct& container,
472  const typename container_traits<Ct>::key_type& target,
473  DistanceType bound)
474  {
475  return neighbor_lower_bound
476  (container,
477  manhattan<Ct, DistanceType, typename
480  target, bound);
481  }
482 
483  template <typename Ct, typename DistanceType>
484  inline typename
485  enable_if_c<details::is_compare_builtin<Ct>::value
486  && import::is_arithmetic<DistanceType>::value,
487  manhattan_neighbor_iterator<const Ct, DistanceType> >::type
489  (const Ct& container,
490  const typename container_traits<Ct>::key_type& target,
491  DistanceType bound)
492  {
493  return neighbor_lower_bound
494  (container,
495  manhattan<Ct, double,
498  target, bound);
499  }
500 
501  template <typename Ct, typename DistanceType>
502  inline typename
503  enable_if_c<details::is_compare_builtin<Ct>::value
504  && import::is_arithmetic<DistanceType>::value,
505  manhattan_neighbor_iterator<const Ct, DistanceType> >::type
507  (const Ct& container,
508  const typename container_traits<Ct>::key_type& target,
509  DistanceType bound)
510  {
511  return neighbor_lower_bound
512  (container,
513  manhattan<Ct, double,
516  target, bound);
517  }
519 
529  template <typename Ct, typename Diff, typename DistanceType>
531  inline typename
532  enable_if<import::is_arithmetic<DistanceType>,
533  manhattan_neighbor_iterator<Ct, DistanceType, Diff> >::type
535  (Ct& container, const Diff& diff,
536  const typename container_traits<Ct>::key_type& target,
537  DistanceType bound)
538  {
539  return neighbor_upper_bound
540  (container, manhattan<Ct, DistanceType, Diff>(diff), target, bound);
541  }
542 
543  template <typename Ct, typename Diff, typename DistanceType>
544  inline typename
545  enable_if<import::is_arithmetic<DistanceType>,
546  manhattan_neighbor_iterator<const Ct, DistanceType, Diff> >::type
548  (const Ct& container, const Diff& diff,
549  const typename container_traits<Ct>::key_type& target,
550  DistanceType bound)
551  {
552  return neighbor_upper_bound
553  (container, manhattan<Ct, DistanceType, Diff>(diff), target, bound);
554  }
555 
556  template <typename Ct, typename Diff, typename DistanceType>
557  inline typename
558  enable_if<import::is_arithmetic<DistanceType>,
559  manhattan_neighbor_iterator<const Ct, DistanceType, Diff> >::type
561  (const Ct& container, const Diff& diff,
562  const typename container_traits<Ct>::key_type& target,
563  DistanceType bound)
564  {
565  return neighbor_upper_bound
566  (container, manhattan<Ct, DistanceType, Diff>(diff), target, bound);
567  }
569 
578  template <typename Ct, typename DistanceType>
580  inline typename
581  enable_if_c<details::is_compare_builtin<Ct>::value
582  && import::is_arithmetic<DistanceType>::value,
583  manhattan_neighbor_iterator<Ct, DistanceType> >::type
585  (Ct& container,
586  const typename container_traits<Ct>::key_type& target,
587  DistanceType bound)
588  {
589  return neighbor_upper_bound
590  (container,
591  manhattan<Ct, DistanceType, typename
594  target, bound);
595  }
596 
597  template <typename Ct, typename DistanceType>
598  inline typename
599  enable_if_c<details::is_compare_builtin<Ct>::value
600  && import::is_arithmetic<DistanceType>::value,
601  manhattan_neighbor_iterator<const Ct, DistanceType> >::type
603  (const Ct& container,
604  const typename container_traits<Ct>::key_type& target,
605  DistanceType bound)
606  {
607  return neighbor_upper_bound
608  (container,
609  manhattan<Ct, double,
612  target, bound);
613  }
614 
615  template <typename Ct, typename DistanceType>
616  inline typename
617  enable_if_c<details::is_compare_builtin<Ct>::value
618  && import::is_arithmetic<DistanceType>::value,
619  manhattan_neighbor_iterator<const Ct, DistanceType> >::type
621  (const Ct& container,
622  const typename container_traits<Ct>::key_type& target,
623  DistanceType bound)
624  {
625  return neighbor_upper_bound
626  (container,
627  manhattan<Ct, double,
630  target, bound);
631  }
633 
652  template <typename Ct, typename Diff>
654  inline manhattan_neighbor_iterator_pair<Ct, double, Diff>
656  (Ct& container, const Diff& diff,
657  const typename container_traits<Ct>::key_type& target)
658  {
659  return neighbor_range
660  (container, manhattan<Ct, double, Diff>(diff), target);
661  }
662 
663  template <typename Ct, typename Diff>
664  inline manhattan_neighbor_iterator_pair<const Ct, double, Diff>
666  (const Ct& container, const Diff& diff,
667  const typename container_traits<Ct>::key_type& target)
668  {
669  return neighbor_range
670  (container, manhattan<Ct, double, Diff>(diff), target);
671  }
672 
673  template <typename Ct, typename Diff>
674  inline manhattan_neighbor_iterator_pair<const Ct, double, Diff>
676  (const Ct& container, const Diff& diff,
677  const typename container_traits<Ct>::key_type& target)
678  {
679  return neighbor_range
680  (container, manhattan<Ct, double, Diff>(diff), target);
681  }
683 
701  template <typename Ct>
703  inline typename
704  enable_if<details::is_compare_builtin<Ct>,
705  manhattan_neighbor_iterator_pair<Ct, double> >::type
707  (Ct& container,
708  const typename container_traits<Ct>::key_type& target)
709  {
710  return neighbor_range
711  (container,
712  manhattan<Ct, double,
715  target);
716  }
717 
718  template <typename Ct>
719  inline typename
720  enable_if<details::is_compare_builtin<Ct>,
721  manhattan_neighbor_iterator_pair<const Ct, double> >::type
723  (const Ct& container,
724  const typename container_traits<Ct>::key_type& target)
725  {
726  return neighbor_range
727  (container,
728  manhattan<Ct, double,
731  target);
732  }
733 
734  template <typename Ct>
735  inline typename
736  enable_if<details::is_compare_builtin<Ct>,
737  manhattan_neighbor_iterator_pair<const Ct, double> >::type
739  (const Ct& container,
740  const typename container_traits<Ct>::key_type& target)
741  {
742  return neighbor_range
743  (container,
744  manhattan<Ct, double,
747  target);
748  }
750 
751 } // namespace spatial
752 
753 #endif // SPATIAL_MANHATTAN_NEIGHBOR_HPP
manhattan_neighbor_iterator< const Ct, double, Diff > manhattan_neighbor_cend(const Ct &container, const Diff &diff, const typename container_traits< Ct >::key_type &target)
Returns a manhattan_neighbor_iterator pointing past-the-end.
enable_if< import::is_arithmetic< DistanceType >, manhattan_neighbor_iterator< Ct, DistanceType, Diff > >::type manhattan_neighbor_lower_bound(Ct &container, const Diff &diff, const typename container_traits< Ct >::key_type &target, DistanceType bound)
Returns a manhattan_neighbor_iterator pointing to the closest element to target that is a least as fa...
manhattan_neighbor_iterator_pair< const Ct, double, Diff > manhattan_neighbor_crange(const Ct &container, const Diff &diff, const typename container_traits< Ct >::key_type &target)
Make a pair of iterators spanning the range of iterable element in container from the closest to the ...
neighbor_iterator_pair< Ct, Metric > neighbor_range(Ct &container, const Metric &metric, const typename container_traits< Ct >::key_type &target)
Returns a neighbor_iterator_pair representing the range of values from the closest to the furthest in...
enable_if< import::is_arithmetic< DistanceType >, manhattan_neighbor_iterator< const Ct, DistanceType, Diff > >::type manhattan_neighbor_clower_bound(const Ct &container, const Diff &diff, const typename container_traits< Ct >::key_type &target, DistanceType bound)
Returns a manhattan_neighbor_iterator pointing to the closest element to target that is a least as fa...
enable_if< import::is_arithmetic< DistanceType > > check_concept_distance_type_is_arithmetic
manhattan_neighbor_iterator< Ct, double, Diff > manhattan_neighbor_end(Ct &container, const Diff &diff, const typename container_traits< Ct >::key_type &target)
Returns a manhattan_neighbor_iterator pointing past-the-end.
enable_if< import::is_arithmetic< DistanceType >, manhattan_neighbor_iterator< const Ct, DistanceType, Diff > >::type manhattan_neighbor_cupper_bound(const Ct &container, const Diff &diff, const typename container_traits< Ct >::key_type &target, DistanceType bound)
Returns a manhattan_neighbor_iterator pointing to the closest element to target that further than bou...
neighbor_iterator< Ct, Metric > neighbor_lower_bound(Ct &container, const Metric &metric, const typename container_traits< Ct >::key_type &target, typename Metric::distance_type bound)
Build a neighbor_iterator pointing to the neighbor closest to target but for which distance to target...
neighbor_iterator< Ct, Metric > neighbor_end(Ct &container, const Metric &metric, const typename container_traits< Ct >::key_type &target)
Build a past-the-end neighbor iterator with a user-defined Metric.
A spatial iterator for a container Ct that goes through the nearest to the furthest element from a ta...
manhattan_neighbor_iterator< Ct, double, Diff > manhattan_neighbor_begin(Ct &container, const Diff &diff, const typename container_traits< Ct >::key_type &target)
Returns a manhattan_neighbor_iterator pointing to the nearest neighbor of target. ...
manhattan_neighbor_iterator_pair< Ct, double, Diff > manhattan_neighbor_range(Ct &container, const Diff &diff, const typename container_traits< Ct >::key_type &target)
Make a pair of iterators spanning the range of iterable element in container from the closest to the ...
The traits type for all containers in the spatial namespace.
Definition: traits.hpp:42
This structure defines a pair of neighbor iterator.
enable_if< import::is_arithmetic< DistanceType > >::type check_concept_distance_type_is_arithmetic
neighbor_iterator< Ct, Metric > neighbor_upper_bound(Ct &container, const Metric &metric, const typename container_traits< Ct >::key_type &target, typename Metric::distance_type bound)
Build a neighbor_iterator pointing to the neighbor closest to target but for which distance to target...
The main namespace used in the library.
Definition: algorithm.hpp:23
enable_if< import::is_arithmetic< DistanceType > > check_concept_distance_type_is_arithmetic
Defines a metric for the a space where distances are the sum of all the elements of the vector...
Definition: metric.hpp:231
neighbor_iterator< Ct, Metric > neighbor_begin(Ct &container, const Metric &metric, const typename container_traits< Ct >::key_type &target)
Build a neighbor_iterator pointing to the nearest neighbor of target using a user-defined Metric...
enable_if< import::is_arithmetic< DistanceType > > check_concept_distance_type_is_arithmetic
The generic helper class to determine if a container uses a built-in compare type.
enable_if< import::is_arithmetic< DistanceType >, manhattan_neighbor_iterator< Ct, DistanceType, Diff > >::type manhattan_neighbor_upper_bound(Ct &container, const Diff &diff, const typename container_traits< Ct >::key_type &target, DistanceType bound)
Returns a manhattan_neighbor_iterator pointing to the closest element to target that further than bou...
Facilitate the creation of an iterator range representing a sequence from closest to furthest from th...
manhattan_neighbor_iterator< const Ct, double, Diff > manhattan_neighbor_cbegin(const Ct &container, const Diff &diff, const typename container_traits< Ct >::key_type &target)
Returns a manhattan_neighbor_iterator pointing to the nearest neighbor of target. ...
Facilitate the creation of neighbor iterator that works with a manhattan metric.