NEML2 1.4.0
Loading...
Searching...
No Matches
LabeledAxisAccessor.cxx
1// Copyright 2023, 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/tensors/LabeledAxisAccessor.h"
26#include "neml2/misc/error.h"
27
28namespace neml2
29{
30LabeledAxisAccessor::LabeledAxisAccessor(const std::vector<std::string> & names)
31 : _item_names(names)
32{
33 for (const auto & name : _item_names)
34 validate_item_name(name);
35}
36
41
44{
45 _item_names = other._item_names;
46 return *this;
47}
48
49LabeledAxisAccessor::operator std::vector<std::string>() const { return _item_names; }
50
51bool
53{
54 return _item_names.empty();
55}
56
57size_t
59{
60 return _item_names.size();
61}
62
64LabeledAxisAccessor::with_suffix(const std::string & suffix) const
65{
66 auto new_names = _item_names;
67 new_names.back() += suffix;
68 return new_names;
69}
70
73{
74 return axis.on(*this);
75}
76
79{
80 auto new_names = axis._item_names;
81 new_names.insert(new_names.end(), _item_names.begin(), _item_names.end());
82 return new_names;
83}
84
87{
88 auto new_names = _item_names;
89 new_names.erase(new_names.begin(), new_names.begin() + n);
90 return new_names;
91}
92
94LabeledAxisAccessor::slice(size_t n1, size_t n2) const
95{
96 auto new_names = _item_names;
97 new_names.erase(new_names.begin() + n2, new_names.end());
98 new_names.erase(new_names.begin(), new_names.begin() + n1);
99 return new_names;
100}
101
102bool
104{
105 return slice(0, axis.size()) == axis;
106}
107
108void
109LabeledAxisAccessor::validate_item_name(const std::string & name) const
110{
111 const auto x = name.find_first_of(" .,;/\t\n\v\f\r");
112 neml_assert(x == std::string::npos,
113 "Invalid item name: ",
114 name,
115 ". The item names cannot contain whitespace, '.', ',', ';', or '/'.");
116}
117
118bool
120{
121 return a.vec() != b.vec();
122}
123
124bool
126{
127 return a.vec() == b.vec();
128}
129
130bool
132{
133 return a.vec() < b.vec();
134}
135
136std::ostream &
137operator<<(std::ostream & os, const LabeledAxisAccessor & accessor)
138{
139 for (size_t i = 0; i < accessor.vec().size(); i++)
140 {
141 if (i != 0)
142 os << "/";
143 os << accessor.vec()[i];
144 }
145 return os;
146}
147} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
The accessor containing all the information needed to access an item in a LabeledAxis.
Definition LabeledAxisAccessor.h:44
size_t size() const
Definition LabeledAxisAccessor.cxx:58
LabeledAxisAccessor append(const LabeledAxisAccessor &axis) const
Add a new item.
Definition LabeledAxisAccessor.cxx:72
bool empty() const
Definition LabeledAxisAccessor.cxx:52
bool start_with(const LabeledAxisAccessor &axis) const
Check if this accessor begins with another accessor.
Definition LabeledAxisAccessor.cxx:103
LabeledAxisAccessor with_suffix(const std::string &suffix) const
Append a suffix to the item name.
Definition LabeledAxisAccessor.cxx:64
LabeledAxisAccessor slice(size_t n) const
Remove the leading n items from the labels.
Definition LabeledAxisAccessor.cxx:86
LabeledAxisAccessor & operator=(const LabeledAxisAccessor &other)
Assignment operator.
Definition LabeledAxisAccessor.cxx:43
LabeledAxisAccessor on(const LabeledAxisAccessor &axis) const
Re-mount the LabeledAxisAccessor on an axis by the axis accessor.
Definition LabeledAxisAccessor.cxx:78
Definition CrossRef.cxx:32
std::ostream & operator<<(std::ostream &os, const OptionCollection &p)
Definition OptionCollection.cxx:37
bool operator==(const LabeledAxis &a, const LabeledAxis &b)
Definition LabeledAxis.cxx:461
bool operator!=(const LabeledAxis &a, const LabeledAxis &b)
Definition LabeledAxis.cxx:467
bool operator<(const LabeledAxisAccessor &a, const LabeledAxisAccessor &b)
The (strict) smaller than operator is created so as to use LabeledAxisAccessor in sorted data structu...
Definition LabeledAxisAccessor.cxx:131
void neml_assert(bool assertion, Args &&... args)
Definition error.h:73