From 119c1debba427f8ee3b733117ac14d63a4e9c7ed Mon Sep 17 00:00:00 2001 From: Theodore Turocy Date: Tue, 2 Dec 2025 13:09:19 +0000 Subject: [PATCH] Tidy NDArray for C++ and STL style and conventions. --- src/games/ndarray.h | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/games/ndarray.h b/src/games/ndarray.h index aebefbf0e..3f9c145b5 100644 --- a/src/games/ndarray.h +++ b/src/games/ndarray.h @@ -2,7 +2,7 @@ // This file is part of Gambit // Copyright (c) 1994-2024, The Gambit Project (http://www.gambit-project.org) // -// FILE: src/solvers/enumpoly/ndarray.h +// FILE: src/games/ndarray.h // A simple N-dimensional array class // // This program is free software; you can redistribute it and/or modify @@ -20,8 +20,8 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // -#ifndef NDARRAY_H -#define NDARRAY_H +#ifndef GAMBIT_GAMES_NDARRAY_H +#define GAMBIT_GAMES_NDARRAY_H #include @@ -42,11 +42,14 @@ namespace Gambit { /// /// @tparam T The data type of elements of the array template class NDArray { + using reference = typename std::vector::reference; + using const_reference = typename std::vector::const_reference; + protected: Array m_index_dim; - int m_vector_dim; + int m_vector_dim{0}; Array m_offsets; - int m_offsets_sum; + int m_offsets_sum{0}; std::vector m_storage; int ComputeOffset(const Array &p_array_index, int p_vector_index) const @@ -59,10 +62,9 @@ template class NDArray { } public: - NDArray() : m_vector_dim(0), m_offsets_sum(0) {} - explicit NDArray(const Array &p_index_dim, int p_vector_dim) + NDArray() = default; + explicit NDArray(const Array &p_index_dim, const int p_vector_dim) : m_index_dim(p_index_dim), m_vector_dim(p_vector_dim), m_offsets(p_index_dim.size() + 1), - m_offsets_sum(0), m_storage(std::accumulate(m_index_dim.begin(), m_index_dim.end(), 1, std::multiplies<>()) * m_vector_dim) { @@ -74,21 +76,24 @@ template class NDArray { // NOLINTEND(cppcoreguidelines-prefer-member-initializer) } - NDArray(const NDArray &) = default; + NDArray(const NDArray &) = default; ~NDArray() = default; - NDArray &operator=(const NDArray &) = default; + NDArray &operator=(const NDArray &) = default; - const T &at(const Array &v, int index) const + const_reference at(const Array &v, const int index) const + { + return m_storage.at(ComputeOffset(v, index)); + } + reference at(const Array &v, const int index) { return m_storage.at(ComputeOffset(v, index)); } - T &at(const Array &v, int index) { return m_storage.at(ComputeOffset(v, index)); } const Array &GetIndexDimension() const { return m_index_dim; } - int GetVectorDimension() { return m_vector_dim; } + int GetVectorDimension() const { return m_vector_dim; } }; } // end namespace Gambit -#endif // NDARRAY_H +#endif // GAMBIT_GAMES_NDARRAY_H