Skip to main content
Tweeted twitter.com/StackCodeReview/status/1487168653422481416
added 97 characters in body
Source Link

I am looking for tips on efficiency of the code and whether forwarding of the arguments are working as intended (perfect forwarding). Also in the get method, would it be better to return value type pointer instead of std::optional?

#include "BasicHash.h"
#include <string>
#include <iostream>

//djb2
size_t hashFunction(const std::string& key)
{
    unsigned long hash = 5381;
    int c;

    const char* str = key.c_str();
    while (c = *str++)
        hash = ((hash << 5) + hash) + c; 

    return hash;
}

class TwoNumberStore
{
public:
    TwoNumberStore(int a, int b) :
        n1{ a }, n2{ b }
    {
    }

    int n1 = 0;
    int n2 = 0;
};

int main()
{
    BasicHash<std::string, TwoNumberStore, 50, hashFunction> table;
    table.EmplaceBack("First Two", 1, 2);

    auto firstTwo = table.Get("First Two");

    if (firstTwo.has_value())
    {
        std::cout << firstTwo.value().n1 << '\n';
        std::cout << firstTwo.value().n2;
    }
}

I just realised I was copying value in std::optional. Changed get to return a optional reference_wrapper instead.

decltype(auto) Get(KeyType&& key)
    {
        size_t hash = Hash(std::forward<KeyType>(key));

        for (auto& pair : hashGroups[hash])
            if (pair.key == key)
                return std::optional<std::reference_wrapper<ValueType>>{ pair.value };

        return std::optional<std::reference_wrapper<ValueType>>{};
    }

This makes retrieving data even messier.

if (firstTwo.has_value())
    {
        std::cout << firstTwo.value().get().n1 << '\n';
        std::cout << firstTwo.value().get().n2;
    }

I am looking for tips on efficiency of the code and whether forwarding of the arguments are working as intended (perfect forwarding).

#include "BasicHash.h"
#include <string>
#include <iostream>

//djb2
size_t hashFunction(const std::string& key)
{
    unsigned long hash = 5381;
    int c;

    const char* str = key.c_str();
    while (c = *str++)
        hash = ((hash << 5) + hash) + c; 

    return hash;
}

class TwoNumberStore
{
public:
    TwoNumberStore(int a, int b) :
        n1{ a }, n2{ b }
    {
    }

    int n1 = 0;
    int n2 = 0;
};

int main()
{
    BasicHash<std::string, TwoNumberStore, 50, hashFunction> table;
    table.EmplaceBack("First Two", 1, 2);

    auto firstTwo = table.Get("First Two");

    if (firstTwo.has_value())
    {
        std::cout << firstTwo.value().n1 << '\n';
        std::cout << firstTwo.value().n2;
    }
}

I am looking for tips on efficiency of the code and whether forwarding of the arguments are working as intended (perfect forwarding). Also in the get method, would it be better to return value type pointer instead of std::optional?

#include "BasicHash.h"
#include <string>
#include <iostream>

//djb2
size_t hashFunction(const std::string& key)
{
    unsigned long hash = 5381;
    int c;

    const char* str = key.c_str();
    while (c = *str++)
        hash = ((hash << 5) + hash) + c; 

    return hash;
}

class TwoNumberStore
{
public:
    TwoNumberStore(int a, int b) :
        n1{ a }, n2{ b }
    {
    }

    int n1 = 0;
    int n2 = 0;
};

int main()
{
    BasicHash<std::string, TwoNumberStore, 50, hashFunction> table;
    table.EmplaceBack("First Two", 1, 2);

    auto firstTwo = table.Get("First Two");

    if (firstTwo.has_value())
    {
        std::cout << firstTwo.value().n1 << '\n';
        std::cout << firstTwo.value().n2;
    }
}

I just realised I was copying value in std::optional. Changed get to return a optional reference_wrapper instead.

decltype(auto) Get(KeyType&& key)
    {
        size_t hash = Hash(std::forward<KeyType>(key));

        for (auto& pair : hashGroups[hash])
            if (pair.key == key)
                return std::optional<std::reference_wrapper<ValueType>>{ pair.value };

        return std::optional<std::reference_wrapper<ValueType>>{};
    }

This makes retrieving data even messier.

if (firstTwo.has_value())
    {
        std::cout << firstTwo.value().get().n1 << '\n';
        std::cout << firstTwo.value().get().n2;
    }
No need to repeat tags in the title
Link
Toby Speight
  • 81.7k
  • 14
  • 101
  • 308

Implementation of generic hash table in c++

Spelling fixes
Source Link
Toby Speight
  • 81.7k
  • 14
  • 101
  • 308

I am learning how to write generic code in c++C++ and I decided to write a hash table. It'sIts key needs to be a primitive type and value can be any type. The hash function needs to be provided by the user. Type of the function is enforced to the user.

I am learning how to write generic code in c++ and I decided to write a hash table. It's key needs to be a primitive type and value can be any type. The hash function needs to be provided by the user. Type of the function is enforced to the user.

I am learning how to write generic code in C++ and I decided to write a hash table. Its key needs to be a primitive type and value can be any type. The hash function needs to be provided by the user. Type of the function is enforced to the user.

Source Link
Loading