UNIVERS  15.3
UNIVERS base processing software API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
AnyHash Class Reference

Macro implementation of universal hash storage method with quick access. More...

#include <AnyHash.H>

Public Member Functions

 AnyHash (unsigned base)
 
virtual ~AnyHash ()
 
Logic known_key (AnyKeyType key_value) const
 
unsigned count () const
 
void value_list (DynAr &vals) const
 
AnyType & hash_fetch (AnyKeyType key)
 
AnyType hash_get (AnyKeyType key) const
 
AnyType & operator[] (AnyKeyType key)
 
AnyType operator() (AnyKeyType key) const
 
void add (const AnyType &val)
 
void put (const AnyType &val)
 

Protected Member Functions

Logic find_key (AnyKeyType key, unsigned &hash_i, unsigned &item_i) const
 

Protected Attributes

DynAr ** hashItems
 
unsigned hashBase
 

Detailed Description

Macro implementation of universal hash storage method with quick access.

The class is designed to provide hash organized array of items. All items having the same hash value (integer) are stored in unordered manner with linear search, so it's better to increase hash base to improve access performance in case you are planning to store great number of items in the hash.

Specification:

typedef struct
{
#define AnyType your-item-type
#define AnyHash your-hash-class
#define AnyKeyType your-custom-type-for-key
#define AnyItem2Key(x) extract-key-from-item
#define AnyKey2Hash(k) convert-key-to-number
#define AnyKeyCmp(k,k_) compare-two-keys
#include <mix/AnyHash.H>

Example 1:

typedef struct
{
int val; //< hash value
float data; //< useful data
} Record;
#define AnyType Record
#define AnyHash RecordHash
#define AnyKeyType int
#define AnyItem2Key(x) (x).val
#define AnyKey2Hash(k) (k)
#define AnyKeyCmp(k,k_) COMPAR0((k)-(k_))
#include <mix/AnyHash.H>

Example 2:

class Record
{
private:
char *name; //< hash value
float data; //< useful data
public:
const char* key () const {
return name;
}
static int hash (const char* s) {
int h = 0, i = 0;
while(s[i] != '\0')
h += s[i++];
return h;
}
};
#define AnyType Record
#define AnyHash RecordHash
#define AnyKeyType const char*
#define AnyItem2Key(x) (x).key()
#define AnyKey2Hash(k) Record::hash(k)
#define AnyKeyCmp(k,k_) strcmp((k),(k_))
#include <mix/AnyHash.H>

Constructor & Destructor Documentation

AnyHash::AnyHash ( unsigned  base)
inline

Create the hash object with given number of subclasses (preferred prime number).

virtual AnyHash::~AnyHash ( )
inlinevirtual

Destroy the whole hash object.

Member Function Documentation

void AnyHash::add ( const AnyType &  val)
inline

Add item (with unique key) to the hash.

unsigned AnyHash::count ( ) const
inline

Get number of stored items.

Logic AnyHash::find_key ( AnyKeyType  key,
unsigned &  hash_i,
unsigned &  item_i 
) const
inlineprotected

Associative operation to find whether the value exist in the hash and where it is.

Logic AnyHash::known_key ( AnyKeyType  key_value) const
inline

Find whether this key exist in the hash.

void AnyHash::put ( const AnyType &  val)
inline

Add item (unique and non-unique key) to the hash

void AnyHash::value_list ( DynAr vals) const
inline

Get list of stored items.

Member Data Documentation

unsigned AnyHash::hashBase
protected

Number of items - hash base.

DynAr** AnyHash::hashItems
protected

Really the array of pointers to AnyType items with the same hash value.


The documentation for this class was generated from the following file: