VTK  9.7.1
vtkCellGridSidesCache.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
3 
4 #ifndef vtkCellGridSidesCache_h
5 #define vtkCellGridSidesCache_h
6 
7 #include "vtkObject.h"
8 
9 #include "vtkCommonDataModelModule.h" // For export macro.
10 #include "vtkHashCombiner.h" // For templated AddSide() method.
11 #include "vtkStringToken.h" // For API.
12 
13 #include <functional>
14 #include <map>
15 #include <set>
16 #include <unordered_map>
17 #include <vector>
18 
19 VTK_ABI_NAMESPACE_BEGIN
20 
21 class vtkIdTypeArray;
22 
33 class VTKCOMMONDATAMODEL_EXPORT vtkCellGridSidesCache : public vtkObject
34 {
35 public:
36  static vtkCellGridSidesCache* New();
38  void PrintSelf(ostream& os, vtkIndent indent) override;
39 
44  struct Side
45  {
53  int SideId;
54 
56  bool operator<(const Side& other) const
57  {
58  return (this->CellType < other.CellType) ||
59  (this->CellType == other.CellType &&
60  ((this->DOF < other.DOF) || (this->DOF == other.DOF && this->SideId < other.SideId)));
61  }
62  };
63 
65  struct Entry
66  {
67  std::set<Side> Sides;
68  };
69 
71  std::unordered_map<std::size_t, Entry>& GetHashes() { return this->Hashes; }
72 
74  template <typename C, typename T = typename C::value_type>
75  std::size_t HashSide(vtkStringToken shape, const C& conn)
76  {
77  std::size_t ss = 0;
78  std::size_t NN = conn.size();
79  if (NN == 0)
80  {
81  return 0;
82  }
83 
84  T smin = conn[0];
85  for (std::size_t jj = 1; jj < NN; ++jj)
86  {
87  if (conn[jj] < smin)
88  {
89  smin = conn[jj];
90  ss = jj;
91  }
92  }
93  bool forward = conn[(ss + 1) % NN] > conn[(ss + NN - 1) % NN];
94 
95  std::size_t hashedValue = std::hash<std::size_t>{}(NN);
96  vtkHashCombiner()(hashedValue, shape.GetId());
97  // std::cout << "Hash(" << (forward ? "F" : "R") << ")";
98  if (forward)
99  {
100  for (std::size_t ii = 0; ii < NN; ++ii)
101  {
102  std::size_t hashedToken = std::hash<T>{}(conn[(ss + ii) % NN]);
103  vtkHashCombiner()(hashedValue, hashedToken);
104  // std::cout << " " << conn[(ss + ii) % NN];
105  }
106  }
107  else // backward
108  {
109  for (std::size_t ii = 0; ii < NN; ++ii)
110  {
111  std::size_t hashedToken = std::hash<T>{}(conn[(ss + NN - ii) % NN]);
112  // hashedValue = hashedValue ^ (hashedToken << (ii + 1));
113  vtkHashCombiner()(hashedValue, hashedToken);
114  // std::cout << " " << conn[(ss + NN - ii) % NN];
115  }
116  }
117  // std::cout << " = " << std::hex << hashedValue << std::dec << "\n";
118  return hashedValue;
119  }
120 
138  //
141 
142  template <typename C, typename T = typename C::value_type>
143  void AddSide(
144  vtkStringToken cellType, vtkIdType cell, int side, vtkStringToken shape, const C& conn)
145  {
146  auto hashedValue = this->HashSide(shape, conn);
147  this->Hashes[hashedValue].Sides.insert(Side{ cellType, shape, cell, side });
148  }
150 
152  void Initialize();
153 
154 protected:
155  vtkCellGridSidesCache() = default;
156  ~vtkCellGridSidesCache() override = default;
157 
158  std::unordered_map<std::size_t, Entry> Hashes;
159 
160 private:
162  void operator=(const vtkCellGridSidesCache&) = delete;
163 };
164 
165 VTK_ABI_NAMESPACE_END
166 #endif // vtkCellGridSidesCache_h
vtkStringToken CellType
The type of cell whose side is hashed.
Each hash entry corresponds to one or more sides of one or more cells.
abstract base class for most VTK objects
Definition: vtkObject.h:52
vtkIdType DOF
The degree of freedom starting the hash sequence.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
dynamic, self-adjusting array of vtkIdType
int vtkIdType
Definition: vtkType.h:363
Combine 4- and 8-byte integers into a single hash value.
std::unordered_map< std::size_t, Entry > & GetHashes()
Return the map of hashed side information.
vtkStringToken SideShape
The shape of the side being hashed.
Hash GetId() const
Return the token's ID (usually its hash but possibly not in the case of collisions).
Records held by a hash-entry that represent the side of one cell.
a simple class to control print indentation
Definition: vtkIndent.h:28
Represent a string by its integer hash.
int SideId
The ID of the side being hashed.
std::size_t HashSide(vtkStringToken shape, const C &conn)
Compute the hash of a side (but do not insert a side into the map).
void AddSide(vtkStringToken cellType, vtkIdType cell, int side, vtkStringToken shape, const C &conn)
Add a side with the given shape and connectivity to the request's state.
std::unordered_map< std::size_t, Entry > Hashes
bool operator<(const Side &other) const
Compare side-hashes to allow set insertion.
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
Hold a map from hash-ids (representing sides of cells of multiple types) to details on the cells that...