array 类模板(检测边界,自动排序)

/************************    ArrayBase.h  ********************
 **************************公共基类
 * Copyright (c) 2004  L.F Studio
 *
 * E-mail: lonelyforest@126.com  OICQ: 36274668
 *
 ***************************************************************/
 
#ifndef AYYAY_BASE_H
#define AYYAY_BASE_H
 
#include <ostream>
using namespace std;

 
 template<class elemType>
 class Array;
 
 template<class elemType>
 ostream &operator<<(ostream &os, const Array<elemType> &Aobj)
 {
 return Aobj.print( os );
}
   
 
 template< class elemType>
 class Array{
  public:
   explicit Array(int sz = ArraySize)
   {  init(0, sz); }
   
   Array(const elemType *ar = 0, int sz)
   {  init(ar, sz); }
   
   Array(const Array<elemType> &Aobj)
   {   init(Aobj._ia, Aobj._size); }
   
   virtual ~Array() { delete [] _ia; }
   
 //------------class--iterator----------------------------------
   class iterator{
    public:
     iterator() { _loc = 0; }
     
     iterator(elemType *p) { _loc = p; }
     
     iterator &operator++() {
      _loc++;
      return *this;
     }
     
     const elemType &operator*() {
      if ( !_loc)
      {
       throw "iterator: pointer not initialized";
      }
      
      return *_loc;
     }
     
     bool operator==(const iterator &b) {
      return _loc == b._loc;
     }
     
     bool operator!=(const iterator &b) {
      return _loc != b._loc;
     }
     
    protected:
     elemType *_loc;
    };

 //--------------class---iterator--end-------------------
 
  virtual ostream &print( ostream & ) const;
  virtual void grow();
  virtual elemType    min() const;
  virtual elemType    max() const;
  virtual int find( const elemType &val ) const;
  virtual void sort(int first = 0, int last);
   
  elemType at( int ix ) const { return _ia[ix]; }
  Array &operator=(const Array &);
   
  virtual elemType &operator[](int ix) const
  {  return _ia[ix]; }
   
  int size() { return _size; }
   
   //friend;
   friend ostream &operator<<<>(ostream &os,
          const Array<elemType> &Aobj);
   
   iterator begin()
   {
    return iterator (_ia);
   }
   
   iterator end()
   {
    return iterator(_ia + _size);
   }
   
   
 protected:
   void init(const elemType *, int);
   void swap(int, int);
   static  const int ArraySize = 12;
   
   int _size;
   elemType *_ia;
   
};
 
#endif

#ifdef  AYYAY_BASE_H
#include "ArrayBase.cpp"
#endif
 
 
/*****************   ArrayBase.cpp  **************************
 *************************************************************
 * E-mail: lonelyforest@126.com  OICQ: 36274668
 *************************************************************/


#ifndef ARRAY_BASE_CPP
#define ARRAY_BASE_CPP

#include <ostream>

template < class elemType >
ostream &Array< elemType >::print( ostream &os) const
{
 const int lineSize = 10;

 os << "( " << _size << " ) <";

 for ( int ix = 0; ix < _size; ++ix)
 {

  if((ix%lineSize) == 0 && ix != 0 )
  {
   os << "/n/t";
  }

  os << _ia[ix];

  if (((ix % lineSize) != lineSize -1) && (ix < _size - 1))
  {
   os << ", ";
  }
 }

 os << " >/n";
 return os;
}

template < class elemType >
void Array< elemType >::grow()
{
 elemType *oldia = _ia;
 int oldSize = _size;

 _size = oldSize + oldSize/2 + 1;
 _ia = new elemType[_size];

 int ix = 0;
 for (; ix < oldSize; ++ix)
 {
  _ia[ix] = oldia[ix];
 }

 for (; ix < _size; ++ix)
 {
  _ia[ix] = elemType();
 }

 delete[] oldia;
}

template < class elemType >
void Array< elemType >::init(const elemType *array, int sz)
{
 _size = sz;
 _ia = new elemType[_size];
 
 for ( int ix = 0; ix < _size; ++ix)
 {
  if ( array )
  {
            _ia[ix] = array[ix];
  }
  else
  {
   _ia[ix] = ix;
  }
 }
}

template <class elemType>
elemType    Array< elemType >::min() const
{
 assert( _ia != 0 );
 elemType    min_val  = _ia[0];
 for ( int ix = 1; ix < _size; ++ix ){
  if ( _ia[ix] < min_val )
      min_val = _ia[ix];
 }

 return  min_val;
}

