Arrays Manual

About

The MAXON API provides various array templates. These array types should be used instead of simple C-arrays to store data.

PointerArray

A maxon::PointerArray is optimized to store C++ pointers and owns the pointed objects.

Note
In general it is recommended to manage the lifetime of an object using references, see References.
// This example shows how to use a PointerArray to manage objects.
// create PointerArray
// create new objects and insert them into the PointerArray.
for (maxon::Int i = 0; i < count; ++i)
{
// create new object
ExampleClass* const obj = NewObj(ExampleClass, i) iferr_return;
// UniqueRef holds temporary ownership
// array takes ownership
// release ownership after the PointerArray successfully took the ownership
storage.Disconnect();
}
// check objects
const maxon::Int numberOfObjects = objects.GetCount();
DiagnosticOutput("The array stores @ pointers.", numberOfObjects);
// reset the array; this will delete all elements owned by the array
objects.Reset();
Py_ssize_t i
Definition: abstract.h:645
Py_ssize_t count
Definition: abstract.h:640
Definition: baseref.h:62
Definition: pointerarray.h:43
void Reset()
Deletes all elements (calls destructors and frees memory).
Definition: pointerarray.h:91
MAXON_ATTRIBUTE_FORCE_INLINE Int GetCount() const
Definition: pointerarray.h:122
ResultPtr< T > AppendPtr(T *x)
Definition: pointerarray.h:224
PyObject * obj
Definition: complexobject.h:60
Int64 Int
signed 32/64 bit int, size depends on the platform
Definition: apibase.h:213
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:176
#define NewObj(T,...)
Definition: newobj.h:108
#define iferr_return
Definition: resultbase.h:1521

SortedArray

A sorted array automatically sorts the stored elements on first read access.

// This example uses a SortedArray to store instances of an example class.
// definition of the custom SortedArray based array class
class ExampleClassSortedArray : public maxon::SortedArray<ExampleClassSortedArray, maxon::BaseArray<ExampleClass>>
{
public:
static maxon::Bool LessThan(const ExampleClass& a, const ExampleClass& b)
{
return a.GetValue() < b.GetValue();
}
static maxon::Bool IsEqual(const ExampleClass& a, const ExampleClass& b)
{
return a.GetValue() == b.GetValue();
}
};
// create sorted array and insert new objects
ExampleClassSortedArray sortedArray;
sortedArray.Append(ExampleClass(100)) iferr_return;
sortedArray.Append(ExampleClass(10)) iferr_return;
// get first object; the array will get sorted
ExampleClass* const object = sortedArray.GetFirst();
if (object)
{
DiagnosticOutput("First Element Value: @", object->GetValue());
}
MAXON_ATTRIBUTE_FORCE_INLINE std::enable_if< maxon::IsCollection< COLLECTION2 >::value &&!STD_IS_REPLACEMENT(same, typename std::decay< COMPARE >::type, EQUALITY), Bool >::type IsEqual(const COLLECTION2 &other, COMPARE &&cmp=COMPARE()) const
Definition: collection.h:236
Definition: sortedarray.h:51
bool Bool
boolean type, possible values are only false/true, 8 bit
Definition: apibase.h:206
Bool LessThan(UInt a1, UInt a2, UInt b1, UInt b2)
Definition: integer.h:151
Definition: object.h:105

Utility

These generic array types are used to define functions that can be used with various array types:

// This example function uses WritableArrayInterface to present
// a flexible interface that can be used with various array types.
static void PrintIntArrayInfo(maxon::WritableArrayInterface<maxon::Int>& array)
{
// get the size of the array
const maxon::Int size = array.GetCount();
DiagnosticOutput("Array Size: @", size);
// print the value of the array's first element
if (size > 0)
{
maxon::Int element = array[0];
DiagnosticOutput("First Element: @", element);
}
}
Py_ssize_t size
Definition: bytesobject.h:86
PyObject * element
Definition: unicodeobject.h:1016

Further Reading