Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F77923919
grid_view.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
Sat, Aug 17, 06:08
Size
5 KB
Mime Type
text/x-c++
Expires
Mon, Aug 19, 06:08 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
19947776
Attached To
rTAMAASPUB tamaas-public
grid_view.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_VIEW_HH__
#define __GRID_VIEW_HH__
/* -------------------------------------------------------------------------- */
#include "grid.hh"
#include "tamaas.hh"
#include <vector>
/* -------------------------------------------------------------------------- */
__BEGIN_TAMAAS__
/* -------------------------------------------------------------------------- */
/**
* @brief View type on grid
* This is a view on a *contiguous* chunk of data defined by a grid
*/
template
<
template
<
typename
,
UInt
>
class
Base
,
typename
T
,
UInt
base_dim
,
UInt
dim
>
class
GridView
:
public
Base
<
T
,
dim
>
{
public
:
using
iterator
=
typename
Base
<
T
,
dim
>::
iterator
;
using
const_iterator
=
typename
Base
<
T
,
dim
>::
const_iterator
;
public
:
/// Constructor
GridView
(
GridBase
<
typename
Base
<
T
,
dim
>::
value_type
>&
grid_base
,
const
std
::
vector
<
UInt
>&
multi_index
,
Int
component
=
-
1
);
/// Move constructor
GridView
(
GridView
&&
o
)
:
Base
<
T
,
dim
>
(
std
::
forward
<
Base
<
T
,
dim
>>
(
o
)),
grid
(
std
::
exchange
(
o
.
grid
,
nullptr
))
{}
/// Destructor
~
GridView
()
override
=
default
;
UInt
dataSize
()
const
override
{
return
this
->
computeSize
();
}
/// Iterators
iterator
begin
(
UInt
n
=
1
)
override
{
return
iterator
(
this
->
getInternalData
(),
this
->
strides
.
back
());
}
iterator
end
(
UInt
n
=
1
)
override
{
return
iterator
(
this
->
getInternalData
()
+
this
->
dataSize
()
*
this
->
strides
.
back
(),
this
->
strides
.
back
());
}
const_iterator
begin
(
UInt
n
=
1
)
const
override
{
return
const_iterator
(
this
->
getInternalData
(),
this
->
strides
.
back
());
}
const_iterator
end
(
UInt
n
=
1
)
const
override
{
return
const_iterator
(
this
->
getInternalData
()
+
this
->
dataSize
()
*
this
->
strides
.
back
(),
this
->
strides
.
back
());
}
protected
:
Base
<
T
,
base_dim
>*
grid
;
};
/* -------------------------------------------------------------------------- */
template
<
template
<
typename
,
UInt
>
class
Base
,
typename
T
,
UInt
base_dim
,
typename
...
Args
>
GridView
<
Base
,
T
,
base_dim
,
base_dim
-
sizeof
...(
Args
)
>
make_view
(
Base
<
T
,
base_dim
>&
base
,
Args
...
indices
)
{
std
::
vector
<
UInt
>
multi_index
=
{
static_cast
<
UInt
>
(
indices
)...};
return
GridView
<
Base
,
T
,
base_dim
,
base_dim
-
sizeof
...(
Args
)
>
(
base
,
multi_index
);
}
template
<
template
<
typename
,
UInt
>
class
Base
,
typename
T
,
UInt
base_dim
>
GridView
<
Base
,
T
,
base_dim
,
base_dim
>
make_component_view
(
Base
<
T
,
base_dim
>&
base
,
UInt
component
)
{
std
::
vector
<
UInt
>
multi_index
{};
return
GridView
<
Base
,
T
,
base_dim
,
base_dim
>
(
base
,
multi_index
,
component
);
}
/* -------------------------------------------------------------------------- */
/* Template implementation */
/* -------------------------------------------------------------------------- */
template
<
template
<
typename
,
UInt
>
class
Base
,
typename
T
,
UInt
base_dim
,
UInt
dim
>
GridView
<
Base
,
T
,
base_dim
,
dim
>::
GridView
(
GridBase
<
typename
Base
<
T
,
dim
>::
value_type
>&
grid_base
,
const
std
::
vector
<
UInt
>&
multi_index
,
Int
component
)
:
Base
<
T
,
dim
>
(),
grid
(
nullptr
)
{
// static_assert(base_dim >= dim,
// "view dimension must be >= than the base class");
// Checking view type
grid
=
dynamic_cast
<
Base
<
T
,
base_dim
>*>
(
&
grid_base
);
if
(
!
grid
)
TAMAAS_EXCEPTION
(
"given base type is incompatible with view"
);
constexpr
int
dim_offset
=
base_dim
-
dim
;
if
(
dim_offset
>=
0
and
dim_offset
!=
static_cast
<
Int
>
(
multi_index
.
size
()))
TAMAAS_EXCEPTION
(
"Number of blocked indices ("
<<
multi_index
.
size
()
<<
") does not match view dimension ("
<<
base_dim
<<
" -> "
<<
dim
<<
")"
);
std
::
copy
(
grid
->
sizes
().
begin
()
+
std
::
max
(
dim_offset
,
0
),
grid
->
sizes
().
end
(),
this
->
n
.
begin
()
+
std
::
max
(
-
dim_offset
,
0
));
std
::
copy
(
grid
->
getStrides
().
begin
()
+
std
::
max
(
dim_offset
,
0
),
grid
->
getStrides
().
end
(),
this
->
strides
.
begin
()
+
std
::
max
(
-
dim_offset
,
0
));
if
(
dim_offset
<
0
)
std
::
fill_n
(
this
->
n
.
begin
(),
-
dim_offset
,
1
);
if
(
component
==
-
1
)
{
// view all components
this
->
nb_components
=
grid
->
getNbComponents
();
component
=
0
;
}
else
if
(
component
>=
0
and
component
<
static_cast
<
Int
>
(
grid
->
getNbComponents
()))
{
// view one component
this
->
nb_components
=
1
;
this
->
strides
.
back
()
=
grid
->
getNbComponents
();
}
else
TAMAAS_EXCEPTION
(
"Components out of bounds for view"
);
auto
offset
=
std
::
inner_product
(
multi_index
.
begin
(),
multi_index
.
end
(),
grid
->
getStrides
().
begin
(),
component
);
this
->
data
.
wrapMemory
(
grid
->
getInternalData
()
+
offset
,
this
->
computeSize
());
}
/* -------------------------------------------------------------------------- */
__END_TAMAAS__
#endif
// __GRID_VIEW_HH__
Event Timeline
Log In to Comment