design-hashmap 1.0.0
Design HashMap
Loading...
Searching...
No Matches
MyHashMap Class Reference
Collaboration diagram for MyHashMap:

Public Member Functions

 MyHashMap ()
 
void put (int key, int value)
 Retrieves valid list and set/appends new value with given key.
 
int get (int key)
 Retrieves value of the key.
 
void remove (int key)
 Retrieves valid list and removes entry for key-value pair.
 

Private Member Functions

int hash (int key)
 Returns index of table used as hashtable with given key.
 

Private Attributes

vector< list< pair< int, int > > > table
 

Detailed Description

Definition at line 7 of file main.cpp.

Constructor & Destructor Documentation

◆ MyHashMap()

MyHashMap::MyHashMap ( )
inline

Definition at line 23 of file main.cpp.

23: table(SIZE) {}
vector< list< pair< int, int > > > table
Definition main.cpp:11
#define SIZE
Definition main.cpp:3

Member Function Documentation

◆ get()

int MyHashMap::get ( int  key)
inline

Retrieves value of the key.

Parameters
keyKey
Returns
Returns value of existent key, -1 in other case

Definition at line 46 of file main.cpp.

47 {
48 int h = hash(key);
49 for (const auto& p : table[h])
50 if (p.first == key)
51 return p.second;
52 return -1;
53 }
int hash(int key)
Returns index of table used as hashtable with given key.
Definition main.cpp:19

References hash(), and table.

Referenced by main().

◆ hash()

int MyHashMap::hash ( int  key)
inlineprivate

Returns index of table used as hashtable with given key.

Parameters
keyKey to store a value.
Returns
Index of table to store data.

Definition at line 19 of file main.cpp.

19{ return key % SIZE; }

References SIZE.

Referenced by get(), put(), and remove().

◆ put()

void MyHashMap::put ( int  key,
int  value 
)
inline

Retrieves valid list and set/appends new value with given key.

Definition at line 28 of file main.cpp.

29 {
30 int h = hash(key);
31 for (auto& p : table[h])
32 if (p.first == key)
33 {
34 p.second = value;
35 return;
36 }
37 table[h].emplace_back(key, value);
38 }

References hash(), and table.

Referenced by main().

◆ remove()

void MyHashMap::remove ( int  key)
inline

Retrieves valid list and removes entry for key-value pair.

Parameters
keyKey for a key-value pair to remove.

Definition at line 60 of file main.cpp.

61 {
62 int h = hash(key);
63 table[h].remove_if([key](const pair<int, int>& p) { return p.first == key; });
64 }

References hash(), and table.

Referenced by main().

Field Documentation

◆ table

vector<list<pair<int, int> > > MyHashMap::table
private

Definition at line 11 of file main.cpp.

Referenced by get(), put(), and remove().


The documentation for this class was generated from the following file: