Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
usadelndsoc
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
JYU Condensed Matter Theory
usadelndsoc
Commits
7745b0e9
Commit
7745b0e9
authored
2 years ago
by
patavirt
Browse files
Options
Downloads
Patches
Plain Diff
array: add StoredArray
parent
a88f5a04
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/array.hpp
+60
-12
60 additions, 12 deletions
src/array.hpp
with
60 additions
and
12 deletions
src/array.hpp
+
60
−
12
View file @
7745b0e9
...
...
@@ -106,6 +106,21 @@ namespace detail
using
type
=
typename
TailNth
<
I
-
1
,
typename
Shape
::
Tail
>::
type
;
};
template
<
typename
Shape
>
inline
size_t
size_from_shape
(
const
std
::
array
<
size_t
,
Shape
::
ndim
>&
shape
)
{
size_t
total
=
1
;
for
(
size_t
i
=
0
;
i
<
Shape
::
ndim
;
++
i
)
{
if
(
Shape
::
dim
(
i
)
==
Dynamic
)
total
*=
shape
[
i
];
else
total
*=
Shape
::
dim
(
i
);
}
return
total
;
}
/**
* \class fixed_base
*
...
...
@@ -227,7 +242,7 @@ namespace detail
template
<
typename
Scalar
,
typename
Shape
,
size_t
Alignment
=
0
,
bool
BoundsCheck
=
true
>
class
Array
:
public
detail
::
Base
<
Shape
,
BoundsCheck
>
{
pr
ivate
:
pr
otected
:
typedef
detail
::
Base
<
Shape
,
BoundsCheck
>
Base
;
Scalar
*
data_
;
...
...
@@ -252,27 +267,28 @@ private:
}
};
public
:
Array
(
Scalar
*
data
,
size_t
size
,
const
std
::
array
<
size_t
,
Shape
::
ndim
>
shape
)
:
Base
(
shape
),
data_
(
data
)
void
data_check
(
size_t
size
)
{
static_assert
(
!
Shape
::
fixed
,
"array shape must not be compile-time fixed"
);
Base
::
data_bounds_check
(
size
);
if
constexpr
(
BoundsCheck
)
{
if
(
reinterpret_cast
<
intptr_t
>
(
data
)
%
alignment
()
!=
0
)
if
(
reinterpret_cast
<
intptr_t
>
(
data
_
)
%
alignment
()
!=
0
)
throw
std
::
runtime_error
(
"data is not aligned"
);
}
};
public
:
Array
(
Scalar
*
data
,
size_t
size
,
const
std
::
array
<
size_t
,
Shape
::
ndim
>
shape
)
:
Base
(
shape
),
data_
(
data
)
{
static_assert
(
!
Shape
::
fixed
,
"array shape must not be compile-time fixed"
);
data_check
(
size
);
}
Array
(
Scalar
*
data
,
size_t
size
)
:
Base
(),
data_
(
data
)
{
static_assert
(
Shape
::
fixed
,
"array shape must be compile-time fixed"
);
Base
::
data_bounds_check
(
size
);
if
constexpr
(
BoundsCheck
)
{
if
(
reinterpret_cast
<
intptr_t
>
(
data
)
%
alignment
()
!=
0
)
throw
std
::
runtime_error
(
"data is not aligned"
);
}
data_check
(
size
);
}
template
<
typename
...
Idx
>
...
...
@@ -357,7 +373,7 @@ public:
}
typedef
Eigen
::
Matrix
<
Scalar
,
eigen_shape
<
0
>
(),
eigen_shape
<
1
>
(),
Eigen
::
RowMajor
>
EigenMatrix
;
typedef
Eigen
::
Map
<
EigenMatrix
,
eigen_alignment
()
>
EigenMap
;
typedef
Eigen
::
Map
<
EigenMatrix
>
EigenMap
;
EigenMap
matrix
()
const
{
...
...
@@ -368,6 +384,38 @@ public:
operator
EigenMap
()
const
{
return
matrix
();
}
};
/**
* \class StoredArray
*
* \brief Array that is also the storage for the data.
*/
template
<
typename
Scalar
,
typename
Shape
,
size_t
Alignment
=
0
,
bool
BoundsCheck
=
true
>
class
StoredArray
:
public
Array
<
Scalar
,
Shape
,
Alignment
,
BoundsCheck
>
{
private:
typedef
Array
<
Scalar
,
Shape
,
Alignment
,
BoundsCheck
>
BaseType
;
typedef
std
::
vector
<
Scalar
>
Storage
;
Storage
storage_
;
public:
StoredArray
(
const
std
::
array
<
size_t
,
Shape
::
ndim
>
shape
)
:
BaseType
(
nullptr
,
detail
::
size_from_shape
<
Shape
>
(
shape
),
shape
),
storage_
(
detail
::
size_from_shape
<
Shape
>
(
shape
))
{
BaseType
::
data_
=
storage_
.
data
();
BaseType
::
data_check
(
storage_
.
size
());
}
StoredArray
()
:
BaseType
(
nullptr
,
Shape
::
size
),
storage_
(
Shape
::
size
)
{
BaseType
::
data_
=
storage_
.
data
();
BaseType
::
data_check
(
storage_
.
size
());
}
};
}
// namespace array
#endif
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment