Code Monkey home page Code Monkey logo

hash-cpp's Introduction

Hash implementation for C++

This is a simple hash. The sole porpouse of this program is learn and practice the basics of Hash Function and Hash Tables. We used C++ only as a learning languague, we did not aim to any particular implementation.

Implementation

We first imported a example database and transform it into an array with objects. Then we recreated the array in a vector with linked list, as in:

list<Element> *main_array;
main_array = new list<Element>[SIZE];

Then we used a hashfunction to determinated the position of a given object in the new array. This hash function is not optimal and it's supposed to be only an example :

int HashTable::hashfunction(string id)
{
    // create "unique" key for element

    int sum = 0;
    int distribution[12] = {1, 10, 100, 1000, 10000, 10000, 10000, 10000, 1000, 100, 10, 1};

    for (int i = 0; i < id.size(); i++)
    {
        sum += int(id[i])*distribution[i];
    }

    return sum%(this->buckets); //where buckets is the size of your hash
}

Finally we inserted every item into the array:

void HashTable::insertElement(Element &e)
{
    int position = hashfunction(e.id);
    main_array[position].push_back(e);
}

As we are using linked list and push_back to insert a new item, collision will be automaticly handled and place in the same position.

Later, if you want to get all of the itens stored in the same positions, just search using:

int HashTable::search_id(string id)
{
/* Search for the element using Hash
    Return number of operations*/

    int operations = 0;
    int position = this->hashfunction(id);
    operations ++;
    for (Element e : this->main_array[position])
        {
            if (e.id == id)
            {
              cout << "Element info: " << e << endl;
              break;
            }
            operations ++;
        }
    return operations;
}

Hash.h

#ifndef HASH
#define HASH

#include "Element.h"
#include <iostream>
#include <string>
#include <list>

class HashTable
{
public:
  int buckets; //number of buckets/nodes
  list<Element> *main_array; //empty array where buckets will be placed
  HashTable();
  HashTable(int buckets); //create hash
  int hashfunction(string id); // creates address for given id
  void insertElement(Element &element); //add obj to address
  int search_id(string id); // find info corresponding to id
};

ostream& operator<< (ostream &os, HashTable &m); //print hash


#endif // HASH

hash-cpp's People

Contributors

m-rauh avatar

Watchers

 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.