TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Map.cpp File Reference
#include "../CascLib.h"
#include "../CascCommon.h"
+ Include dependency graph for Map.cpp:

Macros

#define __CASCLIB_SELF__
 

Functions

static const char * String_GetExtension (const char *szString)
 
static DWORD CalcHashIndex_Key (PCASC_MAP pMap, void *pvKey)
 
static DWORD CalcHashIndex_String (PCASC_MAP pMap, const char *szString, const char *szStringEnd)
 
static bool CompareObject_Key (PCASC_MAP pMap, void *pvObject, void *pvKey)
 
static bool CompareObject_String (PCASC_MAP pMap, const char *szExistingString, const char *szString, const char *szStringEnd)
 
PCASC_MAP Map_Create (DWORD dwMaxItems, DWORD dwKeyLength, DWORD dwKeyOffset)
 
size_t Map_EnumObjects (PCASC_MAP pMap, void **ppvArray)
 
void * Map_FindObject (PCASC_MAP pMap, void *pvKey, PDWORD PtrIndex)
 
bool Map_InsertObject (PCASC_MAP pMap, void *pvNewObject, void *pvKey)
 
bool Map_InsertString (PCASC_MAP pMap, const char *szString, bool bCutExtension)
 
const char * Map_FindString (PCASC_MAP pMap, const char *szString, const char *szStringEnd)
 
void Map_Free (PCASC_MAP pMap)
 

Macro Definition Documentation

#define __CASCLIB_SELF__

Function Documentation

static DWORD CalcHashIndex_Key ( PCASC_MAP  pMap,
void *  pvKey 
)
static
26 {
27  LPBYTE pbKey = (LPBYTE)pvKey;
28  DWORD dwHash = 0x7EEE7EEE;
29 
30  // Construct the hash from the first 8 digits
31  dwHash = (dwHash >> 24) ^ (dwHash << 5) ^ dwHash ^ pbKey[0];
32  dwHash = (dwHash >> 24) ^ (dwHash << 5) ^ dwHash ^ pbKey[1];
33  dwHash = (dwHash >> 24) ^ (dwHash << 5) ^ dwHash ^ pbKey[2];
34  dwHash = (dwHash >> 24) ^ (dwHash << 5) ^ dwHash ^ pbKey[3];
35  dwHash = (dwHash >> 24) ^ (dwHash << 5) ^ dwHash ^ pbKey[4];
36  dwHash = (dwHash >> 24) ^ (dwHash << 5) ^ dwHash ^ pbKey[5];
37  dwHash = (dwHash >> 24) ^ (dwHash << 5) ^ dwHash ^ pbKey[6];
38  dwHash = (dwHash >> 24) ^ (dwHash << 5) ^ dwHash ^ pbKey[7];
39 
40  // Return the hash limited by the table size
41  return (dwHash % pMap->TableSize);
42 }
BYTE * LPBYTE
Definition: CascPort.h:152
size_t TableSize
Definition: Map.h:21
unsigned int DWORD
Definition: CascPort.h:139

+ Here is the caller graph for this function:

static DWORD CalcHashIndex_String ( PCASC_MAP  pMap,
const char *  szString,
const char *  szStringEnd 
)
static
45 {
46  LPBYTE pbKeyEnd = (LPBYTE)szStringEnd;
47  LPBYTE pbKey = (LPBYTE)szString;
48  DWORD dwHash = 0x7EEE7EEE;
49 
50  // Hash the string itself
51  while(pbKey < pbKeyEnd)
52  {
53  dwHash = (dwHash >> 24) ^ (dwHash << 5) ^ dwHash ^ AsciiToUpperTable_BkSlash[pbKey[0]];
54  pbKey++;
55  }
56 
57  // Return the hash limited by the table size
58  return (dwHash % pMap->TableSize);
59 }
unsigned char AsciiToUpperTable_BkSlash[256]
Definition: Common.cpp:42
BYTE * LPBYTE
Definition: CascPort.h:152
size_t TableSize
Definition: Map.h:21
unsigned int DWORD
Definition: CascPort.h:139

+ Here is the caller graph for this function:

static bool CompareObject_Key ( PCASC_MAP  pMap,
void *  pvObject,
void *  pvKey 
)
static
62 {
63  LPBYTE pbObjectKey = (LPBYTE)pvObject + pMap->KeyOffset;
64 
65  return (memcmp(pbObjectKey, pvKey, pMap->KeyLength) == 0);
66 }
size_t KeyOffset
Definition: Map.h:23
BYTE * LPBYTE
Definition: CascPort.h:152
size_t KeyLength
Definition: Map.h:24

+ Here is the caller graph for this function:

static bool CompareObject_String ( PCASC_MAP  pMap,
const char *  szExistingString,
const char *  szString,
const char *  szStringEnd 
)
static
73 {
74  // Keep compiler happy
75  CASCLIB_UNUSED(pMap);
76 
77  // Compare the whole part, case insensitive
78  while(szString < szStringEnd)
79  {
80  if(AsciiToUpperTable_BkSlash[*szExistingString] != AsciiToUpperTable_BkSlash[*szString])
81  return false;
82 
83  szExistingString++;
84  szString++;
85  }
86 
87  return true;
88 }
unsigned char AsciiToUpperTable_BkSlash[256]
Definition: Common.cpp:42
#define CASCLIB_UNUSED(p)
Definition: CascCommon.h:75

