#ifndef NETGEN_CORE_ARRAY_HPP #define NETGEN_CORE_ARRAY_HPP /**************************************************************************/ /* File: array.hpp */ /* Author: Joachim Schoeberl */ /* Date: 01. Jun. 95 */ /**************************************************************************/ #include "archive.hpp" #include "localheap.hpp" #include "utils.hpp" #ifdef DEBUG #define CHECK_RANGE #endif namespace ngcore { using std::ostream; /** Exception thrown by array range check. Only thrown when compiled with RANGE_CHECK */ class ArrayRangeException : public Exception { public: ArrayRangeException () : Exception("ArrayRangeException\n") { ; } }; template class Tuple { public: int Size() const { return 0; } }; template class Tuple : Tuple { typedef Tuple BASE; HEAD head; public: Tuple () { ; } Tuple (HEAD h, TAIL ... t) : Tuple (t...), head(h) { ; } HEAD Head() const { return head; } Tuple Tail() const { return *this; } int Size() const { return BASE::Size()+1; } }; template ostream & operator<< (ostream & ost, Tuple tup) { return ost; } template ostream & operator<< (ostream & ost, Tuple tup) { ost << tup.Head() << ", " << tup.Tail(); return ost; } template Tuple MakeTuple (ARGS ... args) { return Tuple (args...); } /* Some class which can be treated as array */ template // , typename TA = T> class BaseArrayObject { public: NETGEN_INLINE BaseArrayObject() { ; } NETGEN_INLINE const T & Spec() const { return static_cast (*this); } NETGEN_INLINE size_t Size() const { return Spec().Size(); } template NETGEN_INLINE bool Contains(const T2 & el) const { for (size_t i = 0; i < Size(); i++) if (Spec()[i] == el) return true; return false; } static constexpr size_t ILLEGAL_POSITION = size_t(-1); template NETGEN_INLINE size_t Pos(const T2 & el) const { for (size_t i = 0; i < Size(); i++) if (Spec()[i] == el) return i; return ILLEGAL_POSITION; } template NETGEN_INLINE size_t PosSure(const T2 & el) const { for (size_t i = 0; ; i++) if (Spec()[i] == el) return i; } // NETGEN_INLINE auto & operator[] (size_t i) { return Spec()[i]; } NETGEN_INLINE auto operator[] (size_t i) const { return Spec()[i]; } }; template class AOWrapperIterator { const AO & ao; size_t ind; public: NETGEN_INLINE AOWrapperIterator (const AO & aao, size_t ai) : ao(aao), ind(ai) { ; } NETGEN_INLINE AOWrapperIterator operator++ (int) { return AOWrapperIterator(ao, ind++); } NETGEN_INLINE AOWrapperIterator operator++ () { return AOWrapperIterator(ao, ++ind); } NETGEN_INLINE auto operator*() const -> decltype(ao[ind]) { return ao[ind]; } NETGEN_INLINE auto operator*() -> decltype(ao[ind]) { return ao[ind]; } NETGEN_INLINE bool operator != (AOWrapperIterator d2) { return ind != d2.ind; } NETGEN_INLINE bool operator == (AOWrapperIterator d2) { return ind == d2.ind; } }; template class AOWrapper : public BaseArrayObject> { T ar; public: NETGEN_INLINE AOWrapper (T aar) : ar(aar) { ; } NETGEN_INLINE operator T () const { return ar; } NETGEN_INLINE size_t Size() const { return ar.Size(); } NETGEN_INLINE auto operator[] (size_t i) { return ar[i]; } NETGEN_INLINE auto operator[] (size_t i) const { return ar[i]; } NETGEN_INLINE AOWrapperIterator begin () const { return AOWrapperIterator (*this, 0); } NETGEN_INLINE AOWrapperIterator end () const { return AOWrapperIterator (*this, Size()); } }; template NETGEN_INLINE AOWrapper ArrayObject (const T & ar) { return AOWrapper (ar); } template NETGEN_INLINE AOWrapper ArrayObject (T && ar) { return AOWrapper (ar); } template auto ArrayObject (size_t s, FUNC f) { class Dummy { size_t s; FUNC f; public: Dummy (size_t _s, FUNC _f) : s(_s), f(_f) { ; } size_t Size() const { return s; } auto operator[] (size_t i) const { return f(i); } }; return ArrayObject(Dummy(s,f)); } template auto Substitute (const BaseArrayObject & ao, FUNC f) { return ArrayObject(ao.Size(), [&ao,f] (size_t i) { return f(ao[i]); }); } /** nothing more but a new type for a C array. return value for Addr - operator of array */ template class CArray { protected: /// the data T * data; public: /// initialize array NETGEN_INLINE CArray () { data = 0; } /// provide size and memory NETGEN_INLINE CArray (T * adata) : data(adata) { ; } /// Access array NETGEN_INLINE T & operator[] (size_t i) const { return data[i]; } NETGEN_INLINE operator T* () const { return data; } }; template class FlatArray; template class ArrayIterator { FlatArray ar; TSIZE ind; public: NETGEN_INLINE ArrayIterator (FlatArray aar, TSIZE ai) : ar(aar), ind(ai) { ; } NETGEN_INLINE ArrayIterator operator++ (int) { return ArrayIterator(ar, ind++); } NETGEN_INLINE ArrayIterator operator++ () { return ArrayIterator(ar, ++ind); } NETGEN_INLINE TELEM operator*() const { return ar[ind]; } NETGEN_INLINE TELEM & operator*() { return ar[ind]; } NETGEN_INLINE bool operator != (ArrayIterator d2) { return ind != d2.ind; } NETGEN_INLINE bool operator == (ArrayIterator d2) { return ind == d2.ind; } }; template class ArrayRangeIterator { TSIZE ind; public: NETGEN_INLINE ArrayRangeIterator (TSIZE ai) : ind(ai) { ; } NETGEN_INLINE ArrayRangeIterator operator++ (int) { return ind++; } NETGEN_INLINE ArrayRangeIterator operator++ () { return ++ind; } NETGEN_INLINE TSIZE operator*() const { return ind; } NETGEN_INLINE TSIZE Index() { return ind; } NETGEN_INLINE operator TSIZE () const { return ind; } NETGEN_INLINE bool operator != (ArrayRangeIterator d2) { return ind != d2.ind; } NETGEN_INLINE bool operator == (ArrayRangeIterator d2) { return ind == d2.ind; } }; /// a range of integers template class T_Range : public BaseArrayObject > { T first, next; public: NETGEN_INLINE T_Range () { ; } NETGEN_INLINE T_Range (T n) : first(0), next(n) {;} NETGEN_INLINE T_Range (T f, T n) : first(f), next(n) {;} template NETGEN_INLINE T_Range(T_Range r2) : first(r2.First()), next(r2.Next()) { ; } NETGEN_INLINE T First() const { return first; } NETGEN_INLINE T Next() const { return next; } NETGEN_INLINE T & First() { return first; } NETGEN_INLINE T & Next() { return next; } NETGEN_INLINE auto Size() const { return next-first; } NETGEN_INLINE T operator[] (T i) const { return first+i; } NETGEN_INLINE bool Contains (T i) const { return ((i >= first) && (i < next)); } NETGEN_INLINE ArrayRangeIterator begin() const { return first; } NETGEN_INLINE ArrayRangeIterator end() const { return next; } T_Range Split (size_t nr, int tot) const { T diff = next-first; return T_Range (first + nr * diff / tot, first + (nr+1) * diff / tot); } // NETGEN_INLINE operator IntRange () const { return IntRange(first,next); } }; using IntRange = T_Range; template NETGEN_INLINE T_Range Range (T a, T b) { return T_Range(a,b); } template NETGEN_INLINE T_Range Range_impl (T n, std::true_type) { return T_Range (0, n); } template NETGEN_INLINE auto Range_impl (const T & ao, std::false_type) -> T_Range { return T_Range (0, ao.Size()); } template auto Range(const T & x) -> decltype(Range_impl(x, std::is_integral())) { return Range_impl(x, std::is_integral()); } NETGEN_INLINE IntRange operator+ (const IntRange & range, int shift) { return IntRange (range.First()+shift, range.Next()+shift); } NETGEN_INLINE IntRange operator+ (int shift, const IntRange & range) { return IntRange (range.First()+shift, range.Next()+shift); } NETGEN_INLINE IntRange operator* (int scale, const IntRange & range) { return IntRange (scale*range.First(), scale*range.Next()); } NETGEN_INLINE IntRange operator* (const IntRange & range, int scale) { return IntRange (scale*range.First(), scale*range.Next()); } template inline ostream & operator<< (ostream & s, T_Range ir) { s << "[" << ir.First() << "," << ir.Next() << ")"; return s; } template ostream & operator<< (ostream & ost, Tuple tup) { ost << tup.Head() << ", " << tup.Tail(); return ost; } template inline ostream & operator<< (ostream & ost, const BaseArrayObject & array) { for (auto i : Range(array.Size())) ost << i << ":" << array[i] << std::endl; return ost; } template class IndirectArray : public BaseArrayObject > { FlatArray ba; const INDEX_ARRAY & ia; public: NETGEN_INLINE IndirectArray (FlatArray aba, const INDEX_ARRAY & aia) : ba(aba), ia(aia) { ; } NETGEN_INLINE size_t Size() const { return ia.Size(); } NETGEN_INLINE T operator[] (size_t i) const { return ba[ia[i]]; } NETGEN_INLINE T & operator[] (size_t i) { return ba[ia[i]]; } NETGEN_INLINE IndirectArray operator= (const T & val) { for (auto i : Range(Size())) (*this)[i] = val; return IndirectArray (ba, ia); } template NETGEN_INLINE IndirectArray operator= (const BaseArrayObject & a2) { for (auto i : Range(Size())) (*this)[i] = a2[i]; return IndirectArray (ba, ia); } }; /** A simple array container. Array represented by size and data-pointer. No memory allocation and deallocation, must be provided by user. Helper functions for printing. Optional range check by macro CHECK_RANGE */ template class FlatArray : public BaseArrayObject > { protected: /// the size size_t size; /// the data T * __restrict data; public: using BaseArrayObject >::ILLEGAL_POSITION; /// initialize array NETGEN_INLINE FlatArray () = default; // { ; } // size = 0; data = 0; } /// copy constructor allows size-type conversion NETGEN_INLINE FlatArray (const FlatArray & a2) = default; // : size(a2.Size()), data(a2.data) { ; } /// provide size and memory NETGEN_INLINE FlatArray (size_t asize, T * adata) : size(asize), data(adata) { ; } /// memory from local heap NETGEN_INLINE FlatArray(size_t asize, Allocator & lh) : size(asize), data(new (lh) T[asize]) { ; } NETGEN_INLINE FlatArray(size_t asize, LocalHeap & lh) : size(asize), data (lh.Alloc (asize)) { ; } /// the size NETGEN_INLINE size_t Size() const { return size; } /// Fill array with value val NETGEN_INLINE const FlatArray & operator= (const T & val) const { size_t hsize = size; T * hdata = data; for (size_t i = 0; i < hsize; i++) hdata[i] = val; return *this; } /// copies array NETGEN_INLINE const FlatArray & operator= (const FlatArray & a2) const { size_t hsize = size; T * hdata = data; for (size_t i = 0; i < hsize; i++) hdata[i] = a2[i]; return *this; } template NETGEN_INLINE const FlatArray & operator= (const BaseArrayObject & a2) const { size_t hsize = size; T * hdata = data; for (size_t i = 0; i < hsize; i++) hdata[i] = a2[i]; return *this; } NETGEN_INLINE const FlatArray & operator= (const std::function & func) const { for (size_t i = 0; i < size; i++) data[i] = func(i); return *this; } // template // const FlatArray operator= (ParallelValue val); // template // const FlatArray operator= (ParallelFunction val); /// copies pointers NETGEN_INLINE const FlatArray & Assign (const FlatArray & a2) { size = a2.size; data = a2.data; return *this; } /// assigns memory from local heap NETGEN_INLINE const FlatArray & Assign (size_t asize, LocalHeap & lh) { size = asize; data = lh.Alloc (asize); return *this; } /// Access array. range check by macro CHECK_RANGE NETGEN_INLINE T & operator[] (size_t i) const { #ifdef CHECK_RANGE if (i < 0 || i >= size) throw RangeException ("FlatArray::operator[]", i, 0, size-1); #endif return data[i]; } NETGEN_INLINE T_Range Range () const { return T_Range (0, Size()); } NETGEN_INLINE const CArray Addr (size_t pos) const { return CArray (data+pos); } // const CArray operator+ (int pos) // { return CArray (data+pos); } NETGEN_INLINE T * operator+ (size_t pos) const { return data+pos; } /// access last element. check by macro CHECK_RANGE T & Last () const { #ifdef CHECK_RANGE if (!size) throw Exception ("Array should not be empty"); #endif return data[size-1]; } /// takes sub-array starting from position pos NETGEN_INLINE const FlatArray Part (size_t pos) { return FlatArray (size-pos, data+pos); } /// takes subsize elements starting from position pos NETGEN_INLINE const FlatArray Part (size_t pos, size_t subsize) { return FlatArray (subsize, data+pos); } /// takes range starting from position start of end-start elements NETGEN_INLINE FlatArray Range (size_t start, size_t end) const { return FlatArray (end-start, data+start); } /// takes range starting from position start of end-start elements NETGEN_INLINE FlatArray Range (T_Range range) const { return FlatArray (range.Size(), data+range.First()); } /// takes range starting from position start of end-start elements NETGEN_INLINE const FlatArray operator[] (IntRange range) const { return FlatArray (range.Size(), data+range.First()); } template IndirectArray > operator[] (const BaseArrayObject & ind_array) const { return IndirectArray > (*this, ind_array); } /// first position of element elem, returns -1 if element not contained in array NETGEN_INLINE size_t Pos(const T & el) const { for (size_t i = 0; i < Size(); i++) if (data[i] == el) return i; return ILLEGAL_POSITION; } /// does the array contain element elem ? NETGEN_INLINE bool Contains(const T & elem) const { return Pos(elem) != ILLEGAL_POSITION; } ArrayIterator begin() const { return ArrayIterator (*this, 0); } ArrayIterator end() const { return ArrayIterator (*this, size); } }; template FlatArray View (FlatArray fa) { return fa; } /// print array template inline ostream & operator<< (ostream & s, const FlatArray & a) { for (auto i : a.Range()) s << i << ": " << a[i] << "\n"; return s; } /// have arrays the same contents ? template inline bool operator== (const FlatArray & a1, const FlatArray & a2) { if (a1.Size () != a2.Size()) return 0; for (size_t i = 0; i < a1.Size(); i++) if (a1[i] != a2[i]) return false; return true; } /** Dynamic array container. Array is an automatically increasing array container. The allocated memory doubles on overflow. Either the container takes care of memory allocation and deallocation, or the user provides one block of data. */ template class Array : public FlatArray { protected: /// physical size of array size_t allocsize; /// that's the data we have to delete, nullptr for not owning the memory T * mem_to_delete; using FlatArray::size; using FlatArray::data; public: /// Generate array of logical and physical size asize NETGEN_INLINE explicit Array() : FlatArray (0, nullptr) { allocsize = 0; mem_to_delete = nullptr; } NETGEN_INLINE explicit Array(size_t asize) : FlatArray (asize, new T[asize]) { allocsize = asize; mem_to_delete = data; } /// Generate array in user data NETGEN_INLINE Array(size_t asize, T* adata, bool ownMemory = false) : FlatArray (asize, adata) { allocsize = asize; if(ownMemory) mem_to_delete = adata; else mem_to_delete = nullptr; } /// Generate array in user data template NETGEN_INLINE Array(size_t asize, ALLOCATOR & lh) : FlatArray (asize, lh) { allocsize = asize; mem_to_delete = nullptr; } NETGEN_INLINE Array (Array && a2) { size = a2.size; data = a2.data; allocsize = a2.allocsize; mem_to_delete = a2.mem_to_delete; a2.size = 0; a2.allocsize = 0; a2.data = nullptr; a2.mem_to_delete = nullptr; } /// array copy NETGEN_INLINE explicit Array (const Array & a2) : FlatArray (a2.Size(), a2.Size() ? new T[a2.Size()] : nullptr) { allocsize = size; mem_to_delete = data; for (size_t i = 0; i < size; i++) data[i] = a2[i]; } template explicit Array (const BaseArrayObject & a2) : FlatArray (a2.Size(), a2.Size() ? new T[a2.Size()] : nullptr) { allocsize = size; mem_to_delete = data; for (size_t i = 0; i < size; i++) data[i] = a2[i]; } Array (std::initializer_list list) : FlatArray (list.size(), list.size() ? new T[list.size()] : NULL) { allocsize = size; mem_to_delete = data; size_t cnt = 0; for (auto val : list) data[cnt++] = val; } /// array merge-copy explicit Array (const Array & a2, const Array & a3) : FlatArray (a2.Size()+a3.Size(), a2.Size()+a3.Size() ? new T[a2.Size()+a3.Size()] : 0) { allocsize = size; mem_to_delete = data; for(size_t i = 0; i < a2.Size(); i++) (*this)[i] = a2[i]; for (size_t i = a2.Size(), j=0; i < size; i++,j++) (*this)[i] = a3[j]; } /// if responsible, deletes memory NETGEN_INLINE ~Array() { delete [] mem_to_delete; } // Only provide this function if T is archivable template auto DoArchive(Archive& archive) -> typename std::enable_if_t, void> { if(archive.Output()) archive << size; else { size_t s; archive & s; SetSize(s); } archive.Do(data, size); } /// we tell the compiler that there is no need for deleting the array .. NETGEN_INLINE void NothingToDelete () { mem_to_delete = nullptr; } /// Change logical size. If necessary, do reallocation. Keeps contents. NETGEN_INLINE void SetSize(size_t nsize) { if (nsize > allocsize) ReSize (nsize); size = nsize; } /// NETGEN_INLINE void SetSize0() { size = 0; } /// Change physical size. Keeps logical size. Keeps contents. NETGEN_INLINE void SetAllocSize (size_t nallocsize) { if (nallocsize > allocsize) ReSize (nallocsize); } /// Change physical size. Keeps logical size. Keeps contents. NETGEN_INLINE size_t AllocSize () const { return allocsize; } /// assigns memory from local heap NETGEN_INLINE const Array & Assign (size_t asize, LocalHeap & lh) { delete [] mem_to_delete; size = allocsize = asize; data = lh.Alloc (asize); mem_to_delete = nullptr; return *this; } /// Add element at end of array. reallocation if necessary. NETGEN_INLINE size_t Append (const T & el) { if (size == allocsize) ReSize (size+1); data[size] = el; size++; return size; } /// Add element at end of array. reallocation not necessary. NETGEN_INLINE size_t AppendHaveMem (const T & el) { data[size] = el; size++; return size; } /// Add element at end of array. reallocation if necessary. NETGEN_INLINE size_t Append (T && el) { if (size == allocsize) ReSize (size+1); data[size] = std::move(el); size++; return size; } // Add elements of initializer list to end of array. Reallocation if necessary. NETGEN_INLINE size_t Append(std::initializer_list lst) { if(allocsize < size + lst.size()) ReSize(size+lst.size()); for(auto val : lst) data[size++] = val; return size; } /// Add element at end of array. reallocation if necessary. NETGEN_INLINE void Insert (size_t pos, const T & el) { if (size == allocsize) ReSize (size+1); for (size_t i = size; i > pos; i--) data[i] = data[i-1]; data[pos] = el; size++; } NETGEN_INLINE Array & operator += (const T & el) { Append (el); return *this; } /// Append array at end of array. reallocation if necessary. // int Append (const Array & source) NETGEN_INLINE size_t Append (FlatArray source) { if(size + source.Size() >= allocsize) ReSize (size + source.Size() + 1); for(size_t i = size, j=0; jsize-1; j++) this->data[j] = this->data[j+1]; this->size--; } /// Delete last element. NETGEN_INLINE void DeleteLast () { #ifdef CHECK_RANGE // CheckNonEmpty(); #endif size--; } /// Deallocate memory NETGEN_INLINE void DeleteAll () { delete [] mem_to_delete; mem_to_delete = NULL; data = 0; size = allocsize = 0; } /// Fill array with val NETGEN_INLINE Array & operator= (const T & val) { FlatArray::operator= (val); return *this; } /// array copy NETGEN_INLINE Array & operator= (const Array & a2) { SetSize0 (); SetSize (a2.Size()); for (size_t i = 0; i < size; i++) (*this)[i] = a2[i]; return *this; } /// steal array NETGEN_INLINE Array & operator= (Array && a2) { ngcore::Swap (size, a2.size); ngcore::Swap (data, a2.data); ngcore::Swap (allocsize, a2.allocsize); ngcore::Swap (mem_to_delete, a2.mem_to_delete); return *this; } /// array copy NETGEN_INLINE Array & operator= (const FlatArray & a2) { SetSize (a2.Size()); for (size_t i = 0; i < size; i++) (*this)[i] = a2[i]; return *this; } /* /// fill array with first, first+1, ... Array & operator= (const IntRange & range) { SetSize (range.Size()); for (int i = 0; i < size; i++) (*this)[i] = range.First()+i; return *this; } */ template Array & operator= (const BaseArrayObject & a2) { size_t newsize = a2.Spec().Size(); SetSize0 (); SetSize (newsize); // for (size_t i = 0; i < newsize; i++) // (*this)[i] = a2.Spec()[i]; size_t i = 0; for (auto val : a2.Spec()) (*this)[i++] = val; return *this; } template Array & operator= (Tuple tup) { SetSize (ArraySize (tup)); StoreToArray (*this, tup); return *this; } Array & operator= (std::initializer_list list) { *this = Array (list); return *this; } // template // Array & operator= (ParallelValue val) // { // FlatArray::operator= (val); // return *this; // } // template // Array & operator= (ParallelFunction val) // { // FlatArray::operator= (val); // return *this; // } NETGEN_INLINE void Swap (Array & b) { ngcore::Swap (size, b.size); ngcore::Swap (data, b.data); ngcore::Swap (allocsize, b.allocsize); ngcore::Swap (mem_to_delete, b.mem_to_delete); } private: /// resize array, at least to size minsize. copy contents NETGEN_INLINE void ReSize (size_t minsize); }; /// resize array, at least to size minsize. copy contents template NETGEN_INLINE void Array :: ReSize (size_t minsize) { size_t nsize = 2 * allocsize; if (nsize < minsize) nsize = minsize; T * hdata = data; data = new T[nsize]; if (hdata) { size_t mins = (nsize < size) ? nsize : size; #if defined(__GNUG__) && __GNUC__ < 5 && !defined(__clang__) for (size_t i = 0; i < mins; i++) data[i] = std::move(hdata[i]); #else if (std::is_trivially_copyable::value) memcpy ((void*)data, hdata, sizeof(T)*mins); else for (size_t i = 0; i < mins; i++) data[i] = std::move(hdata[i]); #endif delete [] mem_to_delete; } mem_to_delete = data; allocsize = nsize; } //extern template class Array; /** Array with static and dynamic memory management. Declares a static array which size is given by the template parameter. If the dynamic size fits into the static size, use static memory, otherwise perform dynamic allocation */ template class ArrayMem : public Array { T mem[S]; using Array::size; using Array::allocsize; using Array::data; using Array::mem_to_delete; // using Array::ownmem; public: /// Generate array of logical and physical size asize explicit ArrayMem(size_t asize = 0) : Array (S, mem) { size = asize; if (asize > S) { data = new T[asize]; allocsize = size; mem_to_delete = data; } } /// copies from Array a2 explicit ArrayMem(const Array & a2) : Array (S, (T*)mem) { Array::operator= (a2); } /// copies from ArrayMem a2 explicit ArrayMem(const ArrayMem & a2) : Array (S, (T*)mem) { Array::operator= (a2); } ArrayMem(ArrayMem && a2) : Array (a2.Size(), (T*)mem) { if (a2.mem_to_delete) { mem_to_delete = a2.mem_to_delete; data = a2.data; allocsize = a2.allocsize; a2.mem_to_delete = nullptr; a2.data = nullptr; a2.size = 0; } else { allocsize = S; for (size_t i = 0; i < S; i++) mem[i] = a2.mem[i]; } } ArrayMem (std::initializer_list list) : ArrayMem (list.size()) { size_t cnt = 0; for (auto val : list) data[cnt++] = val; } ArrayMem & operator= (const T & val) { FlatArray::operator= (val); return *this; } /// array copy ArrayMem & operator= (const FlatArray & a2) { this->SetSize (a2.Size()); for (size_t i = 0; i < size; i++) (*this)[i] = a2[i]; return *this; } template ArrayMem & operator= (const BaseArrayObject & a2) { this->SetSize (a2.Spec().Size()); size_t i = 0; for (auto val : a2.Spec()) (*this)[i++] = val; return *this; } }; template size_t ArraySize (Tuple tup) { return 0;} template size_t ArraySize (Tuple tup) { return 1+ArraySize(tup.Tail()); } template size_t ArraySize (Tuple tup) { return tup.Head().Size()+ArraySize(tup.Tail()); } template void StoreToArray (FlatArray a, Tuple tup) { ; } template void StoreToArray (FlatArray a, Tuple tup) { a[0] = tup.Head(); StoreToArray (a.Range(1, a.Size()), tup.Tail()); } template void StoreToArray (FlatArray a, Tuple tup) { IntRange r = tup.Head(); a.Range(0,r.Size()) = r; StoreToArray (a.Range(r.Size(), a.Size()), tup.Tail()); } /* template template NETGEN_INLINE Array & Array :: operator= (Tuple tup) { SetSize (ArraySize (tup)); StoreToArray (*this, tup); } */ /* /// append integers to array inline Array & operator+= (Array & array, const IntRange & range) { int oldsize = array.Size(); int s = range.Next() - range.First(); array.SetSize (oldsize+s); for (int i = 0; i < s; i++) array[oldsize+i] = range.First()+i; return array; } */ /* template inline Array & operator+= (Array & array, const BaseArrayObject & a2) { size_t oldsize = array.Size(); size_t s = a2.Spec().Size(); array.SetSize (oldsize+s); for (size_t i = 0; i < s; i++) array[oldsize+i] = a2.Spec()[i]; return array; } */ template inline Array & operator+= (Array & array, const BaseArrayObject & a2) { auto oldsize = array.Size(); auto s = a2.Spec().Size(); array.SetSize (oldsize+s); for (auto val : a2.Spec()) array[oldsize++] = val; return array; } /// bubble sort array template inline void BubbleSort (FlatArray data) { T hv; for (size_t i = 0; i < data.Size(); i++) for (size_t j = i+1; j < data.Size(); j++) if (data[i] > data[j]) { hv = data[i]; data[i] = data[j]; data[j] = hv; } } /// bubble sort array template inline void BubbleSort (FlatArray data, FlatArray slave) { for (size_t i = 0; i < data.Size(); i++) for (size_t j = i+1; j < data.Size(); j++) if (data[i] > data[j]) { T hv = data[i]; data[i] = data[j]; data[j] = hv; S hvs = slave[i]; slave[i] = slave[j]; slave[j] = hvs; } } template void QuickSort (FlatArray data, TLESS less) { if (data.Size() <= 1) return; ptrdiff_t i = 0; ptrdiff_t j = data.Size()-1; T midval = data[ (i+j)/2 ]; do { while (less (data[i], midval)) i++; while (less (midval, data[j])) j--; if (i <= j) { Swap (data[i], data[j]); i++; j--; } } while (i <= j); QuickSort (data.Range (0, j+1), less); QuickSort (data.Range (i, data.Size()), less); } template NETGEN_INLINE bool DefaultLess (const T & a, const T & b) { return a < b; } template class DefaultLessCl { public: bool operator() (const T & a, const T & b) const { return a < b; } }; template NETGEN_INLINE void QuickSort (FlatArray data) { QuickSort (data, DefaultLessCl()); } template void QuickSortI (FlatArray data, FlatArray index, TLESS less) { if (index.Size() <= 1) return; ptrdiff_t i = 0; ptrdiff_t j = index.Size()-1; int midval = index[ (i+j)/2 ]; do { while (less (data[index[i]],data[midval]) ) i++; while (less (data[midval], data[index[j]])) j--; if (i <= j) { Swap (index[i], index[j]); i++; j--; } } while (i <= j); QuickSortI (data, index.Range (0, j+1), less); QuickSortI (data, index.Range (i, index.Size()), less); } template NETGEN_INLINE void QuickSortI (FlatArray data, FlatArray index) { QuickSortI (data, index, DefaultLessCl()); } template NETGEN_INLINE T xxxRemoveRef (const T & x) { return x; } template class SumArray : public BaseArrayObject> { const TA1 & a1; const TA2 & a2; public: SumArray (const TA1 & aa1, const TA2 & aa2) : a1(aa1), a2(aa2) { ; } size_t Size() const { return a1.Size()+a2.Size(); } auto operator[] (size_t i) const -> decltype (xxxRemoveRef (a1[0])) { return (i < a1.Size()) ? a1[i] : a2[i-a1.Size()]; } }; template SumArray operator+ (const BaseArrayObject & a1, const BaseArrayObject & a2) { return SumArray (a1.Spec(), a2.Spec()); } // head-tail array template class HTArray { HTArray tail; T head; public: HTArray () = default; HTArray (const HTArray &) = default; HTArray & operator= (const HTArray &) = default; T * Ptr () { return tail.Ptr(); } T & operator[] (size_t i) { return Ptr()[i]; } const T * Ptr () const { return tail.Ptr(); } const T & operator[] (size_t i) const { return Ptr()[i]; } template T & Elem() { return (NR==S-1) ? head : tail.template Elem(); } }; template class HTArray<1,T> { T head; public: HTArray () = default; HTArray (const HTArray &) = default; HTArray & operator= (const HTArray &) = default; T * Ptr () { return &head; } T & operator[] (size_t i) { return Ptr()[i]; } const T * Ptr () const { return &head; } const T & operator[] (size_t i) const { return Ptr()[i]; } template T & Elem() { // assert(NR==0, "HTArray index error"); return head; } }; template class HTArray<0,T> { // T head; // dummy variable public: HTArray () = default; HTArray (const HTArray &) = default; HTArray & operator= (const HTArray &) = default; /* T * Ptr () { return &head; } T & operator[] (size_t i) { return Ptr()[i]; } const T * Ptr () const { return &head; } const T & operator[] (size_t i) const { return Ptr()[i]; } template T & Elem() { // assert(false, "HTArray index error"); return head; } */ // T * Ptr () { return (T*)(void*)&head; } T * Ptr () { return (T*)(void*)this; } T & operator[] (size_t i) { return Ptr()[i]; } // const T * Ptr () const { return (const T*)(const void*)&head; } const T * Ptr () const { return (const T*)(const void*)this; } const T & operator[] (size_t i) const { return Ptr()[i]; } template T & Elem() { throw Exception("illegal HTArray<0>::Elem<0>"); } }; template const T * operator+ (const HTArray & ar, size_t i) { return ar.Ptr()+i; } template T * operator+ (HTArray & ar, size_t i) { return ar.Ptr()+i; } } #endif // NETGEN_CORE_ARRAY_HPP