Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit db186cd

Browse files
+ DataStructures/HashMap
1 parent 1915045 commit db186cd

File tree

5 files changed

+178
-29
lines changed

5 files changed

+178
-29
lines changed

‎.idea/workspace.xml‎

Lines changed: 5 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <ArduinoUnit.h>
2+
#include <eDataStructures.h>
3+
4+
5+
using namespace Eloquent::DataStructures;
6+
7+
8+
test(exists) {
9+
HashMap<int, int, 10> hashmap;
10+
11+
hashmap.put(0, 10);
12+
hashmap.put(1, 20);
13+
14+
assertTrue(hashmap.exists(0));
15+
assertTrue(hashmap.exists(1));
16+
assertFalse(hashmap.exists(2));
17+
}
18+
19+
20+
test(get) {
21+
HashMap<int, int, 10> hashmap;
22+
23+
hashmap.put(0, 10);
24+
hashmap.put(1, 20);
25+
26+
assertEqual(hashmap.get(0), 10);
27+
assertEqual(hashmap.get(1), 20);
28+
assertEqual(hashmap.get(2), 0);
29+
}
30+
31+
test(get_string) {
32+
HashMap<String, int, 10> hashmap;
33+
34+
hashmap.put("abc", 10);
35+
hashmap.put("xyz", 20);
36+
37+
assertEqual(hashmap.get("abc"), 10);
38+
assertEqual(hashmap.get("xyz"), 20);
39+
assertEqual(hashmap.get("foo"), 0);
40+
}
41+
42+
43+
test(prevents_overflow) {
44+
HashMap<int, int, 2> hashmap;
45+
46+
hashmap.put(0, 10);
47+
hashmap.put(1, 20);
48+
hashmap.put(2, 30);
49+
50+
assertEqual(hashmap.get(0), 10);
51+
assertEqual(hashmap.get(1), 20);
52+
assertFalse(hashmap.exists(2));
53+
}
54+
55+
56+
void setup() {
57+
Serial.begin(115200);
58+
}
59+
60+
61+
void loop() {
62+
Test::run();
63+
}

‎src/data_structures/HashMap.h‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#pragma once
2+
3+
4+
namespace Eloquent {
5+
namespace DataStructures {
6+
7+
/**
8+
* A key: value data structure
9+
* Not really an "hash" map, but works the same
10+
*/
11+
template<typename K, typename V, uint8_t size>
12+
class HashMap {
13+
public:
14+
/**
15+
*
16+
*/
17+
HashMap() :
18+
i(0) {
19+
20+
}
21+
22+
/**
23+
* Set key value
24+
* @param key
25+
* @param value
26+
*/
27+
void put(K key, V value) {
28+
if (i >= size)
29+
return;
30+
31+
keys[i] = key;
32+
values[i] = value;
33+
i++;
34+
}
35+
36+
/**
37+
* Get key's value
38+
* @param key
39+
* @return
40+
*/
41+
V get(K key) {
42+
int16_t index = indexOf(key);
43+
44+
return index >= 0 ? values[index] : 0;
45+
}
46+
47+
/**
48+
* Check if key exists.
49+
* @param key
50+
* @return
51+
*/
52+
bool exists(K key) {
53+
return indexOf(key) >= 0;
54+
}
55+
56+
/**
57+
* Get keys buffer
58+
* @return
59+
*/
60+
K* getKeys() {
61+
return this->keys;
62+
}
63+
64+
/**
65+
* Get values buffer
66+
* @return
67+
*/
68+
V* getValues() {
69+
return this->values;
70+
}
71+
72+
protected:
73+
uint8_t i;
74+
K keys[size];
75+
V values[size];
76+
77+
/**
78+
* Get index of key.
79+
* Returns -1 if not found.
80+
* @param key
81+
* @return
82+
*/
83+
int16_t indexOf(K key) {
84+
for (uint8_t j = 0; j < i; j++)
85+
if (keys[j] == key)
86+
return j;
87+
88+
return -1;
89+
}
90+
};
91+
}
92+
}

‎src/dev/logging.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@
66
* Adheres to the severities of PSR-3 Logger Interface @ https://www.php-fig.org/psr/psr-3/
77
*/
88

9+
10+
/**
11+
* 0 - LOG_LEVEL_SILENT no output
12+
* 1 - LOG_LEVEL_FATAL fatal errors
13+
* 2 - LOG_LEVEL_ERROR all errors
14+
* 3 - LOG_LEVEL_WARNING errors, and warnings
15+
* 4 - LOG_LEVEL_NOTICE errors, warnings and notices
16+
* 5 - LOG_LEVEL_TRACE errors, warnings, notices & traces
17+
* 6 - LOG_LEVEL_VERBOSE all
18+
*/
19+
20+
//#if LOG_SEVERITY >= LOG_SEVERITY_FATAL
21+
//#define log_fatal(message) Serial.print(...)
22+
//#else
23+
//#define log_fatal(message)
24+
//#endif
25+
926
#define LOG_SEVERITY_SILENT 0
1027
#define LOG_SEVERITY_EMERGENCY 1
1128
#define LOG_SEVERITY_ALERT 2

‎src/eDataStructures.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "data_structures/Array.h"
22
#include "data_structures/Counter.h"
3+
#include "data_structures/HashMap.h"
34
#include "data_structures/PackedBitArray.h"
45
#include "data_structures/Queue.h"

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /