// $Source: /usr/local/SimSoft/SoftShop/Basics/Structur/Array.h,v $
// $Revision: 1.20 $
// $Author: baum $
// $Date: 2001/11/29 15:52:23 $
//
// Login name of the user who locked the revision
// $Locker:  $
// Tag name used to checkout the file
// $Name: Release-20 $
// State assigned to revision
// $State: Exp $
//
//
// $Log: Array.h,v $
// Revision 1.20  2001/11/29 15:52:23  baum
// -In Kalender.cpp und Kalender.h Handsteuerung eingebaut
//

#ifndef _Array_h
#define _Array_h 1

#if defined(_MSC_VER)
#pragma warning(disable:4251)
#endif  // _MSC_VER

#include <string.h>

#if defined __GNUG__ || defined __hpux
#  pragma interface
#endif

//==================================================================
//		Class-Header
//		for class Array<T>
//==================================================================

//##ModelId=3FBCBCC702DE
template <class T>
class Array
  {
  public:
    // Konstruktor
	//##ModelId=3FBCBCC702EF
    Array (unsigned size = 10);

    // Copyconstructor
	//##ModelId=3FBCBCC702F1
    Array (const Array<T>&);

    // destructor
	//##ModelId=3FBCBCC702F3
    ~Array ();

    // assignment operators
	//##ModelId=3FBCBCC702FD
    Array<T>& operator = (const Array<T>&);

    // remove Obj at index (if any)
	//##ModelId=3FBCBCC702FF
    T* remove (const unsigned index);

    // remove all Objects
	//##ModelId=3FBCBCC70301
    void removeAll ();

    // get Object at index
	//##ModelId=3FBCBCC7030D
    T*& operator [] (unsigned index) const;

    // find Object and get index
	//##ModelId=3FBCBCC70310
    int find (const T&) const;

    // index of first valid Object
	//##ModelId=3FBCBCC70313
    int First () const;

    // index of last valid Object
	//##ModelId=3FBCBCC7031D
    int Last () const;

    // proof if there are some Objects in the Array
	//##ModelId=3FBCBCC7031F
    bool isEmpty () const;

    // returns the Count of Items
	//##ModelId=3FBCBCC70321
    unsigned Count () const;

  private:
    // data members
	//##ModelId=3FBCBCC7032E
    mutable unsigned ObjSize;
	//##ModelId=3FBCBCC70333
    mutable T** ObjVal;

	//##ModelId=3FBCBCC7033C
    void grow (unsigned) const;
    
};	// class Array

//==================================================================
//		Inline-Quellcode
//		fuer class Array<T>
//==================================================================

//##ModelId=3FBCBCC702EF
template <class T>
Array<T>::Array (unsigned size /* = 10 */)
  : ObjSize (size ? size : 1), ObjVal (new T* [ObjSize]) {
  for (register unsigned i = 0; i < ObjSize; ObjVal [i++] = NULL)  ;
}

//##ModelId=3FBCBCC702F1
template <class T>
Array<T>::Array (const Array<T>& a)
  : ObjSize (a.ObjSize), ObjVal (new T* [ObjSize]) {
  for (register unsigned i = 0; i < ObjSize; i++)  ObjVal [i] = a.ObjVal [i];
}

//##ModelId=3FBCBCC702F3
template <class T>
inline Array<T>::~Array () {
  delete[] ObjVal;
}

// assignment operators
//##ModelId=3FBCBCC702FD
template <class T>
Array<T>& Array<T>::operator = (const Array<T>& a) {
  delete[] ObjVal;
  ObjVal = new T* [ObjSize = a.ObjSize];
  for (register unsigned i = 0; i < ObjSize; i++)  ObjVal [i] = a.ObjVal [i];
  return *this;
}

//##ModelId=3FBCBCC702FF
template <class T>
T* Array<T>::remove (const unsigned index) {
  if (index < ObjSize) {
    T* oldT = ObjVal [index];
    ObjVal [index] = NULL;
    return oldT;
  }
  else return NULL;
}

//##ModelId=3FBCBCC70301
template <class T>
void Array<T>::removeAll () {
  for (register unsigned i = 0; i < ObjSize; ObjVal[i++] = NULL);
}

//##ModelId=3FBCBCC7030D
template <class T>
inline T*& Array<T>::operator [] (unsigned index) const {
  if (index >= ObjSize)  grow (index);
  return ObjVal [index];
}

//##ModelId=3FBCBCC70310
template <class T>
int Array<T>::find (const T& obj) const {
  for (register unsigned i = 0; i < ObjSize; i++)
    if (ObjVal [i] == &obj)  return i;
  return -1;
}

// index of first valid Object
//##ModelId=3FBCBCC70313
template <class T>
int Array<T>::First () const {
  for (register unsigned i = 0; i < ObjSize; i++)
    if (ObjVal [i])  return i;
  return -1;
}

// index of last valid Object
//##ModelId=3FBCBCC7031D
template <class T>
int Array<T>::Last () const {
  for (register int i = (int)(ObjSize - 1); i >= 0; i--)
    if (ObjVal [i])  return i;
  return -1;
}

//##ModelId=3FBCBCC7031F
template <class T>
bool Array<T>::isEmpty () const {
  for (register unsigned i = 0; i < ObjSize; i++)
    if (ObjVal [i]) return false;
  return true;
}

//##ModelId=3FBCBCC70321
template <class T>
unsigned Array<T>::Count () const {
  register unsigned count = 0;
  for (register unsigned i = 0; i < ObjSize; i++)
    if (ObjVal [i])  count++;
  return count;
}

//##ModelId=3FBCBCC7033C
template <class T>
void Array<T>::grow (unsigned newSize) const {
  T** newval = new T* [++newSize];
  for (register unsigned i = ObjSize; i < newSize; newval [i++] = NULL)  ;
  memcpy (newval, ObjVal, sizeof (T*) * ObjSize);
  delete[] ObjVal;
  ObjVal = newval;
  ObjSize = newSize;
}

#endif	// !defined _Array_h
