1 //============================================================================
2 //  Copyright (c) Kitware, Inc.
3 //  All rights reserved.
4 //  See LICENSE.txt for details.
5 //  This software is distributed WITHOUT ANY WARRANTY; without even
6 //  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
7 //  PURPOSE.  See the above copyright notice for more information.
8 //
9 //  Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
10 //  Copyright 2014 UT-Battelle, LLC.
11 //  Copyright 2014 Los Alamos National Security.
12 //
13 //  Under the terms of Contract DE-NA0003525 with NTESS,
14 //  the U.S. Government retains certain rights in this software.
15 //
16 //  Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
17 //  Laboratory (LANL), the U.S. Government retains certain rights in
18 //  this software.
19 //============================================================================
20 #ifndef vtk_m_cont_ArrayPortalToIterators_h
21 #define vtk_m_cont_ArrayPortalToIterators_h
22 
23 #include <vtkm/cont/ArrayPortal.h>
24 #include <vtkm/cont/internal/IteratorFromArrayPortal.h>
25 
26 namespace vtkm
27 {
28 namespace cont
29 {
30 
31 /// \brief Convert an \c ArrayPortal to STL iterators.
32 ///
33 /// \c ArrayPortalToIterators is a class that holds an \c ArrayPortal and
34 /// builds iterators that access the data in the \c ArrayPortal. The point of
35 /// this class is to use an \c ArrayPortal with generic functions that expect
36 /// STL iterators such as STL algorithms or Thrust operations.
37 ///
38 /// The default template implementation constructs iterators that provide
39 /// values through the \c ArrayPortal itself. This class can be specialized to
40 /// provide iterators that more directly access the data. For example, \c
41 /// ArrayPortalFromIterator has a specialization to return the original
42 /// iterators.
43 ///
44 template <typename PortalType>
45 class ArrayPortalToIterators
46 {
47 public:
48   /// \c ArrayPortaltoIterators should be constructed with an instance of
49   /// the array portal.
50   ///
51   VTKM_SUPPRESS_EXEC_WARNINGS
52   VTKM_EXEC_CONT
ArrayPortalToIterators(const PortalType & portal)53   ArrayPortalToIterators(const PortalType& portal)
54     : Portal(portal)
55   {
56   }
57 
58   /// The type of the iterator.
59   ///
60   using IteratorType = vtkm::cont::internal::IteratorFromArrayPortal<PortalType>;
61 
62   /// Returns an iterator pointing to the beginning of the ArrayPortal.
63   ///
64   VTKM_SUPPRESS_EXEC_WARNINGS
65   VTKM_EXEC_CONT
GetBegin()66   IteratorType GetBegin() const { return vtkm::cont::internal::make_IteratorBegin(this->Portal); }
67 
68   /// Returns an iterator pointing to one past the end of the ArrayPortal.
69   ///
70   VTKM_SUPPRESS_EXEC_WARNINGS
71   VTKM_EXEC_CONT
GetEnd()72   IteratorType GetEnd() const { return vtkm::cont::internal::make_IteratorEnd(this->Portal); }
73 
74 private:
75   PortalType Portal;
76 };
77 
78 /// Convenience function for converting an ArrayPortal to a begin iterator.
79 ///
80 VTKM_SUPPRESS_EXEC_WARNINGS
81 template <typename PortalType>
82 VTKM_EXEC_CONT typename vtkm::cont::ArrayPortalToIterators<PortalType>::IteratorType
ArrayPortalToIteratorBegin(const PortalType & portal)83 ArrayPortalToIteratorBegin(const PortalType& portal)
84 {
85   vtkm::cont::ArrayPortalToIterators<PortalType> iterators(portal);
86   return iterators.GetBegin();
87 }
88 
89 /// Convenience function for converting an ArrayPortal to an end iterator.
90 ///
91 VTKM_SUPPRESS_EXEC_WARNINGS
92 template <typename PortalType>
93 VTKM_EXEC_CONT typename vtkm::cont::ArrayPortalToIterators<PortalType>::IteratorType
ArrayPortalToIteratorEnd(const PortalType & portal)94 ArrayPortalToIteratorEnd(const PortalType& portal)
95 {
96   vtkm::cont::ArrayPortalToIterators<PortalType> iterators(portal);
97   return iterators.GetEnd();
98 }
99 }
100 } // namespace vtkm::cont
101 
102 #endif //vtk_m_cont_ArrayPortalToIterators_h
103