Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file hash_table_internal.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 5 | * @brief libyang hash table internal header |
| 6 | * |
| 7 | * Copyright (c) 2015 - 2023 CESNET, z.s.p.o. |
| 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
| 15 | |
| 16 | #ifndef LY_HASH_TABLE_INTERNAL_H_ |
| 17 | #define LY_HASH_TABLE_INTERNAL_H_ |
| 18 | |
| 19 | #include <pthread.h> |
| 20 | #include <stddef.h> |
| 21 | #include <stdint.h> |
| 22 | |
| 23 | #include "compat.h" |
| 24 | #include "hash_table.h" |
| 25 | |
| 26 | /** reference value for 100% */ |
| 27 | #define LYHT_HUNDRED_PERCENTAGE 100 |
| 28 | |
| 29 | /** when the table is at least this much percent full, it is enlarged (double the size) */ |
| 30 | #define LYHT_ENLARGE_PERCENTAGE 75 |
| 31 | |
| 32 | /** only once the table is this much percent full, enable shrinking */ |
| 33 | #define LYHT_FIRST_SHRINK_PERCENTAGE 50 |
| 34 | |
| 35 | /** when the table is less than this much percent full, it is shrunk (half the size) */ |
| 36 | #define LYHT_SHRINK_PERCENTAGE 25 |
| 37 | |
| 38 | /** when the table has less than this much percent empty records, it is rehashed to get rid of all the invalid records */ |
| 39 | #define LYHT_REHASH_PERCENTAGE 2 |
| 40 | |
| 41 | /** never shrink beyond this size */ |
| 42 | #define LYHT_MIN_SIZE 8 |
| 43 | |
| 44 | /** |
| 45 | * @brief Generic hash table record. |
| 46 | */ |
| 47 | struct ly_ht_rec { |
| 48 | uint32_t hash; /* hash of the value */ |
| 49 | int32_t hits; /* collision/overflow value count - 1 (a filled entry has 1 hit, |
| 50 | * special value -1 means a deleted record) */ |
| 51 | unsigned char val[1]; /* arbitrary-size value */ |
| 52 | } _PACKED; |
| 53 | |
| 54 | /** |
| 55 | * @brief (Very) generic hash table. |
| 56 | * |
| 57 | * Hash table with open addressing collision resolution and |
| 58 | * linear probing of interval 1 (next free record is used). |
| 59 | * Removal is lazy (removed records are only marked), but |
| 60 | * if possible, they are fully emptied. |
| 61 | */ |
| 62 | struct ly_ht { |
| 63 | uint32_t used; /* number of values stored in the hash table (filled records) */ |
| 64 | uint32_t size; /* always holds 2^x == size (is power of 2), actually number of records allocated */ |
| 65 | uint32_t invalid; /* number of invalid records (deleted) */ |
| 66 | lyht_value_equal_cb val_equal; /* callback for testing value equivalence */ |
| 67 | void *cb_data; /* user data callback arbitrary value */ |
| 68 | uint16_t resize; /* 0 - resizing is disabled, * |
| 69 | * 1 - enlarging is enabled, * |
| 70 | * 2 - both shrinking and enlarging is enabled */ |
| 71 | uint16_t rec_size; /* real size (in bytes) of one record for accessing recs array */ |
| 72 | unsigned char *recs; /* pointer to the hash table itself (array of struct ht_rec) */ |
| 73 | }; |
| 74 | |
| 75 | /** |
| 76 | * @brief Dictionary hash table record. |
| 77 | */ |
| 78 | struct ly_dict_rec { |
| 79 | char *value; /**< stored string */ |
| 80 | uint32_t refcount; /**< reference count of the string */ |
| 81 | }; |
| 82 | |
| 83 | /** |
| 84 | * @brief Dictionary for storing repeated strings. |
| 85 | */ |
| 86 | struct ly_dict { |
| 87 | struct ly_ht *hash_tab; |
| 88 | pthread_mutex_t lock; |
| 89 | }; |
| 90 | |
| 91 | /** |
| 92 | * @brief Initiate content (non-zero values) of the dictionary |
| 93 | * |
| 94 | * @param[in] dict Dictionary table to initiate |
| 95 | */ |
| 96 | void lydict_init(struct ly_dict *dict); |
| 97 | |
| 98 | /** |
| 99 | * @brief Cleanup the dictionary content |
| 100 | * |
| 101 | * @param[in] dict Dictionary table to cleanup |
| 102 | */ |
| 103 | void lydict_clean(struct ly_dict *dict); |
| 104 | |
| 105 | #endif /* LY_HASH_TABLE_INTERNAL_H_ */ |