wchbase.mh

来自「开放源码的编译器open watcom 1.6.0版的源代码」· MH 代码 · 共 167 行

MH
167
字号
//
//  wchbase.h    Definitions for the base classes used by
//               the WATCOM Container Hash Classes
//
:keep CPP_HDR
:include crwat.sp
//
#ifndef _WCHBASE_H_INCLUDED
#define _WCHBASE_H_INCLUDED
:include readonly.sp

#ifndef __cplusplus
#error wchbase.h is for use with C++
#endif

#ifndef _WCDEFS_H_INCLUDED
 #include <wcdefs.h>
#endif

:include undefnew.sp


//
// The default number of buckets in a hash table.  If a second parameter is
// passed to the hash table constructor, this value will not be used
//

const unsigned WC_DEFAULT_HASH_SIZE = 101;




//
// Hash implementation object:
// A singly linked list element for storing the values in the hash table
//

template <class Type>
class WCHashLink : public WCSLink {
public:
    Type data;

    // used by WCHashNew to be able to call a constructor on user allocator
    // allocated memory
    inline void * operator new( size_t, void * ptr ){
        return( ptr );
    }
    inline WCHashLink( const Type & datum ) : data( datum ) {};
    inline ~WCHashLink() {};
};



//
// Hash Dictionary implementation object:
// Combines the Key and Value into one object
//

template <class Key, class Value>
class WCHashDictKeyVal{
public:
    Key key;
    Value value;

    inline WCHashDictKeyVal( const WCHashDictKeyVal &orig )
            : key( orig.key ), value( orig.value ) {};
    inline WCHashDictKeyVal( const Key &new_key, const Value &new_value )
            : key( new_key ), value( new_value ) {};
    inline WCHashDictKeyVal( const Key &new_key ) : key( new_key ) {};
    inline WCHashDictKeyVal() {};
    inline ~WCHashDictKeyVal() {};

    // this operator is NEVER used, but necessary for compilation
    // (needed by WCValHashSet, inherited by WCValHashDict)
    inline int operator==( const WCHashDictKeyVal & ) const {
        return( 0 );
    };
};




//
// Provide base functionality for WATCOM container hash tables, sets and
// dictionaries.
//
// WCExcept provides exception handling.
//
// This class is an abstract class so that objects of this type cannot be
// created.
//


class WCHashBase : public WCExcept {
protected:
    WCIsvSList<WCSLink> *hash_array;
    unsigned num_buckets;
    unsigned num_entries;

    // link base non-templated class
    typedef WCSLink BaseHashLink;
    // pointer to element of templated type
    typedef const void *TTypePtr;

    enum find_type {            // enumerations for base_find
        FIND_FIRST,             // find first matching element
        NEXT_MULT_FIND };       // find next matching element in bucket

    // copy constructor base
    _WPRTLINK void base_construct( const WCHashBase * orig );

    // for copy constructor
    virtual BaseHashLink *base_copy_link( const BaseHashLink *orig ) = 0;

    virtual int base_equivalent( BaseHashLink *elem1
                               , TTypePtr elem2 ) const = 0;

    virtual unsigned base_get_bucket( TTypePtr elem ) const = 0;

    // for the resize member function
    virtual unsigned base_get_bucket_for_link( BaseHashLink *link ) const = 0;

    _WPRTLINK BaseHashLink *base_find( TTypePtr elem, unsigned *bucket
                           , unsigned *index, find_type type ) const;

    // common initialization code for the constructors
    _WPRTLINK void base_init( unsigned buckets );

    _WPRTLINK BaseHashLink *base_remove_but_not_delete( TTypePtr elem );

    // the insert function for WCValHashSet and WCPtrHashSet
    _WPRTLINK BaseHashLink *base_set_insert( TTypePtr elem );

    virtual BaseHashLink * WCHashNew( TTypePtr elem ) = 0;

    virtual void WCHashDelete( BaseHashLink *old_link ) = 0;


    inline WCHashBase() : hash_array( 0 ), num_buckets( 0 )
                        , num_entries( 0 ) {};

    inline WCHashBase( unsigned buckets ) {
        base_init( buckets );
    };

    virtual ~WCHashBase() = 0;

    _WPRTLINK void clear ();

    _WPRTLINK WCbool insert( TTypePtr elem );

    _WPRTLINK unsigned occurrencesOf( TTypePtr elem ) const;

    _WPRTLINK unsigned removeAll( TTypePtr elem );

    _WPRTLINK void resize( unsigned buckets );

public:
    _WPRTLINK static unsigned bitHash( const void * data, size_t size );

    friend class WCHashIterBase;
};

:include redefnew.sp

#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?