SmallArray

This class allows you to declare a small array of max const size. Not like the ordinary c array it provides boundry checking both on inserting and on requiring information from it.

[ GAMB | Source | Keywords | Summary | Ancestors | All Members | Descendants ]

Quick Index

AUTHORS
CHANGES LOG
GOALS
USAGE

Class Summary

class SmallArray

{
public:
// STL compliance defintions
typedef DataT* iterator;
typedef const DataT* const_iterator;
typedef DataT& reference;
typedef const DataT& const_reference;
// Constructors/Destructor
explicit SmallArray(unsigned short size);
SmallArray(const SmallArray< DataT>& sa);
~SmallArray();
bool push_back(const DataT& elem);
// OPERATORS
SmallArray< DataT>& operator=(const SmallArray< DataT>& sa);
reference operator[](unsigned short index);
const_reference operator[](unsigned short index) const;
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
unsigned short size() const ;
unsigned short max_size() const ;
unsigned short capacity() const ;
bool empty() ;
bool full() ;
protected:
}; // SmallArray

Back to the top of SmallArray


AUTHORS

Zipi Fligelman (zipo@math.tau.ac.il).

Copyright: SAMBA group, Tel-Aviv Univ. Israel, 1999.

Back to the top of SmallArray


CHANGES LOG

Back to the top of SmallArray


GOALS

The goals of this class is to make available a dynamically allocated array that has constant (not more than unsigned short) length. This class is STL compliance and like the vector class allows push_back operation keeping in mind we want pass the boundries, and iteration options on the array in a safe manner.

Back to the top of SmallArray


USAGE

Since the class is a template class one might want to use a typedef operation to simplify the readability of the class. This is an example for reading the appropriate transformation (assumed to be less then 10) to a Small Array of refernce frames. All these arrays are kept in their own Small Array, based on from which molecule to which molecule the transformations are defined. The different molecules are given identifying numbers.

  // in this example we assume the molecule number is given
  typedef SmallArray<ReferenceFrame> TransInfo;
  typedef SmallArray<TransInfo> TransInfoArray;

  ifstream ini_file(iniFileName);
  assert(iniFileName);
  char buffer[1024];
  int i=0;
  bool found = false;
  while (!ini_file.eof()) {
    ini_file.getline(buffer,100);
      if (!(*buffer=='#')) { 
         if (strncmp(buffer, "-->TRANSFORMATIONS_START",24) == 0) { 
            found = true;
             i=0;
      }
      else if (found && i <max_trans) {
        istrstream ist(buffer,100);
        ist >> r1 >> r2 >> r3 >> t1 >> t2 >> t3;
        ReferenceFrame tmp = 
                    ReferenceFrame(RigidTrans3(Vector3(r1,r2,r3), 
                                               Vector3(t1,t2,t3)));
        trans_array[mol_number].push_back(tmp);
        ++i;
      }
  }

For printing this array we only need to take two iterators and go through the motion of printing them. One might notice that I used the capabilities of the operator[] though they are not necessary and one might have used (*it).begin() insteand.

  for (TransInfoArray::const_iterator it = trans_array.begin();
       it != trans_array.end() ; ++it) {
       s << "Transformations from mol 0 to mol " << it-trans_array.begin()+1 
         << endl;
        for(TransInfo::iterator jt=trans_array[it-trans_array.begin()].begin();
            jt != trans_array[it].end(); ++jt)
            s << *jt << endl;
        s << "--------------------------------------------------\n";
  }

Back to the top of SmallArray


typedef DataT* iterator;

STL compliant typedef.

  typedef DataT* iterator;

Back to the top of SmallArray


typedef const DataT* const_iterator;

STL compliant typedef.

  typedef const DataT* const_iterator;

Back to the top of SmallArray


typedef DataT& reference;

STL compliant typedef.

  typedef DataT& reference;

Back to the top of SmallArray


typedef const DataT& const_reference;

STL compliant typedef.

  typedef const DataT& const_reference;

Back to the top of SmallArray


explicit SmallArray(unsigned short size);

Constructor receives initial space to allocate. Default is 3.

  explicit SmallArray(unsigned short size);

Back to the top of SmallArray


SmallArray(const SmallArray< DataT>& sa);

Copy constructor

  SmallArray(const SmallArray< DataT>& sa);

Back to the top of SmallArray


~SmallArray();

Destructor. Deletes vector data.

  ~SmallArray();

Back to the top of SmallArray


bool push_back(const DataT& elem);

Adds an item at the end of the array add to the occ and return true. // Return flase if no space left

  bool push_back(const DataT& elem);

Back to the top of SmallArray


SmallArray< DataT>& operator=(const SmallArray< DataT>& sa);

assignment operator

  SmallArray< DataT>& operator=(const SmallArray< DataT>& sa);

Back to the top of SmallArray


reference operator[](unsigned short index);

Returns reference to indexed element of the array. checking bounds

  reference operator[](unsigned short index);

Back to the top of SmallArray


const_reference operator[](unsigned short index) const;

Returns a const reference to indexed element of the array. cheking bounds

  const_reference operator[](unsigned short index) const;

Back to the top of SmallArray


iterator begin();

Returns a pointer to the vector's head.

  iterator begin();

Back to the top of SmallArray


const_iterator begin() const;

Returns a pointer to the vector's head.

  const_iterator begin() const;

Back to the top of SmallArray


iterator end();

Returns a pointer to the vector's end. The end is past the last element of the vector.

  iterator end();

Back to the top of SmallArray


const_iterator end() const;

Returns a pointer to the vector's end. The end is past the last element of the vector.

  const_iterator end() const;

Back to the top of SmallArray


unsigned short size() const ;

This function return the current size of the array

  inline unsigned short size() const                
;

Function is currently defined inline.


Back to the top of SmallArray


unsigned short max_size() const ;

This function returns the maximum size of the array the demand is that always size() <= max_size()

  inline unsigned short max_size() const               
;

Function is currently defined inline.


Back to the top of SmallArray


unsigned short capacity() const ;

This function returns the maximum size of the array the demand is that always size() <= capacity() we add this function to be compliant with the STL vector

  inline unsigned short capacity() const               
;

Function is currently defined inline.


Back to the top of SmallArray


bool empty() ;

returns true if there are no elements in the array

  inline bool empty()                        
;

Function is currently defined inline.


Back to the top of SmallArray


bool full() ;

returns true if there is no room left in the array

  inline bool full()                         
;

Function is currently defined inline.


Back to the top of SmallArray


All Members

public:
// STL compliance defintions
typedef DataT* iterator;
typedef const DataT* const_iterator;
typedef DataT& reference;
typedef const DataT& const_reference;
// Constructors/Destructor
explicit SmallArray(unsigned short size);
bool push_back(const DataT& elem);
// OPERATORS
SmallArray< DataT>& operator=(const SmallArray< DataT>& sa);
reference operator[](unsigned short index);
const_reference operator[](unsigned short index) const;
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
unsigned short size() const ;
unsigned short max_size() const ;
unsigned short capacity() const ;
bool empty() ;
bool full() ;
protected:

Back to the top of SmallArray


Ancestors

Class does not inherit from any other class.

Back to the top of SmallArray


Descendants

Back to the top of SmallArray


Generated from source by the Cocoon utilities on Sun Nov 15 13:35:26 2009 .

Report problems to jkotula@unimax.com