libcruft-util/pascal.cpp

213 lines
5.3 KiB
C++
Raw Normal View History

/*
2015-04-13 18:05:28 +10:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
2015-04-13 18:05:28 +10:00
* http://www.apache.org/licenses/LICENSE-2.0
*
2015-04-13 18:05:28 +10:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
2016-03-17 18:10:46 +11:00
* Copyright 2010-2016 Danny Robson <danny@nerdcruft.net>
*/
#include "pascal.hpp"
2017-02-21 21:19:58 +11:00
#include "debug.hpp"
#include <iterator>
#include <stdexcept>
using util::parray;
2016-03-17 18:10:46 +11:00
///////////////////////////////////////////////////////////////////////////////
2017-02-21 21:19:58 +11:00
template <typename DataT, typename SizeT>
parray<DataT,SizeT>::parray (SizeT _size, DataT *_data):
2016-03-17 18:10:46 +11:00
m_size (_size),
m_data (_data)
{ ; }
2016-03-17 18:10:46 +11:00
///////////////////////////////////////////////////////////////////////////////
2017-02-21 21:19:58 +11:00
template <typename DataT, typename SizeT>
DataT&
parray<DataT, SizeT>::operator[] (SizeT idx)
2016-03-17 18:10:46 +11:00
{
2017-02-21 21:19:58 +11:00
CHECK_LIMIT(idx, 0u, m_size);
2016-03-17 18:10:46 +11:00
return data ()[idx];
}
2016-03-17 18:10:46 +11:00
//-----------------------------------------------------------------------------
2017-02-21 21:19:58 +11:00
template <typename DataT, typename SizeT>
const DataT&
parray<DataT, SizeT>::operator[] (SizeT idx) const
2016-03-17 18:10:46 +11:00
{
2017-02-21 21:19:58 +11:00
CHECK_LIMIT(idx, 0u, m_size);
2016-03-17 18:10:46 +11:00
return data ()[idx];
}
2016-03-17 18:10:46 +11:00
///////////////////////////////////////////////////////////////////////////////
2017-02-21 21:19:58 +11:00
template <typename DataT, typename SizeT>
DataT&
parray<DataT, SizeT>::at (SizeT idx)
2016-03-17 18:10:46 +11:00
{
if (idx >= size ())
throw std::out_of_range ("invalid index for parray");
return (*this)[idx];
}
2016-03-17 18:10:46 +11:00
//-----------------------------------------------------------------------------
2017-02-21 21:19:58 +11:00
template <typename DataT, typename SizeT>
const DataT&
parray<DataT, SizeT>::at (SizeT idx) const
2016-03-17 18:10:46 +11:00
{
if (idx >= size ())
throw std::out_of_range ("invalid index for parray");
return (*this)[idx];
}
2016-03-17 18:10:46 +11:00
///////////////////////////////////////////////////////////////////////////////
2017-02-21 21:19:58 +11:00
template <typename DataT, typename SizeT>
DataT*
parray<DataT, SizeT>::begin (void)
2016-03-17 18:10:46 +11:00
{
return data ();
}
2016-03-17 18:10:46 +11:00
//-----------------------------------------------------------------------------
2017-02-21 21:19:58 +11:00
template <typename DataT, typename SizeT>
DataT*
parray<DataT, SizeT>::end (void)
2016-03-17 18:10:46 +11:00
{
return data () + size ();
}
///////////////////////////////////////////////////////////////////////////////
2017-02-21 21:19:58 +11:00
template <typename DataT, typename SizeT>
const DataT*
parray<DataT, SizeT>::cbegin (void) const
2016-03-17 18:10:46 +11:00
{
return data ();
}
//-----------------------------------------------------------------------------
2017-02-21 21:19:58 +11:00
template <typename DataT, typename SizeT>
const DataT*
parray<DataT, SizeT>::cend (void) const
2016-03-17 18:10:46 +11:00
{
return data () + size ();
}
2016-03-17 18:10:46 +11:00
///////////////////////////////////////////////////////////////////////////////
2017-02-21 21:19:58 +11:00
template <typename DataT, typename SizeT>
const DataT*
parray<DataT, SizeT>::data (void) const
2016-03-17 18:10:46 +11:00
{
return m_data;
}
//-----------------------------------------------------------------------------
2017-02-21 21:19:58 +11:00
template <typename DataT, typename SizeT>
DataT*
parray<DataT, SizeT>::data (void)
2016-03-17 18:10:46 +11:00
{
return m_data;
}
//-----------------------------------------------------------------------------
2017-02-21 21:19:58 +11:00
template <typename DataT, typename SizeT>
SizeT
parray<DataT, SizeT>::size (void) const
2016-03-17 18:10:46 +11:00
{
return m_size;
}
///////////////////////////////////////////////////////////////////////////////
2017-02-21 21:19:58 +11:00
template <typename SizeT>
std::ostream&
2017-02-21 21:19:58 +11:00
util::operator<< (std::ostream &os, parray<const char,SizeT> p)
2016-03-17 18:10:46 +11:00
{
2017-02-21 21:19:58 +11:00
std::copy_n (
p.cbegin (),
p.size (),
std::ostream_iterator<const char> (os)
);
return os;
}
2016-03-17 18:10:46 +11:00
//-----------------------------------------------------------------------------
2017-02-21 21:19:58 +11:00
template <typename SizeT>
std::ostream&
util::operator<< (std::ostream &os, parray<char,SizeT> p)
{
std::copy_n (
p.cbegin (),
p.size (),
std::ostream_iterator<char> (os)
);
return os;
}
///////////////////////////////////////////////////////////////////////////////
template <typename DataT, typename SizeT>
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;
}
2016-03-17 18:10:46 +11:00
///////////////////////////////////////////////////////////////////////////////
2017-02-21 21:19:58 +11:00
#define INSTANTIATE_D_S(D,S) \
template std::ostream& util::operator<< (std::ostream&, parray< D,S>); \
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 (uint8_t)
2017-02-21 21:19:58 +11:00
INSTANTIATE_D (uint16_t)
INSTANTIATE_D (uint32_t)
INSTANTIATE_D (uint64_t)
INSTANTIATE_D (int8_t)
INSTANTIATE_D (int16_t)
INSTANTIATE_D (int32_t)
INSTANTIATE_D (int64_t)