OpenTTD Source: Hash Struct Reference

OpenTTD Source 12.2

Public Member Functions | Data Fields | Protected Member Functions
Hash Struct Reference

## Public Member Functions
void | Init (Hash_HashProc *hash, uint num_buckets)
| Builds a new hash in an existing struct. More...
void * | Get (uint key1, uint key2) const
| Gets the value associated with the given key pair, or nullptr when it is not present.
void * | Set (uint key1, uint key2, void *value)
| Sets the value associated with the given key pair to the given value. More...
void * | DeleteValue (uint key1, uint key2)
| Deletes the value with the specified key pair from the hash and returns that value. More...
void | Clear (bool free_values)
| Cleans the hash, but keeps the memory allocated.
void | Delete (bool free_values)
| Deletes the hash and cleans up. More...
uint | GetSize () const
| Gets the current size of the hash.

## Data Fields
Hash_HashProc * | hash
uint | size
uint | num_buckets
HashNode * | buckets
bool * | buckets_in_use

## Protected Member Functions
HashNode * | FindNode (uint key1, uint key2, HashNode **prev_out) const
| Finds the node that that saves this key pair. More...

## Detailed Description

Definition at line 71 of file queue.h.

## Member Function Documentation

## ◆ Delete()

void Hash::Delete | ( | bool | free_values | ) |

Deletes the hash and cleans up.

Only cleans up memory allocated by new_Hash & friends. If free is true, it will call free() on all the values that are left in the hash.

Definition at line 253 of file queue.cpp.

References free().

Referenced by AyStar::Free().

## ◆ DeleteValue()

void * Hash::DeleteValue | ( | uint | key1,
|  | uint | key2
| ) |  |

Deletes the value with the specified key pair from the hash and returns that value.

Returns nullptr when the value was not present. The value returned is not free()'d!

Definition at line 409 of file queue.cpp.

References FindNode(), and free().

Referenced by AyStar::OpenListPop().

## ◆ FindNode()

HashNode * Hash::FindNode | ( | uint | key1,
|  | uint | key2,
|  | HashNode ** | prev_out
| ) |  | const
| protected

Finds the node that that saves this key pair.

If it is not found, returns nullptr. If it is found, *prev is set to the node before the one found, or if the node found was the first in the bucket to nullptr. If it is not found, *prev is set to the last HashNode in the bucket, or nullptr if it is empty. prev can also be nullptr, in which case it is not used for output.

Definition at line 372 of file queue.cpp.

Referenced by DeleteValue(), Get(), and Set().

## ◆ Init()

void Hash::Init | ( | Hash_HashProc * | hash,
|  | uint | num_buckets
| ) |  |

Builds a new hash in an existing struct.

Make sure that hash() always returns a hash less than num_buckets! Call delete_hash after use

Definition at line 232 of file queue.cpp.

References CheckAllocationConstraints().

Referenced by AyStar::Init().

## ◆ Set()

void * Hash::Set | ( | uint | key1,
|  | uint | key2,
|  | void * | value
| ) |  |

Sets the value associated with the given key pair to the given value.

Returns the old value if the value was replaced, nullptr when it was not yet present.

Definition at line 452 of file queue.cpp.

References FindNode().

Referenced by AyStar::ClosedListAdd(), and AyStar::OpenListAdd().

The documentation for this struct was generated from the following files:

- src/pathfinder/npf/queue.h
- src/pathfinder/npf/queue.cpp