A simple hash table implementation for a JSON structure.
This module defines a hash table type with methods for initialization, insertion, lookup, and deletion. It uses the DJB hash algorithm for hashing string keys and supports optional case-insensitive key comparison.
It can be used independently, or in a json_file object to speed up lookups of the root-level members of the JSON file.
This is a linked list to avoid collisions in the hash table buckets.
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| character(kind=CK, len=:), | private, | allocatable | :: | key |
string key. if |
||
| type(hash_node_t), | private, | pointer | :: | next | => | null() |
pointer to the next node in the linked list |
| type(json_value), | private, | pointer | :: | value | => | null() |
associated value in the hash table |
Hash table type.
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| type(hash_node_t), | private, | dimension(:), allocatable | :: | buckets |
array of buckets for the hash table.
Each bucket is a linked list of |
||
| integer(kind=IK), | private | :: | capacity | = | 16_IK |
Number of buckets in the table |
|
| logical(kind=LK), | private | :: | case_sensitive_keys | = | .true. |
Case-sensitive key comparison |
|
| type(json_core), | private | :: | json |
The instance of the json_core factory used for this table. |
|||
| real(kind=RK), | private | :: | load_factor | = | 0.75_RK |
Resize threshold that determines when the hash table should automatically resize to maintain optimal performance. |
|
| integer(kind=IK), | private | :: | size | = | 0_IK |
Number of elements in the table |
|
| logical(kind=LK), | private | :: | trailing_spaces_significant | = | .false. |
Whether trailing spaces in keys are significant for comparison |
| final :: hash_table_finalize |
| procedure, public :: create => hash_table_create_from_value | |
| procedure, public :: destroy => hash_table_destroy | |
| procedure, public :: get => hash_table_get | |
| procedure, private :: hash => hash_table_hash | |
| procedure, private :: insert => hash_table_insert | |
| procedure, private :: keys_equal | |
| procedure, private :: preprocess_key | |
| procedure, private :: resize => hash_table_resize |
DJB hash algorithm for a string.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(json_hash_table), | intent(in) | :: | me | |||
| character(kind=CK, len=*), | intent(in) | :: | key |
Compare two keys for equality, taking into account the setting for trailing spaces.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(json_hash_table), | intent(in) | :: | me | |||
| character(kind=CK, len=*), | intent(in) | :: | key1 | |||
| character(kind=CK, len=*), | intent(in) | :: | key2 |
Preprocess key for saving in the hash table, as well as for insertion and lookup (handle trim and case-sensitivity).
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(json_hash_table), | intent(in) | :: | me | |||
| character(kind=CK, len=*), | intent(in) | :: | key |
the key to preprocess |
the preprocessed key to use for storage and comparison in the hash table.
Create a hash table from the children of an existing JSON object. The keys are the object member names and the values are pointers to the associated JSON values.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(json_hash_table), | intent(out) | :: | me | |||
| type(json_core), | intent(in) | :: | json |
the JSON core factory to use for this hash table. a copy of this will be made and stored in the table. |
||
| type(json_value), | pointer | :: | p |
the JSON value whose children will be used to populate the hash table. This must be a JSON object. |
||
| logical(kind=LK), | intent(out) | :: | status_ok |
true if no problems. |
||
| integer(kind=IK), | intent(in), | optional | :: | initial_capacity |
initial capacity of the hash table (number of buckets). If not provided, it will be set based on the number of members divided by the load factor to minimize the number of resizes needed as members are inserted |
Destroy hash table and free all memory
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(json_hash_table), | intent(inout) | :: | me |
Finalizer - automatically clean up when hash table goes out of scope.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(json_hash_table), | intent(inout) | :: | me |
Get value for a given key
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(json_hash_table), | intent(in) | :: | me | |||
| character(kind=CK, len=*), | intent(in) | :: | key |
key to look up. |
||
| type(json_value), | pointer | :: | value |
pointer to the value associated with the key. This will be set to null() if the key is not found. |
||
| logical(kind=LK), | intent(out), | optional | :: | found |
flag indicating whether the key was found in the table (true) or not (false). |
Insert or update a key-value pair
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(json_hash_table), | intent(inout) | :: | me | |||
| character(kind=CK, len=*), | intent(in) | :: | key |
key to insert. This will be preprocessed (trimmed and/or lowercased) according to the settings of the hash table. |
||
| type(json_value), | pointer | :: | value |
value to associate with the key. This is a pointer to a JSON value. |
Resize the hash table (internal method)
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(json_hash_table), | intent(inout) | :: | me | |||
| integer(kind=IK), | intent(in) | :: | new_capacity |