Giter Club home page Giter Club logo

hashmap's Introduction

HashMap

Overview

In this repo I implement a hashmap with amortized constant time lookups in Python without using a hashmap primitive.

The way I implemented the hashmap was by creating a list of linked lists, where each linked list node stores a key/value pair in the hashmap. Conceptually, these linked lists can be thought of as "buckets" that store a subset of the key/value pairs in the entire hashmap. In order to determine which bucket a given key/value pair belongs on, I use a hash function (from Python's hashlib module) and take the output % the total number of buckets to give the index in the list of the appropriate bucket. The reason I chose to use a linked list for each bucket is it allows for constant time insertion and deletion one a specific node is found.

This implementation allows for amoritzed constant time lookup becuase I make sure that the number of key/value pairs does not exceed the number of buckets. Therefore, on average each bucket will contain one key/value pair, which allows for amortized constant time lookup. This also allows for amortized constant time insertion, updating, and deletion. Of course, ensuring that the number of buckets is at least as many as the total number of key/value pairs involves growing and shrinking the number of buckets when the number of key/value pairs reaches a specific threshold. Though the resizing operation is linear time with respect to the number of key/value pairs, because it only happens occasionally the total cost of the operation is amortized over time. Specifically, the number of buckets doubles when the number of key/value pairs exceeds the number of buckets, and is halved when the number of key/value pairs falls short of a quarter of the number of buckets.

Methods Supported

The following instance methods are implemented for the hashmap:

  • []
  • []=
  • get(key, default = None)
  • delete(key)
  • has_key(key)
  • update(other_hash_map)
  • items()
  • keys()
  • values()
  • len(hash_map)
  • is_empty()
  • num_buckets()

Setup

This repo includes unittest files for the hashmap as well as the linked list that represents each bucket in the hashmap. I also include a basic script that can be run to show the basic functionality of the hashmap.

TL;DR

  1. Clone this repo
  2. cd hashmap
  3. run python main.py for brief tutorial
  4. run python hash_map_tests.py to run tests on the hashmap
  5. run python linked_list_tests.py to run tests on the linked list "buckets"

Thanks for visiting! :)

hashmap's People

Contributors

flaurida avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

rlehman221

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.