From 37ccbf463c99f377b9ce451c71dbe94be409a78a Mon Sep 17 00:00:00 2001 From: kai Date: Fri, 10 Jan 2014 12:06:01 +0100 Subject: [PATCH] Add standard container methods to Array<>. With these methods the Array<> class can be used like std::vector<>. Please be aware that Array::reserve() has a slightly different semantic than std::vector::reserve(). The intention is to make usage of Array<> more natural. --- dmd2/root/root.h | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/dmd2/root/root.h b/dmd2/root/root.h index 3e1ce634..67032e8d 100644 --- a/dmd2/root/root.h +++ b/dmd2/root/root.h @@ -17,6 +17,10 @@ #include "port.h" #include "rmem.h" +#if IN_LLVM +#include +#endif + #if __DMC__ #pragma once #endif @@ -520,6 +524,78 @@ struct Array } return 0; } + +#if IN_LLVM + // Define members and types like std::vector + typedef size_t size_type; + + Array(const Array &a) : dim(0), data(0), allocdim(0) + { + setDim(a.dim); + memcpy(data, a.data, dim * sizeof(*data)); + } + + Array &operator=(Array &a) + { + setDim(a.dim); + memcpy(data, a.data, dim * sizeof(*data)); + return *this; + } + + size_type size() + { + return static_cast(dim); + } + + bool empty() + { + return dim == 0; + } + + TYPE *front() + { + return data[0]; + } + + TYPE *back() + { + return data[dim-1]; + } + + void push_back(TYPE *a) + { + push(a); + } + + void pop_back() + { + pop(); + } + + typedef TYPE **iterator; + typedef std::reverse_iterator reverse_iterator; + + iterator begin() + { + return static_cast(&data[0]); + } + + iterator end() + { + return static_cast(&data[dim]); + } + + reverse_iterator rbegin() + { + return reverse_iterator(end()); + } + + reverse_iterator rend() + { + return reverse_iterator(begin()); + } + +#endif }; // TODO: Remove (only used by disabled GC)