| 1 |
// |
| 2 |
// Copyright(C) 2022 by Ryan Krafnick |
| 3 |
// |
| 4 |
// This program is free software; you can redistribute it and/or |
| 5 |
// modify it under the terms of the GNU General Public License |
| 6 |
// as published by the Free Software Foundation; either version 2 |
| 7 |
// of the License, or (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// DESCRIPTION: |
| 15 |
// DSDA Dehacked Hash |
| 16 |
// |
| 17 |
|
| 18 |
#include "z_zone.h" |
| 19 |
|
| 20 |
#include "deh_hash.h" |
| 21 |
|
| 22 |
int dsda_FindDehIndex(int index, deh_index_hash_t* hash) { |
| 23 |
deh_index_entry_t* entry; |
| 24 |
|
| 25 |
// this index is not in the hash |
| 26 |
if (index < hash->start_index) |
| 27 |
return index; |
| 28 |
|
| 29 |
entry = &hash->table[index % DEH_INDEX_HASH_SIZE]; |
| 30 |
|
| 31 |
while (entry->next && entry->index_in != index) |
| 32 |
entry = entry->next; |
| 33 |
|
| 34 |
if (entry->index_in != index) |
| 35 |
return DEH_INDEX_NOT_FOUND; |
| 36 |
|
| 37 |
return entry->index_out; |
| 38 |
} |
| 39 |
|
| 40 |
int dsda_GetDehIndex(int index, deh_index_hash_t* hash) { |
| 41 |
deh_index_entry_t* entry; |
| 42 |
|
| 43 |
// this index is not in the hash |
| 44 |
if (index < hash->start_index) |
| 45 |
return index; |
| 46 |
|
| 47 |
entry = &hash->table[index % DEH_INDEX_HASH_SIZE]; |
| 48 |
|
| 49 |
while (entry->next && entry->index_in != index) |
| 50 |
entry = entry->next; |
| 51 |
|
| 52 |
if (entry->index_in != index) { |
| 53 |
entry->next = Z_Calloc(1, sizeof(*entry)); |
| 54 |
entry = entry->next; |
| 55 |
entry->index_in = index; |
| 56 |
entry->index_out = hash->end_index; |
| 57 |
++hash->end_index; |
| 58 |
} |
| 59 |
|
| 60 |
return entry->index_out; |
| 61 |
} |