-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashmap.h
More file actions
74 lines (63 loc) · 2.52 KB
/
hashmap.h
File metadata and controls
74 lines (63 loc) · 2.52 KB
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
#pragma once
#include "stock.h"
class hashmap
{
public:
hashmap(int capacity);
~hashmap(void);
// Gets the stock associated with the provided stock symbol from the hashmap,
// returns true if successful, false if not.
//
// Additional data returned:
// symbolHash: result of applying hashStr() to stock symbol
// hashIndex: array index produced by applying the modulo operator
// to the hash value produced by hashStr()
// usedIndex: array index where the stock was actually found
// chainLgth: length of the linear probe chain to the stock
bool get(char const * const symbol, stock& s,
unsigned int& symbolHash, unsigned int& hashIndex,
unsigned int& usedIndex, unsigned int& chainLgth)
const;
// Adds the stock to the hashmap, returns true if successful,
// false if not (if the symbol is already present as a key or
// if the hash table was already full).
//
// Additional data returned:
// symbolHash: result of applying hashStr() to stock symbol
// hashIndex: array index produced by applying the modulo operator
// to the hash value produced by hashStr()
// usedIndex: array index where the stock will actually be stored
// chainLgth: length of the linear probe chain to the stock
bool put(const stock& s,
unsigned int& symbolHash, unsigned int& hashIndex,
unsigned int& usedIndex, unsigned int& chainLgth);
// Removes the stock associated with the provided symbol from the hashmap,
// returns true if successful, false if not (if the symbol is not present as a key).
// Returns a copy of the stock in s.
//
// Additional data returned:
// symbolHash: result of applying hashStr() to stock symbol
// hashIndex: array index produced by applying the modulo operator
// to the hash value produced by hashStr()
// usedIndex: array index where the stock was actually found
// chainLgth: length of the linear probe chain to the stock
bool remove(char const * const symbol, stock &s,
unsigned int& symbolHash, unsigned int& hashIndex,
unsigned int& usedIndex, unsigned int& chainLgth);
friend ostream& operator<<(ostream& out, const hashmap& h);
private:
static unsigned int hashStr(char const * const symbol); // hashing function
struct slot
{
enum state {empty, deleted, full};
stock slotStock;
state slotState;
};
slot *slots;
int capacity;
int nStocks;
int nextIndex(int index) const;
int prevIndex(int index) const;
bool slotEmpty(int index) const;
void setSlotEmpty(int index);
};