home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (c) IBM Corp. 1992 */
- #ifndef _ISTDOPS_H
- #define _ISTDOPS_H
-
- #include <iglobals.h>
- #include <string.h>
-
- // indirection for function/operator application
-
- template < class Element >
- inline Element& elementForOps (Element& e)
- { return e; }
-
- template < class Element >
- inline Element const& elementForOps (Element const& e)
- { return e; }
-
- // memory management
-
- class IStdMemOps {
- public:
- void* allocate (size_t s) const;
- void deallocate (void* p) const;
- };
-
- inline void* IStdMemOps::
- allocate (size_t s) const {
- return ::operator new (s);
- }
-
- inline void IStdMemOps::
- deallocate (void* p) const {
- ::operator delete (p);
- }
-
- // assignment
-
- template < class Element >
- inline void assign (Element& e1, Element const& e2) {
- e1 = e2;
- }
-
- template < class Element >
- class IStdAsOps {
- public:
- void assign (Element& e1, Element const& e2) const;
- };
-
- template < class Element >
- inline void IStdAsOps < Element >::
- assign (Element& e1, Element const& e2) const {
- ::assign (elementForOps (e1), elementForOps (e2));
- }
-
- // equality
-
- template < class Element >
- inline Boolean equal (Element const& e1, Element const& e2) {
- return e1 == e2;
- }
-
- inline Boolean equal (char const* e1, char const* e2) {
- return strcmp (e1, e2) == 0;
- }
-
- template < class Element >
- class IStdEqOps {
- public:
- Boolean equal (Element const& e1, Element const& e2) const;
- };
-
- template < class Element >
- inline Boolean IStdEqOps < Element >::
- equal (Element const& e1, Element const& e2) const {
- return ::equal (elementForOps (e1), elementForOps (e2));
- }
-
- // ordering relation
-
- template < class Element >
- inline long compare (Element const& e1, Element const& e2) {
- if (e1 < e2) return -1;
- else if (e2 < e1) return 1;
- else return 0;
- }
-
- inline long compare (int e1, int e2) {
- return e1 - e2;
- }
-
- inline long compare (char* e1, char* e2) {
- return strcmp (e1, e2);
- }
-
- template < class Element >
- class IStdCmpOps {
- public:
- long compare (Element const& e1, Element const& e2) const;
- };
-
- template < class Element >
- inline long IStdCmpOps < Element >::
- compare (Element const& e1, Element const& e2) const {
- return ::compare (elementForOps (e1), elementForOps (e2));
- }
-
- // hash function
-
- inline unsigned long hash (int e, unsigned long n) {
- return e % n;
- }
-
- inline unsigned long hash (char* e, unsigned long n) {
- unsigned int len = strlen (e);
- if (len == 0)
- return 0;
- else
- return (e [0] + e [len-1] + e [len/2]) % n;
- }
-
- template < class Element >
- class IStdHshOps {
- public:
- unsigned long hash (Element const& e, unsigned long n) const;
- };
-
- template < class Element >
- inline unsigned long IStdHshOps < Element >::
- hash (Element const& e, unsigned long n) const {
- return ::hash (elementForOps (e), n);
- }
-
- // key selection
-
- template < class Element, class Key >
- class IStdKeyOps {
- public:
- Key const& key (Element const& e) const;
- };
-
- template < class Element, class Key >
- inline Key const& IStdKeyOps < Element, Key >::
- key (Element const& e) const {
- return ::key (elementForOps (e));
- }
-
- // standard operation combinations
-
- template < class Element >
- class IStdOps : public IStdMemOps,
- public IStdAsOps < Element > {
- };
-
- template < class Element >
- class IEOps : public IStdOps < Element >,
- public IStdEqOps < Element > {
- };
-
- template < class Element >
- class IECOps : public IEOps < Element >,
- public IStdCmpOps < Element > {
- };
-
- template < class Element >
- class IEHOps : public IEOps < Element >,
- public IStdHshOps < Element > {
- };
-
- template < class Element, class Key >
- class IKCOps : public IStdOps < Element >,
- public IStdKeyOps < Element, Key > {
- public:
- class KeyOps : public IStdCmpOps < Key > {
- } keyOps;
- };
-
- template < class Element, class Key >
- class IKEHOps : public IStdOps < Element >,
- public IStdKeyOps < Element, Key > {
- public:
- class KeyOps : public IStdEqOps < Key >,
- public IStdHshOps < Key > {
- } keyOps;
- };
-
- template < class Element, class Key >
- class IEKCOps : public IEOps < Element >,
- public IStdKeyOps < Element, Key > {
- public:
- class KeyOps : public IStdCmpOps < Key > {
- } keyOps;
- };
-
- template < class Element, class Key >
- class IEKEHOps : public IEOps < Element >,
- public IStdKeyOps < Element, Key > {
- public:
- class KeyOps : public IStdEqOps < Key >,
- public IStdHshOps < Key > {
- } keyOps;
- };
-
- // definition macor for templates with given operation class
-
- #define IDefineCollectionWithOps(IGCollection, Ops, ICollection) \
- template < class Element > \
- class ICollection : \
- public IGCollection < Element, Ops < Element > > { \
- public: \
- ICollection (INumber n = 100, IBoundIndicator b = IUnbounded) : \
- IGCollection < Element, Ops < Element > > (n, b) {} \
- };
-
- #define IDefineKeyCollectionWithOps(IGCollection, Ops, ICollection) \
- template < class Element, class Key > \
- class ICollection : \
- public IGCollection < Element, Key, Ops < Element, Key > > { \
- public: \
- ICollection (INumber n = 100, IBoundIndicator b = IUnbounded) : \
- IGCollection < Element, Key, Ops < Element, Key > > (n, b) {} \
- };
-
- #define IDefineStdCollectionWithOps(Collection) IDefineCollectionWithOps \
- (IG ## Collection, I ## Collection ## Ops, I ## Collection)
-
- #define IDefineStdKeyCollectionWithOps(Collection) IDefineKeyCollectionWithOps \
- (IG ## Collection, I ## Collection ## Ops, I ## Collection)
-
- // key extension
-
- template < class Element, class ElementOps >
- class IOpsWithKey : public ElementOps {
- public:
- Element const& key (Element const& e) const {
- return e;
- }
- ElementOps keyOps;
- };
-
- #endif