| 1 |
// |
| 2 |
// Copyright(C) 2023 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 ID List |
| 16 |
// |
| 17 |
|
| 18 |
#include "z_zone.h" |
| 19 |
|
| 20 |
#include "id_list.h" |
| 21 |
|
| 22 |
typedef struct { |
| 23 |
int id; |
| 24 |
int size; |
| 25 |
int allocated_size; |
| 26 |
int* data; |
| 27 |
} id_list_t; |
| 28 |
|
| 29 |
typedef struct { |
| 30 |
int size; |
| 31 |
id_list_t* lists; |
| 32 |
} id_index_t; |
| 33 |
|
| 34 |
typedef struct { |
| 35 |
int size; |
| 36 |
id_index_t* data; |
| 37 |
} id_hash_t; |
| 38 |
|
| 39 |
static id_hash_t line_id_hash; |
| 40 |
static id_hash_t sector_id_hash; |
| 41 |
|
| 42 |
static id_list_t* dsda_NewListForIndex(id_index_t* index, int id) { |
| 43 |
id_list_t* new_list; |
| 44 |
|
| 45 |
index->lists = Z_ReallocLevel(index->lists, sizeof(*index->lists) * (index->size + 1)); |
| 46 |
|
| 47 |
new_list = &index->lists[index->size++]; |
| 48 |
new_list->id = id; |
| 49 |
new_list->size = 0; |
| 50 |
new_list->allocated_size = 1; |
| 51 |
new_list->data = Z_CallocLevel(new_list->allocated_size, sizeof(*new_list->data)); |
| 52 |
new_list->data[0] = -1; |
| 53 |
|
| 54 |
return new_list; |
| 55 |
} |
| 56 |
|
| 57 |
static id_list_t* dsda_GetIDList(id_hash_t* hash, int id) { |
| 58 |
int i; |
| 59 |
id_index_t* index; |
| 60 |
|
| 61 |
index = &hash->data[(unsigned int)id % hash->size]; |
| 62 |
|
| 63 |
for (i = 0; i < index->size; ++i) |
| 64 |
if (index->lists[i].id == id) |
| 65 |
return &index->lists[i]; |
| 66 |
|
| 67 |
return dsda_NewListForIndex(index, id); |
| 68 |
} |
| 69 |
|
| 70 |
static void dsda_AddToIDHash(id_hash_t* hash, int id, int value) { |
| 71 |
id_list_t* list; |
| 72 |
|
| 73 |
list = dsda_GetIDList(hash, id); |
| 74 |
if (list->allocated_size < list->size + 2) { |
| 75 |
while (list->allocated_size < list->size + 2) |
| 76 |
list->allocated_size *= 2; |
| 77 |
list->data = Z_ReallocLevel(list->data, list->allocated_size * sizeof(*list->data)); |
| 78 |
} |
| 79 |
list->data[list->size++] = value; |
| 80 |
list->data[list->size] = -1; |
| 81 |
} |
| 82 |
|
| 83 |
void dsda_AddLineID(int id, int value) { |
| 84 |
dsda_AddToIDHash(&line_id_hash, id, value); |
| 85 |
} |
| 86 |
|
| 87 |
void dsda_AddSectorID(int id, int value) { |
| 88 |
dsda_AddToIDHash(§or_id_hash, id, value); |
| 89 |
} |
| 90 |
|
| 91 |
static int empty_list[] = { -1 }; |
| 92 |
static int missing_id_list[] = { -1, -1 }; |
| 93 |
|
| 94 |
const int* dsda_FindLinesFromID(int id) { |
| 95 |
return dsda_GetIDList(&line_id_hash, id)->data; |
| 96 |
} |
| 97 |
|
| 98 |
const int* dsda_FindSectorsFromID(int id) { |
| 99 |
return dsda_GetIDList(§or_id_hash, id)->data; |
| 100 |
} |
| 101 |
|
| 102 |
const int* dsda_FindSectorsFromIDOrLine(int id, const line_t* line) |
| 103 |
{ |
| 104 |
if (id == 0) { |
| 105 |
if (!line || !line->backsector) |
| 106 |
return empty_list; |
| 107 |
|
| 108 |
missing_id_list[0] = line->backsector->iSectorID; |
| 109 |
return missing_id_list; |
| 110 |
} |
| 111 |
else |
| 112 |
return dsda_FindSectorsFromID(id); |
| 113 |
} |
| 114 |
|
| 115 |
const int hash_factor = 10; |
| 116 |
|
| 117 |
void dsda_ResetLineIDList(int size) { |
| 118 |
line_id_hash.size = (size > hash_factor ? size / hash_factor : size); |
| 119 |
line_id_hash.data = Z_CallocLevel(line_id_hash.size, sizeof(*line_id_hash.data)); |
| 120 |
} |
| 121 |
|
| 122 |
void dsda_ResetSectorIDList(int size) { |
| 123 |
sector_id_hash.size = (size > hash_factor ? size / hash_factor : size); |
| 124 |
sector_id_hash.data = Z_CallocLevel(sector_id_hash.size, sizeof(*sector_id_hash.data)); |
| 125 |
} |