Main Page   Class Hierarchy   Compound List   File List   Compound Members   Related Pages  

ar2tem.h

00001 //
00002 // ar2tem.h --- template for the Array2 class
00003 //
00004 // Copyright (C) 1996 Limit Point Systems, Inc.
00005 //
00006 // Author: Curtis Janssen <cljanss@limitpt.com>
00007 // Maintainer: LPS
00008 //
00009 // This file is part of the SC Toolkit.
00010 //
00011 // The SC Toolkit is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Library General Public License as published by
00013 // the Free Software Foundation; either version 2, or (at your option)
00014 // any later version.
00015 //
00016 // The SC Toolkit is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 // GNU Library General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Library General Public License
00022 // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to
00023 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
00024 //
00025 // The U.S. Government is granted a limited license as per AL 91-7.
00026 //
00027 
00028 template <class Type>
00029 class Array2 {
00030   protected:
00031     int _length0;
00032     int _length1;
00033     int _managed;
00034     Type * _array;
00035   public:
00036     Array2():_length0(0),_length1(0),_array(0) {}
00037     Array2(const Array2<Type> &a): _length0(0),_length1(0),_array(0) {
00038         operator=(a);
00039       }
00040     Array2(Type* data,int size0,int size1):
00041       _length0(size0),_length1(size1),_managed(0),_array(data) {}
00042     Array2(int size0,int size1): _length0(0),_length1(0),_array(0) {
00043         set_lengths(size0,size1);
00044       }
00045     ~Array2() { clear(); }
00046     int length0() const { return _length0; };
00047     int length1() const { return _length1; };
00048     void clear() { set_lengths(0,0); }
00049     void set_lengths(int size0,int size1) {
00050         if (_managed && _array) delete[] _array;
00051         _managed = 1;
00052         if (size0*size1) _array = new Type [ size0*size1 ];
00053         else _array = 0;
00054         _length0 = size0;
00055         _length1 = size1;
00056       }
00057     Array2<Type>& operator = (const Array2<Type> & s) {
00058         if (_managed && _array) delete[] _array;
00059         _managed = 1;
00060         _length0 = s._length0;
00061         _length1 = s._length1;
00062         if (_length0*_length1) _array = new Type [ _length0*_length1 ];
00063         else _array = 0;
00064         for (int i=0; i<_length0*_length1; i++) {
00065             _array[i] = s._array[i];
00066           }
00067         return (*this);
00068       }
00069     Type& operator() (int i,int j) {
00070         if (i<0 || i>=_length0 || j<0 || j>=_length1) {
00071             ExEnv::err() << "Array2::operator()(" << i << "," << j << "): "
00072                  << "out of range (" << _length0 << "," << _length1
00073                  << ",)" << std::endl;
00074             abort();
00075           };
00076         return _array[i*_length1+j];
00077       }
00078     const Type& operator() (int i,int j) const {
00079         if (i<0 || i>=_length0 || j<0 || j>=_length1) {
00080             ExEnv::err() << "Array2::operator()(" << i << "," << j << "): "
00081                  << "out of range (" << _length0 << "," << _length1
00082                  << ",)" << std::endl;
00083             abort();
00084           };
00085         return _array[i*_length1+j];
00086       }
00087 };
00088 
00089 // ///////////////////////////////////////////////////////////////////////////
00090 
00091 // Local Variables:
00092 // mode: c++
00093 // c-file-style: "CLJ"
00094 // End:

Generated at Thu Oct 4 18:08:42 2001 for MPQC 2.0.0 using the documentation package Doxygen 1.2.5.