19 #ifndef __TPIE_ARRAY_H__
20 #define __TPIE_ARRAY_H__
27 #include <boost/type_traits/is_pod.hpp>
28 #include <boost/iterator/iterator_facade.hpp>
29 #include <boost/utility/enable_if.hpp>
39 template <
typename TT,
bool forward>
41 array_iter_base<TT, forward>,
42 TT , boost::random_access_traversal_tag> {
44 template <
typename,
typename>
friend class array;
45 friend class boost::iterator_core_access;
51 inline TT & dereference()
const {
return * elm;}
54 inline void increment() {elm += forward?1:-1;}
55 inline void decrement() {elm += forward?-1:1;}
56 inline void advance(
size_t n) {
if (forward) elm += n;
else elm -= n;}
57 inline ptrdiff_t distance_to(
array_iter_base const & o)
const {
return o.elm - elm;}
72 boost::is_convertible<U*,TT*>, enabler>::type = enabler())
142 template <
typename T,
typename Allocator = allocator<T> >
167 assert(idx <=
size());
168 return get_iter(idx);
178 assert(idx <=
size());
179 return get_iter(idx);
187 T &
at(
size_t i)
throw() {
189 return m_elements[i];
195 const T &
at(
size_t i)
const throw() {
197 return m_elements[i];
208 template <
typename OtherAllocator>
211 for (
size_t i=0; i <
size(); ++i) m_elements[i] = other[i];
252 if (
size() != other.
size())
return false;
253 for (
size_t i=0;i<
size();++i)
if (*get_iter(i) != *other.get_iter(i))
return false;
264 if (
size() != other.
size())
return true;
265 for (
size_t i=0; i<
size(); ++i)
if (*get_iter(i) != *other.get_iter(i))
return true;
300 inline const T &
front()
const {
return at(0);}
341 inline iterator get_iter(
size_t idx) {
361 return (
double)
sizeof(T);
376 const Allocator & alloc=Allocator()
377 ): m_elements(0), m_size(0), m_tss_used(false), m_allocator(alloc)
385 array(size_type s=0,
const Allocator & alloc=
386 Allocator()): m_elements(0), m_size(0), m_tss_used(false),
387 m_allocator(alloc) {
resize(s);}
393 array(
const array & other): m_elements(0), m_size(other.m_size), m_tss_used(false), m_allocator(other.m_allocator) {
394 if (other.
size() == 0)
return;
395 alloc_copy(other.m_elements);
400 , m_size(view.
size())
403 if (view.
size() == 0)
return;
404 alloc_copy(&*view.
begin());
407 array(
const array_view_base<const T> & view)
409 , m_size(view.
size())
412 if (view.size() == 0)
return;
413 alloc_copy(&*view.begin());
432 if (size != m_size) {
433 destruct_and_dealloc();
438 std::fill(m_elements+0, m_elements+m_size, elm);
446 std::swap(m_allocator, other.m_allocator);
447 std::swap(m_elements, other.m_elements);
448 std::swap(m_size, other.m_size);
449 std::swap(m_tss_used, other.m_tss_used);
462 destruct_and_dealloc();
472 inline size_type
size()
const {
return m_size;}
477 inline T *
get() {
return m_elements;}
482 inline const T *
get()
const {
return m_elements;}
503 inline void alloc_fill(
const T & elm) { bits::allocator_usage<T, Allocator>::alloc_fill(*
this, elm); }
510 inline void alloc_dfl() { bits::allocator_usage<T, Allocator>::alloc_dfl(*
this); }
517 inline void destruct_and_dealloc() { bits::allocator_usage<T, Allocator>::destruct_and_dealloc(*
this); }
523 Allocator m_allocator;
528 template <
typename T>
531 host.m_elements = host.m_size ?
reinterpret_cast<T*
>(tpie_new_array<trivial_same_size<T> >(host.m_size)) : 0;
532 host.m_tss_used =
true;
533 std::uninitialized_copy(copy_from+0, copy_from+host.m_size, host.m_elements+0);
537 host.m_elements = host.m_size ?
reinterpret_cast<T*
>(tpie_new_array<trivial_same_size<T> >(host.m_size)) : 0;
538 host.m_tss_used =
true;
541 std::uninitialized_fill(host.m_elements+0, host.m_elements+host.m_size, elm);
545 host.m_elements = host.m_size ? tpie_new_array<T>(host.m_size) : 0;
546 host.m_tss_used =
false;
550 if (!host.m_tss_used) {
557 for (
size_t i = 0; i < host.m_size; ++i) {
558 host.m_elements[i].~T();
564 template <
typename T,
typename Allocator>
567 host.m_elements = host.m_size ? host.m_allocator.allocate(host.m_size) : 0;
568 for (
size_t i = 0; i < host.m_size; ++i) {
569 host.m_allocator.construct(host.m_elements+i, copy_from[i]);
573 static void alloc_fill(array<T, Allocator> & host,
const T & elm) {
574 host.m_elements = host.m_size ? host.m_allocator.allocate(host.m_size) : 0;
575 for (
size_t i = 0; i < host.m_size; ++i) {
576 host.m_allocator.construct(host.m_elements+i, elm);
580 static void alloc_dfl(array<T, Allocator> & host) {
581 host.m_elements = host.m_size ? host.m_allocator.allocate(host.m_size) : 0;
582 for (
size_t i = 0; i < host.m_size; ++i) {
583 host.m_allocator.construct(host.m_elements+i);
587 static void destruct_and_dealloc(array<T, Allocator> & host) {
588 for (
size_t i = 0; i < host.m_size; ++i) {
589 host.m_allocator.destroy(host.m_elements+i);
591 host.m_allocator.deallocate(host.m_elements, host.m_size);
597 template <
typename T>
598 std::ostream & operator<<(std::ostream & o, const array<T> & a) {
601 for(
size_t i=0; i < a.size(); ++i) {
602 if (first) first =
false;
632 template <
typename T>
640 template <
typename TT1,
bool forward1,
typename TT2,
bool forward2>
646 ptrdiff_t dist = copy(&*first, &*last, &*d_first) - &*d_first;
647 return d_first + dist;
653 template <
typename TT,
bool forward,
typename OutputIterator>
657 OutputIterator d_first) {
659 return copy(&*first, &*last, d_first);
665 template <
typename TT,
bool forward,
typename InputIterator>
667 copy(InputIterator first,
671 ptrdiff_t dist = copy(first, last, &*d_first) - &*d_first;
672 return d_first + dist;
677 #endif //__TPIE_ARRAY_H__
Base class for array_view.
const_iterator end() const
Return a const iterator to the end of the array.
array_iter_base< T const, false > const_reverse_iterator
Reverse iterator over a const array.
T & front()
Return the first element in the array.
array(size_type s=0, const Allocator &alloc=Allocator())
Construct array of given size.
array(const array &other)
Construct a copy of another array.
iterator find(size_t idx)
Return an iterator to the i'th element of the array.
T & at(size_t i)
Return the element located at the given index.
Memory management subsystem.
T value_type
Type of values containd in the array.
Base class for array_view.
Base class of data structures with linear memory usage.
array_iter_base(array_iter_base< U, forward > const &o, typename boost::enable_if< boost::is_convertible< U *, TT * >, enabler >::type=enabler())
Copy constructor.
reverse_iterator rend()
Reverse iterator to end of reverse sequence.
A generic array with a fixed size.
const T & at(size_t i) const
const_reverse_iterator rbegin() const
Const reverse iterator to beginning of reverse sequence.
array_iter_base()
Default constructor.
const T & front() const
Return the first element in the array.
const_iterator begin() const
Return a const iterator to the beginning of the array.
void resize(size_t s)
Change the size of the array.
const_iterator find(size_t idx) const
Return a const iterator to the i'th element of the array.
Shared implementation of array iterators.
T & operator[](size_t i)
Return a reference to an array entry.
const T & back() const
Return the last element in the array.
array_iter_base< T, false > reverse_iterator
Reverse iterator over an array.
A allocator object usable in STL containers, using the TPIE memory manager.
Miscellaneous utility functions.
static double memory_coefficient()
Return the memory coefficient of the structure.
array_iter_base< T const, true > const_iterator
Iterator over a const array.
reverse_iterator rbegin()
Reverse iterator to beginning of reverse sequence.
size_t size() const
Get number of elements in the array.
array & operator=(const array< T, OtherAllocator > &other)
Copy elements from another array into this.
void resize(size_t size, const T &elm)
Change the size of the array.
array_iter_base< T, true > iterator
Iterator over an array.
static double memory_overhead()
Return the memory overhead of the structure.
iterator begin() const
Return an iterator to the beginning of the array.
const_reverse_iterator rend() const
Const reverse iterator to end of reverse sequence.
const T & operator[](size_t i) const
Return a const reference to an array entry.
void swap(array &other)
Swap two arrays.
T & back()
Return the last element in the array.
iterator end()
Return an iterator to the end of the array.
bool operator==(const array &other) const
Compare if the other array has the same elements in the same order as this.
iterator begin()
Return an iterator to the beginning of the array.
size_type size() const
Return the size of the array.
bool empty() const
Check if the array is empty.
bool operator!=(const array &other) const
Check if two arrays differ.
array(size_type s, const T &value, const Allocator &alloc=Allocator())
Construct array of given size.
~array()
Free up all memory used by the array.
void tpie_delete_array(T *a, size_t size)
Delete an array allocated with tpie_new_array.