home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / cmtarray.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-06  |  3.6 KB  |  81 lines

  1. // CmTArray.h
  2. // -----------------------------------------------------------------
  3. // Compendium - C++ Container Class Library
  4. // Copyright (C) 1992-1994, Glenn M. Poorman, All rights reserved
  5. // -----------------------------------------------------------------
  6. // Array template definition.
  7. // -----------------------------------------------------------------
  8.  
  9. #ifndef _CMTARRAY_H
  10. #define _CMTARRAY_H
  11.  
  12. #include <stdlib.h>
  13. #include <cm/include/cmtcont.h>
  14.  
  15. template <class T> class CmTArrayIterator;    // Iterator class stub.
  16.  
  17. template <class T> class CmTArray : public CmTContainer<T> {
  18. public:
  19.   CmTArray(unsigned = 0, unsigned = 0);       // Default array constructor.
  20.   CmTArray(const CmTArray<T>&);               // Array copy constructor.
  21.  ~CmTArray();                                 // Array destructor.
  22.  
  23.   CmTArray<T>& operator=(const CmTArray<T>&); // Assignment operator.
  24.  
  25.   const T& operator[](int) const;             // Indexing operator.
  26.                                               // (Can not be used as lvalue).
  27.  
  28.   void     delta      (unsigned);             // Set delta value.
  29.   unsigned delta      () const;               // Get delta value.
  30.   int      total      () const;               // Return number of items.
  31.   const T& at         (int) const;            // Get object at index.
  32.   Bool     add        (const T&);             // Append object to array.
  33.   Bool     insertAt   (int, const T&);        // Insert object at index.
  34.   Bool     replaceAt  (int, const T&);        // Replace object at index.
  35.   Bool     remove     (const T&);             // Remove specified object.
  36.   Bool     removeAt   (int);                  // Remove object at index.
  37.   int      index      (const T&) const;       // Get index of object.
  38.   const T& lookup     (const T&) const;       // Look for equal object.
  39.   Bool     contains   (const T&) const;       // Object is in array?
  40.   unsigned occurrences(const T&) const;       // How many occurrences?
  41.   void     removeAll  ();                     // Remove all objects.
  42.   Bool     resize     (unsigned);             // Resize the array.
  43.   Bool     isEmpty    () const;               // Is array empty?
  44.   void     quickSort  ();                     // Quick sort the array.
  45.  
  46.   CmTIterator<T>* newIterator() const;        // Get array iterator.
  47.  
  48. protected:
  49.   void copy(const CmTArray<T>&);              // Internal copy method.
  50.  
  51.   static int cmpObjs(const void*, const void*); // Quick sort compare func.
  52.  
  53.   unsigned  _delta;                           // Delta value.   
  54.   unsigned  _total;                           // Number of objects.
  55.   T        *_entries;                         // Array of "T" pointers.
  56.   friend    CmTArrayIterator<T>;              // Iterator can access,
  57. };
  58.  
  59. template <class T> class CmTArrayIterator : public CmTIterator<T> {
  60. public:
  61.   CmTArrayIterator(const CmTArray<T>&);       // Iterator constructor.
  62.  
  63.   Bool     done    () const;                  // Check if done iterating.
  64.   const T& next    ();                        // Return and advance.
  65.   const T& previous();                        // Return and backup.
  66.   const T& current () const;                  // Return current item.
  67.   void     first   ();                        // Move to first item.
  68.   void     last    ();                        // Move to last item.
  69.  
  70. protected:
  71.   const CmTArray<T>& _array;                  // Array being iterated.
  72.   int                _index;                  // Current array index.
  73.   friend             CmTArray<T>;             // Array class can access.
  74. };
  75.  
  76. #if defined(__TURBOC__) || defined(__xlC__)
  77. #include <cm/include/cmtarray.cc>
  78. #endif
  79.  
  80. #endif
  81.