⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hash.h

📁 在VC6环境下开发
💻 H
字号:
/*
** This is the header file for the generic hash-table implemenation
** used in eDb.
*/
#ifndef _eDb_HASH_H_
#define _eDb_HASH_H_

/* Forward declarations of structures. */
typedef struct Hash Hash;
typedef struct HashElem HashElem;

/* A complete hash table is an instance of the following structure.
** The internals of this structure are intended to be opaque -- client
** code should not attempt to access or modify the fields of this structure
** directly.  Change this structure only by using the routines below.
** However, many of the "procedures" and "functions" for modifying and
** accessing this structure are really macros, so we can't really make
** this structure opaque.
*/
struct Hash {
	char keyClass;          /* eDb_HASH_INT, _POINTER, _STRING, _BINARY */
	char copyKey;           /* True if copy of key made on insert */
	int count;              /* Number of entries in this table */
	HashElem *first;        /* The first element of the array */
	int htsize;             /* Number of buckets in the hash table */
	struct _ht {            /* the hash table */
		int count;               /* Number of entries with this hash */
		HashElem *chain;         /* Pointer to first entry with this hash */
	} *ht;
};

/* Each element in the hash table is an instance of the following 
** structure.  All elements are stored on a single doubly-linked list.
**
** Again, this structure is intended to be opaque, but it can't really
** be opaque because it is used by macros.
*/
struct HashElem {
  HashElem *next, *prev;   /* Next and previous elements in the table */
  void *data;              /* Data associated with this element */
  void *pKey; int nKey;    /* Key associated with this element */
};

/*
** There are 4 different modes of operation for a hash table:
**
**   eDb_HASH_INT         nKey is used as the key and pKey is ignored.
**
**   eDb_HASH_POINTER     pKey is used as the key and nKey is ignored.
**
**   eDb_HASH_STRING      pKey points to a string that is nKey bytes long
**                           (including the null-terminator, if any).  Case
**                           is ignored in comparisons.
**
**   eDb_HASH_BINARY      pKey points to binary data nKey bytes long. 
**                           memcmp() is used to compare keys.
**
** A copy of the key is made for eDb_HASH_STRING and eDb_HASH_BINARY
** if the copyKey parameter to HashInit is 1.  
*/
#define eDb_HASH_INT       1
#define eDb_HASH_POINTER   2 
#define eDb_HASH_STRING    3
#define eDb_HASH_BINARY    4

/*
** Access routines.  To delete, insert a NULL pointer.
*/
void eDbHashInit(Hash*, int keytype, int copyKey);
void *eDbHashInsert(Hash*, const void *pKey, int nKey, void *pData);
void *eDbHashFind(const Hash*, const void *pKey, int nKey);
void eDbHashClear(Hash*);

/*
** Macros for looping over all elements of a hash table.  The idiom is
** like this:
**
**   Hash h;
**   HashElem *p;
**   ...
**   for(p=eDbHashFirst(&h); p; p=eDbHashNext(p)){
**     SomeStructure *pData = eDbHashData(p);
**     // do something with pData
**   }
*/
#define eDbHashFirst(H)  ((H)->first)
#define eDbHashNext(E)   ((E)->next)
#define eDbHashData(E)   ((E)->data)
#define eDbHashKey(E)    ((E)->pKey)
#define eDbHashKeysize(E) ((E)->nKey)

/*
** Number of entries in a hash table
*/
#define eDbHashCount(H)  ((H)->count)

#endif /* _eDb_HASH_H_ */

⌨️ 快捷键说明

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