https://mooseframework.inl.gov
MapConversionUtils.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 namespace Moose
13 {
21 template <typename T, typename C>
22 std::map<T, C> createMapFromVectors(const std::vector<T> & keys, const std::vector<C> & values);
23 
30 template <typename T>
31 std::map<T, MooseEnum> createMapFromVectorAndMultiMooseEnum(const std::vector<T> & keys,
32  const MultiMooseEnum & values);
33 
34 template <typename T, typename C>
35 std::map<T, C>
36 createMapFromVectors(const std::vector<T> & keys, const std::vector<C> & values)
37 {
38  std::map<T, C> map;
39  mooseAssert(keys.size() == values.size(),
40  "Map should be made from keys (" + std::to_string(keys.size()) + ") and values (" +
41  std::to_string(values.size()) + ") of the same size");
42 
43  // No values have been specified.
44  if (!values.size())
45  {
46  return map;
47  }
48  std::transform(keys.begin(),
49  keys.end(),
50  values.begin(),
51  std::inserter(map, map.end()),
52  [](const T & a, const C & b) { return std::make_pair(a, b); });
53  return map;
54 }
55 
56 template <typename T>
57 std::map<T, MooseEnum>
58 createMapFromVectorAndMultiMooseEnum(const std::vector<T> & keys, const MultiMooseEnum & values)
59 {
60  std::map<T, MooseEnum> map;
61  mooseAssert(keys.size() == values.size(),
62  "Map should be made from keys and values of the same size");
63  // No values have been specified. We cant form a map of empty MooseEnum
64  if (!values.size())
65  return map;
66  std::transform(keys.begin(),
67  keys.end(),
68  values.begin(),
69  std::inserter(map, map.end()),
70  [values](const T & a, const MooseEnumItem & b)
71  {
72  // Create a MooseEnum from the available values in the MultiMooseEnum and an
73  // actual current active item from that same MultiMooseEnum
74  MooseEnum single_value(values.getRawNames(), b.name());
75  return std::make_pair(a, single_value);
76  });
77  return map;
78 }
79 }
const std::string & name() const
Definition: MooseEnumItem.h:35
MooseEnumIterator begin() const
Returns a begin/end iterator to all of the set values in the enum.
unsigned int size() const
Return the number of active items in the MultiMooseEnum.
std::string getRawNames() const
Method for returning the raw name strings for this instance.
std::map< T, MooseEnum > createMapFromVectorAndMultiMooseEnum(const std::vector< T > &keys, const MultiMooseEnum &values)
Create a map from a vector of keys and MultiMooseEnum acting as a vector.
std::map< T, C > createMapFromVectors(const std::vector< T > &keys, const std::vector< C > &values)
Create a map from two vectors.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:33
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:18
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type...