home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv8.zip / VACPP / IBMCPP / samples / IOC / INTKYSET / DEMOELEM.H < prev    next >
Text File  |  1995-03-15  |  2KB  |  87 lines

  1. /*************************************************************************
  2.   IBM C/C++ Tools Version 3.00 - Collection Class Library
  3.  (C) Copyright IBM Corporation 1992 ,1995, Licensed Program-Property of
  4.  IBM.  All Rights Reserved.  US Government Users Restricted Rights - Use,
  5.  duplication or disclosure restricted by GSA ADP Schedule Contract with
  6.  IBM Corp.
  7.  *************************************************************************/
  8.  
  9. /*-------------------------------------------------------------*\
  10. |  demoelem.h  -  DemoElement for use with Key Collections      |
  11. \*-------------------------------------------------------------*/
  12. #ifndef _DEMOELEM_H
  13. #define _DEMOELEM_H
  14.  
  15. #include <stdlib.h>
  16. #include <iglobals.h>
  17. #include <iostream.h>
  18. #include <istdops.h>
  19.  
  20. class DemoElement {
  21.  
  22.   int i;
  23.   int j;
  24.  
  25. public:
  26.  
  27.            DemoElement ()
  28.            : i(0), j(0)
  29.            {
  30.            }
  31.  
  32.            DemoElement (int i,int j)
  33.            : i (i), j(j)
  34.            {
  35.            }
  36.  
  37.            operator int () const
  38.            { return i;
  39.            }
  40.  
  41.   IBoolean operator == (DemoElement const& k) const
  42.            { return i == k.i && j == k.j;
  43.            }
  44.  
  45.   IBoolean operator < (DemoElement const& k) const
  46.            { return i < k.i || (i == k.i && j < k.j);
  47.            }
  48.  
  49.   friend unsigned long
  50.            hash (DemoElement const& k, unsigned long n)
  51.            { return k.i % n;
  52.            }
  53.  
  54.   int const &
  55.            geti () const
  56.            { return i;
  57.            }
  58.  
  59.   int const &
  60.            getj () const
  61.            { return j;
  62.            }
  63.  
  64. };
  65.  
  66. inline ostream & operator << (ostream &sout, DemoElement const& e)
  67. { sout << e.geti () << "," << e.getj ();
  68.   return sout;
  69. }
  70.  
  71. inline int const& key (DemoElement const& k)
  72. { return k.geti ();
  73. }
  74.  
  75. // NOTE: You must return a const & in the key function!
  76. // Otherwise the implicitly created standard element operations
  77. // will return a reference to a temporary. This would lead to
  78. // incorrect behavior of the collection operations.
  79.  
  80. // The key function must be declared in the header file of
  81. // the collection's element type.
  82.  
  83. // If either of this is not possible or undesirable,
  84. // an element operations class must be used.
  85.  
  86. #endif
  87.