template < class elemType>
elemType    Array< elemType >::max() const
{
 assert( _ia != 0 );
 elemType    max_val = _ia[0];
 for ( int   ix = 1; ix < _size; ++ix )
 {
  if ( _ia[ix] > max_val )
      max_val = _ia[ix];
 }

 return max_val;
}

template < class elemType >
int    Array< elemType >::find(const elemType &val)  const
{
 for ( int ix = 0; ix < _size; ++ix )
     if (val == _ia[ix] )    return ix;

 return -1;
}

//===============quick sort====================================
template < class elemType >                             //=====
void    Array< elemType >::sort( int first, int last )  //=====
{                                                       //=====
 if ( first >= last )   return;                      //=====
                                                     //=====
 int low = first;                                    //=====
 int high = last + 1;                               //=====
                                                     //=====
 elemType elem = _ia[low];                           //=====
              //=====
 for (;;) {                                          //=====
  while ( _ia[++low] < elem && low < last );      //=====
  while ( _ia[--high] > elem && high > first );   //=====
  if ( low < high )                               //=====
      swap( low, high );                          //=====
  else break;                                     //=====
 }                                                   //=====
              //=====
  swap( first, high );                                //-----
 sort( first, high - 1 );                            //-----
 sort( high+1, last);                                //-----
              //-----
}                                                       //-----
//=================quick sort==================================

template < class elemType >
void Array< elemType >::swap(int i, int j)
{
 elemType    tmp = _ia[i];
 _ia[i] = _ia[j];
 _ia[j] = tmp;
}

template < class elemType >
Array< elemType >  &Array< elemType >::
operator=( const Array< elemType > &Aobj)
{
 if ( this != &Aobj )
 {
    delete[] _ia;
   init( Aobj._ia, Aobj._size);
 }

   return *this;
}

#endif

/*******************  Array_RC.h     **********************
 ***********检测边界的array
 **********************************************************/
#ifndef ARRAY_RC_H
#define ARRAY_RC_H

#include "ArrayBase.h"

template <class Type>
class Array_RC : public virtual Array<Type> {
 public:
  Array_RC( int sz = Array<Type>::ArraySize )
          : Array<Type>( sz ) {}

  Array_RC( const Array_RC& r );
  Array_RC( const Type *ar, int sz );
  Type& operator[](int ix );
};

#include "Array_RC.cpp"
#endif


/************   Array_RC.cpp   ******************
 *
 *******************************************************/

//---------define the Array_RC.h as ARRAY_RC_CPP---
#ifndef ARRAY_RC_CPP
#define ARRAY_RC_CPP

#include <assert.h>

template < class Type >
Array_RC< Type >::
Array_RC( const Array_RC< Type > &r)
  : Array<Type>( r ) {}
  
template < class Type >
Array_RC< Type >::
Array_RC( const Type *ar, int sz )
  : Array<Type>(ar, sz ) {}
  
template < class Type >
Type &Array_RC< Type >::operator[]( int ix )
{
 assert( ix >= 0 && ix < Array<Type>::_size );
 return  Array<Type>::_ia[ix];
}

#endif


/***************   Array_Sort.h  **********自动排序的array
 *********************************************************/

#ifndef ARRAY_SORT_H
#define ARRAY_SORT_H

#include "ArrayBase.h"

#include <iostream>
using namespace std;

template <class Type>
class Array_Sort : public virtual Array<Type> {
 protected:
  void set_bit() { dirty_bit = true; }
  void clear_bit() { dirty_bit = false; }
  void check_bit() {
   if ( dirty_bit ) {
    Array<Type>::sort(0, Array<Type>::_size - 1);
    clear_bit();
   }
  }
  
 public:
  Array_Sort(int sz = Array<Type>::ArraySize)
    : Array<Type>(sz)
  {
   clear_bit();
  }
  
  Array_Sort(const Array_Sort& );
  Array_Sort(const Type *arr, int sz )
      : Array<Type>(arr, sz)
  {
   Array<Type >::sort(0, Array<Type>::_size - 1);
   clear_bit();
  }
  
  Type& operator[]( int ix )
  { set_bit(); return Array<Type>::_ia[ix]; }
  
 virtual void print(ostream& os = cout )
  {
   check_bit();
   Array<Type>::print( os );
  }
  
 inline Type& min()
  {
   check_bit();
   return Array<Type>::_ia[0];
  }
  
 inline Type& max()
  {
   check_bit();
   return Array<Type>::_ia[Array<Type>::_size - 1 ];
  }
  
 inline bool is_dirty() const
  {
   return dirty_bit;
  }
  
