Merge topic 'getfieldindex-not-throw-backport' into release-1.8

8587c8282 Merge branch 'release' into getfieldindex-not-throw-backport
c0364e71c DataSet::GetFieldIndex should not throw

Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: Vicente Bolea <vicente.bolea@kitware.com>
Merge-request: !2829
This commit is contained in:
Sujin Philip 2022-08-03 12:40:09 +00:00 committed by Kitware Robot
commit 3b9dd256d3
2 changed files with 29 additions and 42 deletions

@ -59,16 +59,35 @@ vtkm::cont::Field& DataSet::GetField(vtkm::Id index)
vtkm::Id DataSet::GetFieldIndex(const std::string& name, vtkm::cont::Field::Association assoc) const
{
bool found;
vtkm::Id index = this->FindFieldIndex(name, assoc, found);
if (found)
const auto it = this->Fields.find({ name, assoc });
if (it != this->Fields.end())
{
return index;
return static_cast<vtkm::Id>(std::distance(this->Fields.begin(), it));
}
else
return -1;
}
const vtkm::cont::Field& DataSet::GetField(const std::string& name,
vtkm::cont::Field::Association assoc) const
{
auto idx = this->GetFieldIndex(name, assoc);
if (idx == -1)
{
throw vtkm::cont::ErrorBadValue("No field with requested name: " + name);
}
return this->GetField(idx);
}
vtkm::cont::Field& DataSet::GetField(const std::string& name, vtkm::cont::Field::Association assoc)
{
auto idx = this->GetFieldIndex(name, assoc);
if (idx == -1)
{
throw vtkm::cont::ErrorBadValue("No field with requested name: " + name);
}
return this->GetField(idx);
}
const vtkm::cont::CoordinateSystem& DataSet::GetCoordinateSystem(vtkm::Id index) const
@ -150,21 +169,6 @@ void DataSet::PrintSummary(std::ostream& out) const
out.flush();
}
vtkm::Id DataSet::FindFieldIndex(const std::string& name,
vtkm::cont::Field::Association association,
bool& found) const
{
const auto it = this->Fields.find({ name, association });
if (it != this->Fields.end())
{
found = true;
return static_cast<vtkm::Id>(std::distance(this->Fields.begin(), it));
}
found = false;
return -1;
}
VTKM_CONT void DataSet::AddField(const Field& field)
{
// map::insert will not replace the duplicated element

@ -50,25 +50,19 @@ public:
bool HasField(const std::string& name,
vtkm::cont::Field::Association assoc = vtkm::cont::Field::Association::Any) const
{
bool found = false;
this->FindFieldIndex(name, assoc, found);
return found;
return (this->GetFieldIndex(name, assoc) != -1);
}
VTKM_CONT
bool HasCellField(const std::string& name) const
{
bool found = false;
this->FindFieldIndex(name, vtkm::cont::Field::Association::Cells, found);
return found;
return (this->GetFieldIndex(name, vtkm::cont::Field::Association::Cells) != -1);
}
VTKM_CONT
bool HasPointField(const std::string& name) const
{
bool found = false;
this->FindFieldIndex(name, vtkm::cont::Field::Association::Points, found);
return found;
return (this->GetFieldIndex(name, vtkm::cont::Field::Association::Points) != -1);
}
@ -85,18 +79,12 @@ public:
VTKM_CONT
const vtkm::cont::Field& GetField(
const std::string& name,
vtkm::cont::Field::Association assoc = vtkm::cont::Field::Association::Any) const
{
return this->GetField(this->GetFieldIndex(name, assoc));
}
vtkm::cont::Field::Association assoc = vtkm::cont::Field::Association::Any) const;
VTKM_CONT
vtkm::cont::Field& GetField(
const std::string& name,
vtkm::cont::Field::Association assoc = vtkm::cont::Field::Association::Any)
{
return this->GetField(this->GetFieldIndex(name, assoc));
}
vtkm::cont::Field::Association assoc = vtkm::cont::Field::Association::Any);
//@}
/// Returns the first cell field that matches the provided name.
@ -296,11 +284,6 @@ private:
std::vector<vtkm::cont::CoordinateSystem> CoordSystems;
std::map<FieldCompare::Key, vtkm::cont::Field, FieldCompare> Fields;
vtkm::cont::UnknownCellSet CellSet;
VTKM_CONT
vtkm::Id FindFieldIndex(const std::string& name,
vtkm::cont::Field::Association association,
bool& found) const;
};
} // namespace cont