forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array.hh
129 lines (117 loc) · 4.09 KB
/
Array.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//----------------------------------*-C++-*----------------------------------//
// Copyright 2020-2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file corecel/cont/Array.hh
//---------------------------------------------------------------------------//
#pragma once
#include "corecel/Macros.hh"
#include "corecel/Types.hh"
namespace celeritas
{
//---------------------------------------------------------------------------//
/*!
* Fixed-size simple array for storage.
*
* This isn't fully standards-compliant with std::array: there's no support for
* N=0 for example. Additionally it uses the native celeritas \c size_type,
* even though this has *no* effect on generated code for values of N inside
* the range of \c size_type.
*
* \note For supplementary functionality, include:
* - \c corecel/math/ArrayUtils.hh for real-number vector/matrix applications
* - \c corecel/math/ArrayOperators.hh for mathematical operators
* - \c ArrayIO.hh for streaming and string conversion
* - \c ArrayIO.json.hh for JSON input and output
*/
template<class T, ::celeritas::size_type N>
struct Array
{
//!@{
//! \name Type aliases
using value_type = T;
using size_type = ::celeritas::size_type;
using pointer = value_type*;
using const_pointer = value_type const*;
using reference = value_type&;
using const_reference = value_type const&;
using iterator = pointer;
using const_iterator = const_pointer;
//!@}
//// DATA ////
T data_[N]; //!< Storage
//// ACCESSORS ////
//!@{
//! \name Element access
CELER_CONSTEXPR_FUNCTION const_reference operator[](size_type i) const
{
return data_[i];
}
CELER_CONSTEXPR_FUNCTION reference operator[](size_type i)
{
return data_[i];
}
CELER_CONSTEXPR_FUNCTION const_reference front() const { return data_[0]; }
CELER_CONSTEXPR_FUNCTION reference front() { return data_[0]; }
CELER_CONSTEXPR_FUNCTION const_reference back() const
{
return data_[N - 1];
}
CELER_CONSTEXPR_FUNCTION reference back() { return data_[N - 1]; }
CELER_CONSTEXPR_FUNCTION const_pointer data() const { return data_; }
CELER_CONSTEXPR_FUNCTION pointer data() { return data_; }
//!@}
//!@{
//! \name Iterators
CELER_CONSTEXPR_FUNCTION iterator begin() { return data_; }
CELER_CONSTEXPR_FUNCTION iterator end() { return data_ + N; }
CELER_CONSTEXPR_FUNCTION const_iterator begin() const { return data_; }
CELER_CONSTEXPR_FUNCTION const_iterator end() const { return data_ + N; }
CELER_CONSTEXPR_FUNCTION const_iterator cbegin() const { return data_; }
CELER_CONSTEXPR_FUNCTION const_iterator cend() const { return data_ + N; }
//!@}
//!@{
//! \name Capacity
CELER_CONSTEXPR_FUNCTION bool empty() const { return N == 0; }
static CELER_CONSTEXPR_FUNCTION size_type size() { return N; }
//!@}
//!@{
//! \name Operations
//! Fill the array with a constant value
CELER_CONSTEXPR_FUNCTION void fill(const_reference value)
{
for (size_type i = 0; i != N; ++i)
data_[i] = value;
}
//!@}
};
//---------------------------------------------------------------------------//
// INLINE DEFINITIONS
//---------------------------------------------------------------------------//
/*!
* Test equality of two arrays.
*/
template<class T, size_type N>
CELER_CONSTEXPR_FUNCTION bool
operator==(Array<T, N> const& lhs, Array<T, N> const& rhs)
{
for (size_type i = 0; i != N; ++i)
{
if (lhs[i] != rhs[i])
return false;
}
return true;
}
//---------------------------------------------------------------------------//
/*!
* Test inequality of two arrays.
*/
template<class T, size_type N>
CELER_CONSTEXPR_FUNCTION bool
operator!=(Array<T, N> const& lhs, Array<T, N> const& rhs)
{
return !(lhs == rhs);
}
//---------------------------------------------------------------------------//
} // namespace celeritas