From a8e6f4fb0a41cb26fc32a7a69a85988e84e1e4a1 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sun, 2 Apr 2017 20:18:38 -0700 Subject: [PATCH] ircd::util: Add vector_view. --- include/ircd/util.h | 73 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/include/ircd/util.h b/include/ircd/util.h index 8b834dc77..3d29bd39e 100644 --- a/include/ircd/util.h +++ b/include/ircd/util.h @@ -601,6 +601,79 @@ struct string_view using std::string_view::string_view; }; +template +struct vector_view +{ + T *_data { nullptr }; + T *_stop { nullptr }; + + public: + T *data() const { return _data; } + size_t size() const { return std::distance(_data, _stop); } + bool empty() const { return !size(); } + + const T *begin() const { return data(); } + const T *end() const { return _stop; } + const T *cbegin() { return data(); } + const T *cend() { return _stop; } + T *begin() { return data(); } + T *end() { return _stop; } + + const T &operator[](const size_t &pos) const + { + return *(data() + pos); + } + + T &operator[](const size_t &pos) + { + return *(data() + pos); + } + + const T &at(const size_t &pos) const + { + if(unlikely(pos >= size())) + throw std::out_of_range(); + + return operator[](pos); + } + + T &at(const size_t &pos) + { + if(unlikely(pos >= size())) + throw std::out_of_range(); + + return operator[](pos); + } + + vector_view(T *const &start, T *const &stop) + :_data{start} + ,_stop{stop} + {} + + vector_view(T *const &start, const size_t &size) + :_data{start} + ,_stop{start + size} + {} + + template + vector_view(std::vector &v) + :vector_view{v.data(), v.size()} + {} + + template + vector_view(T (&buffer)[SIZE]) + :vector_view{std::addressof(buffer), SIZE} + {} + + template + vector_view(std::array &array) + :vector_view{array.data(), array.size()} + {} + + vector_view() = default; +}; + // // Error-checking closure for POSIX system calls. Note the usage is