casa::Block< T > Class Template Reference
[Containers]

#include <Block.h>

Collaboration diagram for casa::Block< T >:

Collaboration graph
[legend]
List of all members.

Detailed Description

template<class T>
class casa::Block< T >

simple 1-D array

Intended use:

Part of API

Review Status

Reviewed By:
UNKNOWN
Date Reviewed:
before2004/08/25

Etymology

This should be viewed as a block of memory without sophisticated manipulation functions. Thus it is called Block.

Synopsis

Block<T> is a simple templated 1-D array class. Indices are always 0-based. For efficiency reasons, no index checking is done unless the preprocessor symbol AIPS_ARRAY_INDEX_CHECK is defined. Block<T>'s may be assigned to and constructed from other Block<T>'s. As no reference counting is done this can be an expensive operation, however.

The net effect of this class is meant to be unsurprising to users who think of arrays as first class objects. The name "Block" is intended to convey the concept of a solid "chunk" of things without any intervening "fancy" memory management, etc. This class was written to be used in the implementations of more functional Vector, Matrix, etc. classes, although it is expected Block<T> will be useful on its own.

The Block class should be efficient. You should normally use Block.

Warning: If you use the assignment operator on an element of this class, you may leave dangling references to pointers released from storage(). Resizing the array will also have this effect if the underlying storage is actually affected.

If index checking is turned on, an out-of-bounds index will generate an indexError<uInt> exception.

Example

    Block<Int> a(100,0);  // 100 ints initialized to 0
    Block<Int> b;         // 0-length Block
    // .\..
    b = a;                // resize b and copy a into it
    for (uInt i=0; i < a.nelements(); i++) {
        a[i] = i;    // Generate a sequence
                     // with Vectors, could simply say "indgen(myVector);"
    }
    b.set(-1);       // All positions in b have the value -1
    b.resize(b.nelements()*2); // Make b twice as long, by default the old
                               // elements are copied over, although this can
                               // be defeated.
    some_c_function(b.storage());  // Use a fn that takes an
                                   // <tt>Int *</tt> pointer

Definition at line 100 of file Block.h.

Public Types

typedef T value_type
 STL-style typedefs.
typedef T * iterator
typedef const T * const_iterator
typedef value_typepointer
typedef const value_typeconst_pointer
typedef value_typereference
typedef const value_typeconst_reference
typedef size_t size_type
typedef ptrdiff_t difference_type

