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