user1645240
user1645240

Reputation: 55

C++ Templated HashMap implementation errors

I seem to be having some templating issues with my HashMap functions. I need to run a <string, vector<string>> hash map in the end. And it's giving a lot of issues.

I need to be able to be able to push items on the vector. I was using the [] operator.

test

using namespace std;
#include "MyHashMap.hpp"
#include <vector>
int main()
{
    MyHashMap <string, vector <string> > test (10);
    test["x"].push_back("Y");    
}

ERRORS (There are a lot of them. I think they are related so I will only show 1)

In member function ‘valueType& MyHashMap::operator [with keyType = std::basic_string, std::allocator >, valueType = std::vector, std::allocator >, std::allocator, std::allocator > > >]’:

test.cpp:13: instantiated from here

MyHashMap.hpp:52: error: cannot convert ‘MyHashMap, std::allocator >, std::vector, std::allocator >, std::allocator, std::allocator > > > >::EntryType’ to ‘EntryType’ in assignment MyHashMap.hpp: In member function ‘int MyHashMap::findPos(keyType&) [with keyType = std::basic_string, std::allocator >, valueType = std::vector, std::allocator >, std::allocator, std::allocator > > >]’:

MyHashMap.hpp:46: instantiated from ‘valueType& MyHashMap::operator [with keyType = std::basic_string, std::allocator >, valueType = std::vector, std::allocator >, std::allocator, std::allocator > > >]’

MyHashMap.hpp

#include <string>
#include "HashEntry.hpp"
using namespace std;

template <typename keyType, typename valueType>
class MyHashMap
{
    public:
        /***********************************************
        explicit HashTable( int size = 1009 ) : array(size)
        CONSTRUCTOR with default size of 1009
        ***********************************************/

        explicit MyHashMap(int size=1000):tableSize(size)
        {
        array = new HashEntry <keyType, valueType> [tableSize];
        }

        int size()
        {
        return tableSize;
    }

    /***********************************************

    ***********************************************/
    valueType& operator[](keyType x )
    {
        // Insert x as active
        int currentPos = findPos( x );
        if( isActive( currentPos ) )
        {
            return array[currentPos].value;
        }
        array[ currentPos ].key = x;
        array[ currentPos ].info = ACTIVE;
        return array[currentPos].value;
    }
    /***********************************************
    Returns pointer to HashEntry if it is active
    Returns NULL otherwise
    ***********************************************/
    HashEntry<keyType,valueType>& getPos(int n)
    {
        if(isActive(n))
        {
            return array[n];
        }
    }

    /***********************************************
    bool insert(  keyType & x,  & valueType y)
    inserts x and y into hashMap    
    ***********************************************/
    bool insert(  keyType & x,  valueType&  y)
    {
        // Insert x as active
        //currentPos gets position to try to insert 
        int currentPos = findPos( x );
        if( isActive( currentPos ) )
        {
            return false;
        }
        array[ currentPos ].key = x;
        array[ currentPos ].value = y;
        return true;
    }


    /***********************************************
    LAZY DELETION
    ***********************************************/
    bool remove(  keyType x )
    {
        int currentPos = findPos( x );
        if( !isActive( currentPos ) )
            return false;
        array[ currentPos ].info = DELETED;
        return true;
    }

    /***********************************************

    ***********************************************/
    valueType* find(  keyType & x ) 
    {
        //currentPos gets position where Search for x terminates
        int currentPos = findPos(x);
        if(isActive(currentPos))
            return array[currentPos].value;
        else
            return NULL;

    }

    enum EntryType { ACTIVE, EMPTY, DELETED };

private:

    HashEntry <keyType, valueType>* array;
    int tableSize;
    /***********************************************
    ***********************************************/
    bool isActive( int currentPos ) 
    {
        return array[ currentPos ].info == ACTIVE; 
    }

    /***********************************************
    int findPos(  HashedObj & x ) 
    Returns the position where the search for x terminates
    ***********************************************/
    int findPos(  keyType & x ) 
    {
        int offset = 1;
        int currentPos = myhash( x );

      // Assuming table is half-empty, and table length is prime,
      // this loop terminates
        while( array[ currentPos ].info != EMPTY &&
        array[ currentPos ].element != x )
        {
            currentPos += offset;  // Compute ith probe
            offset += 2;
            if( currentPos >= tableSize )
                currentPos -= tableSize;
        }

        return currentPos;
    }
    /***********************************************
    Hash function for string
    ***********************************************/

    int myhash(  string & x )
    {
        int hashVal = 0;
        for(int i = 0; i < x.length(); i++)
        {
            hashVal = 37*hashVal+x[i];
        }
        hashVal %= tableSize;
        if(hashVal < 0)
            hashVal += tableSize;
        return hashVal;
    }
};



#endif

Hash Entry

using namespace std;

enum EntryType { ACTIVE, EMPTY, DELETED };
template <typename keyType, typename valueType>

struct HashEntry
{
    keyType key;
    valueType value;
    EntryType info;
    /***********************************************
    HashEntry CONSTRUCTOR
    ***********************************************/
    HashEntry(keyType x = keyType(), valueType y = valueType(),
     EntryType i = EMPTY ):key(x), value(y),info( i ) { }
};
#endif

Upvotes: 1

Views: 127

Answers (1)

Murilo Vasconcelos
Murilo Vasconcelos

Reputation: 4827

The declaration of your member MyHashMap::array is wrong. Change it to:

HashEntry <keyType, valueType>* array;

In the constructor:

explicit MyHashMap(int size = 1000) : tableSize(size)
{
    array = new HashEntry<keyType, valueType>[tableSize];
}

And it should work correctly!

Upvotes: 2

Related Questions