33 #include <casacore/casa/aips.h> 34 #include <casacore/casa/Utilities/Assert.h> 35 #include <casacore/casa/Utilities/Copy.h> 36 #include <casacore/casa/Utilities/DataType.h> 37 #include <casacore/casa/Containers/Allocator.h> 40 #if __cplusplus < 201103L 43 #include <type_traits> 47 #if defined(AIPS_ARRAY_INDEX_CHECK) 48 #include <casacore/casa/Exceptions/Error.h> 120 static void doTraceAlloc (
const void* addr,
size_t nelem,
121 DataType type,
size_t sz);
122 static void doTraceFree (
const void* addr,
size_t nelem,
123 DataType type,
size_t sz);
128 template<
typename T>
class Block;
130 #if __cplusplus < 201103L 134 template<
typename U>
friend class Block;
140 template<
typename U>
friend class Block;
144 #define CASA_TMP_939727(x) template<> class Block_internal_IsFundamental<x> { template<typename U> friend class Block; enum { value = 1 }; } 157 #define CASA_TMP_939727_int(x) CASA_TMP_939727(x); CASA_TMP_939727(unsigned x) 161 #undef CASA_TMP_939727_int 162 #undef CASA_TMP_939727 166 template<
typename U>
friend class Block;
170 #else // __cplusplus < 201103L 174 template<
typename U>
friend class Block;
180 template<
typename U>
friend class Block;
184 #endif // __cplusplus < 201103L 253 allocator_p(get_allocator<typename
DefaultAllocator<T>::type>()), capacity_p(
254 0), used_p(0),
array(0), destroyPointer(
True), keep_allocator_p(
False) {
258 template<
typename Allocator>
260 allocator_p(get_allocator<typename Allocator::type>()), capacity_p(0), used_p(
269 n), destroyPointer(
True), keep_allocator_p(
False) {
275 template<
typename Allocator>
277 allocator_p(get_allocator<typename Allocator::type>()), used_p(n), destroyPointer(
287 n), destroyPointer(
True), keep_allocator_p(
False) {
293 template<
typename Allocator>
296 allocator_p(get_allocator<typename Allocator::type>()), used_p(n), destroyPointer(
306 n), destroyPointer(
True), keep_allocator_p(
False) {
309 allocator_p->construct(
array, get_size(), val);
318 template<
typename Allocator>
320 allocator_p(get_allocator<typename Allocator::type>()), used_p(n), destroyPointer(
324 allocator_p->construct(
array, get_size(), val);
342 allocator_p(get_allocator<typename
NewDelAllocator<T>::type>()), capacity_p(
343 n), used_p(n),
array(storagePointer), destroyPointer(takeOverStorage), keep_allocator_p(
353 template<
typename Allocator>
354 Block(
size_t n, T *&storagePointer,
Bool takeOverStorage,
356 allocator_p(get_allocator<typename Allocator::type>()), capacity_p(n), used_p(
357 n),
array(storagePointer), destroyPointer(takeOverStorage), keep_allocator_p(
365 allocator_p(other.allocator_p), used_p(other.size()), destroyPointer(
372 allocator_p->construct(
array, get_size(), other.
array);
382 if (&other !=
this) {
389 allocator_p->construct(
array, get_size(), other.
array);
425 resize(n, forceSmaller, copyElements,
430 if (n == get_size()) {
433 if (n < get_size() && forceSmaller ==
False) {
435 allocator_p->destroy(&
array[n], get_size() - n);
440 if (get_size() < n && n <= get_capacity()) {
441 allocator_p->construct(&
array[get_size()], n - get_size());
445 T *tp = n > 0 ? allocator_p->allocate(n) : 0;
450 size_t nmin =
std::min(get_size(), n);
453 allocator_p->construct(tp, nmin,
array);
456 allocator_p->deallocate(tp, n);
464 allocator_p->construct(&tp[start], n - start);
466 allocator_p->destroy(tp, start);
468 allocator_p->deallocate(tp, n);
474 destroyPointer =
True;
491 void remove(
size_t whichOne,
Bool forceSmaller =
True) {
492 remove(whichOne, forceSmaller,
496 if (whichOne >= get_size()) {
497 #if defined(AIPS_ARRAY_INDEX_CHECK) 499 "index out of range"));
504 size_t n = get_size() - 1;
505 if (forceSmaller ==
True) {
506 T *tp = n > 0 ? allocator_p->allocate(n) : 0;
507 traceAlloc(
array, n);
510 allocator_p->construct(tp, n);
513 allocator_p->deallocate(tp, n);
518 objcopy(tp,
array, whichOne);
521 allocator_p->deallocate(tp, n);
525 objcopy(tp + whichOne,
array + whichOne + 1, get_size() - whichOne - 1);
527 allocator_p->destroy(tp, whichOne);
529 allocator_p->deallocate(tp, n);
532 if (
array && destroyPointer) {
533 traceFree(
array, get_capacity());
534 allocator_p->destroy(
array, get_size());
535 allocator_p->deallocate(
array, get_capacity());
541 destroyPointer =
True;
543 objmove(&
array[whichOne], &
array[whichOne + 1], get_size() - whichOne - 1);
545 allocator_p->destroy(&
array[n], 1);
555 keep_allocator_p =
True;
559 keep_allocator_p =
False;
579 template<
typename Allocator>
581 if (keep_allocator_p && ! isCompatibleAllocator<Allocator>()) {
582 throw AipsError(
"Block::replaceStorage - Attemption to change allocator of Block");
585 if (
array && destroyPointer) {
586 traceFree (
array, get_capacity());
587 allocator_p->destroy(
array, get_size());
588 allocator_p->deallocate(
array, get_capacity());
593 allocator_p = get_allocator<typename Allocator::type>();
594 array = storagePointer;
595 destroyPointer = takeOverStorage;
596 if (destroyPointer) storagePointer = 0;
609 #if defined(AIPS_ARRAY_INDEX_CHECK) 612 if ((get_size() == 0) || (index > get_size() - 1)) {
614 "index out of range"));
620 #if defined(AIPS_ARRAY_INDEX_CHECK) 621 if ((get_size() == 0) || (index > get_size() - 1)) {
623 "index out of range"));
633 { T tmp=val; objset(
array, tmp, get_size());
return *
this;}
634 void set(
const T &val) { *
this = val; }
659 size_t size()
const {
return get_capacity();}
697 {
return array + size(); }
698 const_iterator
end()
const 699 {
return array + size(); }
706 doTraceAlloc (addr, sz, whatType(static_cast<T*>(0)),
sizeof(T));
709 inline void traceFree (
const void* addr,
size_t sz)
const 712 doTraceFree (addr, sz, whatType(static_cast<T*>(0)),
sizeof(T));
721 allocator_p(allocator), used_p(n), destroyPointer(
726 allocator_p(allocator.allocator), used_p(n), destroyPointer(
730 Block(
size_t n, T *&storagePointer,
Bool takeOverStorage,
732 allocator_p(allocator), capacity_p(n), used_p(
733 n),
array(storagePointer), destroyPointer(takeOverStorage), keep_allocator_p(
739 allocator_p->construct(&
array[pos], n, src);
742 T
const &initial_value) {
743 allocator_p->construct(&
array[pos], n, initial_value);
746 allocator_p->construct(&
array[pos], n);
749 allocator_p->destroy(&
array[pos], n);
763 set_capacity(get_size());
764 if (get_capacity() > 0) {
765 array = allocator_p->allocate(get_capacity());
766 traceAlloc(
array, get_capacity());
769 allocator_p->construct(
array, get_size());
781 if (
array && destroyPointer) {
782 allocator_p->destroy(
array, get_size());
787 if (
array && destroyPointer) {
788 traceFree(
array, get_capacity());
789 allocator_p->deallocate(
array, get_capacity());
794 template<
typename Allocator>
801 template<
typename Allocator>
804 typename Allocator::type::value_type> *other_allocator =
805 Allocator_private::get_allocator<typename Allocator::type>();
806 return other_allocator == allocator_p;
820 capacity_p = new_value;
821 set_size(
std::min(get_size(), capacity_p));
865 PtrBlock(
size_t n, T val) : block_p(n, (void *)val) {}
867 : block_p(n, (void **&)storagePointer, takeOverStorage) {}
870 { block_p = other.
block_p;
return *
this;}
873 { block_p.resize(n,forceSmaller, copyElements); }
874 void resize(
size_t n) {block_p.resize(n);}
875 void resize(
size_t n,
Bool forceSmaller) {block_p.resize(n, forceSmaller);}
876 void remove(
size_t whichOne,
Bool forceSmaller) {
877 block_p.remove(whichOne, forceSmaller);}
878 void remove(
size_t whichOne) {block_p.remove(whichOne);}
881 {block_p.replaceStorage(n, (
void **&)storagePointer, takeOverStorage);}
883 const T &
operator[](
size_t index)
const {
return (
const T &)block_p[index];}
884 void set(
const T &val) {block_p.set((
void *
const &)val);}
887 const T *
storage()
const {
return (
const T *)block_p.storage();}
889 size_t size()
const {
return block_p.size();}
Block(size_t n)
Create a Block with the given number of points.
void prohibitChangingAllocator()
Prohibit changing allocator for this instance.
void set_capacity(size_t new_value)
Set the capacity of the vector.
Block(size_t n, T *&storagePointer, Bool takeOverStorage=True)
Create a Block from a C-array (i.e.
const T * storage() const
TableExprNode array(const TableExprNode &values, const TableExprNodeSet &shape)
Create an array of the given shape and fill it with the values.
const T & operator[](size_t index) const
static ArrayInitPolicy const NO_INIT
Don't initialize elements in the array.
void construct(size_t pos, size_t n, T const *src)
T value_type
Define the STL-style iterators.
An aligned allocator with the default alignment.
void resize(size_t n, Bool forceSmaller=False, Bool copyElements=True)
Resizes the Block.
ptrdiff_t difference_type
size_t nelements() const
The number of elements contained in this Block<T>.
static bool init_anyway()
const T * storage() const
const value_type & const_reference
static void setTraceSize(size_t sz)
Set the trace size.
Index errors returning the bad index.
Block(size_t n, Allocator_private::AllocSpec< T > allocator)
A global enum used by some Array/Block constructors.
const T & operator[](size_t index) const
Block()
Create a zero-length Block.
void replaceStorage(size_t n, T *&storagePointer, Bool takeOverStorage=True)
Replace the internal storage with a C-array (i.e.
size_t capacity() const
The capacity in this Block<T>.
const value_type * const_pointer
PtrBlock(const PtrBlock< T > &other)
size_t used_p
The number of used elements in the vector.
T & operator[](size_t index)
Index into the block (0-based).
void traceFree(const void *addr, size_t sz) const
Block(size_t n, ArrayInitPolicy initPolicy, AllocSpec< Allocator > const &)
Create a Block with the given number of points.
void replaceStorage(size_t n, T *&storagePointer, Bool takeOverStorage, AllocSpec< Allocator > const &)
Bool keep_allocator_p
Can we change allocator or not?
LatticeExprNode min(const LatticeExprNode &left, const LatticeExprNode &right)
PtrBlock(size_t n, T val)
Allocator_private::BulkAllocator< T > * allocator_p
The allocator.
const_iterator end() const
static void doTraceAlloc(const void *addr, size_t nelem, DataType type, size_t sz)
Write alloc and free trace messages.
Bool empty() const
Is the block empty (i.e.
static ArrayInitPolicy const INIT
Initialize all elements in the array with the default value.
Block(size_t n, ArrayInitPolicy initPolicy, Allocator_private::BulkAllocator< T > *allocator)
Block(size_t n, AllocSpec< Allocator > const &)
Create a Block with the given number of points.
T & operator[](size_t index)
void permitChangingAllocator()
Permit changing allocator for this instance.
void replaceStorage(size_t n, T *&storagePointer, Bool takeOverStorage=True)
Block(size_t n, T *&storagePointer, Bool takeOverStorage, Allocator_private::BulkAllocator< T > *allocator)
#define AlwaysAssert(expr, exception)
These marcos are provided for use instead of simply using the constructors of assert_ to allow additi...
bool Bool
Define the standard types used by Casacore.
void set_size(size_t new_value)
Set the number of used elements in the vector.
Allocator_private::BulkAllocator< T > * get_allocator()
void resize(size_t n, Bool forceSmaller, Bool copyElements)
PtrBlock< T > & operator=(const PtrBlock< T > &other)
size_t get_capacity() const
The capacity of the vector.
Bool isCompatibleAllocator()
A drop-in replacement for Block<T*>.
iterator begin()
Get the begin and end iterator object for this block.
template <class T, class U> class vector;
void construct(size_t pos, size_t n, T const &initial_value)
Block(size_t n, T const &val)
Create a Block of the given length, and initialize (via copy constructor for objects of type T) with ...
static size_t itsTraceSize
PtrBlock(size_t n, T *&storagePointer, Bool takeOverStorage=True)
Block(size_t n, ArrayInitPolicy initPolicy)
Create a Block with the given number of points.
void init(ArrayInitPolicy initPolicy)
end of friend
Base class for all Casacore library errors.
const_iterator begin() const
Block(AllocSpec< Allocator > const &)
Create a zero-length Block.
void resize(size_t n, Bool forceSmaller, Bool copyElements, ArrayInitPolicy initPolicy)
void construct(size_t pos, size_type n)
static Allocator_private::BulkAllocator< typename Allocator::value_type > * get_allocator()
Bool destroyPointer
Can we delete the storage upon destruction?
static BulkAllocator< typename Allocator::value_type > * get_allocator()
An allocator behaves like operator new[]/delete[].
void traceAlloc(const void *addr, size_t sz) const
size_t get_size() const
The number of used elements in the vector.
Block< T > & operator=(const T &val)
Set all values in the block to "val".
T * array
The actual storage.
T * storage()
If you really, really, need a "raw" pointer to the beginning of the storage area this will give it to...
Block(const Block< T > &other)
Copy the other block into this one.
void destroy(size_t pos, size_type n)
void resize(size_t n, Bool forceSmaller)
~Block()
Frees up the storage pointed contained in the Block.
PtrBlock< T > & operator=(const T &val)
Block< T > & operator=(const Block< T > &other)
Assign other to this.
this file contains all the compiler specific defines
size_t capacity_p
The capacity of the vector.
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
Block(size_t n, T const &val, AllocSpec< Allocator > const &)
Create a Block of the given length, and initialize (via copy constructor for objects of type T) with ...
static void doTraceFree(const void *addr, size_t nelem, DataType type, size_t sz)
Block(size_t n, T *&storagePointer, Bool takeOverStorage, AllocSpec< Allocator > const &)
Create a Block from a C-array (i.e.