1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2025-07-02 14:04:22 +00:00

Move MaxSizedArray to helpers include dir.

This commit is contained in:
King_DuckZ 2016-05-23 10:06:59 +02:00
parent b6d45eee3c
commit baa67138eb
6 changed files with 15 additions and 13 deletions

View file

@ -0,0 +1,114 @@
/* Copyright 2015, 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" 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.
*
* "dindexer" 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 "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef idBC9F804ADD33468A9C7657E823FFC706
#define idBC9F804ADD33468A9C7657E823FFC706
#include "implem/AutomemBase.hpp"
#include "implem/IteratorOnPtr.hpp"
#include <memory>
#include <cstddef>
#include <type_traits>
#include <cassert>
#include <utility>
namespace dinhelp {
const size_t MAXSZARR_MAX_STACK_ALLOC_SIZE = MAX_STACK_ALLOC_SIZE;
//TODO: add a template parameter to force stack allocation regardless of
//sizeof(T) * S
template <typename T, size_t S, typename A=std::allocator<T> >
class MaxSizedArray :
private std::conditional<
(sizeof(T) * S > (MAXSZARR_MAX_STACK_ALLOC_SIZE > 4 * sizeof(T*) ? MAXSZARR_MAX_STACK_ALLOC_SIZE - sizeof(T*) : MAXSZARR_MAX_STACK_ALLOC_SIZE)),
AutomemRawBase_heap<T, S, A>,
AutomemRawBase_stack<T, S>
>::type
{
typedef typename std::conditional<
(sizeof(T) * S > (MAXSZARR_MAX_STACK_ALLOC_SIZE > 4 * sizeof(T*) ? MAXSZARR_MAX_STACK_ALLOC_SIZE - sizeof(T*) : MAXSZARR_MAX_STACK_ALLOC_SIZE)),
AutomemRawBase_heap<T, S, A>,
AutomemRawBase_stack<T, S>
>::type parent_type;
public:
typedef IteratorOnPtr<T*, T, false> iterator;
typedef IteratorOnPtr<const T*, const T, false> const_iterator;
typedef IteratorOnPtr<T*, T, true> reverse_iterator;
typedef IteratorOnPtr<const T*, const T, true> const_reverse_iterator;
typedef T& reference;
typedef T&& mov_reference;
typedef const T& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
enum MAX_SIZE_ENUM {
MAX_SIZE = S
};
MaxSizedArray ( void );
MaxSizedArray ( const MaxSizedArray& parOther );
MaxSizedArray ( MaxSizedArray&& parOther );
~MaxSizedArray ( void );
bool empty ( void ) const;
size_type size ( void ) const;
size_type capacity ( void ) const { return S; }
size_type max_size ( void ) const { return S; }
void push_back ( const value_type& parNewItem );
void push_back ( value_type&& parNewItem );
void pop_back ( void );
iterator erase ( const iterator& parDele );
iterator erase ( const iterator& parFrom, const iterator& parToExcl );
void clear ( void );
mov_reference operator[] ( size_type parIndex );
const_reference operator[] ( size_type parIndex ) const;
MaxSizedArray& operator= ( const MaxSizedArray& parOther );
bool operator== ( const MaxSizedArray& parOther ) const;
bool operator!= ( const MaxSizedArray& parOther ) const;
pointer GetPointer ( void );
const_pointer GetPointer ( void ) const;
void reserve ( size_type parReserve );
iterator begin ( void );
const_iterator begin ( void ) const;
iterator end ( void );
const_iterator end ( void ) const;
reverse_iterator rbegin ( void );
const_reverse_iterator rbegin ( void ) const;
reverse_iterator rend ( void );
const_reverse_iterator rend ( void ) const;
reference front ( void ) { return (*this)[0]; }
reference back ( void ) { return (*this)[size() - 1]; }
const_reference front ( void ) const { return (*this)[0]; }
const_reference back ( void ) const { return (*this)[size() - 1]; }
private:
pointer GetPointer_NoAssert ( void ) { return m_localMem; }
const_pointer GetPointer_NoAssert ( void ) const { return m_localMem; }
T* m_localMem; //A copy of memory pointer is required
size_type m_used;
};
} //namespace dinhelp
#include "MaxSizedArray.inl"
#endif

View file

@ -0,0 +1,281 @@
/* Copyright 2015, 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" 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.
*
* "dindexer" 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 "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
namespace dinhelp {
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
MaxSizedArray<T, S, A>::MaxSizedArray() {
m_localMem = this->AllocMemory();
m_used = 0;
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
MaxSizedArray<T, S, A>::MaxSizedArray (const MaxSizedArray& parOther) :
parent_type(),
m_used(0)
{
m_localMem = this->AllocMemory();
const size_type count = parOther.size();
for (size_type z = 0; z < count; ++z) {
this->push_back(parOther[z]);
}
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
MaxSizedArray<T, S, A>::MaxSizedArray (MaxSizedArray&& parOther) :
parent_type(),
m_used(0)
{
m_localMem = this->AllocMemory();
const size_type count = parOther.size();
for (size_type z = 0; z < count; ++z) {
this->push_back(std::move(parOther[z]));
}
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
MaxSizedArray<T, S, A>::~MaxSizedArray() {
this->clear();
this->FreeMemory();
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
bool MaxSizedArray<T, S, A>::empty() const {
return 0 == m_used;
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
typename MaxSizedArray<T, S, A>::size_type MaxSizedArray<T, S, A>::size() const {
return m_used;
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
void MaxSizedArray<T, S, A>::push_back (value_type&& parNewItem) {
assert(size() < capacity());
this->GetNewT(m_used, std::move(parNewItem));
++m_used;
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
void MaxSizedArray<T, S, A>::push_back (const value_type& parNewItem) {
assert(size() < capacity());
this->GetNewT(m_used, parNewItem);
++m_used;
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
void MaxSizedArray<T, S, A>::pop_back() {
assert(not empty());
m_localMem[m_used - 1].~T();
--m_used;
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
typename MaxSizedArray<T, S, A>::mov_reference MaxSizedArray<T, S, A>::operator[] (size_type parIndex) {
assert(parIndex < size());
return std::move(m_localMem[parIndex]);
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
typename MaxSizedArray<T, S, A>::const_reference MaxSizedArray<T, S, A>::operator[] (size_type parIndex) const {
assert(parIndex < size());
return m_localMem[parIndex];
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
MaxSizedArray<T, S, A>& MaxSizedArray<T, S, A>::operator= (const MaxSizedArray& parOther) {
m_used = parOther.m_used;
std::copy(parOther.GetMemPtr(), parOther.GetMemPtr() + parOther.size(), this->GetMemPtr());
return *this;
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
bool MaxSizedArray<T, S, A>::operator== (const MaxSizedArray& parOther) const {
if (size() != parOther.size())
return false;
for (size_type z = 0; z < size(); ++z) {
if ((*this)[z] != parOther[z])
return false;
}
return true;
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
bool MaxSizedArray<T, S, A>::operator!= (const MaxSizedArray& parOther) const {
return not (*this == parOther);
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
typename MaxSizedArray<T, S, A>::pointer MaxSizedArray<T, S, A>::GetPointer() {
assert(size() > 0);
return GetPointer_NoAssert();
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
typename MaxSizedArray<T, S, A>::const_pointer MaxSizedArray<T, S, A>::GetPointer() const {
assert(size() > 0);
return GetPointer_NoAssert();
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
void MaxSizedArray<T, S, A>::clear() {
const size_type count = this->size();
for (size_type z = 0; z < count; ++z) {
(*this)[z].~T();
}
m_used = 0;
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
typename MaxSizedArray<T, S, A>::iterator MaxSizedArray<T, S, A>::begin() {
return iterator(GetPointer_NoAssert(), size());
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
typename MaxSizedArray<T, S, A>::const_iterator MaxSizedArray<T, S, A>::begin() const {
return const_iterator(GetPointer_NoAssert(), size());
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
typename MaxSizedArray<T, S, A>::iterator MaxSizedArray<T, S, A>::end() {
return iterator(GetPointer_NoAssert() + size(), 0);
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
typename MaxSizedArray<T, S, A>::const_iterator MaxSizedArray<T, S, A>::end() const {
return const_iterator(GetPointer_NoAssert() + size(), 0);
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
typename MaxSizedArray<T, S, A>::reverse_iterator MaxSizedArray<T, S, A>::rbegin() {
return reverse_iterator(GetPointer_NoAssert() + size() - 1, size());
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
typename MaxSizedArray<T, S, A>::const_reverse_iterator MaxSizedArray<T, S, A>::rbegin() const {
return const_reverse_iterator(GetPointer_NoAssert() + size() - 1, size());
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
typename MaxSizedArray<T, S, A>::reverse_iterator MaxSizedArray<T, S, A>::rend() {
return reverse_iterator(GetPointer_NoAssert() - 1, 0);
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
typename MaxSizedArray<T, S, A>::const_reverse_iterator MaxSizedArray<T, S, A>::rend() const {
return const_reverse_iterator(GetPointer_NoAssert() - 1, 0);
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
typename MaxSizedArray<T, S, A>::iterator MaxSizedArray<T, S, A>::erase (const iterator& parDele) {
assert(end() != parDele);
return erase(parDele, parDele + 1);
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
typename MaxSizedArray<T, S, A>::iterator MaxSizedArray<T, S, A>::erase (const iterator& parFrom, const iterator& parToExcl) {
assert(parFrom >= begin());
assert(parToExcl <= end());
assert(parToExcl >= parFrom);
//I'm doing this in two passes: first, I'm deleting as many elements as
//the ones that would be left at the end (that is, delete and move),
//then delete everything to the end of the buffer, if necessary.
const size_type deleCount = static_cast<size_type>(parToExcl - parFrom);
const size_type firstIndexToDele = static_cast<size_type>(parFrom - begin());
const size_type& sz = m_used;
assert(firstIndexToDele + deleCount <= sz);
const size_type deleAndCopyCount = sz - (firstIndexToDele + deleCount);
//As said, make room and copy from the cut tail
for (size_type z = firstIndexToDele; z < firstIndexToDele + deleAndCopyCount; z++) {
(*this)[z].~T();
new(&(*this)[z]) T((*this)[z + deleCount]);
}
//Any leftover is rubbish
for (size_type z = firstIndexToDele + deleAndCopyCount; z < sz; z++) {
(*this)[z].~T();
}
m_used -= deleCount;
return begin() + firstIndexToDele;
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
void MaxSizedArray<T, S, A>::reserve (size_type parReserve) {
assert(parReserve <= S);
if (parReserve > S) {
throw std::length_error("Unable to reserve more memory than the build-time size for MaxSizedArray");
}
}
} //namespace dinhelp

View file

@ -0,0 +1,91 @@
/* Copyright 2015, 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" 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.
*
* "dindexer" 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 "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef idDB299AE3079F4830BB1D543C07F7B594
#define idDB299AE3079F4830BB1D543C07F7B594
//Classes in this file are low level and only intended to be used by library
//code. They don't do much by themselves and shoud never be used from outside.
#include <memory>
#include <algorithm>
#include <cassert>
#include <cstdint>
#if !defined(NDEBUG)
# define ASSERTIONSENABLED
#endif
namespace dinhelp {
const size_t MAX_STACK_ALLOC_SIZE = 128;
///-------------------------------------------------------------------------
///Heap-based allocation, only gets raw memory
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A=std::allocator<T> >
class AutomemRawBase_heap {
protected:
AutomemRawBase_heap ( void );
AutomemRawBase_heap ( const AutomemRawBase_heap& ) = delete; //Copy ctor can't be implemented at this level
AutomemRawBase_heap ( AutomemRawBase_heap&& parOther );
template <typename... Args>
T* GetNewT ( size_t parIndex, Args&&... parArgs );
T* AllocMemory ( void );
void FreeMemory ( void ) noexcept;
void swap ( AutomemRawBase_heap& parOther ) noexcept { std::swap(m_localMem, parOther.m_localMem); }
private:
void operator= (const AutomemRawBase_heap&);
#if defined(ASSERTIONSENABLED)
typedef uintptr_t PTR_INT_TYPE;
static_assert(sizeof(PTR_INT_TYPE) == sizeof(char*), "Wrong uintptr_t size");
#endif
T* m_localMem;
};
///-------------------------------------------------------------------------
///Stack-based allocation, only gets raw memory
///-------------------------------------------------------------------------
template <typename T, size_t S>
class AutomemRawBase_stack {
protected:
AutomemRawBase_stack ( void ) = default;
AutomemRawBase_stack ( const AutomemRawBase_stack& ) = delete; //Copy ctor can't be implemented at this level
AutomemRawBase_stack ( AutomemRawBase_stack&& ) = delete;
~AutomemRawBase_stack ( void ) = default;
template <typename... Args>
T* GetNewT ( size_t parIndex, Args&&... parArgs );
T* AllocMemory ( void );
void FreeMemory ( void ) noexcept { return; }
private:
#if defined(ASSERTIONSENABLED)
typedef uintptr_t PTR_INT_TYPE;
static_assert(sizeof(PTR_INT_TYPE) == sizeof(char*), "Wrong uintptr_t size");
#endif
void operator= (const AutomemRawBase_stack&);
typename std::aligned_storage<sizeof(T), alignof(T)>::type m_localMem[S];
};
} //namespace dinhelp
#include "AutomemBase.inl"
#if defined(ASSERTIONSENABLED)
# undef ASSERTIONSENABLED
#endif
#endif

View file

@ -0,0 +1,118 @@
/* Copyright 2015, 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" 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.
*
* "dindexer" 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 "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
namespace dinhelp {
namespace {
#if defined(ASSERTIONSENABLED)
const char g_guard = 0xAB;
#endif
} //unnamed namespace
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
AutomemRawBase_heap<T, S, A>::AutomemRawBase_heap() {
#if !defined(NDEBUG)
m_localMem = nullptr;
#endif
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
AutomemRawBase_heap<T, S, A>::AutomemRawBase_heap (AutomemRawBase_heap&& parOther) {
#if !defined(NDEBUG)
m_localMem = nullptr;
#endif
this->swap(parOther);
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
T* AutomemRawBase_heap<T, S, A>::AllocMemory() {
#if !defined(NDEBUG)
Assert(nullptr == m_localMem);
#endif
m_localMem = A().allocate(S);
#if defined(ASSERTIONSENABLED)
assert(reinterpret_cast<PTR_INT_TYPE>(m_localMem) % alignof(T) == 0); //Make sure alignment is correct
std::fill(
reinterpret_cast<char*>(&m_localMem[0]),
reinterpret_cast<char*>(&m_localMem[0]) + sizeof(m_localMem),
g_guard
);
#endif
return m_localMem;
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
void AutomemRawBase_heap<T, S, A>::FreeMemory() noexcept {
#if !defined(NDEBUG)
Assert(nullptr != m_localMem);
#endif
A().deallocate(m_localMem, S);
#if !defined(NDEBUG)
m_localMem = nullptr;
#endif
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S, typename A>
template <typename... Args>
T* AutomemRawBase_heap<T, S, A>::GetNewT (size_t parIndex, Args&&... parArgs) {
assert(parIndex < S);
T* const location = m_localMem + parIndex;
#if defined(ASSERTIONSENABLED)
assert(reinterpret_cast<PTR_INT_TYPE>(location) % alignof(T) == 0);
assert(g_guard == *reinterpret_cast<const char*>(location));
#endif
return new(location) T(std::forward<Args...>(parArgs)...);
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S>
T* AutomemRawBase_stack<T, S>::AllocMemory() {
#if defined(ASSERTIONSENABLED)
assert(reinterpret_cast<PTR_INT_TYPE>(m_localMem) % alignof(T) == 0); //Make sure alignment is correct
std::fill(
reinterpret_cast<char*>(&m_localMem[0]),
reinterpret_cast<char*>(&m_localMem[0]) + sizeof(m_localMem),
g_guard
);
#endif
return reinterpret_cast<T*>(&m_localMem[0]);
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename T, size_t S>
template <typename... Args>
T* AutomemRawBase_stack<T, S>::GetNewT (size_t parIndex, Args&&... parArgs) {
assert(parIndex < S);
auto* const location = &m_localMem[parIndex];
#if defined(ASSERTIONSENABLED)
assert(reinterpret_cast<PTR_INT_TYPE>(location) % alignof(T) == 0);
assert(g_guard == *reinterpret_cast<const char*>(location));
#endif
return new(location) T(std::forward<Args...>(parArgs)...);
}
} //namespace dinhelp

View file

@ -0,0 +1,314 @@
/* Copyright 2015, 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" 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.
*
* "dindexer" 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 "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id1B822B1775424D879AA6AD1739B5BC91
#define id1B822B1775424D879AA6AD1739B5BC91
#include <iterator>
#include <cstdint>
#include <type_traits>
#include <cassert>
namespace dinhelp {
namespace implem_iop {
template <typename P, typename T, int32_t I>
class IteratorOnPtr_base : public std::iterator<std::random_access_iterator_tag, T> {
typedef std::iterator<std::random_access_iterator_tag, T> parent_type;
public:
//Typedefs to be STL-compliant
typedef typename parent_type::iterator_category iterator_category;
typedef typename parent_type::value_type value_type;
typedef typename parent_type::difference_type difference_type;
typedef typename parent_type::pointer pointer;
typedef typename std::remove_reference<T>::type& reference;
IteratorOnPtr_base ( P parPointer, difference_type parSize );
IteratorOnPtr_base ( const IteratorOnPtr_base& parOther );
template <typename P1, typename T1>
explicit IteratorOnPtr_base ( const IteratorOnPtr_base<P1, T1, I>& parOther );
~IteratorOnPtr_base ( void );
difference_type operator- ( const IteratorOnPtr_base& parOther ) const;
bool operator== ( const IteratorOnPtr_base& parOther ) const { return m_pointer == parOther.m_pointer; }
bool operator!= ( const IteratorOnPtr_base& parOther ) const { return not operator==(parOther); }
bool operator< ( const IteratorOnPtr_base& parOther ) const { return m_pointer < parOther.m_pointer; }
bool operator>= ( const IteratorOnPtr_base& parOther ) const { return not operator<(parOther); }
bool operator> ( const IteratorOnPtr_base& parOther ) const { return parOther < *this; }
bool operator<= ( const IteratorOnPtr_base& parOther ) const { return not operator>(parOther); }
P GetPointer ( void ) { return m_pointer; }
const P GetPointer ( void ) const { return m_pointer; }
#if !defined(NDEBUG)
difference_type GetSize ( void ) const { return GetSize(0); }
#endif
protected:
enum {
STEP = (I < 0 ? -I : I)
};
#if !defined(NDEBUG)
difference_type GetSize ( difference_type parAdvance ) const;
bool CanAdvance ( difference_type parAdvance ) const;
void AlterSize ( difference_type parAdvance );
#endif
void MoveIterator ( difference_type parAdvance );
P m_pointer;
private:
#if !defined(NDEBUG)
difference_type m_size;
#endif
};
} //namespace implem_iop
template <typename P, typename T, bool R>
class IteratorOnPtr : public implem_iop::IteratorOnPtr_base<P, T, (R ? -1 : 1)> {
typedef implem_iop::IteratorOnPtr_base<P, T, (R ? -1 : 1)> parent_type;
enum {
STEP = parent_type::STEP
};
public:
typedef typename parent_type::iterator_category iterator_category;
typedef typename parent_type::value_type value_type;
typedef typename parent_type::difference_type difference_type;
typedef typename parent_type::pointer pointer;
typedef typename parent_type::reference reference;
IteratorOnPtr ( void );
IteratorOnPtr ( P parPointer, difference_type parSize );
IteratorOnPtr ( const IteratorOnPtr& parOther ) : parent_type(parOther) { return; }
template <typename P1, typename T1>
IteratorOnPtr ( const IteratorOnPtr<P1, T1, R>& parOther ) : parent_type(parOther) {}
~IteratorOnPtr ( void );
IteratorOnPtr& operator++ ( void ); //pre
IteratorOnPtr operator++ ( int ); //post
IteratorOnPtr& operator-- ( void );
IteratorOnPtr operator-- ( int );
reference operator* ( void );
pointer operator-> ( void );
using parent_type::operator-;
IteratorOnPtr operator+ ( difference_type parOther ) const;
IteratorOnPtr operator- ( difference_type parOther ) const;
IteratorOnPtr& operator+= ( difference_type parOther );
IteratorOnPtr& operator-= ( difference_type parOther );
protected:
private:
};
namespace implem_iop {
///---------------------------------------------------------------------
///---------------------------------------------------------------------
template <typename P, typename T, int32_t I>
#if !defined(NDEBUG)
IteratorOnPtr_base<P, T, I>::IteratorOnPtr_base (P parPointer, difference_type parSize) :
m_pointer(parPointer),
m_size(parSize)
#else
IteratorOnPtr_base<P, T, I>::IteratorOnPtr_base (P parPointer, difference_type) :
m_pointer(parPointer)
#endif
{
static_assert(I != 0, "Step must be non-zero");
}
///---------------------------------------------------------------------
///---------------------------------------------------------------------
template <typename P, typename T, int32_t I>
template <typename P1, typename T1>
IteratorOnPtr_base<P, T, I>::IteratorOnPtr_base (const IteratorOnPtr_base<P1, T1, I>& parOther) {
m_pointer = parOther.GetPointer();
#if !defined(NDEBUG)
m_size = parOther.GetSize();
#endif
}
///---------------------------------------------------------------------
///---------------------------------------------------------------------
template <typename P, typename T, int32_t I>
IteratorOnPtr_base<P, T, I>::IteratorOnPtr_base (const IteratorOnPtr_base& parOther) {
m_pointer = parOther.m_pointer;
#if !defined(NDEBUG)
m_size = parOther.m_size;
#endif
}
///---------------------------------------------------------------------
///---------------------------------------------------------------------
template <typename P, typename T, int32_t I>
IteratorOnPtr_base<P, T, I>::~IteratorOnPtr_base() {
}
///---------------------------------------------------------------------
///---------------------------------------------------------------------
template <typename P, typename T, int32_t I>
typename IteratorOnPtr_base<P, T, I>::difference_type IteratorOnPtr_base<P, T, I>::operator- (const IteratorOnPtr_base& parOther) const {
if (I > 0)
return m_pointer - parOther.m_pointer;
else
return parOther.m_pointer - m_pointer;
}
#if !defined(NDEBUG)
///---------------------------------------------------------------------
///---------------------------------------------------------------------
template <typename P, typename T, int32_t I>
typename IteratorOnPtr_base<P, T, I>::difference_type IteratorOnPtr_base<P, T, I>::GetSize (difference_type parAdvance) const {
return m_size - STEP * parAdvance;
}
///---------------------------------------------------------------------
///---------------------------------------------------------------------
template <typename P, typename T, int32_t I>
bool IteratorOnPtr_base<P, T, I>::CanAdvance (difference_type parAdvance) const {
return (m_size >= STEP * parAdvance);
}
///---------------------------------------------------------------------
///---------------------------------------------------------------------
template <typename P, typename T, int32_t I>
void IteratorOnPtr_base<P, T, I>::AlterSize (difference_type parAdvance) {
m_size = GetSize(parAdvance);
}
#endif
///---------------------------------------------------------------------
///---------------------------------------------------------------------
template <typename P, typename T, int32_t I>
void IteratorOnPtr_base<P, T, I>::MoveIterator (difference_type parAdvance) {
#if !defined(NDEBUG)
assert(CanAdvance(parAdvance));
AlterSize(parAdvance);
#endif
m_pointer += I * parAdvance;
}
} //namespace implem_iop
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename P, typename T, bool R>
IteratorOnPtr<P, T, R>::IteratorOnPtr() :
parent_type(NULL, 0)
{
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename P, typename T, bool R>
IteratorOnPtr<P, T, R>::IteratorOnPtr (P parPointer, difference_type parSize) :
parent_type(parPointer, parSize)
{
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename P, typename T, bool R>
IteratorOnPtr<P, T, R>::~IteratorOnPtr() {
}
///-------------------------------------------------------------------------
///Pre-increment.
///-------------------------------------------------------------------------
template <typename P, typename T, bool R>
IteratorOnPtr<P, T, R>& IteratorOnPtr<P, T, R>::operator++() {
this->MoveIterator(1);
return *this;
}
///-------------------------------------------------------------------------
///Post-increment.
///-------------------------------------------------------------------------
template <typename P, typename T, bool R>
IteratorOnPtr<P, T, R> IteratorOnPtr<P, T, R>::operator++ (int) {
IteratorOnPtr<P, T, R> retVal(*this);
this->MoveIterator(1);
return retVal;
}
///-------------------------------------------------------------------------
///Pre-decrement.
///-------------------------------------------------------------------------
template <typename P, typename T, bool R>
IteratorOnPtr<P, T, R>& IteratorOnPtr<P, T, R>::operator--() {
this->MoveIterator(-1);
return *this;
}
///-------------------------------------------------------------------------
///Post-decrement.
///-------------------------------------------------------------------------
template <typename P, typename T, bool R>
IteratorOnPtr<P, T, R> IteratorOnPtr<P, T, R>::operator-- (int) {
IteratorOnPtr<P, T, R> retVal(*this);
this->MoveIterator(-1);
return retVal;
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename P, typename T, bool R>
typename IteratorOnPtr<P, T, R>::reference IteratorOnPtr<P, T, R>::operator*() {
return *(this->m_pointer);
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename P, typename T, bool R>
typename IteratorOnPtr<P, T, R>::pointer IteratorOnPtr<P, T, R>::operator->() {
return this->m_pointer;
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename P, typename T, bool R>
IteratorOnPtr<P, T, R> IteratorOnPtr<P, T, R>::operator+ (difference_type parOther) const {
IteratorOnPtr<P, T, R> retVal(*this);
retVal += parOther;
return retVal;
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename P, typename T, bool R>
IteratorOnPtr<P, T, R> IteratorOnPtr<P, T, R>::operator- (difference_type parOther) const {
IteratorOnPtr<P, T, R> retVal(*this);
retVal -= parOther;
return retVal;
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename P, typename T, bool R>
IteratorOnPtr<P, T, R>& IteratorOnPtr<P, T, R>::operator+= (difference_type parOther) {
this->MoveIterator(parOther);
return *this;
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
template <typename P, typename T, bool R>
IteratorOnPtr<P, T, R>& IteratorOnPtr<P, T, R>::operator-= (difference_type parOther) {
this->MoveIterator(-parOther);
return *this;
}
} //namespace dinhelp
#endif