-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvectorMap.h
More file actions
76 lines (63 loc) · 1.38 KB
/
vectorMap.h
File metadata and controls
76 lines (63 loc) · 1.38 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
75
76
/**
* @file vectorMap.h
* @brief Class which allows for data to be accessed both as a vector and as an (unordered) map.
*
* Inspired by ChatGPT, an AI language model developed by:
* OpenAI. (2023). ChatGPT (Version 3.5) [Computer software].
* Retrieved from https://openai.com/gpt-3/
* Accessed on: 2023 April 03-07
*
* @author Adam King
* @date 2023-04-19
*/
#pragma once
#include <cstddef>
#include <unordered_map>
#include <vector>
namespace vm {
template<typename typKeyType, typename typValueType>
class clsVectorMap {
public:
clsVectorMap() {}
void
insert(
typKeyType const typKey,
typValueType const typValue
) {
size_t const sizVctIdx = vctInternal.size();
vctInternal.push_back(typValue);
mapInternal.insert({ typKey, sizVctIdx });
}
// key count (map)
size_t
count(
typKeyType const typKey
) const {
return mapInternal.count(typKey);
}
// element count (vector)
size_t
size(
void
) const {
return vctInternal.size();
}
// indexing
typValueType
operator[] (
std::size_t const sizIndex
) const {
return vctInternal.at(sizIndex);
}
// lookup
typValueType
operator[] (
typKeyType const typKey
) const {
return vctInternal.at(mapInternal.at(typKey));
}
private:
std::unordered_map<typKeyType, size_t> mapInternal;
std::vector<typValueType> vctInternal;
};
}