// Copyright 2009-2021 Intel Corporation // SPDX-License-Identifier: Apache-2.0 #pragma once #include "platform.h" #include "alloc.h" namespace embree { /*! static array with static size */ template class array_t { public: /********************** Iterators ****************************/ __forceinline T* begin() const { return items; }; __forceinline T* end () const { return items+N; }; /********************** Capacity ****************************/ __forceinline bool empty () const { return N == 0; } __forceinline size_t size () const { return N; } __forceinline size_t max_size () const { return N; } /******************** Element access **************************/ __forceinline T& operator[](size_t i) { assert(i < N); return items[i]; } __forceinline const T& operator[](size_t i) const { assert(i < N); return items[i]; } __forceinline T& at(size_t i) { assert(i < N); return items[i]; } __forceinline const T& at(size_t i) const { assert(i < N); return items[i]; } __forceinline T& front() const { assert(N > 0); return items[0]; }; __forceinline T& back () const { assert(N > 0); return items[N-1]; }; __forceinline T* data() { return items; }; __forceinline const T* data() const { return items; }; private: T items[N]; }; /*! static array with dynamic size */ template class darray_t { public: __forceinline darray_t () : M(0) {} __forceinline darray_t (const T& v) : M(0) { for (size_t i=0; i 0); return items[0]; }; __forceinline T& back () const { assert(M > 0); return items[M-1]; }; __forceinline T* data() { return items; }; __forceinline const T* data() const { return items; }; private: size_t M; T items[N]; }; /*! dynamic sized array that is allocated on the stack */ #define dynamic_large_stack_array(Ty,Name,N,max_stack_bytes) StackArray Name(N) template struct __aligned(64) StackArray { __forceinline StackArray (const size_t N) : N(N) { if (N*sizeof(Ty) <= max_stack_bytes) data = &arr[0]; else data = (Ty*) alignedMalloc(N*sizeof(Ty),64); } __forceinline ~StackArray () { if (data != &arr[0]) alignedFree(data); } __forceinline operator Ty* () { return data; } __forceinline operator const Ty* () const { return data; } __forceinline Ty& operator[](const int i) { assert(i>=0 && i=0 && i struct __aligned(64) DynamicStackArray { __forceinline DynamicStackArray () : data(&arr[0]) {} __forceinline ~DynamicStackArray () { if (!isStackAllocated()) delete[] data; } __forceinline bool isStackAllocated() const { return data == &arr[0]; } __forceinline size_t size() const { if (isStackAllocated()) return max_stack_elements; else return max_total_elements; } __forceinline void resize(size_t M) { assert(M <= max_total_elements); if (likely(M <= max_stack_elements)) return; if (likely(!isStackAllocated())) return; data = new Ty[max_total_elements]; for (size_t i=0; i=0 && ioperator[] (i) = other[i]; } DynamicStackArray& operator= (const DynamicStackArray& other) { for (size_t i=0; ioperator[] (i) = other[i]; return *this; } private: Ty arr[max_stack_elements]; Ty* data; }; }