Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F65951422
array.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
Fri, Jun 7, 07:34
Size
4 KB
Mime Type
text/x-c++
Expires
Sun, Jun 9, 07:34 (2 d)
Engine
blob
Format
Raw Data
Handle
18150121
Attached To
rTAMAAS tamaas
array.hh
View Options
/**
* @file
*
* @author Guillaume Anciaux <guillaume.anciaux@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 __ARRAY__HH__
#define __ARRAY__HH__
/* -------------------------------------------------------------------------- */
#include "tamaas.hh"
#include <thrust/fill.h>
#include <thrust/copy.h>
#include <memory>
#include <utility>
/* -------------------------------------------------------------------------- */
__BEGIN_TAMAAS__
/**
* @brief Generic storage class with wrapping capacities
*/
template
<
typename
T
>
class
Array
final
{
public
:
/// Default
Array
()
=
default
;
/// Empty array of given size
Array
(
UInt
size
)
:
Array
()
{
this
->
resize
(
size
);
}
/// Copy constructor (deep)
Array
(
const
Array
&
v
)
:
Array
()
{
this
->
resize
(
v
.
_size
);
std
::
copy
(
v
.
_data
,
v
.
_data
+
v
.
_size
,
this
->
_data
);
}
/// Move constructor (transfers data ownership)
Array
(
Array
&&
v
)
:
Array
()
{
_data
=
std
::
exchange
(
v
.
_data
,
nullptr
);
_size
=
std
::
exchange
(
v
.
_size
,
0
);
wrapped
=
std
::
exchange
(
v
.
wrapped
,
false
);
}
/// Wrap array on data
Array
(
T
*
data
,
UInt
size
)
:
Array
()
{
this
->
wrapMemory
(
data
,
size
);
}
/// Destructor
~
Array
()
{
if
(
wrapped
==
false
)
_alloc
.
deallocate
(
_data
,
_size
);
}
public
:
/// Copy operator
Array
&
operator
=
(
const
Array
&
v
)
{
this
->
wrapped
=
false
;
if
(
v
.
size
()
!=
this
->
size
())
this
->
resize
(
v
.
size
());
thrust
::
copy
(
v
.
_data
,
v
.
_data
+
_size
,
_data
);
return
*
this
;
}
/// Move operator
Array
&
operator
=
(
Array
&&
v
)
{
if
(
this
!=
&
v
)
{
if
(
!
wrapped
)
_alloc
.
deallocate
(
_data
,
_size
);
_data
=
std
::
exchange
(
v
.
_data
,
nullptr
);
_size
=
std
::
exchange
(
v
.
_size
,
0
);
wrapped
=
std
::
exchange
(
v
.
wrapped
,
false
);
}
return
*
this
;
}
void
wrap
(
Array
&
other
)
{
this
->
wrapMemory
(
other
.
_data
,
other
.
_size
);
}
void
wrap
(
const
Array
&
other
)
{
this
->
wrapMemory
(
other
.
_data
,
other
.
_size
);
}
/// Wrap a memory pointer
void
wrapMemory
(
T
*
data
,
UInt
size
)
{
this
->
_data
=
data
;
this
->
_size
=
size
;
this
->
wrapped
=
true
;
}
/// Assign a sigle value to the array
void
assign
(
UInt
size
,
const
T
&
value
)
{
this
->
resize
(
size
);
thrust
::
fill
(
_data
,
_data
+
size
,
value
);
}
void
setWrapped
(
bool
w
)
{
wrapped
=
w
;
}
/// Data pointer access (const)
const
T
*
data
()
const
{
return
_data
;
}
/// Data pointer access (non-const)
T
*
data
()
{
return
_data
;
}
/// Data pointer setter
void
setData
(
T
*
new_ptr
)
{
_data
=
new_ptr
;
}
/// Resize array
void
resize
(
UInt
size
)
{
if
(
wrapped
==
true
)
TAMAAS_EXCEPTION
(
"cannot resize wrapped array"
);
// Erase array
if
(
size
==
0
)
{
_alloc
.
deallocate
(
_data
,
_size
);
this
->
_data
=
nullptr
;
this
->
_size
=
0
;
return
;
}
// Do nothing
if
(
size
==
this
->
_size
)
return
;
// Allocate new data
_alloc
.
deallocate
(
_data
,
_size
);
this
->
_data
=
_alloc
.
allocate
(
size
);
this
->
_size
=
size
;
}
/// Access operator
inline
T
&
operator
[](
UInt
i
)
{
TAMAAS_ASSERT
(
i
<
_size
,
"memory overflow"
);
return
_data
[
i
];
}
/// Access operator (const)
inline
const
T
&
operator
[](
UInt
i
)
const
{
TAMAAS_ASSERT
(
i
<
_size
,
"memory overflow"
);
return
_data
[
i
];
}
/// Get size of array
inline
UInt
size
()
const
{
return
_size
;
}
private
:
T
*
_data
=
nullptr
;
UInt
_size
=
0
;
bool
wrapped
=
false
;
DEFAULT_ALLOCATOR
_alloc
=
DEFAULT_ALLOCATOR
();
};
__END_TAMAAS__
/* -------------------------------------------------------------------------- */
#endif
/* __ARRAY__HH__ */
Event Timeline
Log In to Comment