VecFromPortalPermute.h 5.81 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* const Indices;
79 80 81
  PortalType Portal;
};

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
template <typename IndexVecType, typename PortalType>
class VecFromPortalPermute<IndexVecType, const PortalType*>
{
public:
  using ComponentType = typename std::remove_const<typename PortalType::ValueType>::type;

  VTKM_SUPPRESS_EXEC_WARNINGS
  VTKM_EXEC_CONT
  VecFromPortalPermute() {}

  VTKM_SUPPRESS_EXEC_WARNINGS
  VTKM_EXEC_CONT
  VecFromPortalPermute(const IndexVecType* indices, const PortalType* const portal)
    : Indices(indices)
    , Portal(portal)
  {
  }

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

  VTKM_SUPPRESS_EXEC_WARNINGS
  template <vtkm::IdComponent DestSize>
  VTKM_EXEC_CONT void CopyInto(vtkm::Vec<ComponentType, DestSize>& dest) const
  {
    vtkm::IdComponent numComponents = vtkm::Min(DestSize, this->GetNumberOfComponents());
    for (vtkm::IdComponent index = 0; index < numComponents; index++)
    {
      dest[index] = (*this)[index];
    }
  }

  VTKM_SUPPRESS_EXEC_WARNINGS
  VTKM_EXEC_CONT
  ComponentType operator[](vtkm::IdComponent index) const
  {
    return this->Portal->Get((*this->Indices)[index]);
  }

private:
  const IndexVecType* const Indices;
  const PortalType* const Portal;
};

127 128
template <typename IndexVecType, typename PortalType>
struct TypeTraits<vtkm::VecFromPortalPermute<IndexVecType, PortalType>>
129 130
{
private:
131 132
  using VecType = vtkm::VecFromPortalPermute<IndexVecType, PortalType>;
  using ComponentType = typename PortalType::ValueType;
133 134

public:
135 136
  using NumericTag = typename vtkm::TypeTraits<ComponentType>::NumericTag;
  using DimensionalityTag = TypeTraitsVectorTag;
137

138 139
  VTKM_SUPPRESS_EXEC_WARNINGS
  VTKM_EXEC_CONT
140
  static VecType ZeroInitialization() { return VecType(); }
141 142
};

143 144
template <typename IndexVecType, typename PortalType>
struct VecTraits<vtkm::VecFromPortalPermute<IndexVecType, PortalType>>
145
{
146
  using VecType = vtkm::VecFromPortalPermute<IndexVecType, PortalType>;
147

148 149 150
  using ComponentType = typename VecType::ComponentType;
  using HasMultipleComponents = vtkm::VecTraitsTagMultipleComponents;
  using IsSizeStatic = vtkm::VecTraitsTagSizeVariable;
151

152 153
  VTKM_SUPPRESS_EXEC_WARNINGS
  VTKM_EXEC_CONT
154 155
  static vtkm::IdComponent GetNumberOfComponents(const VecType& vector)
  {
156 157 158
    return vector.GetNumberOfComponents();
  }

159 160
  VTKM_SUPPRESS_EXEC_WARNINGS
  VTKM_EXEC_CONT
161
  static ComponentType GetComponent(const VecType& vector, vtkm::IdComponent componentIndex)
162 163 164 165
  {
    return vector[componentIndex];
  }

166
  VTKM_SUPPRESS_EXEC_WARNINGS
167 168
  template <vtkm::IdComponent destSize>
  VTKM_EXEC_CONT static void CopyInto(const VecType& src, vtkm::Vec<ComponentType, destSize>& dest)
169 170 171 172 173
  {
    src.CopyInto(dest);
  }
};

Sujin Philip's avatar
Sujin Philip committed
174 175 176 177 178 179 180 181
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);
}

182 183 184 185 186 187 188 189
template <typename IndexVecType, typename PortalType>
inline VTKM_EXEC VecFromPortalPermute<IndexVecType, const PortalType*> make_VecFromPortalPermute(
  const IndexVecType* index,
  const PortalType* const portal)
{
  return VecFromPortalPermute<IndexVecType, const PortalType*>(index, portal);
}

190 191
} // namespace vtkm

192
#endif //vtk_m_VecFromPortalPermute_h