NimbleSM
NimbleSM is a solid mechanics simulation code for dynamic systems
Loading...
Searching...
No Matches
nimble.quanta.h
Go to the documentation of this file.
1/*
2//@HEADER
3// ************************************************************************
4//
5// NimbleSM
6// Copyright 2018
7// National Technology & Engineering Solutions of Sandia, LLC (NTESS)
8//
9// Under the terms of Contract DE-NA0003525 with NTESS, the U.S. Government
10// retains certain rights in this software.
11//
12// Redistribution and use in source and binary forms, with or without
13// modification, are permitted provided that the following conditions are
14// met:
15//
16// 1. Redistributions of source code must retain the above copyright
17// notice, this list of conditions and the following disclaimer.
18//
19// 2. Redistributions in binary form must reproduce the above copyright
20// notice, this list of conditions and the following disclaimer in the
21// documentation and/or other materials provided with the distribution.
22//
23// 3. Neither the name of the Corporation nor the names of the
24// contributors may be used to endorse or promote products derived from
25// this software without specific prior written permission.
26//
27// THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY EXPRESS OR IMPLIED
28// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
29// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
30// NO EVENT SHALL NTESS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
32// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37//
38// Questions? Contact David Littlewood (djlittl@sandia.gov)
39//
40// ************************************************************************
41//@HEADER
42*/
43
44#ifndef NIMBLE_QUANTA_H
45#define NIMBLE_QUANTA_H
46
47namespace nimble {
48namespace quanta {
49template <class T>
50T
51declval_exact() noexcept;
52
53template <class T>
54typename std::remove_reference<T>::type&
55declref() noexcept;
56
57// Gets the literal type of a container when it's iterator is dereferenced
58// for example, 'iterated_t<std::vector<int>>' is of type 'int&', and
59// 'iterated_t<const std::vector<int>>' is of type 'const int&'
60template <class list_t>
61using iterated_t = decltype(*std::begin(declref<list_t>()));
62
63// Gets the unmodified type of a container when it's iterator is defererenced
64// For example, 'elem_t<std::vector<int>>' is of type 'int'
65template <class list_t>
66using elem_t = typename std::decay<iterated_t<list_t>>::type;
67
68template <class F, class... Args>
69using return_t = decltype(declref<F>()(declval_exact<Args>()...));
70
71template <class F, class... list_t>
73
74template <class T>
75auto
76len(T&& obj) -> decltype(obj.size())
77{
78 return obj.size();
79}
80
81template <class list_t>
83{
84 list_t& list;
85 constexpr indexer_t(list_t& list) : list(list) {}
86 constexpr indexer_t(const indexer_t&) = default;
87 template <class int_t>
88 auto
89 operator()(int_t&& index) const -> decltype(list[index])
90 {
91 return list[index];
92 }
93};
94
95template <class T>
96struct indexer_t<T*>
97{
98 T* const ptr;
99 constexpr indexer_t(T* const ptr) : ptr(ptr) {}
100 indexer_t(const indexer_t&) = default;
101 template <class int_t>
102 T&
103 operator()(int_t&& index) const
104 {
105 return ptr[index];
106 }
107};
108
109template <class T>
110indexer_t<T>
112{
113 return indexer_t<T>(list);
114}
115
116template <class T>
117indexer_t<T*>
119{
120 return indexer_t<T*>(ptr);
121}
122
123template <class int_t = int>
125{
126 mutable int_t count;
127
128 public:
129 invoke_counter_t() : count{} {}
130 invoke_counter_t(int_t i) : count{i} {}
134 operator=(const invoke_counter_t&) = default;
137 void
138 reset(const int_t& value = 0)
139 {
140 count = value;
141 }
142 int_t&
143 get_count() const
144 {
145 return count;
146 }
147 auto
148 operator()() const -> decltype(count++)
149 {
150 return count++;
151 }
152 auto
153 increment() const -> decltype(count++)
154 {
155 return count++;
156 }
157};
158template <class int_t>
159invoke_counter_t<int_t>
160make_counter(int_t initial = 0)
161{
162 return invoke_counter_t<int_t>{initial};
163}
164
165template <class list_t, class F>
166void
167remap(list_t& list, F&& func)
168{
169 for (auto& elem : list) { elem = func(elem); }
170}
171
172} // namespace quanta
173} // namespace nimble
174
175#endif // NIMBLE_QUANTA_H
Definition nimble.quanta.h:125
auto operator()() const -> decltype(count++)
Definition nimble.quanta.h:148
invoke_counter_t & operator=(const invoke_counter_t &)=default
auto increment() const -> decltype(count++)
Definition nimble.quanta.h:153
void reset(const int_t &value=0)
Definition nimble.quanta.h:138
invoke_counter_t(const invoke_counter_t &)=default
int_t & get_count() const
Definition nimble.quanta.h:143
invoke_counter_t(invoke_counter_t &&)=default
invoke_counter_t()
Definition nimble.quanta.h:129
invoke_counter_t(int_t i)
Definition nimble.quanta.h:130
invoke_counter_t & operator=(invoke_counter_t &&)=default
Definition nimble.quanta.arrayview.h:53
void remap(list_t &list, F &&func)
Definition nimble.quanta.h:167
decltype(*std::begin(declref< list_t >())) iterated_t
Definition nimble.quanta.h:61
T declval_exact() noexcept
indexer_t< T > make_indexer(T &list)
Definition nimble.quanta.h:111
return_t< F, iterated_t< list_t >... > transformed_iterated_t
Definition nimble.quanta.h:72
std::remove_reference< T >::type & declref() noexcept
typename std::decay< iterated_t< list_t > >::type elem_t
Definition nimble.quanta.h:66
decltype(declref< F >()(declval_exact< Args >()...)) return_t
Definition nimble.quanta.h:69
invoke_counter_t< int_t > make_counter(int_t initial=0)
Definition nimble.quanta.h:160
auto len(T &&obj) -> decltype(obj.size())
Definition nimble.quanta.h:76
Definition kokkos_contact_manager.h:49
T & operator()(int_t &&index) const
Definition nimble.quanta.h:103
constexpr indexer_t(T *const ptr)
Definition nimble.quanta.h:99
T *const ptr
Definition nimble.quanta.h:98
indexer_t(const indexer_t &)=default
Definition nimble.quanta.h:83
constexpr indexer_t(list_t &list)
Definition nimble.quanta.h:85
constexpr indexer_t(const indexer_t &)=default
auto operator()(int_t &&index) const -> decltype(list[index])
Definition nimble.quanta.h:89
list_t & list
Definition nimble.quanta.h:84