+ Here is the caller graph for this function:

PCASC_MAP Map_Create ( DWORD  dwMaxItems,
DWORD  dwKeyLength,
DWORD  dwKeyOffset 
)
94 {
95  PCASC_MAP pMap;
96  size_t cbToAllocate;
97  size_t dwTableSize;
98 
99  // Calculate the size of the table
100  dwTableSize = (dwMaxItems * 3 / 2) | 0x01;
101 
102  // Allocate new map for the objects
103  cbToAllocate = sizeof(CASC_MAP) + (dwTableSize * sizeof(void *));
104  pMap = (PCASC_MAP)CASC_ALLOC(LPBYTE, cbToAllocate);
105  if(pMap != NULL)
106  {
107  memset(pMap, 0, cbToAllocate);
108  pMap->KeyLength = dwKeyLength;
109  pMap->TableSize = dwTableSize;
110  pMap->KeyOffset = dwKeyOffset;
111  }
112 
113  // Return the allocated map
114  return pMap;
115 }
size_t KeyOffset
Definition: Map.h:23
#define CASC_ALLOC(type, count)
Definition: CascCommon.h:302
arena_t NULL
Definition: jemalloc_internal.h:624
struct _CASC_MAP CASC_MAP
BYTE * LPBYTE
Definition: CascPort.h:152
size_t TableSize
Definition: Map.h:21
struct _CASC_MAP * PCASC_MAP
Definition: Map.h:19
size_t KeyLength
Definition: Map.h:24

+ Here is the caller graph for this function:

size_t Map_EnumObjects ( PCASC_MAP  pMap,
void **  ppvArray 
)
118 {
119  size_t nIndex = 0;
120 
121  // Verify pointer to the map
122  if(pMap != NULL && ppvArray != NULL)
123  {
124  // Enumerate all items in main table
125  for(size_t i = 0; i < pMap->TableSize; i++)
126  {
127  // Is that cell valid?
128  if(pMap->HashTable[i] != NULL)
129  {
130  ppvArray[nIndex++] = pMap->HashTable[i];
131  }
132  }
133  }
134 
135  return pMap->ItemCount;
136 }
arena_t NULL
Definition: jemalloc_internal.h:624
size_t TableSize
Definition: Map.h:21
void * HashTable[1]
Definition: Map.h:25
size_t ItemCount
Definition: Map.h:22
void* Map_FindObject ( PCASC_MAP  pMap,
void *  pvKey,
PDWORD  PtrIndex 
)
139 {
140  void * pvObject;
141  DWORD dwHashIndex;
142 
143  // Verify pointer to the map
144  if(pMap != NULL)
145  {
146  // Construct the main index
147  dwHashIndex = CalcHashIndex_Key(pMap, pvKey);
148  while(pMap->HashTable[dwHashIndex] != NULL)
149  {
150  // Get the pointer at that position
151  pvObject = pMap->HashTable[dwHashIndex];
152 
153  // Compare the hash
154  if(CompareObject_Key(pMap, pvObject, pvKey))
155  {
156  if(PtrIndex != NULL)
157  PtrIndex[0] = dwHashIndex;
158  return pvObject;
159  }
160 
161  // Move to the next entry
162  dwHashIndex = (dwHashIndex + 1) % pMap->TableSize;
163  }
164  }
165 
166  // Not found, sorry
167  return NULL;
168 }
arena_t NULL
Definition: jemalloc_internal.h:624
size_t TableSize
Definition: Map.h:21
static DWORD CalcHashIndex_Key(PCASC_MAP pMap, void *pvKey)
Definition: Map.cpp:25
unsigned int DWORD
Definition: CascPort.h:139
static bool CompareObject_Key(PCASC_MAP pMap, void *pvObject, void *pvKey)
Definition: Map.cpp:61
void * HashTable[1]
Definition: Map.h:25

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

const char* Map_FindString ( PCASC_MAP  pMap,
const char *  szString,
const char *  szStringEnd 
)
252 {
253  const char * szExistingString;
254  DWORD dwHashIndex;
255 
256  // Verify pointer to the map
257  if(pMap != NULL)
258  {
259  // Construct the main index
260  dwHashIndex = CalcHashIndex_String(pMap, szString, szStringEnd);
261  while(pMap->HashTable[dwHashIndex] != NULL)
262  {
263  // Get the pointer at that position
264  szExistingString = (const char *)pMap->HashTable[dwHashIndex];
265 
266  // Compare the hash
267  if(CompareObject_String(pMap, szExistingString, szString, szStringEnd))
268  return szExistingString;
269 
270  // Move to the next entry
271  dwHashIndex = (dwHashIndex + 1) % pMap->TableSize;
272  }
273  }
274 
275  // Not found, sorry
276  return NULL;
277 }
arena_t NULL
Definition: jemalloc_internal.h:624
size_t TableSize
Definition: Map.h:21
unsigned int DWORD
Definition: CascPort.h:139
void * HashTable[1]
Definition: Map.h:25
static bool CompareObject_String(PCASC_MAP pMap, const char *szExistingString, const char *szString, const char *szStringEnd)
Definition: Map.cpp:68
static DWORD CalcHashIndex_String(PCASC_MAP pMap, const char *szString, const char *szStringEnd)
Definition: Map.cpp:44

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void Map_Free ( PCASC_MAP  pMap)
280 {
281  if(pMap != NULL)
282  {
283  CASC_FREE(pMap);
284  }
285 }
arena_t NULL
Definition: jemalloc_internal.h:624
#define CASC_FREE(ptr)
Definition: CascCommon.h:303

