Jump to content
  • Advertisement
Sign in to follow this  
Phil123

Industrial Strength Hash Table

This topic is 2047 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been enjoying the coding horrors forum for a while, so I decided to post a snippet of code that I found in a large c++ project that I'm (unfortunately) a part of.  Normally I wouldn't poke fun at code like this, but I couldn't resist this one.

 

Enjoy.

#pragma once

#include "Includes.h"



template<class T>

class Dictionary

{

private:

    std::vector<std::string> keys;

    std::vector<T> values;

public:

    Dictionary()

    {

        keys = new std::vector<std::string>{};

        values = new std::vector<T>{};

    }



    ~Dictionary()

    {

        delete[] keys;

        delete[] values;

    };



    std::vector* Values() { return &values; };

    std::vector* Keys() { return &keys; };



    size_t size() { return keys.size(); }



    void Add(std::string _key, T value)

    {

        keys.push_back(_key);

        values.push_back(value);

    };

    void Remove(std::string _key)

    {

        for (size_t i = 0; i < keys.size(); i++)

        {

            if (keys.at(i) == _key)

            {

                keys.erase(keys.begin() + i);

                values.erase(values.begin() + i);

                break;

            }

        }

    };

    T Get(std::string _key)

    {

        for (size_t i = 0; i < keys.size(); i++)

        {

            if (keys.at(i) == _key)

            {

                return values.at(i);

            }

        }

        return nullptr;

    };

};

Share this post


Link to post
Share on other sites
Advertisement

One also wonders why they didn't made a pair to use a single vector.

Want to talk about exception safety?

Share this post


Link to post
Share on other sites

Does that code even compile? In what compiler?

 

In every compiler.

 

 

 

 

As long as you don't use that template.

Share this post


Link to post
Share on other sites


So let's see what the WTFs basically are...

 

I would add:

 

- Creating the std::vectors with new, where they would better be created on the stack. Its not like one of the vectors benefits is that it takes away the memory management for dynamic arrays.

 

- Returning both vectors as non-const pointer, basically breaking every concept of encapsulation of that class. Especially hard since both vectors are meant to store dependant values for one entry, imagine someone deleting values out of only the Value-vector.

 

- Also, returning the vectors by pointer instead of reference when they can never be nullptr.

Share this post


Link to post
Share on other sites

Method names begin with a capital letter

 

Hey! What's wrong with this? rolleyes.gif

 

I would like to add one.  How about every line being spaced with the beginning of each function being double spaced.  That might be "personal" preference, but it is a hell of an annoying one.

 

 

Does that code even compile? In what compiler?

 

In every compiler.

 

 

 

 

As long as you don't use that template.

 

 

Hehehehe.

EDIT: Or the fact that every function is terminated with a semicolon.  Again, personal preference; hell of an annoying one.

Edited by ByteTroll

Share this post


Link to post
Share on other sites

 

Does that code even compile? In what compiler?

 

In every compiler.

 

As long as you don't use that template.

 

Not instantiating a template doesn't prevent the first compilation pass, which will catch syntactical errors such as:

std::vector* Values() { return &values; }; // invalid use of template-name "std::vector" without an argument list
Edited by TheComet

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!