-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhash.h
109 lines (86 loc) · 3.79 KB
/
hash.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* This source file is part of the Atlantis Little Helper program.
* Copyright (C) 2001 Maxim Shariy.
*
* Atlantis Little Helper is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Atlantis Little Helper is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Atlantis Little Helper; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __AH_HASH_H_INCL__
#define __AH_HASH_H_INCL__
typedef struct _HASH_LIST_ITEM
{
void * key;
void * data;
struct _HASH_LIST_ITEM * next;
} HASH_LIST_ITEM;
//---------------------------------------------------------------
// duplicates are allowed and expected
class CHash
{
public:
CHash(unsigned int sizeidx);
virtual ~CHash();
void FreeAll();
void Insert (const void * key, const void * data);
void Free (const void * key);
HASH_LIST_ITEM * LocateItem(const void * key);
bool Locate (const void * key, const void *& data);
void ForEach (void (*action)(void*, void*, void*), void * user_data);
void ForEachCheckRemove(int (*action)(void*, void*, void*), void * user_data);
protected:
virtual void FreeItem(HASH_LIST_ITEM * pItem) = 0;
virtual HASH_LIST_ITEM * NewItem (const void * key, const void * data) = 0;
virtual unsigned long HashValue(const void * key) = 0;
virtual bool Equal (const void * key1, const void * key2) = 0;
int m_nHashSize;
HASH_LIST_ITEM ** m_Buckets;
};
//---------------------------------------------------------------
class CHashStrToLong : public CHash
{
public:
CHashStrToLong(unsigned int sizeidx) : CHash(sizeidx) {};
void Dbg_Print();
protected:
virtual void FreeItem(HASH_LIST_ITEM * pItem);
virtual HASH_LIST_ITEM * NewItem (const void * key, const void * data);
virtual unsigned long HashValue(const void * key);
virtual bool Equal (const void * key1, const void * key2);
};
//---------------------------------------------------------------
// stores duplicates of strings, destroys them
class CHashLongToStr : public CHash
{
public:
CHashLongToStr(unsigned int sizeidx) : CHash(sizeidx) {};
protected:
virtual void FreeItem(HASH_LIST_ITEM * pItem);
virtual HASH_LIST_ITEM * NewItem (const void * key, const void * data);
virtual unsigned long HashValue(const void * key);
virtual bool Equal (const void * key1, const void * key2);
};
//---------------------------------------------------------------
// Stores generic pointers, user is responsible for deleting the memory pointed to
class CHashLongToPointer : public CHash
{
public:
CHashLongToPointer(unsigned int sizeidx) : CHash(sizeidx) {};
protected:
virtual void FreeItem(HASH_LIST_ITEM * pItem);
virtual HASH_LIST_ITEM * NewItem (const void * key, const void * data);
virtual unsigned long HashValue(const void * key);
virtual bool Equal (const void * key1, const void * key2);
};
//---------------------------------------------------------------
#endif