+ Here is the caller graph for this function:

bool Map_InsertObject ( PCASC_MAP  pMap,
void *  pvNewObject,
void *  pvKey 
)
171 {
172  void * pvExistingObject;
173  DWORD dwHashIndex;
174 
175  // Verify pointer to the map
176  if(pMap != NULL)
177  {
178  // Limit check
179  if((pMap->ItemCount + 1) >= pMap->TableSize)
180  return false;
181 
182  // Construct the hash index
183  dwHashIndex = CalcHashIndex_Key(pMap, pvKey);
184  while(pMap->HashTable[dwHashIndex] != NULL)
185  {
186  // Get the pointer at that position
187  pvExistingObject = pMap->HashTable[dwHashIndex];
188 
189  // Check if hash being inserted conflicts with an existing hash
190  if(CompareObject_Key(pMap, pvExistingObject, pvKey))
191  return false;
192 
193  // Move to the next entry
194  dwHashIndex = (dwHashIndex + 1) % pMap->TableSize;
195  }
196 
197  // Insert at that position
198  pMap->HashTable[dwHashIndex] = pvNewObject;
199  pMap->ItemCount++;
200  return true;
201  }
202 
203  // Failed
204  return false;
205 }
arena_t NULL
Definition: jemalloc_internal.h:624
size_t TableSize
Definition: Map.h:21
static DWORD CalcHashIndex_Key(PCASC_MAP pMap, void *pvKey)
Definition: Map.cpp:25
unsigned int DWORD
Definition: CascPort.h:139
static bool CompareObject_Key(PCASC_MAP pMap, void *pvObject, void *pvKey)
Definition: Map.cpp:61
void * HashTable[1]
Definition: Map.h:25
size_t ItemCount
Definition: Map.h:22

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool Map_InsertString ( PCASC_MAP  pMap,
const char *  szString,
bool  bCutExtension 
)
208 {
209  const char * szExistingString;
210  const char * szStringEnd = NULL;
211  DWORD dwHashIndex;
212 
213  // Verify pointer to the map
214  if(pMap != NULL)
215  {
216  // Limit check
217  if((pMap->ItemCount + 1) >= pMap->TableSize)
218  return false;
219 
220  // Retrieve the length of the string without extension
221  if(bCutExtension)
222  szStringEnd = String_GetExtension(szString);
223  if(szStringEnd == NULL)
224  szStringEnd = szString + strlen(szString);
225 
226  // Construct the hash index
227  dwHashIndex = CalcHashIndex_String(pMap, szString, szStringEnd);
228  while(pMap->HashTable[dwHashIndex] != NULL)
229  {
230  // Get the pointer at that position
231  szExistingString = (const char *)pMap->HashTable[dwHashIndex];
232 
233  // Check if hash being inserted conflicts with an existing hash
234  if(CompareObject_String(pMap, szExistingString, szString, szStringEnd))
235  return false;
236 
237  // Move to the next entry
238  dwHashIndex = (dwHashIndex + 1) % pMap->TableSize;
239  }
240 
241  // Insert at that position
242  pMap->HashTable[dwHashIndex] = (void *)szString;
243  pMap->ItemCount++;
244  return true;
245  }
246 
247  // Failed
248  return false;
249 }
static const char * String_GetExtension(const char *szString)
Definition: Map.cpp:19
arena_t NULL
Definition: jemalloc_internal.h:624
size_t TableSize
Definition: Map.h:21
unsigned int DWORD
Definition: CascPort.h:139
void * HashTable[1]
Definition: Map.h:25
size_t ItemCount
Definition: Map.h:22
static bool CompareObject_String(PCASC_MAP pMap, const char *szExistingString, const char *szString, const char *szStringEnd)
Definition: Map.cpp:68
static DWORD CalcHashIndex_String(PCASC_MAP pMap, const char *szString, const char *szStringEnd)
Definition: Map.cpp:44

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static const char* String_GetExtension ( const char *  szString)
static
20 {
21  const char * szExtension = strrchr(szString, '.');
22  return (szExtension != NULL) ? szExtension + 1 : NULL;
23 }
arena_t NULL
Definition: jemalloc_internal.h:624

+ Here is the caller graph for this function: