Line data Source code
1 : // Copyright 2024, UChicago Argonne, LLC
2 : // All Rights Reserved
3 : // Software Name: NEML2 -- the New Engineering material Model Library, version 2
4 : // By: Argonne National Laboratory
5 : // OPEN SOURCE LICENSE (MIT)
6 : //
7 : // Permission is hereby granted, free of charge, to any person obtaining a copy
8 : // of this software and associated documentation files (the "Software"), to deal
9 : // in the Software without restriction, including without limitation the rights
10 : // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 : // copies of the Software, and to permit persons to whom the Software is
12 : // furnished to do so, subject to the following conditions:
13 : //
14 : // The above copyright notice and this permission notice shall be included in
15 : // all copies or substantial portions of the Software.
16 : //
17 : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 : // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 : // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 : // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 : // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 : // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 : // THE SOFTWARE.
24 :
25 : #include "neml2/base/MultiEnumSelection.h"
26 : #include "neml2/base/Parser.h"
27 : #include "neml2/misc/assertions.h"
28 :
29 : namespace neml2
30 : {
31 : std::ostream &
32 1 : operator<<(std::ostream & os, const MultiEnumSelection & es)
33 : {
34 1 : auto selections = std::vector<std::string>(es);
35 1 : os << '(';
36 3 : for (size_t i = 0; i < selections.size(); i++)
37 : {
38 2 : os << selections[i];
39 2 : if (i < selections.size() - 1)
40 1 : os << ", ";
41 : }
42 1 : os << ')';
43 1 : return os;
44 1 : }
45 :
46 : std::stringstream &
47 37 : operator>>(std::stringstream & ss, MultiEnumSelection & es)
48 : {
49 39 : es.select(utils::parse_vector<std::string>(ss.str()));
50 36 : return ss;
51 : }
52 :
53 3 : MultiEnumSelection::MultiEnumSelection(const std::vector<std::string> & candidates,
54 3 : const std::vector<std::string> & selections)
55 3 : : EnumSelectionBase(candidates)
56 : {
57 3 : select(selections);
58 5 : }
59 :
60 19 : MultiEnumSelection::MultiEnumSelection(const std::vector<std::string> & candidates,
61 : const std::vector<int> & values,
62 19 : const std::vector<std::string> & selections)
63 19 : : EnumSelectionBase(candidates, values)
64 : {
65 19 : select(selections);
66 21 : }
67 :
68 : bool
69 4 : MultiEnumSelection::operator==(const MultiEnumSelection & other) const
70 : {
71 5 : return _candidate_map == other._candidate_map && _selections == other._selections &&
72 5 : _values == other._values;
73 : }
74 :
75 : bool
76 3 : MultiEnumSelection::operator!=(const MultiEnumSelection & other) const
77 : {
78 3 : return !(*this == other);
79 : }
80 :
81 : void
82 59 : MultiEnumSelection::select(const std::vector<std::string> & selections)
83 : {
84 59 : _selections.clear();
85 59 : _values.clear();
86 160 : for (const auto & selection : selections)
87 : {
88 104 : neml_assert(_candidate_map.count(selection),
89 : "Invalid selection for MultiEnumSelection. Candidates are ",
90 211 : candidates_str());
91 101 : _selections.push_back(selection);
92 101 : _values.push_back(_candidate_map[selection]);
93 : }
94 56 : }
95 : } // namesace neml2
|