Public Member Functions

 Block ()
 Create a zero-length Block.
 Block (uInt n)
 Create a Block with the given number of points.
 Block (uInt n, T val)
 Create a Block of the given length, and initialize (via operator= for objects of type T) with the provided value.
 Block (uInt n, T *&storagePointer, Bool takeOverStorage=True)
 Create a Block from a C-array (i.e.
 Block (const Block< T > &other)
 Copy the other block into this one.
Block< T > & operator= (const Block< T > &other)
 Assign other to this.
 ~Block ()
 Frees up the storage pointed contained in the Block.
void replaceStorage (uInt n, T *&storagePointer, Bool takeOverStorage=True)
 Replace the internal storage with a C-array (i.e.
void resize (uInt n, Bool forceSmaller=False, Bool copyElements=True)
 Resizes the Block.
void remove (uInt whichOne, Bool forceSmaller=True)
 Remove a single element from the Block.
T & operator[] (uInt index)
 Index into the block (0-based).
const T & operator[] (uInt index) const
Block< T > & operator= (const T &val)
 Set all values in the block to "val".
void set (const T &val)
T * storage ()
 If you really, really, need a "raw" pointer to the beginning of the storage area this will give it to you.
const T * storage () const
uInt nelements () const
 The number of elements contained in this Block<T>.
uInt size () const
iterator begin ()
 Get the begin and end iterator object for this block.
const_iterator begin () const
iterator end ()
const_iterator end () const

Private Attributes

uInt npts
 The number of points in the vector.
T * array
 The actual storage.
Bool destroyPointer
 Can we delete the storage upon destruction?


Member Typedef Documentation

template<class T>
typedef T casa::Block< T >::value_type

STL-style typedefs.

Definition at line 300 of file Block.h.

template<class T>
typedef T* casa::Block< T >::iterator

Definition at line 301 of file Block.h.

template<class T>
typedef const T* casa::Block< T >::const_iterator

Definition at line 302 of file Block.h.

template<class T>
typedef value_type* casa::Block< T >::pointer

Definition at line 303 of file Block.h.

template<class T>
typedef const value_type* casa::Block< T >::const_pointer

Definition at line 304 of file Block.h.

template<class T>
typedef value_type& casa::Block< T >::reference

Definition at line 305 of file Block.h.

template<class T>
typedef const value_type& casa::Block< T >::const_reference

Definition at line 306 of file Block.h.

template<class T>
typedef size_t casa::Block< T >::size_type

Definition at line 307 of file Block.h.

template<class T>
typedef ptrdiff_t casa::Block< T >::difference_type

Definition at line 308 of file Block.h.


Constructor & Destructor Documentation

template<class T>
casa::Block< T >::Block (  )  [inline]

Create a zero-length Block.

Note that any index into this Block is an error.

Definition at line 104 of file Block.h.

template<class T>
casa::Block< T >::Block ( uInt  n  )  [inline, explicit]

Create a Block with the given number of points.

The values in Block are uninitialized. Note that indices range between 0 and n-1.

Definition at line 107 of file Block.h.

template<class T>
casa::Block< T >::Block ( uInt  n,
val 
) [inline]

Create a Block of the given length, and initialize (via operator= for objects of type T) with the provided value.

Definition at line 111 of file Block.h.

template<class T>
casa::Block< T >::Block ( uInt  n,
T *&  storagePointer,
Bool  takeOverStorage = True 
) [inline]

Create a Block from a C-array (i.e.

pointer). If takeOverStorage is True, The Block assumes that it owns the pointer, i.e. that it is safe to delet[] it when the Block is destructed, otherwise the actual storage is not destroyed. If true, storagePointer is set to 0.

Definition at line 119 of file Block.h.

template<class T>
casa::Block< T >::Block ( const Block< T > &  other  )  [inline]

Copy the other block into this one.

Uses copy, not reference, semantics.

Definition at line 124 of file Block.h.

template<class T>
casa::Block< T >::~Block (  )  [inline]

Frees up the storage pointed contained in the Block.

Definition at line 138 of file Block.h.


Member Function Documentation

template<class T>
Block<T>& casa::Block< T >::operator= ( const Block< T > &  other  )  [inline]

Assign other to this.

this resizes itself to the size of other, so after the assignment, this->nelements() == other.elements() always.

Definition at line 130 of file Block.h.

template<class T>
void casa::Block< T >::resize ( uInt  n,
Bool  forceSmaller = False,
Bool  copyElements = True 
) [inline]

Resizes the Block.

If n == nelements() resize just returns. If a larger size is requested (n > nelements()) the Block always resizes. If the requested size is smaller (n < nelements()), by default the Block does not resize smaller, although it can be forced to with forceSmaller. The reasoning behind this is that often the user will just want a buffer of at least a certain size, and won't want to pay the cost of multiple resizings.

      Block<float> bf(100, 0.0);
      bf.resize(10);        // bf.nelements() == 100
      bf.resize(10, True)   // bf.nelements() == 10
      bf.resize(200)        // bf.nelements() == 200
Normally the old elements are copied over (although if the Block is lengthened the trailing elements will have undefined values), however this can be turned off by setting copyElements to False.

This is written as three functions because default parameters do not always work properly with templates.

Definition at line 161 of file Block.h.

Referenced by casa::Block< ArgType >::operator=(), casa::MSAsRaster::resetFreq_(), and casa::PtrBlock< casa::Function< T > * >::resize().

template<class T>
void casa::Block< T >::remove ( uInt  whichOne,
Bool  forceSmaller = True 
) [inline]

Remove a single element from the Block.

If forceSmaller is True this will resize the Block and hence involve new memory allocations. This is relatively expensive so setting forceSmaller to False is preferred. When forcesmaller is False the Block is not resized but the elements with an index above the removed element are shuffled down by one. For backward compatibility forceSmaller is True by default.

Definition at line 186 of file Block.h.

Referenced by casa::PtrBlock< casa::Function< T > * >::remove().

template<class T>
void casa::Block< T >::replaceStorage ( uInt  n,
T *&  storagePointer,
Bool  takeOverStorage = True 
) [inline]

Replace the internal storage with a C-array (i.e.

pointer). If takeOverStorage is True, The Block assumes that it owns the pointer, i.e. that it is safe to delete[] it when the Blockis destructed, otherwise the actual storage is not destroyed. If true, storagePointer is set to NULL.

Definition at line 215 of file Block.h.

Referenced by casa::PtrBlock< casa::Function< T > * >::replaceStorage().

template<class T>
T& casa::Block< T >::operator[] ( uInt  index  )  [inline]

Index into the block (0-based).

If the preprocessor symbol AIPS_ARRAY_INDEX_CHECK is defined, index checking will be done and an out-of-bounds index will cause an indexError<uInt> to be thrown. Note that valid indices range between 0 and nelements()-1.

Thrown Exceptions

Definition at line 234 of file Block.h.

template<class T>
const T& casa::Block< T >::operator[] ( uInt  index  )  const [inline]

Definition at line 245 of file Block.h.

template<class T>
Block<T>& casa::Block< T >::operator= ( const T &  val  )  [inline]

Set all values in the block to "val".

Definition at line 258 of file Block.h.

template<class T>
void casa::Block< T >::set ( const T &  val  )  [inline]

Definition at line 260 of file Block.h.

Referenced by casa::PtrBlock< casa::Function< T > * >::set().

template<class T>
T* casa::Block< T >::storage (  )  [inline]

If you really, really, need a "raw" pointer to the beginning of the storage area this will give it to you.

This may leave dangling pointers if the block is destructed or if the assignment operator or resize is used. Returns a null pointer if nelements() == 0. It is best to only use this if you completely control the extent and lifetime of the Block.

Examples of misuse

      Block<Int> *bp = new Block<Int>(100);
      Int *ip = bp->storage();
      delete bp;      // Oops, ip is now dangling
      Block<Int> a(100),b(100);
      Int *ip = a.storage();
      a = b;          // Likewise

Definition at line 278 of file Block.h.

Referenced by casa::Ionosphere::addTargetSlants(), casa::Ionosphere::setTargetSlants(), and casa::PtrBlock< casa::Function< T > * >::storage().

template<class T>
const T* casa::Block< T >::storage (  )  const [inline]

Definition at line 279 of file Block.h.

template<class T>
uInt casa::Block< T >::nelements (  )  const [inline]

The number of elements contained in this Block<T>.

Definition at line 284 of file Block.h.

Referenced by casa::Ionosphere::addTargetSlants(), casa::GenSort_global_functions_genSortIndirect::genSort(), casa::GenSort_global_functions_genSortInPlace::genSort(), casa::DisplayData::getFirstZIndex(), casa::PtrBlock< casa::Function< T > * >::nelements(), casa::SimpleOrderedMap< const casa::Colormap *, casa::ColormapInfo * >::ntot(), casa::BlockIO_global_functions_BlockIO::putBlock(), casa::MSAsRaster::resetFreq_(), casa::Ionosphere::setTargetSlants(), and casa::BlockIO_global_functions_BlockIO::showBlock().

template<class T>
uInt casa::Block< T >::size (  )  const [inline]

Definition at line 285 of file Block.h.

Referenced by casa::PtrBlock< casa::Function< T > * >::size().

template<class T>
iterator casa::Block< T >::begin (  )  [inline]

Get the begin and end iterator object for this block.

Definition at line 312 of file Block.h.

template<class T>
const_iterator casa::Block< T >::begin (  )  const [inline]

Definition at line 314 of file Block.h.

template<class T>
iterator casa::Block< T >::end (  )  [inline]

Definition at line 316 of file Block.h.

template<class T>
const_iterator casa::Block< T >::end (  )  const [inline]

Definition at line 318 of file Block.h.


Member Data Documentation

template<class T>
uInt casa::Block< T >::npts [private]

The number of points in the vector.

Definition at line 324 of file Block.h.

Referenced by casa::Block< ArgType >::Block(), casa::Block< ArgType >::end(), casa::Block< ArgType >::nelements(), casa::Block< ArgType >::operator=(), casa::Block< ArgType >::operator[](), casa::Block< ArgType >::remove(), casa::Block< ArgType >::replaceStorage(), casa::Block< ArgType >::resize(), and casa::Block< ArgType >::size().

template<class T>
T* casa::Block< T >::array [private]

The actual storage.

Definition at line 326 of file Block.h.

Referenced by casa::Block< ArgType >::begin(), casa::Block< ArgType >::Block(), casa::Block< ArgType >::end(), casa::Block< ArgType >::operator=(), casa::Block< ArgType >::operator[](), casa::Block< ArgType >::remove(), casa::Block< ArgType >::replaceStorage(), casa::Block< ArgType >::resize(), casa::Block< ArgType >::storage(), and casa::Block< ArgType >::~Block().

template<class T>
Bool casa::Block< T >::destroyPointer [private]

Can we delete the storage upon destruction?

Definition at line 328 of file Block.h.

Referenced by casa::Block< ArgType >::Block(), casa::Block< ArgType >::remove(), casa::Block< ArgType >::replaceStorage(), casa::Block< ArgType >::resize(), and casa::Block< ArgType >::~Block().


The documentation for this class was generated from the following file:
Generated on Mon Sep 1 22:43:55 2008 for NRAOCASA by  doxygen 1.5.1