# NDArray

```cpp
#include <qbruntime/ndarray.h>

template <typename T>
class mobilint::NDArray
```

A class representing an N-dimensional array ([NDArray](<https://docs.mobilint.com/runtime/kr/v1.5/api/cpp/ndarray/index.md>)).

The class automatically manages memory when it owns the data. If the [NDArray](<https://docs.mobilint.com/runtime/kr/v1.5/api/cpp/ndarray/index.md>) is created from a raw pointer without taking ownership, it simply acts as a view of the existing data, and no memory management is performed by the [NDArray](<https://docs.mobilint.com/runtime/kr/v1.5/api/cpp/ndarray/index.md>).

[`NDArray`](<https://docs.mobilint.com/runtime/kr/v1.5/api/cpp/ndarray/index.md>) is the recommended way for providing input and receiving output to/from model inferences. By using [`NDArray`](<https://docs.mobilint.com/runtime/kr/v1.5/api/cpp/ndarray/index.md>), unnecessary copies inside the qbruntime are avoided, which helps improve performance during model inference operations.

**Template parameters**

- `T` — The type of the elements in the array.

## Types

| Member | Description |
| --- | --- |
| [`value_type`](<#value-type>) |  |

### value\_type

```cpp
using mobilint::NDArray<T>::value_type = T
```

## Member functions

| Member | Description |
| --- | --- |
| [`NDArray`](<#ndarray>) ×5 | Default constructor. Creates an empty [NDArray](<https://docs.mobilint.com/runtime/kr/v1.5/api/cpp/ndarray/index.md>). |
| [`data`](<#data>) | Returns a pointer to the underlying data. |
| [`shape`](<#shape>) | Returns the shape (dimensions) of the array. |
| [`ndim`](<#ndim>) | Returns the number of dimensions of the array. |
| [`size`](<#size>) | Returns the total number of elements in the array. |
| [`begin`](<#begin>) ×2 | Returns an iterator to the beginning of the data. |
| [`end`](<#end>) ×2 | Returns an iterator to the end of the data. |
| [`operator[]`](<#operator-subscript>) ×2 | Accesses an element of the array. |

### NDArray

```cpp
NDArray()=default
```

Default constructor. Creates an empty [NDArray](<https://docs.mobilint.com/runtime/kr/v1.5/api/cpp/ndarray/index.md>).

```cpp
NDArray(const std::vector<int64_t> &shape, StatusCode &sc)
```

Constructs an [NDArray](<https://docs.mobilint.com/runtime/kr/v1.5/api/cpp/ndarray/index.md>) with the given shape, allocating memory for elements.

This constructor allocates memory for the array and takes ownership of the allocated data. The memory is automatically managed by the [NDArray](<https://docs.mobilint.com/runtime/kr/v1.5/api/cpp/ndarray/index.md>) instance.

**Parameters**

- `shape` *\[in\]* — The shape (dimensions) of the array.
- `sc` *\[out\]* — Status code indicating success or failure of memory allocation.

```cpp
NDArray(T *ptr, const std::vector<int64_t> &shape)
```

Constructs an [NDArray](<https://docs.mobilint.com/runtime/kr/v1.5/api/cpp/ndarray/index.md>) from a raw pointer without taking ownership.

This constructor creates an [NDArray](<https://docs.mobilint.com/runtime/kr/v1.5/api/cpp/ndarray/index.md>) that acts as a view of the provided data. It does not manage the memory, and the memory is not owned by the [NDArray](<https://docs.mobilint.com/runtime/kr/v1.5/api/cpp/ndarray/index.md>) instance. The user is responsible for ensuring that the data remains valid for the lifetime of the [NDArray](<https://docs.mobilint.com/runtime/kr/v1.5/api/cpp/ndarray/index.md>).

**Parameters**

- `ptr` *\[in\]* — A pointer to the data.
- `shape` *\[in\]* — The shape (dimensions) of the array.

```cpp
NDArray(T *ptr, const std::vector<int64_t> &shape, bool owner)
```

Constructs an [NDArray](<https://docs.mobilint.com/runtime/kr/v1.5/api/cpp/ndarray/index.md>) from a raw pointer without taking ownership.

> [!WARNING]
>
> **Deprecated**
>
> This constructor is deprecated. Use [`NDArray(T*, const std::vector<int64_t>&)`](<https://docs.mobilint.com/runtime/kr/v1.5/api/cpp/ndarray/index.md>) instead.

**Parameters**

- `ptr` *\[in\]* — A pointer to the data.
- `shape` *\[in\]* — The shape (dimensions) of the array.
- `owner` — Ignored parameter (was intended to indicate ownership).

```cpp
NDArray(std::shared_ptr<void> data, const std::vector<int64_t> &shape)
```

Constructs an [NDArray](<https://docs.mobilint.com/runtime/kr/v1.5/api/cpp/ndarray/index.md>) from a shared memory block with shared ownership.

This constructor allows the [NDArray](<https://docs.mobilint.com/runtime/kr/v1.5/api/cpp/ndarray/index.md>) to share ownership of an existing memory buffer, preventing unnecessary copies. The `data` parameter should be a `std::shared_ptr<void>` pointing to a valid memory block.

Example usage:

```cpp
size_t bytesize = sizeof(float) * 100; // Allocate memory for 100 floats
void* raw_ptr = std::malloc(bytesize);
if (!raw_ptr) {
    // Handle memory allocation failure
}
auto buffer = std::shared_ptr<void>(raw_ptr, std::free);
NDArray<float> array(buffer, {10, 10}); // Create a 10x10 array
```

**Parameters**

- `data` *\[in\]* — A shared pointer to the memory buffer. For example, it can be initialized using `std::shared_ptr<void>(std::malloc(bytesize), std::free)`, but ensure that the allocation succeeds before passing it.
- `shape` *\[in\]* — The shape (dimensions) of the array.

### data

```cpp
T * data() const
```

Returns a pointer to the underlying data.

**Returns**

A pointer to the array's data.

### shape

```cpp
const std::vector<int64_t> & shape() const
```

Returns the shape (dimensions) of the array.

**Returns**

A reference to the vector representing the shape.

### ndim

```cpp
std::size_t ndim() const
```

Returns the number of dimensions of the array.

**Returns**

The number of dimensions.

### size

```cpp
int64_t size() const
```

Returns the total number of elements in the array.

**Returns**

The total number of elements.

### begin

```cpp
T * begin()
```

Returns an iterator to the beginning of the data.

**Returns**

A pointer to the first element.

```cpp
const T * begin() const
```

Returns a constant iterator to the beginning of the data.

**Returns**

A pointer to the first element.

### end

```cpp
T * end()
```

Returns an iterator to the end of the data.

**Returns**

A pointer past the last element.

```cpp
const T * end() const
```

Returns a constant iterator to the end of the data.

**Returns**

A pointer past the last element.

### operator\[\]

```cpp
T & operator[](size_t idx)
```

Accesses an element of the array.

**Parameters**

- `idx` — The index of the element.

**Returns**

A reference to the element at the specified index.

```cpp
T operator[](size_t idx) const
```

Accesses an element of the array (const version).

**Parameters**

- `idx` — The index of the element.

**Returns**

A copy of the element at the specified index.
