json_hash_module Module

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.


Uses

  • module~~json_hash_module~~UsesGraph module~json_hash_module json_hash_module module~json_kinds json_kinds module~json_hash_module->module~json_kinds module~json_parameters json_parameters module~json_hash_module->module~json_parameters module~json_string_utilities json_string_utilities module~json_hash_module->module~json_string_utilities module~json_value_module json_value_module module~json_hash_module->module~json_value_module iso_fortran_env iso_fortran_env module~json_kinds->iso_fortran_env module~json_parameters->module~json_kinds module~json_string_utilities->module~json_kinds module~json_string_utilities->module~json_parameters ieee_arithmetic ieee_arithmetic module~json_string_utilities->ieee_arithmetic iso_c_binding iso_c_binding module~json_string_utilities->iso_c_binding module~json_value_module->module~json_kinds module~json_value_module->module~json_parameters module~json_value_module->module~json_string_utilities module~json_value_module->ieee_arithmetic module~json_value_module->iso_fortran_env

Used by

  • module~~json_hash_module~~UsedByGraph module~json_hash_module json_hash_module module~json_file_module json_file_module module~json_file_module->module~json_hash_module module~json_module json_module module~json_module->module~json_hash_module module~json_module->module~json_file_module

Derived Types

type, private ::  hash_node_t

This is a linked list to avoid collisions in the hash table buckets.

Components

Type Visibility Attributes Name Initial
character(kind=CK, len=:), private, allocatable :: key

string key. if case_sensitive_keys=False then this will always be lowercase.

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

type, public ::  json_hash_table

Hash table type.

Read more…

Components

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 hash_node_t to handle collisions.

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

Finalizations Procedures

final :: hash_table_finalize

Type-Bound Procedures

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

Functions

private function hash_table_hash(me, key) result(hash_value)

DJB hash algorithm for a string.

Read more…

Arguments

Type IntentOptional Attributes Name
class(json_hash_table), intent(in) :: me
character(kind=CK, len=*), intent(in) :: key

Return Value integer(kind=IK)

private function keys_equal(me, key1, key2) result(equal)

Compare two keys for equality, taking into account the setting for trailing spaces.

Read more…

Arguments

Type IntentOptional Attributes Name
class(json_hash_table), intent(in) :: me
character(kind=CK, len=*), intent(in) :: key1
character(kind=CK, len=*), intent(in) :: key2

Return Value logical(kind=LK)

private function preprocess_key(me, key) result(proc_key)

Preprocess key for saving in the hash table, as well as for insertion and lookup (handle trim and case-sensitivity).

Arguments

Type IntentOptional Attributes Name
class(json_hash_table), intent(in) :: me
character(kind=CK, len=*), intent(in) :: key

the key to preprocess

Return Value character(kind=CK, len=:), allocatable

the preprocessed key to use for storage and comparison in the hash table.


Subroutines

private subroutine hash_table_create_from_value(me, json, p, status_ok, initial_capacity)

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.

Arguments

Type IntentOptional 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

private subroutine hash_table_destroy(me)

Destroy hash table and free all memory

Arguments

Type IntentOptional Attributes Name
class(json_hash_table), intent(inout) :: me

private subroutine hash_table_finalize(me)

Finalizer - automatically clean up when hash table goes out of scope.

Arguments

Type IntentOptional Attributes Name
type(json_hash_table), intent(inout) :: me

private subroutine hash_table_get(me, key, value, found)

Get value for a given key

Arguments

Type IntentOptional 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).

private subroutine hash_table_insert(me, key, value)

Insert or update a key-value pair

Arguments

Type IntentOptional 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.

private subroutine hash_table_resize(me, new_capacity)

Resize the hash table (internal method)

Arguments

Type IntentOptional Attributes Name
class(json_hash_table), intent(inout) :: me
integer(kind=IK), intent(in) :: new_capacity