| 1 |
/* lhash.h Emacs, this file is -*- C++ -*- |
| 2 |
$Id: lhash.h,v 1.4 1997/10/18 04:57:36 elf Exp $ |
| 3 |
|
| 4 |
written by Marc Singer |
| 5 |
15 October 1997 |
| 6 |
|
| 7 |
This file is part of the project XO. See the file README for |
| 8 |
more information. |
| 9 |
|
| 10 |
Copyright (C) 1997 Marc Singer |
| 11 |
|
| 12 |
This program is free software; you can redistribute it and/or |
| 13 |
modify it under the terms of the GNU General Public License as |
| 14 |
published by the Free Software Foundation; either version 2 of the |
| 15 |
License, or (at your option) any later version. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but |
| 18 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 |
General Public License for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License |
| 23 |
in a file called COPYING along with this program; if not, write to |
| 24 |
the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA |
| 25 |
02139, USA. |
| 26 |
|
| 27 |
|
| 28 |
*/ |
| 29 |
|
| 30 |
#ifndef __LHASH_H__ |
| 31 |
#define __LHASH_H__ |
| 32 |
|
| 33 |
/* ----- Inclusions */ |
| 34 |
|
| 35 |
#include "larray.h" |
| 36 |
|
| 37 |
/* ----- Constants */ |
| 38 |
|
| 39 |
#define LOWORD(v) ((v)&0xffff) |
| 40 |
#define HIWORD(v) (((v)>>16)&0xffff) |
| 41 |
#define MAKELONG(l,h) ((((h) & 0xffff)<<16) | ((l) & 0xffff)) |
| 42 |
|
| 43 |
/* ----- Typedefs */ |
| 44 |
|
| 45 |
typedef unsigned long HashKey; |
| 46 |
|
| 47 |
/* ----- Classes */ |
| 48 |
|
| 49 |
class LHashBucket { |
| 50 |
private: |
| 51 |
LArray* m_pArrayItem; // Array of data items (user specified lengths) |
| 52 |
LArray* m_pArrayKey; // Array of keys |
| 53 |
int m_cbEach; // Size of user items |
| 54 |
|
| 55 |
int _find (HashKey); |
| 56 |
|
| 57 |
public: |
| 58 |
LHashBucket (int cbEach) { |
| 59 |
zero (); init (cbEach); } |
| 60 |
~LHashBucket () { |
| 61 |
release_all (); } |
| 62 |
void init (int); |
| 63 |
void release_all (void); |
| 64 |
void zero (void); |
| 65 |
|
| 66 |
bool add (HashKey, const void*); |
| 67 |
void* find (HashKey); |
| 68 |
bool find (HashKey, void*); |
| 69 |
bool is_init (void) { |
| 70 |
return m_cbEach != 0; } |
| 71 |
long get_count (void) { |
| 72 |
return m_pArrayKey->get_count (); } |
| 73 |
HashKey get_key (long i) { |
| 74 |
HashKey* pKey = (HashKey*) m_pArrayKey->at (i); |
| 75 |
return pKey ? *pKey : 0; } |
| 76 |
void* reference_item (long i) { |
| 77 |
return m_pArrayItem->reference (i); } |
| 78 |
bool remove (HashKey); |
| 79 |
}; |
| 80 |
|
| 81 |
class LHashTable { |
| 82 |
private: |
| 83 |
LArray* m_pArrayBucket; // Array of hash buckets |
| 84 |
|
| 85 |
int m_cBuckets; // Number of buckets in table |
| 86 |
int m_cbEach; // Size of user's data item |
| 87 |
|
| 88 |
public: |
| 89 |
LHashTable () { |
| 90 |
zero (); init (0, 0); } |
| 91 |
LHashTable (int cbEach, int cBuckets) { |
| 92 |
zero (); init (cbEach, cBuckets); } |
| 93 |
~LHashTable () { |
| 94 |
release_all (); } |
| 95 |
bool is_init (void) { |
| 96 |
return m_cbEach != 0 && m_cBuckets != 0; } |
| 97 |
void init (int, int); |
| 98 |
void release_all (void); |
| 99 |
void zero (void); |
| 100 |
|
| 101 |
bool add (HashKey, const void*); |
| 102 |
int bucket_of (HashKey key) { |
| 103 |
return (int) (m_cBuckets |
| 104 |
? ((LOWORD (key) + HIWORD (key)) % m_cBuckets) |
| 105 |
: 0); } |
| 106 |
unsigned32 enumerate_reverse ( // Enumerate ALL keys in table |
| 107 |
unsigned32 index, |
| 108 |
HashKey* pKey, |
| 109 |
const void** ppv); |
| 110 |
void* find (HashKey); |
| 111 |
bool find (HashKey, void*); |
| 112 |
static HashKey make_string_key (const char*); |
| 113 |
static HashKey make_string_key (const char*, int); |
| 114 |
static HashKey make_case_string_key (const char*); |
| 115 |
static HashKey make_case_string_key (const char*, int); |
| 116 |
bool remove (HashKey); |
| 117 |
}; |
| 118 |
|
| 119 |
#endif /* __LHASH_H__ */ |