array: move sarray and parray into array directory

This commit is contained in:
Danny Robson 2018-12-17 12:35:45 +11:00
parent a2ef7cb75c
commit a7786f04b9
9 changed files with 154 additions and 84 deletions

View File

@ -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

View File

@ -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 <std::size_t CapacityV, typename ValueT>
class darray {
public:

View File

@ -6,9 +6,9 @@
* Copyright 2010-2016 Danny Robson <danny@nerdcruft.net>
*/
#include "pascal.hpp"
#include "parray.hpp"
#include "debug.hpp"
#include "../debug.hpp"
#include <iterator>
#include <stdexcept>
@ -18,9 +18,9 @@ using cruft::parray;
///////////////////////////////////////////////////////////////////////////////
template <typename DataT, typename SizeT>
parray<DataT,SizeT>::parray (SizeT _size, DataT *_data):
m_size (_size),
m_data (_data)
parray<DataT,SizeT>::parray (DataT *_data, SizeT _size)
: m_data (_data)
, m_size (_size)
{ ; }

106
array/parray.hpp Normal file
View File

@ -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 <danny@nerdcruft.net>
*/
#pragma once
#include <algorithm>
#include <iterator>
#include <cstddef>
#include <iosfwd>
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 <SizeT SizeV>
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 <typename ValueA, typename SizeA, typename ValueB, std::size_t SizeB>
bool
equal (parray<ValueA,SizeA> const &a, ValueB const (&b)[SizeB])
{
return std::equal (
std::begin (a), std::end (b),
std::begin (b), std::end (b)
);
}
template <typename ValueA, typename SizeA, typename ValueB, std::size_t SizeB>
bool
equal (ValueB const (&a)[SizeB], parray<ValueA,SizeA> const &b)
{
return std::equal (
std::begin (a), std::end (b),
std::begin (b), std::end (b)
);
}
//-------------------------------------------------------------------------
template <typename DataT, std::size_t SizeV>
parray (DataT (&)[SizeV]) -> parray<DataT,std::size_t>;
///////////////////////////////////////////////////////////////////////////
template <typename SizeT>
std::ostream&
operator<< (std::ostream&, cruft::parray<const char, SizeT>);
//-------------------------------------------------------------------------
template <typename SizeT>
std::ostream&
operator<< (std::ostream&, cruft::parray<char, SizeT>);
//-------------------------------------------------------------------------
template <typename DataT, typename SizeT>
std::ostream&
operator<< (std::ostream&, cruft::parray<DataT, SizeT>);
}

View File

@ -6,24 +6,35 @@
* Copyright 2017 Danny Robson <danny@nerdcruft.net>
*/
#ifndef CRUFT_UTIL_SARRAY_HPP
#define CRUFT_UTIL_SARRAY_HPP
#pragma once
#include "iterator.hpp"
#include "../iterator.hpp"
#include <cstdlib>
#include <stdexcept>
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 <std::size_t S, typename T>
class sarray {
public:
//---------------------------------------------------------------------
sarray (T const &data)
: sarray<S,T> (&data, &data + 1)
{ ; }
//-------------------------------------------------------------------------
sarray (const T(&data)[S], std::size_t count = S):
sarray<S,T> (data, data + count)
{ ; }
template <typename InputIt>
sarray (InputIt first, InputIt last):
m_size (std::distance (first, last))
@ -86,15 +97,4 @@ namespace cruft {
const std::size_t m_size;
};
//-------------------------------------------------------------------------
template <typename T, std::size_t S>
auto
make_sarray (const T(&data)[S], std::size_t count = S)
{
return sarray<S,T> (data, data + count);
}
}
#endif

View File

@ -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 <danny@nerdcruft.net>
*/
#ifndef __UTIL_PASCAL_HPP
#define __UTIL_PASCAL_HPP
#include <cstdlib>
#include <iosfwd>
namespace cruft {
template <typename DataT, typename SizeT = std::size_t>
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 <typename SizeT>
std::ostream&
operator<< (std::ostream&, cruft::parray<const char, SizeT>);
template <typename SizeT>
std::ostream&
operator<< (std::ostream&, cruft::parray<char, SizeT>);
template <typename DataT, typename SizeT>
std::ostream&
operator<< (std::ostream&, cruft::parray<DataT, SizeT>);
}
#endif

18
test/array/parray.cpp Normal file
View File

@ -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 ();
}

View File

@ -1,6 +1,6 @@
#include "tap.hpp"
#include "sarray.hpp"
#include "array/sarray.hpp"
#include "debug.hpp"
#include <vector>