8000 Add hash table · Issue #47 · tinygraph/tinygraph · GitHub
[go: up one dir, main page]

Skip to content

Add hash table #47

@daniel-j-h

Description

@daniel-j-h

Once we have a reasonably good hash function

we can start implementing a first simple hash table.

Again the design space is quite big and there are various trade-offs to think about.

Good starting points

To stay without our philosophy of simple and practical solutions, I believe we could start with the following design

  1. Use a single flat array storing (key-value) pairs based on the key's hash interpreted as an index
  2. If there is a collision, use open addressing and in particular linear probing, which is good on cache hierarchies and simple to implement
  3. If the load factor is higher than around 0.7-0-8 we resize and rehash

Pros

  • simple to implement
  • simple to reason about
  • good on caches since linear probing simply does a linear scan on collision

Cons

  • clustering, worst case linear probing needs to make a full scan, that's why it's important to have a good hash function and keep load low
  • probably not the most efficient or best in class

Overall seems like a good pragmatic solution.

Note that we can start e.g. with a uint32 key-value hash table, and if users want to hash their own type it's possible by storing them in a flat array and using the hash table for indirection.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0