  int find( Type& );
  void grow();
 protected:
  bool dirty_bit;
};

 

#include "Array_Sort.cpp"
#endif

 

/****************   Array_Srot.cpp   ************************
 ***********************************************************/

#ifndef ARRAY_SORT_CPP
#define ARRAY_SORT_CPP

template < class Type >
Array_Sort< Type >::
Array_Sort( const Array_Sort<Type > &as )
 : Array< Type >( as )
{
 dirty_bit = as.dirty_bit;
 check_bit();
}

template < class Type >
void Array_Sort< Type >::grow()
{
 Array<Type>::grow();
 Array<Type>::sort(0, Array<Type>::_size - 1);
 clear_bit();
}

template < class Type >
int Array_Sort< Type >::find( Type& val)
{
 int low = 0;
 int high = Array<Type>::_size-1;
 check_bit();
 
 while ( low <= high ) {
  int mid = ( low + high ) / 2;
  if ( val == Array<Type>::_ia[mid] )  return mid;
  if ( val < Array<Type>::_ia[mid] )
  {
   high = mid - 1;
  }
  else
  {
   low = mid + 1;
  }
 }
 
 return -1;
}

 


#endif

 

/**********   Array.h   *********************
 ***********检测边界,自动排序的array
 *********************************************/
#ifndef ARRAY_RC_S_H
#define ARRAY_RC_S_H

#include "Array_RC.h"
#include "Array_Sort.h"

template< class Type >
class ARRAY : public virtual Array_RC< Type >,
     public virtual Array_Sort< Type >
{
public:
 ARRAY(int sz = Array<Type>::Arraysize )
     : Array<Type>( sz )
 {
  Array_Sort<Type>::clear_bit();
 }

 ARRAY( const ARRAY &rca )
     : Array<Type>( rca )
 {
  Array<Type>::sort( 0, Array<Type>::_size-1);
  Array_Sort<Type>::clear_bit();
 }

 ARRAY( Type *arr, int sz )
     : Array<Type>(arr, sz )
 {
  Array<Type>::sort(0, Array<Type>::_size-1);
  Array_Sort<Type>::clear_bit();
 }

 Type& operator[]( int ix )
 {
  Array_Sort<Type>::set_bit();
  return Array_RC<Type>::operator[](ix);
 }
 
 virtual void print( ostream& os = cout )
 {
  Array_Sort<Type>::print(os);
 }

};


#endif


/*******************   try_array.cpp  ********************

****array 测试函数
 **********************************************************/
 
#include "Array.h"

template <class elemType>
 void    try_array( ARRAY< elemType > &iA)
{

 cout << "try_array: initial array values:/n";
 cout << iA << endl;
 
 elemType    find_val    = iA[iA.size() - 1 ];
 iA[iA.size() - 1 ] = iA.min();
 
 int mid = iA.size() / 2;
 iA[0] = iA.max();
 iA[mid] = iA[0];
 cout << "try_array: after assignments:/n";
 cout << iA << endl;
 
 ARRAY< elemType > iA2 = iA;
 iA2[mid/2] = iA2[mid];
 cout << "try_array: memberwise initializetion/n";
 cout << iA << endl;
 
 iA = iA2;
 cout << "try_array: after memberwise copy/n";
 cout << iA << endl;
 
 iA.grow();
 cout << "try_array: after grow/n";
 cout << iA << endl;
 int index = iA.find(find_val);
 cout << "value to find:" << find_val;
 cout << "/tindex returned: " << index << endl;
 
 elemType value = iA[index];
 cout << "value found at index: ";
 cout << value << endl;
}


//----------------------array_test.cpp (main) -----------------------

#include "MyHeader/try_array.cpp"  // All header stored in MyHeader
#include "MyHeader/ARRAY.h"

#include <cstdlib>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
 cout << "try array begin.../n";

 static int ia[10] = {12, 7, 14, 9, 128, 17, 6, 3, 27, 5};
 static string sa[7] = { "Eeyore", "Pooh", "Tigger", "Piglet",
                     "Owl", "Gopher", "Heffalump" };

 ARRAY< int >  iA( ia, 10 );
 ARRAY< string > SA( sa, 7 );

  cout << "class template instantiation ARRAY<int >/n";
 //try_array(iA);

 cout << "class template instantiation ARRAY<string> /n";
 try_array(SA);

    system("PAUSE");
    return EXIT_SUCCESS;
}

posted on 2005-06-11 16:46  h2内存数据库  阅读(223)  评论(0编辑  收藏  举报

导航