| 1 | /* |
| 2 | * Copyright (C) 2000,2001 Ross Combs ([email protected]) |
| 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 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | #define HASHTABLE_INTERNAL_ACCESS |
| 19 | #include "common/setup_before.h" |
| 20 | #include "common/hashtable.h" |
| 21 | |
| 22 | #include "common/eventlog.h" |
| 23 | #include "common/xalloc.h" |
| 24 | #include "common/setup_after.h" |
| 25 | |
| 26 | |
| 27 | namespace pvpgn |
| 28 | { |
| 29 | |
| 30 | static int nodata; /* if data points to this, then the entry was actually deleted */ |
| 31 | |
| 32 | |
| 33 | static t_entry * hashtable_entry_export(t_internentry * entry, t_hashtable const * hashtable, unsigned int row); |
| 34 | |
| 35 | |
| 36 | static t_entry * hashtable_entry_export(t_internentry * entry, t_hashtable const * hashtable, unsigned int row) |
| 37 | { |
| 38 | t_entry * temp; |
| 39 | |
| 40 | if (!entry) |
| 41 | { |
| 42 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL entry"); |
| 43 | return NULL; |
| 44 | } |
| 45 | if (!hashtable) |
| 46 | { |
| 47 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL hashtable"); |
| 48 | return NULL; |
| 49 | } |
| 50 | if (row >= hashtable->num_rows) |
| 51 | { |
| 52 | eventlog(eventlog_level_error, __FUNCTION__, "got bad row {} (max {})", row, hashtable->num_rows - 1); |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | temp = (t_entry*)xmalloc(sizeof(t_entry)); |
| 57 | temp->row = row; |
| 58 | temp->real = entry; |
| 59 | temp->hashtable = hashtable; |
| 60 | |
| 61 | return temp; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | extern t_hashtable * hashtable_create(unsigned int num_rows) |
| 66 | { |
| 67 | t_hashtable * newh; |
| 68 | unsigned int i; |
| 69 | |
| 70 | if (num_rows < 1) |
| 71 | { |
| 72 | eventlog(eventlog_level_error, __FUNCTION__, "num_rows must be at least 1"); |
| 73 | return NULL; |
| 74 | } |
| 75 | |
| 76 | newh = (t_hashtable*)xmalloc(sizeof(t_hashtable)); |
| 77 | newh->rows = (t_internentry**)xmalloc(sizeof(t_internentry *)*num_rows); |
| 78 | newh->num_rows = num_rows; |
| 79 | newh->len = 0; |
| 80 | for (i = 0; i < num_rows; i++) |
| 81 | newh->rows[i] = NULL; |
| 82 | |
| 83 | return newh; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | extern int hashtable_destroy(t_hashtable * hashtable) |
| 88 | { |
| 89 | unsigned int i; |
| 90 | |
| 91 | if (!hashtable) |
| 92 | { |
| 93 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL hashtable"); |
| 94 | return -1; |
| 95 | } |
| 96 | |
| 97 | hashtable_purge(hashtable); |
| 98 | for (i = 0; i < hashtable->num_rows; i++) |
| 99 | if (hashtable->rows[i]) |
| 100 | eventlog(eventlog_level_error, __FUNCTION__, "got non-empty hashtable"); |
| 101 | |
| 102 | xfree(hashtable->rows); |
| 103 | xfree(hashtable); |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | extern int hashtable_purge(t_hashtable * hashtable) |
| 110 | { |
| 111 | unsigned int row; |
| 112 | t_internentry * curr; |
| 113 | t_internentry * head; |
| 114 | t_internentry * next; |
| 115 | t_internentry * * change; |
| 116 | |
| 117 | if (!hashtable) |
| 118 | { |
| 119 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL hashtable"); |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | for (row = 0; row < hashtable->num_rows; row++) |
| 124 | { |
| 125 | head = NULL; |
| 126 | change = NULL; |
| 127 | for (curr = hashtable->rows[row]; curr; curr = next) |
| 128 | { |
| 129 | next = curr->next; |
| 130 | if (curr->data == &nodata) |
| 131 | { |
| 132 | if (change) |
| 133 | *change = next; |
| 134 | xfree(curr); |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | if (!head) |
| 139 | head = curr; |
| 140 | change = &curr->next; |
| 141 | } |
| 142 | } |
| 143 | hashtable->rows[row] = head; |
| 144 | } |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | extern unsigned int hashtable_get_length(t_hashtable const * hashtable) |
| 150 | { |
| 151 | if (!hashtable) |
| 152 | { |
| 153 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL hashtable"); |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | return hashtable->len; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | extern int hashtable_insert_data(t_hashtable * hashtable, void * data, unsigned int hash) |
| 162 | { |
| 163 | unsigned int row; |
| 164 | t_internentry * entry; |
| 165 | |
| 166 | if (!hashtable) |
| 167 | { |
| 168 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL hashtable"); |
| 169 | return -1; |
| 170 | } |
| 171 | |
| 172 | entry = (t_internentry*)xmalloc(sizeof(t_internentry)); |
| 173 | entry->data = data; |
| 174 | |
| 175 | row = hash%hashtable->num_rows; |
| 176 | entry->next = hashtable->rows[row]; |
| 177 | hashtable->rows[row] = entry; |
| 178 | hashtable->len++; |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | |
| 184 | extern t_entry * hashtable_get_entry_by_data(t_hashtable const * hashtable, void const * data, unsigned int hash) |
| 185 | { |
| 186 | unsigned int row; |
| 187 | t_internentry * curr; |
| 188 | |
| 189 | if (!hashtable) |
| 190 | { |
| 191 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL hashtable"); |
| 192 | return NULL; |
| 193 | } |
| 194 | |
| 195 | row = hash%hashtable->num_rows; |
| 196 | for (curr = hashtable->rows[row]; curr; curr = curr->next) |
| 197 | if (curr->data == data) |
| 198 | return hashtable_entry_export(curr, hashtable, row); |
| 199 | |
| 200 | return NULL; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | extern t_entry const * hashtable_get_entry_by_data_const(t_hashtable const * hashtable, void const * data, unsigned int hash) |
| 205 | { |
| 206 | unsigned int row; |
| 207 | t_internentry * curr; |
| 208 | |
| 209 | if (!hashtable) |
| 210 | { |
| 211 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL hashtable"); |
| 212 | return NULL; |
| 213 | } |
| 214 | |
| 215 | row = hash%hashtable->num_rows; |
| 216 | for (curr = hashtable->rows[row]; curr; curr = curr->next) |
| 217 | if (curr->data == data) |
| 218 | return hashtable_entry_export(curr, hashtable, row); |
| 219 | |
| 220 | return NULL; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | extern int hashtable_remove_entry(t_hashtable * hashtable, t_entry * entry) |
| 225 | { |
| 226 | if (!hashtable) |
| 227 | { |
| 228 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL hashtable"); |
| 229 | return -1; |
| 230 | } |
| 231 | if (!entry) |
| 232 | { |
| 233 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL entry"); |
| 234 | return -1; |
| 235 | } |
| 236 | if (!entry->real) |
| 237 | { |
| 238 | eventlog(eventlog_level_error, __FUNCTION__, "entry has NULL real pointer"); |
| 239 | return -1; |
| 240 | } |
| 241 | if (entry->real->data == &nodata) |
| 242 | { |
| 243 | eventlog(eventlog_level_error, __FUNCTION__, "got deleted entry"); |
| 244 | return -1; |
| 245 | } |
| 246 | |
| 247 | entry->real->data = &nodata; |
| 248 | hashtable->len--; |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | |
| 254 | extern int hashtable_remove_data(t_hashtable * hashtable, void const * data, unsigned int hash) |
| 255 | { |
| 256 | t_entry * entry; |
| 257 | int retval; |
| 258 | |
| 259 | if (!hashtable) |
| 260 | { |
| 261 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL hashtable"); |
| 262 | return -1; |
| 263 | } |
| 264 | |
| 265 | if (!(entry = hashtable_get_entry_by_data(hashtable, data, hash))) |
| 266 | return -1; |
| 267 | |
| 268 | retval = hashtable_remove_entry(hashtable, entry); |
| 269 | |
| 270 | hashtable_entry_release(entry); |
| 271 | |
| 272 | return retval; |
| 273 | } |
| 274 | |
| 275 | |
| 276 | extern void * entry_get_data(t_entry const * entry) |
| 277 | { |
| 278 | if (!entry) |
| 279 | { |
| 280 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL entry"); |
| 281 | return NULL; |
| 282 | } |
| 283 | if (!entry->real) |
| 284 | { |
| 285 | eventlog(eventlog_level_error, __FUNCTION__, "entry has NULL real pointer"); |
| 286 | return NULL; |
| 287 | } |
| 288 | if (entry->real->data == &nodata) |
| 289 | { |
| 290 | eventlog(eventlog_level_error, __FUNCTION__, "got deleted entry"); |
| 291 | return NULL; |
| 292 | } |
| 293 | |
| 294 | return entry->real->data; |
| 295 | } |
| 296 | |
| 297 | |
| 298 | extern void * hashtable_get_data_by_pos(t_hashtable const * hashtable, unsigned int pos) |
| 299 | { |
| 300 | t_internentry const * curr; |
| 301 | unsigned int row; |
| 302 | unsigned int len; |
| 303 | |
| 304 | if (!hashtable) |
| 305 | { |
| 306 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL hashtable"); |
| 307 | return NULL; |
| 308 | } |
| 309 | |
| 310 | len = 0; |
| 311 | row = 0; |
| 312 | curr = NULL; |
| 313 | for (;;) |
| 314 | { |
| 315 | if (!curr) |
| 316 | { |
| 317 | if (row >= hashtable->num_rows) |
| 318 | break; |
| 319 | curr = hashtable->rows[row++]; |
| 320 | continue; |
| 321 | } |
| 322 | if (curr->data != &nodata && len++ == pos) |
| 323 | return curr->data; |
| 324 | curr = curr->next; |
| 325 | } |
| 326 | |
| 327 | eventlog(eventlog_level_error, __FUNCTION__, "requested position {} but len={}", pos, len); |
| 328 | return NULL; |
| 329 | } |
| 330 | |
| 331 | |
| 332 | #ifdef HASHTABLE_DEBUG |
| 333 | extern t_entry * hashtable_get_first_real(t_hashtable const * hashtable, char const * fn, unsigned int ln) |
| 334 | #else |
| 335 | extern t_entry * hashtable_get_first(t_hashtable const * hashtable) |
| 336 | #endif |
| 337 | { |
| 338 | unsigned int row; |
| 339 | t_internentry * curr; |
| 340 | |
| 341 | if (!hashtable) |
| 342 | { |
| 343 | #ifdef HASHTABLE_DEBUG |
| 344 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL hashtable from {}:{}", fn, ln); |
| 345 | #else |
| 346 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL hashtable"); |
| 347 | #endif |
| 348 | return NULL; |
| 349 | } |
| 350 | |
| 351 | for (row = 0; row < hashtable->num_rows; row++) |
| 352 | for (curr = hashtable->rows[row]; curr; curr = curr->next) |
| 353 | if (curr->data != &nodata) |
| 354 | return hashtable_entry_export(curr, hashtable, row); |
| 355 | |
| 356 | return NULL; |
| 357 | } |
| 358 | |
| 359 | |
| 360 | extern t_entry * entry_get_next(t_entry * entry) |
| 361 | { |
| 362 | t_hashtable const * hashtable; |
| 363 | unsigned int row; |
| 364 | t_internentry * curr; |
| 365 | |
| 366 | if (!entry) |
| 367 | { |
| 368 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL entry"); |
| 369 | return NULL; |
| 370 | } |
| 371 | |
| 372 | hashtable = entry->hashtable; |
| 373 | |
| 374 | for (curr = entry->real->next; curr; curr = curr->next) |
| 375 | if (curr->data != &nodata) |
| 376 | { |
| 377 | entry->real = curr; |
| 378 | return entry; |
| 379 | } |
| 380 | |
| 381 | for (row = entry->row + 1; row < hashtable->num_rows; row++) |
| 382 | for (curr = hashtable->rows[row]; curr; curr = curr->next) |
| 383 | if (curr->data != &nodata) |
| 384 | { |
| 385 | entry->real = curr; |
| 386 | entry->row = row; |
| 387 | return entry; |
| 388 | } |
| 389 | |
| 390 | hashtable_entry_release(entry); |
| 391 | return NULL; |
| 392 | } |
| 393 | |
| 394 | |
| 395 | #ifdef HASHTABLE_DEBUG |
| 396 | extern t_entry * hashtable_get_first_matching_real(t_hashtable const * hashtable, unsigned int hash, char const * fn, unsigned int ln) |
| 397 | #else |
| 398 | extern t_entry * hashtable_get_first_matching(t_hashtable const * hashtable, unsigned int hash) |
| 399 | #endif |
| 400 | { |
| 401 | unsigned int row; |
| 402 | t_internentry * curr; |
| 403 | |
| 404 | if (!hashtable) |
| 405 | { |
| 406 | #ifdef HASHTABLE_DEBUG |
| 407 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL hashtable from {}:{}", fn, ln); |
| 408 | #else |
| 409 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL hashtable"); |
| 410 | #endif |
| 411 | return NULL; |
| 412 | } |
| 413 | |
| 414 | row = hash%hashtable->num_rows; |
| 415 | for (curr = hashtable->rows[row]; curr; curr = curr->next) |
| 416 | if (curr->data != &nodata) |
| 417 | return hashtable_entry_export(curr, hashtable, row); |
| 418 | |
| 419 | return NULL; |
| 420 | } |
| 421 | |
| 422 | |
| 423 | extern t_entry * entry_get_next_matching(t_entry * entry) |
| 424 | { |
| 425 | t_internentry * curr; |
| 426 | |
| 427 | if (!entry) |
| 428 | { |
| 429 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL entry"); |
| 430 | return NULL; |
| 431 | } |
| 432 | |
| 433 | for (curr = entry->real->next; curr; curr = curr->next) |
| 434 | if (curr->data != &nodata) |
| 435 | { |
| 436 | entry->real = curr; |
| 437 | return entry; |
| 438 | } |
| 439 | |
| 440 | hashtable_entry_release(entry); |
| 441 | return NULL; |
| 442 | } |
| 443 | |
| 444 | |
| 445 | extern int hashtable_entry_release(t_entry * entry) |
| 446 | { |
| 447 | if (!entry) |
| 448 | { |
| 449 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL entry"); |
| 450 | return -1; |
| 451 | } |
| 452 | if (!entry->hashtable) |
| 453 | { |
| 454 | eventlog(eventlog_level_error, __FUNCTION__, "got entry with NULL hashtable"); |
| 455 | return -1; |
| 456 | } |
| 457 | if (!entry->real) |
| 458 | { |
| 459 | eventlog(eventlog_level_error, __FUNCTION__, "got entry with NULL real pointer"); |
| 460 | return -1; |
| 461 | } |
| 462 | if (entry->row >= entry->hashtable->num_rows) |
| 463 | { |
| 464 | eventlog(eventlog_level_error, __FUNCTION__, "entry has bad row {} (max {})", entry->row, entry->hashtable->num_rows - 1); |
| 465 | return -1; |
| 466 | } |
| 467 | |
| 468 | xfree(entry); |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | } |