Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3026

How to search for an object that has been hashed

$
0
0
Ran into a logic issue with my hashing program that stores a state object. It's storing the object just fine now but I need to be able to search for it.
The user enters a state name and I'm supposed to be able to search for it. I am at a complete loss on how to handle hashing objects. Does anybody have a link with an example for me to look at?

here is the main
Code:

#include <iostream>
#include <fstream>
#include <string>
#include "hashT.h"
#include "stateData.h"

using namespace std;

int hashFunc(string name, int size);

int main()
{
        hashT<StateData> HashTable(50);
        int size = 15;

        ifstream infile;

        infile.open("states.txt");
        if(!infile)
        {
                cout << "Error reading states file.\n";
                exit(1);
        }

        char ch;
        int year, order, key;
        string state, capitol;
        bool found;

        getline(infile, state);

        while(infile)
        {
                getline(infile, capitol);
                infile >> year;
                infile >> order;
                StateData temp;
                temp.setStateInfo(state, capitol, year, order);
                infile.get(ch);
                key = hashFunc(state, size);
                HashTable.insert(key, temp);

                getline(infile, state);
        }

        HashTable.print();

        cout<<"Enter item to be deleted: ";
        getline(cin, state);
        cout<<endl;
        key = hashFunc(state, size);       

        HashTable.remove(key, dtemp);      //here is where I am lost. how do I get the StateData object dtemp?  Do I need to completely

        HashTable.print();                          // replace my search function? It requires a StateData object.


        infile.close();
        system("pause");
        return 0;

}

int hashFunc(string name, int size)
{
        int i, sum, len;
        i= 0;
        sum = 0;
        len = name.length();
        for (int k=0; k < 15 - len; k++)
                name = name + ' ';
        for (int k = 0; k < 5; k++)
        {
                sum = sum + static_cast<int>(name[i]) * 128 * 128
                        + static_cast<int>(name[i+1]) * 128
                        + static_cast<int>(name[i+2]);
                i = i+3;
        }
        return sum % size;
}

hashT class
Code:

private:
    elemType *HTable;                //pointer to the hash table
    int *indexStatusList;        //pointer to the array indicating
                                                        //the status of a position in the
                                                        //hash table
    int length;                                //number of items in the hash table
    int HTSize;                                //maximum size of the hash table
};


template <class elemType>
void hashT<elemType>::insert(int hashIndex, elemType& rec)
{
        int pCount;
        int inc;

        pCount = 0;
        inc = 1;

        while(indexStatusList[hashIndex] == 1
                  && HTable[hashIndex] != rec
                  && pCount < HTSize / 2)
        {
                cout <<"inc = " << inc << endl;
                pCount++;
                hashIndex = (hashIndex + inc ) % HTSize;
//                cout << "new hashIndex = " << hashIndex << endl;
                inc = inc + 2;
        }


        if(indexStatusList[hashIndex] != 1)
        {
                HTable[hashIndex] = rec;
                cout << "HTable["<< hashIndex <<"]" <<" = " << rec << endl;
                indexStatusList[hashIndex] = 1;
                length++;
        }
        else
                if(HTable[hashIndex] == rec)
                        cerr<<"Error: No duplicates are allowed"<<endl;
                else
                        cerr<<"Error: The table is full. "
                            <<"Unable to resolve the collision"<<endl;
}

template <class elemType>
void hashT<elemType>::search(int& hashIndex, const elemType& rec, bool& found)
{
        int pCount;
        int inc;

        pCount = 0;
        inc = 1;

        while(indexStatusList[hashIndex] != 0
                  && HTable[hashIndex] != rec
                  && pCount < HTSize / 2)
        {
                pCount++;
                hashIndex = (hashIndex + inc ) % HTSize;
                inc = inc + 2;
        }

        if(indexStatusList[hashIndex] == 1 && HTable[hashIndex] == rec )
        {
                hashIndex = hashIndex;
                found = true;
        }
        else
                found = false;
}

template <class elemType>
bool hashT<elemType>::isItemAtEqual(int hashIndex, const elemType& rec)
{
        return(HTable[hashIndex] == rec);
}

template <class elemType>
void hashT<elemType>::retrieve(int hashIndex, elemType& rec)
{       
        if(indexStatusList[hashIndex] == 1)
                rec = HTable[hashIndex];
}

template <class elemType>
void hashT<elemType>::remove(int hashIndex, const elemType& rec)
{
        bool found;

        search(hashIndex,rec,found);

        if(found)
        {
                indexStatusList[hashIndex] = -1;
                length--;
        }
        else
                cerr<<"The item to be deleted is not in the list."<<endl;
}

StateData class
Code:

private:
        string name;
        string capitol;
        int yearAdmit;
        int order;

};
#endif

        StateData StateData::getStateData(string n)      //once I've figured out the main, I think I can do this
                {
                        if (name == n)
                                return this;
        }


        bool StateData::operator==(StateData &left)
        {          return (this->name == left.name);                  }


        bool StateData::operator!=(StateData &left)
        {          return (this->name != left.name);    }


Viewing all articles
Browse latest Browse all 3026

Trending Articles