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

artem.h

00001 //
00002 // artem.h --- template for the Array 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 #ifdef __GNUG__
00029 #pragma implementation
00030 #endif
00031 
00032 template <class Type>
00033 class Array {
00034   protected:
00035     int _length;
00036     int _managed;
00037     Type * _array;
00038   public:
00039     Array():_length(0),_managed(0),_array(0) {}
00040     Array(const Array<Type>&a):_length(0),_managed(0),_array(0) {operator=(a);}
00041     Array(Type* data,int size):_length(size),_managed(0),_array(data){}
00042     Array(int size):_length(0),_managed(0),_array(0) { set_length(size); }
00043     ~Array() { clear(); }
00044     //int length() const { return _length; };
00045     int size() const { return _length; };
00046     void clear() { set_length(0); }
00047     void set_length(int size) {
00048         if (_array && _managed) delete[] _array;
00049         _managed = 1;
00050         if (size) _array = new Type [ size ];
00051         else _array = 0;
00052         _length = size;
00053       }
00054     void resize(int size) {
00055         Type*tmp=_array;
00056         if (size) _array = new Type [ size ];
00057         else _array = 0;
00058         int maxi;
00059         if (size < _length) maxi = size;
00060         else maxi = _length;
00061         for (int i=0; i<maxi; i++) _array[i] = tmp[i];
00062         if (_managed && tmp) delete[] tmp;
00063         _managed = 1;
00064         _length = size;
00065       }
00066     Array<Type>& operator = (const Array<Type> & s) {
00067         if (_managed && _array) delete[] _array;
00068         _managed = 1;
00069         _length = s._length;
00070         if (_length) _array = new Type [ _length ];
00071         else _array = 0;
00072         for (int i=0; i<_length; i++) {
00073             _array[i] = s._array[i];
00074           }
00075         return (*this);
00076       }
00077     Type& operator[] (int i) const {
00078         if (i<0 || i>=_length) {
00079             ExEnv::err() << "Array::operator[](" << i << ") "
00080                  << "out of range (" << _length << "0" << std::endl;
00081             abort();
00082           };
00083         return _array[i];
00084       }
00085     Type& operator() (int i) const {
00086         if (i<0 || i>=_length) {
00087             ExEnv::err() << "Array::operator()(" << i << ") "
00088                  << "out of range (" << _length << "0" << std::endl;
00089             abort();
00090           };
00091         return _array[i];
00092       }
00093     void push_back(const Type &d) {
00094         resize(_length+1);
00095         _array[_length-1] = d;
00096     }
00097     void pop_back() {
00098         resize(_length-1);
00099     }
00100 };
00101 
00102 // ///////////////////////////////////////////////////////////////////////////
00103 
00104 // Local Variables:
00105 // mode: c++
00106 // c-file-style: "CLJ"
00107 // End:

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