Skip to content

Commit 2db5f7a

Browse files
authored
Tidy NDArray for C++ and STL style and conventions. (#657)
1 parent 586454b commit 2db5f7a

1 file changed

Lines changed: 19 additions & 14 deletions

File tree

src/games/ndarray.h

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// This file is part of Gambit
33
// Copyright (c) 1994-2024, The Gambit Project (http://www.gambit-project.org)
44
//
5-
// FILE: src/solvers/enumpoly/ndarray.h
5+
// FILE: src/games/ndarray.h
66
// A simple N-dimensional array class
77
//
88
// This program is free software; you can redistribute it and/or modify
@@ -20,8 +20,8 @@
2020
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2121
//
2222

23-
#ifndef NDARRAY_H
24-
#define NDARRAY_H
23+
#ifndef GAMBIT_GAMES_NDARRAY_H
24+
#define GAMBIT_GAMES_NDARRAY_H
2525

2626
#include <numeric>
2727

@@ -42,11 +42,14 @@ namespace Gambit {
4242
///
4343
/// @tparam T The data type of elements of the array
4444
template <class T> class NDArray {
45+
using reference = typename std::vector<T>::reference;
46+
using const_reference = typename std::vector<T>::const_reference;
47+
4548
protected:
4649
Array<int> m_index_dim;
47-
int m_vector_dim;
50+
int m_vector_dim{0};
4851
Array<int> m_offsets;
49-
int m_offsets_sum;
52+
int m_offsets_sum{0};
5053
std::vector<T> m_storage;
5154

5255
int ComputeOffset(const Array<int> &p_array_index, int p_vector_index) const
@@ -59,10 +62,9 @@ template <class T> class NDArray {
5962
}
6063

6164
public:
62-
NDArray() : m_vector_dim(0), m_offsets_sum(0) {}
63-
explicit NDArray(const Array<int> &p_index_dim, int p_vector_dim)
65+
NDArray() = default;
66+
explicit NDArray(const Array<int> &p_index_dim, const int p_vector_dim)
6467
: m_index_dim(p_index_dim), m_vector_dim(p_vector_dim), m_offsets(p_index_dim.size() + 1),
65-
m_offsets_sum(0),
6668
m_storage(std::accumulate(m_index_dim.begin(), m_index_dim.end(), 1, std::multiplies<>()) *
6769
m_vector_dim)
6870
{
@@ -74,21 +76,24 @@ template <class T> class NDArray {
7476
// NOLINTEND(cppcoreguidelines-prefer-member-initializer)
7577
}
7678

77-
NDArray(const NDArray<T> &) = default;
79+
NDArray(const NDArray &) = default;
7880
~NDArray() = default;
7981

80-
NDArray<T> &operator=(const NDArray<T> &) = default;
82+
NDArray &operator=(const NDArray &) = default;
8183

82-
const T &at(const Array<int> &v, int index) const
84+
const_reference at(const Array<int> &v, const int index) const
85+
{
86+
return m_storage.at(ComputeOffset(v, index));
87+
}
88+
reference at(const Array<int> &v, const int index)
8389
{
8490
return m_storage.at(ComputeOffset(v, index));
8591
}
86-
T &at(const Array<int> &v, int index) { return m_storage.at(ComputeOffset(v, index)); }
8792

8893
const Array<int> &GetIndexDimension() const { return m_index_dim; }
89-
int GetVectorDimension() { return m_vector_dim; }
94+
int GetVectorDimension() const { return m_vector_dim; }
9095
};
9196

9297
} // end namespace Gambit
9398

94-
#endif // NDARRAY_H
99+
#endif // GAMBIT_GAMES_NDARRAY_H

0 commit comments

Comments
 (0)