Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Onnx Onnx ArrayRef

From Leeroopedia


Knowledge Sources
Domains Utility, Data Structures
Last Updated 2026-02-10 00:00 GMT

Overview

Concrete tool for providing a lightweight non-owning array reference wrapper provided by the ONNX library.

Description

The onnx/common/array_ref.h header defines the ArrayRef<T> template class, a constant reference wrapper for contiguous arrays. Adapted from LLVM's ArrayRef, it represents a start pointer and a length, allowing various APIs to accept consecutive elements through a single unified interface without copying data.

ArrayRef is a non-owning view: it does not manage the lifetime of the underlying data. The data must reside in some other buffer whose lifetime extends past that of the ArrayRef. The class is designed to be trivially copyable and should be passed by value.

The class provides multiple implicit constructors that accept different array-like sources:

  • Default constructor (empty array)
  • Single element reference
  • Pointer and length pair
  • Pointer range (begin/end)
  • std::vector<T>
  • std::array<T, N>
  • C arrays (T[N])
  • std::initializer_list<T>

It offers STL-compatible forward and reverse iterators, element access methods (front(), back(), operator[], at()), query methods (empty(), size(), data()), element comparison (equals()), array slicing (slice()), and conversion back to std::vector via vec() or implicit conversion operator. Assignment from temporaries is explicitly deleted to prevent dangling references.

Usage

Use ArrayRef when designing APIs that need to accept various array-like types without forcing callers to convert to a specific container type. It eliminates the need for multiple function overloads or templates for different container types. Because ArrayRef does not own the data, ensure the underlying storage outlives the ArrayRef instance.

Code Reference

Source Location

Signature

namespace ONNX_NAMESPACE {

template <typename T>
class ArrayRef {
 public:
  using iterator = const T*;
  using const_iterator = const T*;
  using size_type = size_t;
  using reverse_iterator = std::reverse_iterator<iterator>;

  /*implicit*/ ArrayRef();
  /*implicit*/ ArrayRef(const T& OneElt);
  /*implicit*/ ArrayRef(const T* data, size_t length);
  ArrayRef(const T* begin, const T* end);
  /*implicit*/ ArrayRef(const std::vector<T, A>& Vec);
  /*implicit*/ constexpr ArrayRef(const std::array<T, N>& Arr);
  /*implicit*/ constexpr ArrayRef(const T (&Arr)[N]);
  /*implicit*/ ArrayRef(const std::initializer_list<T>& Vec);

  iterator begin() const;
  iterator end() const;
  reverse_iterator rbegin() const;
  reverse_iterator rend() const;
  bool empty() const;
  const T* data() const;
  size_t size() const;
  const T& front() const;
  const T& back() const;
  bool equals(ArrayRef RHS) const;
  ArrayRef<T> slice(size_t N, size_t M) const;
  ArrayRef<T> slice(size_t N) const;
  const T& operator[](size_t Index) const;
  const T& at(size_t Index) const;
  std::vector<T> vec() const;
  operator std::vector<T>() const;
};

} // namespace ONNX_NAMESPACE

Import

#include "onnx/common/array_ref.h"

I/O Contract

Inputs

Name Type Required Description
source data const T*, std::vector<T>, std::array<T,N>, T[N], or std::initializer_list<T> Yes The underlying contiguous array data to reference (any supported source type)
N size_t No Length when constructing from pointer+length, or offset when slicing
M size_t No Number of elements to keep when using two-argument slice

Outputs

Name Type Description
ArrayRef<T> ArrayRef<T> A non-owning constant view into the contiguous array data
vec() std::vector<T> A copy of the referenced data as a new vector

Usage Examples

#include "onnx/common/array_ref.h"
using namespace ONNX_NAMESPACE;

// Accept any array-like type through a unified interface
void processData(ArrayRef<int> data) {
    for (const auto& val : data) {
        // process val
    }
}

// Call with different array sources
std::vector<int> vec = {1, 2, 3, 4, 5};
processData(vec);                          // from std::vector
processData({1, 2, 3});                    // from initializer_list
int arr[] = {10, 20, 30};
processData(arr);                          // from C array

// Slicing
ArrayRef<int> ref(vec);
ArrayRef<int> sub = ref.slice(1, 3);       // elements at index 1, 2, 3
ArrayRef<int> tail = ref.slice(2);         // elements from index 2 onward

// Element access
const int& first = ref.front();
const int& last = ref.back();
const int& third = ref[2];

// Convert back to vector
std::vector<int> copy = ref.vec();

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment