NEML2 1.4.0
Loading...
Searching...
No Matches
LabeledAxisAccessor.h
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#pragma once
26
27#include <vector>
28#include <iostream>
29
30#include <c10/util/SmallVector.h>
31#include <c10/util/ArrayRef.h>
32
33namespace neml2
34{
47{
48public:
50
52
53 template <typename... S>
54 LabeledAxisAccessor(const char * name, S &&... names)
55 {
56 validate_item_name(name);
57 _item_names.push_back(name);
58
59 (validate_item_name(names), ...);
60 (_item_names.push_back(names), ...);
61 }
62
63 template <typename... S>
64 LabeledAxisAccessor(const std::string & name, S &&... names)
65 {
66 validate_item_name(name);
67 _item_names.push_back(name);
68
69 (validate_item_name(names), ...);
70 (_item_names.push_back(names), ...);
71 }
72
73 template <typename Container,
74 typename = std::enable_if_t<!std::is_convertible_v<Container, std::string>>>
76 {
77 _item_names.append(c.begin(), c.end());
78 for (const auto & name : _item_names)
79 validate_item_name(name);
80 }
81
82 using iterator = c10::SmallVector<std::string>::iterator;
83 using const_iterator = c10::SmallVector<std::string>::const_iterator;
84
91 iterator begin() { return iterator(_item_names.begin()); }
92 iterator end() { return iterator(_item_names.end()); }
93 const_iterator begin() const { return const_iterator(_item_names.begin()); }
94 const_iterator end() const { return const_iterator(_item_names.end()); }
96
97 explicit operator std::vector<std::string>() const;
98
99 const c10::SmallVector<std::string> & vec() const { return _item_names; }
100
101 bool empty() const;
102
103 size_t size() const;
104
106 LabeledAxisAccessor with_suffix(const std::string & suffix) const;
107
110
113
115 LabeledAxisAccessor slice(size_t n) const;
116
118 LabeledAxisAccessor slice(size_t n1, size_t n2) const;
119
121 LabeledAxisAccessor remount(const LabeledAxisAccessor & axis, size_t n = 1) const;
122
124 bool start_with(const LabeledAxisAccessor & axis) const;
125
127 LabeledAxisAccessor old() const;
128
129private:
131 void validate_item_name(const std::string &) const;
132
133 c10::SmallVector<std::string> _item_names;
134};
135
137bool operator==(const LabeledAxisAccessor & a, const LabeledAxisAccessor & b);
138
140bool operator!=(const LabeledAxisAccessor & a, const LabeledAxisAccessor & b);
141
146bool operator<(const LabeledAxisAccessor & a, const LabeledAxisAccessor & b);
147
152std::ostream & operator<<(std::ostream & os, const LabeledAxisAccessor & accessor);
153
154namespace indexing
155{
157using TensorLabels = c10::SmallVector<LabeledAxisAccessor>;
158using TensorLabelsRef = c10::ArrayRef<LabeledAxisAccessor>;
159} // namespace indexing
160} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:56
The accessor containing all the information needed to access an item in a LabeledAxis.
Definition LabeledAxisAccessor.h:47
LabeledAxisAccessor(const std::string &name, S &&... names)
Definition LabeledAxisAccessor.h:64
size_t size() const
Definition LabeledAxisAccessor.cxx:44
const_iterator begin() const
Definition LabeledAxisAccessor.h:93
c10::SmallVector< std::string >::const_iterator const_iterator
Definition LabeledAxisAccessor.h:83
LabeledAxisAccessor append(const LabeledAxisAccessor &axis) const
Append another accessor.
Definition LabeledAxisAccessor.cxx:58
LabeledAxisAccessor prepend(const LabeledAxisAccessor &axis) const
Prepend another accessor.
Definition LabeledAxisAccessor.cxx:64
LabeledAxisAccessor(const char *name, S &&... names)
Definition LabeledAxisAccessor.h:54
bool empty() const
Definition LabeledAxisAccessor.cxx:38
bool start_with(const LabeledAxisAccessor &axis) const
Check if this accessor begins with another accessor.
Definition LabeledAxisAccessor.cxx:95
LabeledAxisAccessor with_suffix(const std::string &suffix) const
Append a suffix to the final item name.
Definition LabeledAxisAccessor.cxx:50
LabeledAxisAccessor remount(const LabeledAxisAccessor &axis, size_t n=1) const
A combination of slice and prepend.
Definition LabeledAxisAccessor.cxx:89
LabeledAxisAccessor old() const
Returns the "old" counterpart.
Definition LabeledAxisAccessor.cxx:101
c10::SmallVector< std::string >::iterator iterator
Definition LabeledAxisAccessor.h:82
LabeledAxisAccessor slice(size_t n) const
Remove the leading n items from the labels.
Definition LabeledAxisAccessor.cxx:72
iterator end()
Definition LabeledAxisAccessor.h:92
const_iterator end() const
Definition LabeledAxisAccessor.h:94
iterator begin()
Definition LabeledAxisAccessor.h:91
const c10::SmallVector< std::string > & vec() const
Definition LabeledAxisAccessor.h:99
~LabeledAxisAccessor()
Definition LabeledAxisAccessor.h:51
LabeledAxisAccessor(const Container &c)
Definition LabeledAxisAccessor.h:75
c10::ArrayRef< LabeledAxisAccessor > TensorLabelsRef
Definition LabeledAxisAccessor.h:158
c10::SmallVector< LabeledAxisAccessor > TensorLabels
Definition LabeledAxisAccessor.h:157
Definition CrossRef.cxx:30
bool operator==(const LabeledAxis &a, const LabeledAxis &b)
Definition LabeledAxis.cxx:393
bool operator!=(const LabeledAxis &a, const LabeledAxis &b)
Definition LabeledAxis.cxx:399
std::ostream & operator<<(std::ostream &os, const EnumSelection &es)
Definition EnumSelection.cxx:31
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:137