diff --git a/CMakeLists.txt b/CMakeLists.txt index fada4b77..b2a32755 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -217,6 +217,10 @@ list ( alloc/raw/stack.hpp annotation.hpp array/darray.hpp + array/sarray.cpp + array/sarray.hpp + array/parray.cpp + array/parray.hpp ascii.hpp backtrace.hpp bezier.cpp @@ -352,8 +356,6 @@ list ( parallel/queue.hpp parse.cpp parse.hpp - pascal.cpp - pascal.hpp platform.hpp point.cpp point.hpp @@ -380,8 +382,6 @@ list ( region.cpp region.hpp roots/bisection.hpp - sarray.cpp - sarray.hpp scoped.hpp si.cpp signal.cpp @@ -525,6 +525,8 @@ if (TESTS) alloc/forwarding affine array/darray + array/sarray + array/parray backtrace bezier bitwise @@ -577,7 +579,6 @@ if (TESTS) rational region roots/bisection - sarray signal singleton stream diff --git a/array/darray.hpp b/array/darray.hpp index 31097f20..889029ce 100644 --- a/array/darray.hpp +++ b/array/darray.hpp @@ -16,7 +16,8 @@ namespace cruft { - /// A runtime-sizable array with a capacity fixed at compile-time. + /// An array-like object with capacity fixed at instantiation time, and a + /// size which is variable at runtime. template class darray { public: diff --git a/pascal.cpp b/array/parray.cpp similarity index 97% rename from pascal.cpp rename to array/parray.cpp index 3c389241..7147f0d5 100644 --- a/pascal.cpp +++ b/array/parray.cpp @@ -6,9 +6,9 @@ * Copyright 2010-2016 Danny Robson */ -#include "pascal.hpp" +#include "parray.hpp" -#include "debug.hpp" +#include "../debug.hpp" #include #include @@ -18,9 +18,9 @@ using cruft::parray; /////////////////////////////////////////////////////////////////////////////// template -parray::parray (SizeT _size, DataT *_data): - m_size (_size), - m_data (_data) +parray::parray (DataT *_data, SizeT _size) + : m_data (_data) + , m_size (_size) { ; } diff --git a/array/parray.hpp b/array/parray.hpp new file mode 100644 index 00000000..6f99c167 --- /dev/null +++ b/array/parray.hpp @@ -0,0 +1,106 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright 2010-2018 Danny Robson + */ + +#pragma once + +#include +#include +#include +#include + + +namespace cruft { + /////////////////////////////////////////////////////////////////////////// + /// A pascal style array consisting of a size and a bare pointer. + template < + typename DataT, + typename SizeT = std::size_t + > + class parray { + public: + parray (DataT *data, SizeT size); + + template + explicit + parray (DataT (&data)[SizeV]): + parray (data+0, SizeV) + { ; } + + parray (parray const&) noexcept = default; + parray (parray &&) noexcept = default; + + DataT& operator[] (SizeT idx); + const DataT& operator[] (SizeT idx) const; + + DataT& at (SizeT idx); + const DataT& at (SizeT idx) const; + + DataT* begin (void); + DataT* end (void); + + DataT const* begin (void) const { return cbegin (); } + DataT const* end (void) const { return cend (); } + + const DataT* cbegin (void) const; + const DataT* cend (void) const; + + const DataT* data (void) const; + + DataT* data (void); + SizeT size (void) const; + + private: + DataT *m_data; + SizeT m_size; + }; + + + template + bool + equal (parray const &a, ValueB const (&b)[SizeB]) + { + return std::equal ( + std::begin (a), std::end (b), + std::begin (b), std::end (b) + ); + } + + + template + bool + equal (ValueB const (&a)[SizeB], parray const &b) + { + return std::equal ( + std::begin (a), std::end (b), + std::begin (b), std::end (b) + ); + } + + + //------------------------------------------------------------------------- + template + parray (DataT (&)[SizeV]) -> parray; + + + /////////////////////////////////////////////////////////////////////////// + template + std::ostream& + operator<< (std::ostream&, cruft::parray); + + + //------------------------------------------------------------------------- + template + std::ostream& + operator<< (std::ostream&, cruft::parray); + + + //------------------------------------------------------------------------- + template + std::ostream& + operator<< (std::ostream&, cruft::parray); +} diff --git a/sarray.cpp b/array/sarray.cpp similarity index 100% rename from sarray.cpp rename to array/sarray.cpp diff --git a/sarray.hpp b/array/sarray.hpp similarity index 83% rename from sarray.hpp rename to array/sarray.hpp index 43e48a5e..fe7ae267 100644 --- a/sarray.hpp +++ b/array/sarray.hpp @@ -6,24 +6,35 @@ * Copyright 2017 Danny Robson */ -#ifndef CRUFT_UTIL_SARRAY_HPP -#define CRUFT_UTIL_SARRAY_HPP +#pragma once -#include "iterator.hpp" +#include "../iterator.hpp" #include #include namespace cruft { - /// An array with constant maximum size, but with actual used storage - /// capacity fixed at construction time. + /// An array-like object with capacity fixed at instantiation time, and a + /// size which is fixed at construction time. /// /// \tparam S maximum number of elements /// \tparam T data type of elements template class sarray { public: + //--------------------------------------------------------------------- + sarray (T const &data) + : sarray (&data, &data + 1) + { ; } + + + //------------------------------------------------------------------------- + sarray (const T(&data)[S], std::size_t count = S): + sarray (data, data + count) + { ; } + + template sarray (InputIt first, InputIt last): m_size (std::distance (first, last)) @@ -86,15 +97,4 @@ namespace cruft { const std::size_t m_size; }; - - - //------------------------------------------------------------------------- - template - auto - make_sarray (const T(&data)[S], std::size_t count = S) - { - return sarray (data, data + count); - } } - -#endif diff --git a/pascal.hpp b/pascal.hpp deleted file mode 100644 index 749321ed..00000000 --- a/pascal.hpp +++ /dev/null @@ -1,56 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * Copyright 2010-2016 Danny Robson - */ - -#ifndef __UTIL_PASCAL_HPP -#define __UTIL_PASCAL_HPP - -#include -#include - -namespace cruft { - template - class parray { - public: - parray (SizeT size, DataT *data); - - DataT& operator[] (SizeT idx); - const DataT& operator[] (SizeT idx) const; - - DataT& at (SizeT idx); - const DataT& at (SizeT idx) const; - - DataT* begin (void); - DataT* end (void); - - const DataT* cbegin (void) const; - const DataT* cend (void) const; - - const DataT* data (void) const; - DataT* data (void); - SizeT size (void) const; - - private: - const SizeT m_size; - DataT *m_data; - }; - - template - std::ostream& - operator<< (std::ostream&, cruft::parray); - - template - std::ostream& - operator<< (std::ostream&, cruft::parray); - - template - std::ostream& - operator<< (std::ostream&, cruft::parray); -} - - -#endif diff --git a/test/array/parray.cpp b/test/array/parray.cpp new file mode 100644 index 00000000..984a491f --- /dev/null +++ b/test/array/parray.cpp @@ -0,0 +1,18 @@ +#include "array/parray.hpp" + +#include "tap.hpp" + + +/////////////////////////////////////////////////////////////////////////////// +int +main () +{ + cruft::TAP::logger tap; + + int raw[3] = { -5, 9, 0 }; + cruft::parray wrapped (raw); + + tap.expect (equal (raw, wrapped), "comparison of array and parray"); + + return tap.status (); +} diff --git a/test/sarray.cpp b/test/array/sarray.cpp similarity index 98% rename from test/sarray.cpp rename to test/array/sarray.cpp index b8f5a047..10edd64f 100644 --- a/test/sarray.cpp +++ b/test/array/sarray.cpp @@ -1,6 +1,6 @@ #include "tap.hpp" -#include "sarray.hpp" +#include "array/sarray.hpp" #include "debug.hpp" #include