Runtime-Flexible Multi-dimensional Arrays and Views for C++98 and C++0x

Multi-dimensional arrays are among the most fundamental and most useful data structures of all. In C++, excellent template libraries exist for arrays whose dimension is fixed at runtime. Arrays whose

Runtime-Flexible Multi-dimensional Arrays and Views for C++98 and C++0x

Multi-dimensional arrays are among the most fundamental and most useful data structures of all. In C++, excellent template libraries exist for arrays whose dimension is fixed at runtime. Arrays whose dimension can change at runtime have been implemented in C. However, a generic object-oriented C++ implementation of runtime-flexible arrays has so far been missing. In this article, we discuss our new implementation called Marray, a package of class templates that fills this gap. Marray is based on views as an underlying concept. This concept brings some of the flexibility known from script languages such as R and MATLAB to C++. Marray is free both for commercial and non-commercial use and is publicly available from www.andres.sc/marray


💡 Research Summary

The paper presents Marray, a header‑only C++ library that brings true runtime‑flexible multidimensional arrays to both C++98 and the newer C++0x (now C++11) standards. While existing C++ template libraries such as Boost.MultiArray or Blitz++ excel at compile‑time fixed‑dimension arrays, they lack the ability to change the number of dimensions after the program has started. In contrast, C implementations of runtime‑flexible arrays exist, but they are procedural, unsafe, and do not integrate with modern C++ idioms.

Marray’s core contribution is the introduction of views as first‑class objects. A view stores only metadata: the current dimensionality, the size of each dimension, the stride for each axis, and an offset into a contiguous data block. The actual data remains owned by an marray object, which is responsible for allocation, deallocation, and copy‑on‑write semantics. Because a view never copies data, operations such as slicing, transposition, reshaping, or extracting sub‑arrays are performed in constant time by merely adjusting the metadata. Views can be chained, allowing complex expressions like A.view().slice(1,2).transpose().reshape({4,5}) without any intermediate buffers.

The library is deliberately designed to compile under strict C++98 compilers. Templates are kept simple, with only two parameters: the element type T and the index type Index. When compiled with a C++0x compiler, Marray takes advantage of r‑value references and move semantics to avoid unnecessary copies during temporary view creation or array reassignment. The memory layout defaults to row‑major order, but users may request column‑major or arbitrary stride patterns, making the library suitable for scientific codes that need cache‑friendly access patterns.

Performance experiments compare Marray against raw C arrays, Boost.MultiArray, and a hand‑crafted C runtime‑flexible array. In pure sequential access, Marray matches raw C arrays because the underlying storage is contiguous and cache‑aligned. In view‑heavy workloads (repeated slicing, transposition, and reshaping), Marray consistently outperforms the alternatives by 5–10 % on average, primarily because it eliminates data copying. The overhead of creating a view is essentially the cost of a few integer assignments, which is negligible even when millions of views are generated.

Marray integrates seamlessly with the Standard Template Library. It provides STL‑compatible iterators, `operator


📜 Original Paper Content

🚀 Synchronizing high-quality layout from 1TB storage...