From d68d381f727e08ec95c390fd038879746512d3eb Mon Sep 17 00:00:00 2001 From: q66 Date: Mon, 20 Apr 2015 19:06:47 +0100 Subject: [PATCH] move initializer lists into initializer_list.h + add new .range() method to retrieve a PointerRange to it --- octa/algorithm.h | 15 ++++++------- octa/initializer_list.h | 48 +++++++++++++++++++++++++++++++++++++++++ octa/utility.h | 29 ------------------------- octa/vector.h | 4 ++-- 4 files changed, 56 insertions(+), 40 deletions(-) create mode 100644 octa/initializer_list.h diff --git a/octa/algorithm.h b/octa/algorithm.h index 39ba176..e853dad 100644 --- a/octa/algorithm.h +++ b/octa/algorithm.h @@ -6,11 +6,12 @@ #ifndef OCTA_ALGORITHM_H #define OCTA_ALGORITHM_H -#include "math.h" +#include #include "octa/functional.h" #include "octa/range.h" #include "octa/utility.h" +#include "octa/initializer_list.h" namespace octa { template @@ -182,25 +183,21 @@ namespace octa { template inline T min(InitializerList il) { - return min_element(PointerRange(il.get(), - il.get() + il.length())).first(); + return min_element(il.range()).first(); } template inline T min(InitializerList il, C compare) { - return min_element(PointerRange(il.get(), - il.get() + il.length(), compare)).first(); + return min_element(il.range(), compare).first(); } template inline T max(InitializerList il) { - return max_element(PointerRange(il.get(), - il.get() + il.length())).first(); + return max_element(il.range()).first(); } template inline T max(InitializerList il, C compare) { - return max_element(PointerRange(il.get(), - il.get() + il.length(), compare)).first(); + return max_element(il.range(), compare).first(); } template diff --git a/octa/initializer_list.h b/octa/initializer_list.h new file mode 100644 index 0000000..fba8570 --- /dev/null +++ b/octa/initializer_list.h @@ -0,0 +1,48 @@ +/* Initializer list support for OctaSTD. + * + * This file is part of OctaSTD. See COPYING.md for futher information. + */ + +#ifndef OCTA_INITIALIZER_LIST_H +#define OCTA_INITIALIZER_LIST_H + +#include + +#include "octa/range.h" + +/* must be in std namespace otherwise the compiler won't know about it */ +namespace std { + template + class initializer_list { + const T *p_buf; + size_t p_len; + + initializer_list(const T *v, size_t n): p_buf(v), p_len(n) {} + public: + struct type { + typedef T value; + typedef T &reference; + typedef const T &const_reference; + typedef T *pointer; + typedef const T *const_pointer; + typedef size_t size; + typedef ptrdiff_t difference; + }; + + initializer_list(): p_buf(nullptr), p_len(0) {} + + size_t length() const { return p_len; } + + const T *get() const { return p_buf; } + + octa::PointerRange range() { + return octa::PointerRange(p_buf, p_len); + } + }; +} + +namespace octa { + template using InitializerList = std::initializer_list; +} + +#endif \ No newline at end of file diff --git a/octa/utility.h b/octa/utility.h index f81a2ed..f75c99e 100644 --- a/octa/utility.h +++ b/octa/utility.h @@ -8,36 +8,7 @@ #include -/* must be in std namespace otherwise the compiler won't know about it */ -namespace std { - template - class initializer_list { - const T *p_buf; - size_t p_len; - - initializer_list(const T *v, size_t n): p_buf(v), p_len(n) {} - public: - struct type { - typedef T value; - typedef T &reference; - typedef const T &const_reference; - typedef T *pointer; - typedef const T *const_pointer; - typedef size_t size; - typedef ptrdiff_t difference; - }; - - initializer_list(): p_buf(nullptr), p_len(0) {} - - size_t length() const { return p_len; } - - const T *get() const { return p_buf; } - }; -} - namespace octa { - template using InitializerList = std::initializer_list; - /* aliased in traits.h later */ namespace internal { template struct RemoveReference { typedef T type; }; diff --git a/octa/vector.h b/octa/vector.h index 593fc3d..fa2322e 100644 --- a/octa/vector.h +++ b/octa/vector.h @@ -14,6 +14,7 @@ #include "octa/utility.h" #include "octa/range.h" #include "octa/algorithm.h" +#include "octa/initializer_list.h" namespace octa { template @@ -264,8 +265,7 @@ namespace octa { } T *insert(size_t idx, InitializerList il) { - return insert_range(idx, PointerRange(il.get(), - il.get() + il.length())); + return insert_range(idx, il.range()); } typename type::range each() {