Giter Site home page Giter Site logo

hash_index's Introduction

hash_index

A fast hash table template for array indexes that keeps the hash keys separated from the mapped values.

hash_index is based on a similar data structure used by the DOOM 3 game from id Software. I have adapted it to be usable as a standalone template class and also added a few extra customization options and additional helper methods. You can find the original idHashIndex class in here.


Basic usage example:

#include "hash_index.hpp"
#include <cassert>
#include <string>
#include <vector>

struct Player
{
    std::string name;
    int level;
    int health;
    // ...
};

class PlayerRegistry
{
public:
    const Player * findPlayerByName(const std::string & name) const
    {
        const auto p_index = hash_idx.find(name_hasher(name), name, player_list,
                                           [](const std::string & name, const Player & player)
                                           {
                                               return name == player.name;
                                           });
        if (p_index == hash_idx.null_index)
        {
            return nullptr;
        }
        return &player_list[p_index];
    }

    void addPlayer(std::string name, const int lvl, const int hp)
    {
        hash_idx.insert(name_hasher(name), player_list.size());
        player_list.push_back({ std::move(name), lvl, hp });
    }

private:
    hash_index<>           hash_idx;
    std::vector<Player>    player_list;
    std::hash<std::string> name_hasher;
};

int main()
{
    PlayerRegistry player_reg;

    player_reg.addPlayer("Timmy",    10, 100);
    player_reg.addPlayer("K1ll3r",   63, 97 );
    player_reg.addPlayer("1337john", 82, 100);

    assert(player_reg.findPlayerByName("Timmy")    != nullptr);
    assert(player_reg.findPlayerByName("K1ll3r")   != nullptr);
    assert(player_reg.findPlayerByName("1337john") != nullptr);

    assert(player_reg.findPlayerByName("bobby")  == nullptr);
    assert(player_reg.findPlayerByName("johnny") == nullptr);
}

You can find more documentation in the hash_index.hpp header file.

License

hash_index is released under the terms of the GPL version 3 to comply with the original license used by the code this was based on.

hash_index's People

Contributors

glampert avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.