forked from stevenarditti/part2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.h
76 lines (63 loc) · 2.01 KB
/
map.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#pragma once
#include "object.h"
#include "string.h"
#include <stdio.h>
#include <stdlib.h>
// Hashmap_pair is a node, which is used to to store a pair of key and value.
// Later a list of Hashmap_pair will be stored into Hashmap data structure.
// It inherit Object class.
class Hashmap_pair : public Object {
public:
Object *key_;
Object *val_;
Hashmap_pair(Object *key, Object *val) : Object() {
key_ = key;
val_ = val;
}
~Hashmap_pair() {
delete key_;
delete val_;
}
// getter method that help to get value from Hashmap_pair.
Object* getVal() { return val_; }
// getter method that help to get key from Hashmap_pair.
Object* getKey() { return key_; }
};
// Hashmap class stores a list of hashmap_pairs, which contains equal number
// of keys and values.
// It has size and capcity, which size is the number of key-value pairs,
// and capcity is the physical size of hashmap.
class Hashmap : public Object {
public:
Hashmap_pair **data;
size_t size_;
size_t capacity_;
// constructor
// capcity will be initilized as 4, size is 0 by default.
Hashmap() {
data = new Hashmap_pair *[4];
size_ = 0;
capacity_ = 4;
}
// destructor
~Hashmap() { delete[] data; }
// hash and return unique hashcode. two hashmaps with
// same hashmap_pairs should have the same hashcode.
size_t hash();
// Double the capacity of hashmap when needed
void expand();
// Returns the value to which the specified key is mapped,
// or null if this map contains no mapping for the key.
Object *get(Object *key);
// Associates the specified value with the specified key in this map.
void put(Object *key, Object *val);
// Removes the mapping for the specified key from this map if present.
void remove(Object *key);
// Returns the number of key-value mappings in this map.
size_t size();
// Returns a list view of the keys contained in this map.
Object **key_array();
// Check if two Hashmaps are equal.
// the input hashmap is an object.
bool equals(Object *map);
};