GithubHelp home page GithubHelp logo

odilson-dev / hashmap Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 11 KB

The Hashmap Project is an assignment from TOP's curriculum, where participants work on creating and implementing a hashmap data structure as part of their learning exercises.

Home Page: https://www.theodinproject.com/lessons/ruby-hashmap

Ruby 100.00%
data-structures hashmap ruby theodinproject

hashmap's Introduction

HashMap Implementation in Ruby

This is an exercise that implements a hash map structure in the Ruby language. Done as part of The Odin Project curriculum.

This repository contains a comprehensive implementation of a hash map in Ruby, featuring a robust linked list for handling collisions. The implementation is designed with scalability and simplicity in mind, making it suitable for a wide range of applications.

Files Included

linked_list.rb

This file defines the LinkedList class, a fundamental component for handling collisions in the hash map. The linked list is a dynamic data structure that allows for efficient insertion and retrieval of key-value pairs. Each node in the linked list contains a key, data, and a reference to the next node.

hashmap.rb

The HashMap class is the core component of this repository, showcasing a powerful hash map implementation utilizing the linked list for managing collisions. The hash map is initialized with a fixed number of buckets (default: 16) to store key-value pairs efficiently. It provides methods for setting key-value pairs, retrieving values by key, checking for key existence, clearing all elements, and obtaining arrays of keys, values, and entries.

node.rb

The Node class defines the structure of a node in the linked list. Each node encapsulates a key, data, and a reference to the next node. This class is an integral part of the linked list and contributes to the overall functionality of the hash map.

Features

  • Collision Handling: Utilizes a linked list to manage collisions efficiently.
  • Dynamic Sizing: The number of buckets in the hash map is customizable for optimization.
  • Key-Value Operations: Provides methods for setting, getting, and checking the existence of key-value pairs.
  • Clearing Elements: Clears all elements from the hash map.

Usage Example

# Create a new HashMap
my_hash_map = HashMap.new

# Set key-value pairs
my_hash_map.set("name", "John")
my_hash_map.set("age", 25)
my_hash_map.set("city", "New York")

# Retrieve values
puts "Name: #{my_hash_map.get("name")}"
puts "Age: #{my_hash_map.get("age")}"
puts "City: #{my_hash_map.get("city")}"

# Display all keys, values, and entries
puts "All Keys: #{my_hash_map.keys}"
puts "All Values: #{my_hash_map.values}"
puts "All Entries: #{my_hash_map.entries}"

# Returns true or false based on whether or not the key is in the hash map
puts my_hash_map.key?("name") #true
puts my_hash_map.key?("age") #true
puts my_hash_map.key?("city") #true

puts my_hash_map.key?("hello") #false

# returns the number of stored keys in the hash map
puts my_hash_map.length # 3

my_hash_map.remove("name")
puts my_hash_map.length # 2
puts my_hash_map.get("name") # nil
puts my_hash_map.key?("name") # false

p my_hash_map.keys ["age", "city"]


# removes all entries in the hash map.
my_hash_map.clear

puts my_hash_map.length # 0
puts my_hash_map.keys # []

hashmap's People

Contributors

odilson-dev avatar

Stargazers

 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.