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 #include <type_traits> 43 #if defined(AIPS_ARRAY_INDEX_CHECK) 44 #include <casacore/casa/Exceptions/Error.h> 116 static void doTraceAlloc (
const void* addr,
size_t nelem,
117 DataType type,
size_t sz);
118 static void doTraceFree (
const void* addr,
size_t nelem,
119 DataType type,
size_t sz);
124 template<
typename T>
class Block;
129 template<
typename U>
friend class Block;
135 template<
typename U>
friend class Block;
206 allocator_p(get_allocator<typename
DefaultAllocator<T>::type>()), capacity_p(
207 0), used_p(0),
array(0), destroyPointer(
True), keep_allocator_p(
False) {
211 template<
typename Allocator>
213 allocator_p(get_allocator<typename Allocator::type>()), capacity_p(0), used_p(
222 n), destroyPointer(
True), keep_allocator_p(
False) {
228 template<
typename Allocator>
230 allocator_p(get_allocator<typename Allocator::type>()), used_p(n), destroyPointer(
240 n), destroyPointer(
True), keep_allocator_p(
False) {
246 template<
typename Allocator>
249 allocator_p(get_allocator<typename Allocator::type>()), used_p(n), destroyPointer(
259 n), destroyPointer(
True), keep_allocator_p(
False) {
262 allocator_p->construct(
array, get_size(), val);
271 template<
typename Allocator>
273 allocator_p(get_allocator<typename Allocator::type>()), used_p(n), destroyPointer(
277 allocator_p->construct(
array, get_size(), val);
295 allocator_p(get_allocator<typename
NewDelAllocator<T>::type>()), capacity_p(
296 n), used_p(n),
array(storagePointer), destroyPointer(takeOverStorage), keep_allocator_p(
306 template<
typename Allocator>
307 Block(
size_t n, T *&storagePointer,
Bool takeOverStorage,
309 allocator_p(get_allocator<typename Allocator::type>()), capacity_p(n), used_p(
310 n),
array(storagePointer), destroyPointer(takeOverStorage), keep_allocator_p(
318 allocator_p(other.allocator_p), used_p(other.size()), destroyPointer(
325 allocator_p->construct(
array, get_size(), other.
array);
335 if (&other !=
this) {
342 allocator_p->construct(
array, get_size(), other.
array);
378 resize(n, forceSmaller, copyElements,
383 if (n == get_size()) {
386 if (n < get_size() && forceSmaller ==
False) {
388 allocator_p->destroy(&
array[n], get_size() - n);
393 if (get_size() < n && n <= get_capacity()) {
394 allocator_p->construct(&
array[get_size()], n - get_size());
398 T *tp = n > 0 ? allocator_p->allocate(n) : 0;
403 size_t nmin =
std::min(get_size(), n);
406 allocator_p->construct(tp, nmin,
array);
409 allocator_p->deallocate(tp, n);
417 allocator_p->construct(&tp[start], n - start);
419 allocator_p->destroy(tp, start);
421 allocator_p->deallocate(tp, n);
427 destroyPointer =
True;
444 void remove(
size_t whichOne,
Bool forceSmaller =
True) {
445 remove(whichOne, forceSmaller,
449 if (whichOne >= get_size()) {
450 #if defined(AIPS_ARRAY_INDEX_CHECK) 452 "index out of range"));
457 size_t n = get_size() - 1;
458 if (forceSmaller ==
True) {
459 T *tp = n > 0 ? allocator_p->allocate(n) : 0;
460 traceAlloc(
array, n);
463 allocator_p->construct(tp, n);
466 allocator_p->deallocate(tp, n);
471 objcopy(tp,
array, whichOne);
474 allocator_p->deallocate(tp, n);
478 objcopy(tp + whichOne,
array + whichOne + 1, get_size() - whichOne - 1);
480 allocator_p->destroy(tp, whichOne);
482 allocator_p->deallocate(tp, n);
485 if (
array && destroyPointer) {
486 traceFree(
array, get_capacity());
487 allocator_p->destroy(
array, get_size());
488 allocator_p->deallocate(
array, get_capacity());
494 destroyPointer =
True;
496 objmove(&
array[whichOne], &
array[whichOne + 1], get_size() - whichOne - 1);
498 allocator_p->destroy(&
array[n], 1);
508 keep_allocator_p =
True;
512 keep_allocator_p =
False;
532 template<
typename Allocator>
534 if (keep_allocator_p && ! isCompatibleAllocator<Allocator>()) {
535 throw AipsError(
"Block::replaceStorage - Attemption to change allocator of Block");
538 if (
array && destroyPointer) {
539 traceFree (
array, get_capacity());
540 allocator_p->destroy(
array, get_size());
541 allocator_p->deallocate(
array, get_capacity());
546 allocator_p = get_allocator<typename Allocator::type>();
547 array = storagePointer;
548 destroyPointer = takeOverStorage;
549 if (destroyPointer) storagePointer = 0;
562 #if defined(AIPS_ARRAY_INDEX_CHECK) 565 if ((get_size() == 0) || (index > get_size() - 1)) {
567 "index out of range"));
573 #if defined(AIPS_ARRAY_INDEX_CHECK) 574 if ((get_size() == 0) || (index > get_size() - 1)) {
576 "index out of range"));
586 { T tmp=val; objset(
array, tmp, get_size());
return *
this;}
587 void set(
const T &val) { *
this = val; }
612 size_t size()
const {
return get_capacity();}
650 {
return array + size(); }
651 const_iterator
end()
const 652 {
return array + size(); }
659 doTraceAlloc (addr, sz, whatType(static_cast<T*>(0)),
sizeof(T));
662 inline void traceFree (
const void* addr,
size_t sz)
const 665 doTraceFree (addr, sz, whatType(static_cast<T*>(0)),
sizeof(T));
674 allocator_p(allocator), used_p(n), destroyPointer(
679 allocator_p(allocator.allocator), used_p(n), destroyPointer(
683 Block(
size_t n, T *&storagePointer,
Bool takeOverStorage,
685 allocator_p(allocator), capacity_p(n), used_p(
686 n),
array(storagePointer), destroyPointer(takeOverStorage), keep_allocator_p(
692 allocator_p->construct(&
array[pos], n, src);
695 T
const &initial_value) {
696 allocator_p->construct(&
array[pos], n, initial_value);
699 allocator_p->construct(&
array[pos], n);
702 allocator_p->destroy(&
array[pos], n);
716 set_capacity(get_size());
717 if (get_capacity() > 0) {
718 array = allocator_p->allocate(get_capacity());
719 traceAlloc(
array, get_capacity());
722 allocator_p->construct(
array, get_size());
734 if (
array && destroyPointer) {
735 allocator_p->destroy(
array, get_size());
740 if (
array && destroyPointer) {
741 traceFree(
array, get_capacity());
742 allocator_p->deallocate(
array, get_capacity());
747 template<
typename Allocator>
754 template<
typename Allocator>
757 typename Allocator::type::value_type> *other_allocator =
758 Allocator_private::get_allocator<typename Allocator::type>();
759 return other_allocator == allocator_p;
773 capacity_p = new_value;
774 set_size(
std::min(get_size(), capacity_p));
818 PtrBlock(
size_t n, T val) : block_p(n, (void *)val) {}
820 : block_p(n, (void **&)storagePointer, takeOverStorage) {}
823 { block_p = other.
block_p;
return *
this;}
826 { block_p.resize(n,forceSmaller, copyElements); }
827 void resize(
size_t n) {block_p.resize(n);}
828 void resize(
size_t n,
Bool forceSmaller) {block_p.resize(n, forceSmaller);}
829 void remove(
size_t whichOne,
Bool forceSmaller) {
830 block_p.remove(whichOne, forceSmaller);}
831 void remove(
size_t whichOne) {block_p.remove(whichOne);}
834 {block_p.replaceStorage(n, (
void **&)storagePointer, takeOverStorage);}
836 const T &
operator[](
size_t index)
const {
return (
const T &)block_p[index];}
837 void set(
const T &val) {block_p.set((
void *
const &)val);}
840 const T *
storage()
const {
return (
const T *)block_p.storage();}
842 size_t size()
const {
return block_p.size();}
Block(size_t n)
Create a Block with the given number of points.
size_t capacity() const
The capacity in this Block<T>.
size_t get_size() const
The number of used elements in the vector.
static constexpr ArrayInitPolicy NO_INIT
Don't initialize elements in the array.
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.
TableExprNode array(const TableExprNode &values, const TableExprNodeSet &shape)
Create an array of the given shape and fill it with the values.
void construct(size_t pos, size_t n, T const *src)
const T * storage() const
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
void traceAlloc(const void *addr, size_t sz) const
static bool init_anyway()
void traceFree(const void *addr, size_t sz) const
const_iterator begin() 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 * storage() 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.
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).
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?
const_iterator end() const
LatticeExprNode min(const LatticeExprNode &left, const LatticeExprNode &right)
PtrBlock(size_t n, T val)
Allocator_private::BulkAllocator< T > * allocator_p
The allocator.
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.
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)
static constexpr ArrayInitPolicy INIT
Initialize all elements in the array with the default value.
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 ...
size_t get_capacity() const
The capacity of the vector.
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
const T & operator[](size_t index) const
Base class for all Casacore library errors.
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)
size_t nelements() const
The number of elements contained in this Block<T>.
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[].
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.
const T & operator[](size_t index) const
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.