aboutsummaryrefslogtreecommitdiff
path: root/dep/CascLib/src/common/Map.cpp
blob: aa1e2a2ada1da48d91f7e3bac7db4b60b0e3550a (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*****************************************************************************/
/* Map.cpp                                Copyright (c) Ladislav Zezula 2014 */
/*---------------------------------------------------------------------------*/
/* Implementation of map for CascLib                                         */
/*---------------------------------------------------------------------------*/
/*   Date    Ver   Who  Comment                                              */
/* --------  ----  ---  -------                                              */
/* 10.06.14  1.00  Lad  The first version of Map.cpp                         */
/*****************************************************************************/

#define __CASCLIB_SELF__
#include "../CascLib.h"
#include "../CascCommon.h"

//-----------------------------------------------------------------------------
// Local functions

// Returns the extension, right after "."
static const char * String_GetExtension(const char * szString)
{
    const char * szExtension = strrchr(szString, '.');
    return (szExtension != NULL) ? szExtension + 1 : NULL;
}

static DWORD CalcHashIndex_Key(PCASC_MAP pMap, void * pvKey)
{
    LPBYTE pbKey = (LPBYTE)pvKey;
    DWORD dwHash = 0x7EEE7EEE;

    // Construct the hash from the first 8 digits
    dwHash = (dwHash >> 24) ^ (dwHash << 5) ^ dwHash ^ pbKey[0];
    dwHash = (dwHash >> 24) ^ (dwHash << 5) ^ dwHash ^ pbKey[1];
    dwHash = (dwHash >> 24) ^ (dwHash << 5) ^ dwHash ^ pbKey[2];
    dwHash = (dwHash >> 24) ^ (dwHash << 5) ^ dwHash ^ pbKey[3];
    dwHash = (dwHash >> 24) ^ (dwHash << 5) ^ dwHash ^ pbKey[4];
    dwHash = (dwHash >> 24) ^ (dwHash << 5) ^ dwHash ^ pbKey[5];
    dwHash = (dwHash >> 24) ^ (dwHash << 5) ^ dwHash ^ pbKey[6];
    dwHash = (dwHash >> 24) ^ (dwHash << 5) ^ dwHash ^ pbKey[7];

    // Return the hash limited by the table size
    return (dwHash % pMap->TableSize);
}

static DWORD CalcHashIndex_String(PCASC_MAP pMap, const char * szString, const char * szStringEnd)
{
    LPBYTE pbKeyEnd = (LPBYTE)szStringEnd;
    LPBYTE pbKey = (LPBYTE)szString;
    DWORD dwHash = 0x7EEE7EEE;

    // Hash the string itself
    while(pbKey < pbKeyEnd)
    {
        dwHash = (dwHash >> 24) ^ (dwHash << 5) ^ dwHash ^ AsciiToUpperTable_BkSlash[pbKey[0]];
        pbKey++;
    }

    // Return the hash limited by the table size
    return (dwHash % pMap->TableSize);
}

static bool CompareObject_Key(PCASC_MAP pMap, void * pvObject, void * pvKey)
{
    LPBYTE pbObjectKey = (LPBYTE)pvObject + pMap->KeyOffset;

    return (memcmp(pbObjectKey, pvKey, pMap->KeyLength) == 0);
}

static bool CompareObject_String(
    PCASC_MAP pMap,
    const char * szExistingString,
    const char * szString,
    const char * szStringEnd)
{
    // Keep compiler happy
    CASCLIB_UNUSED(pMap);

    // Compare the whole part, case insensitive
    while(szString < szStringEnd)
    {
        if(AsciiToUpperTable_BkSlash[*szExistingString] != AsciiToUpperTable_BkSlash[*szString])
            return false;

        szExistingString++;
        szString++;
    }

    return true;
}

//-----------------------------------------------------------------------------
// Public functions

PCASC_MAP Map_Create(DWORD dwMaxItems, DWORD dwKeyLength, DWORD dwKeyOffset)
{
    PCASC_MAP pMap;
    size_t cbToAllocate;
    size_t dwTableSize;

    // Calculate the size of the table
    dwTableSize = (dwMaxItems * 3 / 2) | 0x01;

    // Allocate new map for the objects
    cbToAllocate = sizeof(CASC_MAP) + (dwTableSize * sizeof(void *));
    pMap = (PCASC_MAP)CASC_ALLOC(LPBYTE, cbToAllocate);
    if(pMap != NULL)
    {
        memset(pMap, 0, cbToAllocate);
        pMap->KeyLength = dwKeyLength;
        pMap->TableSize = dwTableSize;
        pMap->KeyOffset = dwKeyOffset;
    }

    // Return the allocated map
    return pMap;
}

size_t Map_EnumObjects(PCASC_MAP pMap, void **ppvArray)
{
    size_t nIndex = 0;

    // Verify pointer to the map
    if(pMap != NULL && ppvArray != NULL)
    {
        // Enumerate all items in main table
        for(size_t i = 0; i < pMap->TableSize; i++)
        {
            // Is that cell valid?
            if(pMap->HashTable[i] != NULL)
            {
                ppvArray[nIndex++] = pMap->HashTable[i];
            }
        }

        return pMap->ItemCount;
    }

    return 0;
}

void * Map_FindObject(PCASC_MAP pMap, void * pvKey, PDWORD PtrIndex)
{
    void * pvObject;
    DWORD dwHashIndex;

    // Verify pointer to the map
    if(pMap != NULL)
    {
        // Construct the main index
        dwHashIndex = CalcHashIndex_Key(pMap, pvKey);
        while(pMap->HashTable[dwHashIndex] != NULL)
        {
            // Get the pointer at that position
            pvObject = pMap->HashTable[dwHashIndex];

            // Compare the hash
            if(CompareObject_Key(pMap, pvObject, pvKey))
            {
                if(PtrIndex != NULL)
                    PtrIndex[0] = dwHashIndex;
                return pvObject;
            }

            // Move to the next entry
            dwHashIndex = (dwHashIndex + 1) % pMap->TableSize;
        }
    }

    // Not found, sorry
    return NULL;
}

bool Map_InsertObject(PCASC_MAP pMap, void * pvNewObject, void * pvKey)
{
    void * pvExistingObject;
    DWORD dwHashIndex;

    // Verify pointer to the map
    if(pMap != NULL)
    {
        // Limit check
        if((pMap->ItemCount + 1) >= pMap->TableSize)
            return false;

        // Construct the hash index
        dwHashIndex = CalcHashIndex_Key(pMap, pvKey);
        while(pMap->HashTable[dwHashIndex] != NULL)
        {
            // Get the pointer at that position
            pvExistingObject = pMap->HashTable[dwHashIndex];

            // Check if hash being inserted conflicts with an existing hash
            if(CompareObject_Key(pMap, pvExistingObject, pvKey))
                return false;

            // Move to the next entry
            dwHashIndex = (dwHashIndex + 1) % pMap->TableSize;
        }

        // Insert at that position
        pMap->HashTable[dwHashIndex] = pvNewObject;
        pMap->ItemCount++;
        return true;
    }

    // Failed
    return false;
}

bool Map_InsertString(PCASC_MAP pMap, const char * szString, bool bCutExtension)
{
    const char * szExistingString;
    const char * szStringEnd = NULL;
    DWORD dwHashIndex;

    // Verify pointer to the map
    if(pMap != NULL)
    {
        // Limit check
        if((pMap->ItemCount + 1) >= pMap->TableSize)
            return false;

        // Retrieve the length of the string without extension
        if(bCutExtension)
            szStringEnd = String_GetExtension(szString);
        if(szStringEnd == NULL)
            szStringEnd = szString + strlen(szString);

        // Construct the hash index
        dwHashIndex = CalcHashIndex_String(pMap, szString, szStringEnd);
        while(pMap->HashTable[dwHashIndex] != NULL)
        {
            // Get the pointer at that position
            szExistingString = (const char *)pMap->HashTable[dwHashIndex];

            // Check if hash being inserted conflicts with an existing hash
            if(CompareObject_String(pMap, szExistingString, szString, szStringEnd))
                return false;

            // Move to the next entry
            dwHashIndex = (dwHashIndex + 1) % pMap->TableSize;
        }

        // Insert at that position
        pMap->HashTable[dwHashIndex] = (void *)szString;
        pMap->ItemCount++;
        return true;
    }

    // Failed
    return false;
}

const char * Map_FindString(PCASC_MAP pMap, const char * szString, const char * szStringEnd)
{
    const char * szExistingString;
    DWORD dwHashIndex;

    // Verify pointer to the map
    if(pMap != NULL)
    {
        // Construct the main index
        dwHashIndex = CalcHashIndex_String(pMap, szString, szStringEnd);
        while(pMap->HashTable[dwHashIndex] != NULL)
        {
            // Get the pointer at that position
            szExistingString = (const char *)pMap->HashTable[dwHashIndex];

            // Compare the hash
            if(CompareObject_String(pMap, szExistingString, szString, szStringEnd))
                return szExistingString;

            // Move to the next entry
            dwHashIndex = (dwHashIndex + 1) % pMap->TableSize;
        }
    }

    // Not found, sorry
    return NULL;
}

void Map_Free(PCASC_MAP pMap)
{
    if(pMap != NULL)
    {
        CASC_FREE(pMap);
    }
}