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

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