From eaad38e5881c2e3096f0d86ed4c4f6c8e1637d0d Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Thu, 7 Mar 2024 09:00:19 -0500 Subject: [PATCH] array --- cloc.sh | 1 + exclude.txt | 1 + include/blt/std/array.h | 170 ++++++++++++++++++++++++++++++++++ include/blt/std/memory.h | 59 ------------ include/blt/std/memory_util.h | 66 ++++++++++++- include/blt/std/vector.h | 8 +- 6 files changed, 241 insertions(+), 64 deletions(-) create mode 100755 cloc.sh create mode 100644 exclude.txt create mode 100644 include/blt/std/array.h diff --git a/cloc.sh b/cloc.sh new file mode 100755 index 0000000..ae7f15d --- /dev/null +++ b/cloc.sh @@ -0,0 +1 @@ +cloc --exclude-list-file=exclude.txt include src diff --git a/exclude.txt b/exclude.txt new file mode 100644 index 0000000..9e7bd9d --- /dev/null +++ b/exclude.txt @@ -0,0 +1 @@ +include/blt/unicode_emoji.h diff --git a/include/blt/std/array.h b/include/blt/std/array.h new file mode 100644 index 0000000..670bd7e --- /dev/null +++ b/include/blt/std/array.h @@ -0,0 +1,170 @@ +/* + * + * Copyright (C) 2024 Brett Terpstra + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef BLT_ARRAY_H +#define BLT_ARRAY_H + +#include +#include +#include +#include +#include + +namespace blt +{ + + template + class array + { + public: + using iterator = blt::ptr_iterator; + using const_iterator = blt::ptr_iterator; + using reverse_iterator = std::reverse_iterator; + using const_reverse_iterator = std::reverse_iterator; + private: + struct metadata + { + // size in number of elements! + blt::size_t size; + + explicit metadata(blt::size_t size): size(size) + {} + } metadata; + + T* _data = static_cast(static_cast(this) + sizeof(metadata)); + public: + /** + * constructs an array out of a block of memory of size bytes + * @param size number of bytes available in the memory allocated to this array. + */ + explicit array(blt::size_t size): metadata( (size - sizeof(metadata)) / sizeof(T)) + {} + + inline T& operator[](blt::size_t index) + { + return _data[index]; + } + + inline const T& operator[](blt::size_t index) const + { + return _data[index]; + } + + [[nodiscard]] inline T& at(blt::size_t index) + { + if (index > size()) + throw std::runtime_error("Index " + std::to_string(index) += " is outside the bounds of this array!"); + return _data[index]; + } + + [[nodiscard]] inline const T& at(blt::size_t index) const + { + if (index > size()) + throw std::runtime_error("Index " + std::to_string(index) += " is outside the bounds of this array!"); + return _data[index]; + } + + [[nodiscard]] inline T* data() + { + return _data; + } + + [[nodiscard]] inline T* data() const + { + return _data; + } + + [[nodiscard]] inline blt::size_t size() const + { + return metadata.size; + } + + [[nodiscard]] inline blt::size_t size_bytes() const + { + return (metadata.size * sizeof(T)) + sizeof(metadata); + } + + constexpr inline T* operator*() + { + return _data; + } + + constexpr inline T& front() + { + return *_data; + } + + constexpr inline const T& front() const + { + return *_data; + } + + constexpr inline T& back() + { + return _data[size() - 1]; + } + + constexpr inline const T& back() const + { + return _data[size() - 1]; + } + + constexpr inline iterator begin() const noexcept + { + return data(); + } + + constexpr inline iterator end() const noexcept + { + return data() + size(); + } + + constexpr inline const_iterator cbegin() const noexcept + { + return data(); + } + + constexpr inline const_iterator cend() const noexcept + { + return data() + size(); + } + + constexpr inline reverse_iterator rbegin() const noexcept + { + return reverse_iterator{end()}; + } + + constexpr inline reverse_iterator rend() const noexcept + { + return reverse_iterator{begin()}; + } + + constexpr inline const_iterator crbegin() const noexcept + { + return const_reverse_iterator{cend()}; + } + + constexpr inline reverse_iterator crend() const noexcept + { + return reverse_iterator{cbegin()}; + } + }; + +} + +#endif //BLT_ARRAY_H diff --git a/include/blt/std/memory.h b/include/blt/std/memory.h index 8cb4406..9b4b2a5 100644 --- a/include/blt/std/memory.h +++ b/include/blt/std/memory.h @@ -22,65 +22,6 @@ namespace blt { - template - struct ptr_iterator - { - public: - using iterator_category = std::random_access_iterator_tag; - using difference_type = std::ptrdiff_t; - using value_type = V; - using pointer = value_type*; - using reference = value_type&; - - explicit ptr_iterator(V* v): _v(v) - {} - - reference operator*() const - { return *_v; } - - pointer operator->() - { return _v; } - - ptr_iterator& operator++() - { - _v++; - return *this; - } - - ptr_iterator& operator--() - { - _v--; - return *this; - } - - ptr_iterator operator++(int) - { - auto tmp = *this; - ++(*this); - return tmp; - } - - ptr_iterator operator--(int) - { - auto tmp = *this; - --(*this); - return tmp; - } - - friend bool operator==(const ptr_iterator& a, const ptr_iterator& b) - { - return a._v == b._v; - } - - friend bool operator!=(const ptr_iterator& a, const ptr_iterator& b) - { - return a._v != b._v; - } - - private: - V* _v; - }; - template || std::is_copy_assignable_v> class scoped_buffer; diff --git a/include/blt/std/memory_util.h b/include/blt/std/memory_util.h index c39d26e..bfe8b06 100644 --- a/include/blt/std/memory_util.h +++ b/include/blt/std/memory_util.h @@ -28,8 +28,9 @@ #if defined(__clang__) || defined(__llvm__) || defined(__GNUC__) || defined(__GNUG__) #if defined(__GNUC__) || defined(__GNUG__) + #include - + #define SWAP16(val) bswap_16(val) #define SWAP32(val) bswap_32(val) #define SWAP64(val) bswap_64(val) @@ -127,6 +128,69 @@ namespace blt::mem std::memcpy(&r, &type, sizeof(type)); return r; } + +} + +namespace blt +{ + template + struct ptr_iterator + { + public: + using iterator_category = std::random_access_iterator_tag; + using difference_type = std::ptrdiff_t; + using value_type = V; + using pointer = value_type*; + using reference = value_type&; + + explicit ptr_iterator(V* v): _v(v) + {} + + reference operator*() const + { return *_v; } + + pointer operator->() + { return _v; } + + ptr_iterator& operator++() + { + _v++; + return *this; + } + + ptr_iterator& operator--() + { + _v--; + return *this; + } + + ptr_iterator operator++(int) + { + auto tmp = *this; + ++(*this); + return tmp; + } + + ptr_iterator operator--(int) + { + auto tmp = *this; + --(*this); + return tmp; + } + + friend bool operator==(const ptr_iterator& a, const ptr_iterator& b) + { + return a._v == b._v; + } + + friend bool operator!=(const ptr_iterator& a, const ptr_iterator& b) + { + return a._v != b._v; + } + + private: + V* _v; + }; } #endif //BLT_MEMORY_UTIL_H diff --git a/include/blt/std/vector.h b/include/blt/std/vector.h index 3318fe7..57bde2e 100644 --- a/include/blt/std/vector.h +++ b/include/blt/std/vector.h @@ -108,12 +108,12 @@ namespace blt return buffer_; } - constexpr inline iterator begin() noexcept + constexpr inline iterator begin() const noexcept { return data(); } - constexpr inline iterator end() noexcept + constexpr inline iterator end() const noexcept { return data() + size(); } @@ -128,12 +128,12 @@ namespace blt return data() + size(); } - constexpr inline reverse_iterator rbegin() noexcept + constexpr inline reverse_iterator rbegin() const noexcept { return reverse_iterator{end()}; } - constexpr inline reverse_iterator rend() noexcept + constexpr inline reverse_iterator rend() const noexcept { return reverse_iterator{begin()}; }