VecFromPortalPermute.h 4.3 KB
Newer Older
1 2 3 4 5 6 7 8
//============================================================================
//  Copyright (c) Kitware, Inc.
//  All rights reserved.
//  See LICENSE.txt for details.
//  This software is distributed WITHOUT ANY WARRANTY; without even
//  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
//  PURPOSE.  See the above copyright notice for more information.
//
9
//  Copyright 2015 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
10 11 12
//  Copyright 2015 UT-Battelle, LLC.
//  Copyright 2015 Los Alamos National Security.
//
13
//  Under the terms of Contract DE-NA0003525 with NTESS,
14 15 16 17 18 19
//  the U.S. Government retains certain rights in this software.
//
//  Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
//  Laboratory (LANL), the U.S. Government retains certain rights in
//  this software.
//============================================================================
20 21
#ifndef vtk_m_VecFromPortalPermute_h
#define vtk_m_VecFromPortalPermute_h
22 23 24

#include <vtkm/Math.h>
#include <vtkm/TypeTraits.h>
25
#include <vtkm/Types.h>
26 27
#include <vtkm/VecTraits.h>

28 29
namespace vtkm
{
30 31 32 33 34 35 36

/// \brief A short vector from an ArrayPortal and a vector of indices.
///
/// The \c VecFromPortalPermute class is a Vec-like class that holds an array
/// portal and a second Vec-like containing indices into the array. Each value
/// of this vector is the value from the array with the respective index.
///
37
template <typename IndexVecType, typename PortalType>
38 39 40
class VecFromPortalPermute
{
public:
41
  using ComponentType = typename std::remove_const<typename PortalType::ValueType>::type;
42

43 44
  VTKM_SUPPRESS_EXEC_WARNINGS
  VTKM_EXEC_CONT
45
  VecFromPortalPermute() {}
46

47 48
  VTKM_SUPPRESS_EXEC_WARNINGS
  VTKM_EXEC_CONT
49 50 51 52 53
  VecFromPortalPermute(const IndexVecType* indices, const PortalType& portal)
    : Indices(indices)
    , Portal(portal)
  {
  }
54

55 56
  VTKM_SUPPRESS_EXEC_WARNINGS
  VTKM_EXEC_CONT
57
  vtkm::IdComponent GetNumberOfComponents() const { return this->Indices->GetNumberOfComponents(); }
58

59
  VTKM_SUPPRESS_EXEC_WARNINGS
60 61
  template <vtkm::IdComponent DestSize>
  VTKM_EXEC_CONT void CopyInto(vtkm::Vec<ComponentType, DestSize>& dest) const
62
  {
63
    vtkm::IdComponent numComponents = vtkm::Min(DestSize, this->GetNumberOfComponents());
64 65
    for (vtkm::IdComponent index = 0; index < numComponents; index++)
    {
66
      dest[index] = (*this)[index];
67 68 69
    }
  }

70
  VTKM_SUPPRESS_EXEC_WARNINGS
71
  VTKM_EXEC_CONT
72 73
  ComponentType operator[](vtkm::IdComponent index) const
  {
74
    return this->Portal.Get((*this->Indices)[index]);
75 76 77
  }

private:
78
  const IndexVecType* Indices;
79 80 81
  PortalType Portal;
};

82 83
template <typename IndexVecType, typename PortalType>
struct TypeTraits<vtkm::VecFromPortalPermute<IndexVecType, PortalType>>
84 85
{
private:
86 87
  using VecType = vtkm::VecFromPortalPermute<IndexVecType, PortalType>;
  using ComponentType = typename PortalType::ValueType;
88 89

public:
90 91
  using NumericTag = typename vtkm::TypeTraits<ComponentType>::NumericTag;
  using DimensionalityTag = TypeTraitsVectorTag;
92

93 94
  VTKM_SUPPRESS_EXEC_WARNINGS
  VTKM_EXEC_CONT
95
  static VecType ZeroInitialization() { return VecType(); }
96 97
};

98 99
template <typename IndexVecType, typename PortalType>
struct VecTraits<vtkm::VecFromPortalPermute<IndexVecType, PortalType>>
100
{
101
  using VecType = vtkm::VecFromPortalPermute<IndexVecType, PortalType>;
102

103 104 105
  using ComponentType = typename VecType::ComponentType;
  using HasMultipleComponents = vtkm::VecTraitsTagMultipleComponents;
  using IsSizeStatic = vtkm::VecTraitsTagSizeVariable;
106

107 108
  VTKM_SUPPRESS_EXEC_WARNINGS
  VTKM_EXEC_CONT
109 110
  static vtkm::IdComponent GetNumberOfComponents(const VecType& vector)
  {
111 112 113
    return vector.GetNumberOfComponents();
  }

114 115
  VTKM_SUPPRESS_EXEC_WARNINGS
  VTKM_EXEC_CONT
116
  static ComponentType GetComponent(const VecType& vector, vtkm::IdComponent componentIndex)
117 118 119 120
  {
    return vector[componentIndex];
  }

121
  VTKM_SUPPRESS_EXEC_WARNINGS
122 123
  template <vtkm::IdComponent destSize>
  VTKM_EXEC_CONT static void CopyInto(const VecType& src, vtkm::Vec<ComponentType, destSize>& dest)
124 125 126 127 128
  {
    src.CopyInto(dest);
  }
};

Sujin Philip's avatar
Sujin Philip committed
129 130 131 132 133 134 135 136
template <typename IndexVecType, typename PortalType>
inline VTKM_EXEC VecFromPortalPermute<IndexVecType, PortalType> make_VecFromPortalPermute(
  const IndexVecType* index,
  const PortalType& portal)
{
  return VecFromPortalPermute<IndexVecType, PortalType>(index, portal);
}

137 138
} // namespace vtkm

139
#endif //vtk_m_VecFromPortalPermute_h