Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F88699182
grid_tmpl.hh
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Oct 20, 05:55
Size
5 KB
Mime Type
text/x-c++
Expires
Tue, Oct 22, 05:55 (2 d)
Engine
blob
Format
Raw Data
Handle
21808732
Attached To
rTAMAASPUB tamaas-public
grid_tmpl.hh
View Options
/**
* @file
*
* @author Lucas Frérot <lucas.frerot@epfl.ch>
*
* @section LICENSE
*
* Copyright (©) 2016 EPFL (Ecole Polytechnique Fédérale de
* Lausanne) Laboratory (LSMS - Laboratoire de Simulation en Mécanique des
* Solides)
*
* Tamaas is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* Tamaas is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Tamaas. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* -------------------------------------------------------------------------- */
#ifndef __GRID_TMPL_HH__
#define __GRID_TMPL_HH__
/* -------------------------------------------------------------------------- */
#include "grid.hh"
#include "tamaas.hh"
__BEGIN_TAMAAS__
template
<
typename
T
,
UInt
dim
>
template
<
typename
RandomAccessIterator
>
Grid
<
T
,
dim
>::
Grid
(
RandomAccessIterator
begin
,
RandomAccessIterator
end
,
UInt
nb_components
)
:
GridBase
<
T
>
()
{
if
(
end
-
begin
!=
dim
)
{
TAMAAS_EXCEPTION
(
"Provided sizes ("
<<
end
-
begin
<<
") for grid do not match dimension ("
<<
dim
<<
")"
);
}
this
->
nb_components
=
nb_components
;
this
->
resize
(
begin
,
end
);
}
/* -------------------------------------------------------------------------- */
template
<
typename
T
,
UInt
dim
>
inline
UInt
Grid
<
T
,
dim
>::
computeSize
()
const
{
UInt
size
=
1
;
for
(
UInt
i
=
0
;
i
<
dim
;
i
++
)
size
*=
n
[
i
];
size
*=
this
->
nb_components
;
return
size
;
}
/* -------------------------------------------------------------------------- */
template
<
typename
T
,
UInt
dim
>
template
<
typename
ForwardIt
>
void
Grid
<
T
,
dim
>::
resize
(
ForwardIt
begin
,
ForwardIt
end
)
{
std
::
copy
(
begin
,
end
,
this
->
n
.
begin
());
UInt
size
=
this
->
computeSize
();
GridBase
<
T
>::
resize
(
size
);
this
->
computeStrides
();
}
/* -------------------------------------------------------------------------- */
template
<
typename
T
,
UInt
dim
>
template
<
typename
...
T1
>
inline
UInt
Grid
<
T
,
dim
>::
unpackOffset
(
UInt
offset
,
UInt
index_pos
,
UInt
index
,
T1
...
rest
)
const
{
offset
+=
index
*
strides
[
index_pos
];
return
unpackOffset
(
offset
,
index_pos
+
1
,
rest
...);
// tail-rec bb
}
template
<
typename
T
,
UInt
dim
>
template
<
typename
...
T1
>
inline
UInt
Grid
<
T
,
dim
>::
unpackOffset
(
UInt
offset
,
UInt
index_pos
,
UInt
index
)
const
{
return
offset
+
index
*
strides
[
index_pos
];
}
template
<
typename
T
,
UInt
dim
>
template
<
typename
...
T1
>
inline
T
&
Grid
<
T
,
dim
>::
operator
()(
T1
...
args
)
{
/// Checking access integrity
constexpr
UInt
nargs
=
sizeof
...(
T1
);
static_assert
(
nargs
==
dim
+
1
||
nargs
==
1
||
nargs
==
dim
,
"number of arguments in operator() does not match dimension"
);
constexpr
UInt
start
=
(
nargs
==
1
)
?
dim
:
0
;
UInt
offset
=
unpackOffset
(
0
,
start
,
args
...);
return
this
->
data
[
offset
];
}
template
<
typename
T
,
UInt
dim
>
template
<
typename
...
T1
>
inline
const
T
&
Grid
<
T
,
dim
>::
operator
()(
T1
...
args
)
const
{
/// Checking access integrity
constexpr
UInt
nargs
=
sizeof
...(
T1
);
static_assert
(
nargs
==
dim
+
1
||
nargs
==
1
||
nargs
==
dim
,
"number of arguments in operator() does not match dimension"
);
constexpr
UInt
start
=
(
nargs
==
1
)
?
dim
:
0
;
UInt
offset
=
unpackOffset
(
0
,
start
,
args
...);
return
this
->
data
[
offset
];
}
/* -------------------------------------------------------------------------- */
template
<
typename
T
,
UInt
dim
>
inline
UInt
Grid
<
T
,
dim
>::
computeOffset
(
std
::
array
<
UInt
,
dim
+
1
>
tuple
)
const
{
return
std
::
inner_product
(
tuple
.
begin
(),
tuple
.
end
(),
strides
.
begin
(),
0
);
}
template
<
typename
T
,
UInt
dim
>
inline
T
&
Grid
<
T
,
dim
>::
operator
()(
std
::
array
<
UInt
,
dim
+
1
>
tuple
)
{
UInt
offset
=
computeOffset
(
tuple
);
return
this
->
data
[
offset
];
}
template
<
typename
T
,
UInt
dim
>
inline
const
T
&
Grid
<
T
,
dim
>::
operator
()(
std
::
array
<
UInt
,
dim
+
1
>
tuple
)
const
{
UInt
offset
=
computeOffset
(
tuple
);
return
this
->
data
[
offset
];
}
/* -------------------------------------------------------------------------- */
template
<
typename
T
,
UInt
dim
>
Grid
<
T
,
dim
>&
Grid
<
T
,
dim
>::
operator
=
(
const
Grid
<
T
,
dim
>&
other
)
{
this
->
copy
(
other
);
return
*
this
;
}
template
<
typename
T
,
UInt
dim
>
Grid
<
T
,
dim
>&
Grid
<
T
,
dim
>::
operator
=
(
Grid
<
T
,
dim
>&
other
)
{
this
->
copy
(
other
);
return
*
this
;
}
template
<
typename
T
,
UInt
dim
>
Grid
<
T
,
dim
>&
Grid
<
T
,
dim
>::
operator
=
(
Grid
<
T
,
dim
>&&
other
)
{
this
->
move
(
std
::
move
(
other
));
return
*
this
;
}
/* -------------------------------------------------------------------------- */
template
<
typename
T
,
UInt
dim
>
template
<
typename
T1
>
void
Grid
<
T
,
dim
>::
copy
(
const
Grid
<
T1
,
dim
>&
other
)
{
GridBase
<
T
>::
copy
(
other
);
this
->
n
=
other
.
n
;
this
->
strides
=
other
.
strides
;
}
template
<
typename
T
,
UInt
dim
>
template
<
typename
T1
>
void
Grid
<
T
,
dim
>::
move
(
Grid
<
T1
,
dim
>&&
other
)
{
GridBase
<
T
>::
move
(
std
::
move
(
other
));
this
->
n
=
std
::
move
(
other
.
n
);
this
->
strides
=
std
::
move
(
other
.
strides
);
}
/* -------------------------------------------------------------------------- */
/* Stream operator */
/* -------------------------------------------------------------------------- */
template
<
typename
T
,
UInt
dim
>
inline
std
::
ostream
&
operator
<<
(
std
::
ostream
&
stream
,
const
Grid
<
T
,
dim
>&
_this
)
{
_this
.
printself
(
stream
);
return
stream
;
}
__END_TAMAAS__
/* -------------------------------------------------------------------------- */
#endif
// __GRID_TMPL_HH__
Event Timeline
Log In to Comment