Code Monkey home page Code Monkey logo

primitivehashtable's Introduction

To pre-compile test.h, execute "g++ -O2 -Wall -x c++-header test.h -o test.h.gch" To compile test file, execute "g++ -O2 -Wall -include test.h primitivehashmap.h main.cpp" To run test file, execute "./test"

The table is built using C++ primitive. Optimization is done by choosing a bucketsize that is approximately 2 times the size of the capacity. The bucketsize is also set to be a power of 2. The constructor built provides flexibility to add custom hashfunction. If a custom hashfunction is not provided, the C++ STL hashfunction will be provided.

Collision is resolved through linear-probing due to the fixed-size constraints. The operations run in amortized of O(1). The space complexity is O(n).

The hash table can be further optimized by implementing an additional "linked-list" to reference the elements in the table to allow iteration. However, because the key is a string and given the time constraints, I thought the trade-off of extra memory to store the string keys will not be worth the improve in run-time of having an additional data structure in the table.

Public API:

bool set(const std::string& key, T value, HashFunction hash = HashFunction())

  • Returns true if successful, false otherwise
  • If key exists, old value will be overwritten

T* get(const std::string& key, HashFunction hash = HashFunction())

  • Returns the pointer if exists, NULL otherwise

T* erase(const std::string& key, HashFunction hash = HashFunction());

  • Removes the item and returns the pointer if exists, NULL otherwise

float load()

  • Returns the load factor of the table. With this implementation, should not exceed 0.5

int size()

  • Returns the number of item currently in the table

primitivehashtable's People

Contributors

denimmazuki avatar

Watchers

James Cloos 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.