pascal: parameterise on size type

This commit is contained in:
Danny Robson 2017-02-21 21:19:58 +11:00
parent a56d1e339a
commit 6ba88a1048
2 changed files with 113 additions and 81 deletions

View File

@ -16,41 +16,46 @@
#include "pascal.hpp" #include "pascal.hpp"
#include "debug.hpp"
#include <iterator>
#include <stdexcept> #include <stdexcept>
using util::parray; using util::parray;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template <typename T> template <typename DataT, typename SizeT>
parray<T>::parray (size_t _size, T *_data): parray<DataT,SizeT>::parray (SizeT _size, DataT *_data):
m_size (_size), m_size (_size),
m_data (_data) m_data (_data)
{ ; } { ; }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template <typename T> template <typename DataT, typename SizeT>
T& DataT&
parray<T>::operator[] (size_t idx) parray<DataT, SizeT>::operator[] (SizeT idx)
{ {
CHECK_LIMIT(idx, 0u, m_size);
return data ()[idx]; return data ()[idx];
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename DataT, typename SizeT>
const T& const DataT&
parray<T>::operator[] (size_t idx) const parray<DataT, SizeT>::operator[] (SizeT idx) const
{ {
CHECK_LIMIT(idx, 0u, m_size);
return data ()[idx]; return data ()[idx];
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template <typename T> template <typename DataT, typename SizeT>
T& DataT&
parray<T>::at (size_t idx) parray<DataT, SizeT>::at (SizeT idx)
{ {
if (idx >= size ()) if (idx >= size ())
throw std::out_of_range ("invalid index for parray"); throw std::out_of_range ("invalid index for parray");
@ -60,9 +65,9 @@ parray<T>::at (size_t idx)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename DataT, typename SizeT>
const T& const DataT&
parray<T>::at (size_t idx) const parray<DataT, SizeT>::at (SizeT idx) const
{ {
if (idx >= size ()) if (idx >= size ())
throw std::out_of_range ("invalid index for parray"); throw std::out_of_range ("invalid index for parray");
@ -72,110 +77,129 @@ parray<T>::at (size_t idx) const
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template <typename T> template <typename DataT, typename SizeT>
T* DataT*
parray<T>::begin (void) parray<DataT, SizeT>::begin (void)
{ {
return data (); return data ();
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename DataT, typename SizeT>
T* DataT*
parray<T>::end (void) parray<DataT, SizeT>::end (void)
{ {
return data () + size (); return data () + size ();
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template <typename T> template <typename DataT, typename SizeT>
const T* const DataT*
parray<T>::cbegin (void) const parray<DataT, SizeT>::cbegin (void) const
{ {
return data (); return data ();
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename DataT, typename SizeT>
const T* const DataT*
parray<T>::cend (void) const parray<DataT, SizeT>::cend (void) const
{ {
return data () + size (); return data () + size ();
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template <typename T> template <typename DataT, typename SizeT>
const T* const DataT*
parray<T>::data (void) const parray<DataT, SizeT>::data (void) const
{ {
return m_data; return m_data;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename DataT, typename SizeT>
T* DataT*
parray<T>::data (void) parray<DataT, SizeT>::data (void)
{ {
return m_data; return m_data;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename DataT, typename SizeT>
size_t SizeT
parray<T>::size (void) const parray<DataT, SizeT>::size (void) const
{ {
return m_size; return m_size;
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template <typename T> template <typename SizeT>
std::ostream& std::ostream&
util::operator<< (std::ostream &os, parray<T> p) util::operator<< (std::ostream &os, parray<const char,SizeT> p)
{ {
auto size = p.size (); std::copy_n (
uintptr_t ptr = reinterpret_cast<uintptr_t> (p.data ()); p.cbegin (),
os << "[" << size << ", " << std::hex << ptr << std::dec << "]"; p.size (),
std::ostream_iterator<const char> (os)
);
return os; return os;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template std::ostream& util::operator<< (std::ostream&, parray<uint16_t>); template <typename SizeT>
template std::ostream& util::operator<< (std::ostream&, parray<uint32_t>); std::ostream&
template std::ostream& util::operator<< (std::ostream&, parray<uint64_t>); util::operator<< (std::ostream &os, parray<char,SizeT> p)
{
std::copy_n (
p.cbegin (),
p.size (),
std::ostream_iterator<char> (os)
);
template std::ostream& util::operator<< (std::ostream&, parray<const uint16_t>); return os;
template std::ostream& util::operator<< (std::ostream&, parray<const uint32_t>); }
template std::ostream& util::operator<< (std::ostream&, parray<const uint64_t>);
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template class util::parray<char>; template <typename DataT, typename SizeT>
template class util::parray<const char>; std::ostream&
util::operator<< (std::ostream &os, parray<DataT,SizeT> p)
{
auto size = p.size ();
uintptr_t ptr = reinterpret_cast<uintptr_t> (p.data ());
os << "[" << +size << ", " << std::hex << ptr << std::dec << "]";
return os;
}
template class util::parray<int8_t>;
template class util::parray<int16_t>;
template class util::parray<int32_t>;
template class util::parray<int64_t>;
template class util::parray<uint8_t>;
template class util::parray<uint16_t>;
template class util::parray<uint32_t>;
template class util::parray<uint64_t>;
template class util::parray<const int8_t>;
template class util::parray<const int16_t>;
template class util::parray<const int32_t>;
template class util::parray<const int64_t>;
template class util::parray<const uint8_t>; ///////////////////////////////////////////////////////////////////////////////
template class util::parray<const uint16_t>; #define INSTANTIATE_D_S(D,S) \
template class util::parray<const uint32_t>; template std::ostream& util::operator<< (std::ostream&, parray< D,S>); \
template class util::parray<const uint64_t>; template std::ostream& util::operator<< (std::ostream&, parray<const D,S>); \
\
template class util::parray< D,S>; \
template class util::parray<const D,S>;
//-----------------------------------------------------------------------------
#define INSTANTIATE_D(D) \
INSTANTIATE_D_S(D, uint16_t) \
INSTANTIATE_D_S(D, uint32_t) \
INSTANTIATE_D_S(D, uint64_t)
//-----------------------------------------------------------------------------
INSTANTIATE_D (char)
INSTANTIATE_D (uint16_t)
INSTANTIATE_D (uint32_t)
INSTANTIATE_D (uint64_t)

View File

@ -21,35 +21,43 @@
#include <ostream> #include <ostream>
namespace util { namespace util {
template <typename T> template <typename DataT, typename SizeT = std::size_t>
class parray { class parray {
public: public:
parray (size_t size, T *data); parray (SizeT size, DataT *data);
T& operator[] (size_t idx); DataT& operator[] (SizeT idx);
const T& operator[] (size_t idx) const; const DataT& operator[] (SizeT idx) const;
T& at (size_t idx); DataT& at (SizeT idx);
const T& at (size_t idx) const; const DataT& at (SizeT idx) const;
T* begin (void); DataT* begin (void);
T* end (void); DataT* end (void);
const T* cbegin (void) const; const DataT* cbegin (void) const;
const T* cend (void) const; const DataT* cend (void) const;
const T* data (void) const; const DataT* data (void) const;
T* data (void); DataT* data (void);
size_t size (void) const; SizeT size (void) const;
private: private:
const size_t m_size; const SizeT m_size;
T *m_data; DataT *m_data;
}; };
template <typename T> template <typename SizeT>
std::ostream& std::ostream&
operator<< (std::ostream&, util::parray<T>); operator<< (std::ostream&, util::parray<const char, SizeT>);
template <typename SizeT>
std::ostream&
operator<< (std::ostream&, util::parray<char, SizeT>);
template <typename DataT, typename SizeT>
std::ostream&
operator<< (std::ostream&, util::parray<DataT, SizeT>);
} }