From 08bb790612339ebad9774d05e701e94f218fa127 Mon Sep 17 00:00:00 2001 From: q66 Date: Wed, 27 May 2015 23:46:58 +0100 Subject: [PATCH] stl-like initializer list interface (allow octastd to be used with stl outside of octaforge) --- octa/algorithm.h | 8 ++++---- octa/initializer_list.h | 11 ++++------- octa/vector.h | 12 ++++++------ 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/octa/algorithm.h b/octa/algorithm.h index 8b9ad36..7c6eac2 100644 --- a/octa/algorithm.h +++ b/octa/algorithm.h @@ -185,21 +185,21 @@ namespace octa { template inline T min(InitializerList il) { - return min_element(il.each()).first(); + return min_element(each(il)).first(); } template inline T min(InitializerList il, C compare) { - return min_element(il.each(), compare).first(); + return min_element(each(il), compare).first(); } template inline T max(InitializerList il) { - return max_element(il.each()).first(); + return max_element(each(il)).first(); } template inline T max(InitializerList il, C compare) { - return max_element(il.each(), compare).first(); + return max_element(each(il), compare).first(); } /* clamp */ diff --git a/octa/initializer_list.h b/octa/initializer_list.h index 43df685..61ac3b9 100644 --- a/octa/initializer_list.h +++ b/octa/initializer_list.h @@ -33,11 +33,8 @@ namespace std { size_t size() const { return p_len; } - const T *data() const { return p_buf; } - - octa::PointerRange each() { - return octa::PointerRange(p_buf, p_len); - } + const T *begin() const { return p_buf; } + const T *end() const { return p_buf + p_len; } }; } @@ -45,8 +42,8 @@ namespace octa { template using InitializerList = std::initializer_list; template - auto each(InitializerList init) -> decltype(init.each()) { - return init.each(); + PointerRange each(InitializerList init) { + return PointerRange(init.begin(), init.end()); } } diff --git a/octa/vector.h b/octa/vector.h index 396e120..592d4db 100644 --- a/octa/vector.h +++ b/octa/vector.h @@ -88,8 +88,8 @@ namespace octa { } Vector(InitializerList v): Vector() { - size_t len = v.size(); - const T *ptr = v.data(); + size_t len = v.end() - v.begin(); + const T *ptr = v.begin(); reserve(len); for (size_t i = 0; i < len; ++i) new (&p_buf[i]) T(ptr[i]); @@ -143,12 +143,12 @@ namespace octa { Vector &operator=(InitializerList il) { clear(); - size_t ilen = il.size(); + size_t ilen = il.end() - il.begin(); reserve(ilen); if (octa::IsPod()) { - memcpy(p_buf, il.data(), ilen); + memcpy(p_buf, il.begin(), ilen); } else { - T *buf = p_buf, *ibuf = il.data(), *last = il.data() + ilen; + T *buf = p_buf, *ibuf = il.begin(), *last = il.end(); while (ibuf != last) { new (buf++) T(*ibuf++); } @@ -295,7 +295,7 @@ namespace octa { } T *insert(size_t idx, InitializerList il) { - return insert_range(idx, il.each()); + return insert_range(idx, octa::each(il)); } RangeType each() {