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 namespace sc {
    00029
    00030 template <class Type>
    00031 class Array2 {
    00032   protected:
    00033     int _length0;
    00034     int _length1;
    00035     int _managed;
    00036     Type * _array;
    00037   public:
    00038     Array2():_length0(0),_length1(0),_array(0) {}
    00039     Array2(const Array2<Type> &a): _length0(0),_length1(0),_array(0) {
    00040         operator=(a);
    00041       }
    00042     Array2(Type* data,int size0,int size1):
    00043       _length0(size0),_length1(size1),_managed(0),_array(data) {}
    00044     Array2(int size0,int size1): _length0(0),_length1(0),_array(0) {
    00045         set_lengths(size0,size1);
    00046       }
    00047     ~Array2() { clear(); }
    00048     int length0() const { return _length0; };
    00049     int length1() const { return _length1; };
    00050     void clear() { set_lengths(0,0); }
    00051     void set_lengths(int size0,int size1) {
    00052         if (_managed && _array) delete[] _array;
    00053         _managed = 1;
    00054         if (size0*size1) _array = new Type [ size0*size1 ];
    00055         else _array = 0;
    00056         _length0 = size0;
    00057         _length1 = size1;
    00058       }
    00059     Array2<Type>& operator = (const Array2<Type> & s) {
    00060         if (_managed && _array) delete[] _array;
    00061         _managed = 1;
    00062         _length0 = s._length0;
    00063         _length1 = s._length1;
    00064         if (_length0*_length1) _array = new Type [ _length0*_length1 ];
    00065         else _array = 0;
    00066         for (int i=0; i<_length0*_length1; i++) {
    00067             _array[i] = s._array[i];
    00068           }
    00069         return (*this);
    00070       }
    00071     Type& operator() (int i,int j) {
    00072         if (i<0 || i>=_length0 || j<0 || j>=_length1) {
    00073             ExEnv::err0() << "Array2::operator()(" << i << "," << j << "): "
    00074                  << "out of range (" << _length0 << "," << _length1
    00075                  << ",)" << std::endl;
    00076             abort();
    00077           };
    00078         return _array[i*_length1+j];
    00079       }
    00080     const Type& operator() (int i,int j) const {
    00081         if (i<0 || i>=_length0 || j<0 || j>=_length1) {
    00082             ExEnv::err0() << "Array2::operator()(" << i << "," << j << "): "
    00083                  << "out of range (" << _length0 << "," << _length1
    00084                  << ",)" << std::endl;
    00085             abort();
    00086           };
    00087         return _array[i*_length1+j];
    00088       }
    00089 };
    00090
    00091 }
    00092
    00093 // ///////////////////////////////////////////////////////////////////////////
    00094
    00095 // Local Variables:
    00096 // mode: c++
    00097 // c-file-style: "CLJ"
    00098 // End: