Skip to content

Commit 983c967

Browse files
committed
#20 Add updateable map
1 parent 569b436 commit 983c967

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

src/tests/test_serial_hierarchy.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
Unit tests for SerialHierarchy
3+
"""
4+
import unittest
5+
from src.utils.serial_hierarchy import SerialHierarchy
6+
7+
8+
class TestSerialHierarchy(unittest.TestCase):
9+
10+
11+
def test_add_values(self):
12+
13+
hierarchy = SerialHierarchy()
14+
hierarchy.add("val1", "val2", "val3")
15+
self.assertEqual(1, len(hierarchy))
16+
self.assertEqual(list, type(hierarchy[0]))
17+
18+
19+
if __name__ == '__main__':
20+
unittest.main()

src/tests/test_updateable_map.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import unittest
2+
import pytest
3+
4+
from src.utils.updateable_map import UpdateableMap
5+
6+
7+
class TestUpdateableMap(unittest.TestCase):
8+
9+
def setUp(self) -> None:
10+
11+
self.map = UpdateableMap(list_size=3)
12+
values = ["val1", "val2", "val3", "val10"]
13+
key = "key1"
14+
self.map.insert(key=key, values=values)
15+
16+
values = ["val4", "val5", "val6", "val10"]
17+
key = "key2"
18+
self.map.insert(key=key, values=values)
19+
20+
def test_invalid_list_size(self):
21+
map = UpdateableMap(list_size=2)
22+
values = ["val1", "val2", "val3"]
23+
key = "key"
24+
25+
with pytest.raises(ValueError) as e_info:
26+
# this should throw
27+
map.insert(key=key, values=values)
28+
29+
def test_get_value(self):
30+
31+
value = self.map.get_value("key1")
32+
self.assertEqual("val1", value)
33+
34+
# the key should be updated
35+
value = self.map.get_value(key=value)
36+
self.assertEqual("val2", value)
37+
38+
keys = list(self.map.keys())
39+
self.assertEqual(["val2"], keys)
40+
41+
values = self.map.values()
42+
self.assertEqual(["key1", "val1", "val3"], list(values)[0])
43+
44+
print(self.map.current_pos)
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
if __name__ == '__main__':
56+
unittest.main()

src/utils/updateable_map.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""
2+
Wrapper class over the traditional dict class.
3+
Every time the get_value function is called,
4+
the key is updated with the current value that
5+
the counter points to
6+
"""
7+
from typing import Any, List, KeysView, ValuesView
8+
from src.exceptions.exceptions import InvalidStateException
9+
10+
11+
class UpdateableMap(object):
12+
13+
def __init__(self, list_size: int) -> None:
14+
self.current_pos = {}
15+
self._values = {}
16+
self.list_size: int = list_size
17+
18+
def __getitem__(self, item) -> Any:
19+
return self._values[item]
20+
21+
def __len__(self):
22+
return len(self._values)
23+
24+
def keys(self) -> KeysView:
25+
"""
26+
Returns the current keys
27+
:return:
28+
"""
29+
return self._values.keys()
30+
31+
def values(self) -> ValuesView:
32+
"""
33+
Returns the current values in the map
34+
:return:
35+
"""
36+
return self._values.values()
37+
38+
def get_value(self, key: Any) -> Any:
39+
40+
values = self._values[key]
41+
current_pos = self.current_pos[key]
42+
43+
val = values[current_pos]
44+
45+
# update keys
46+
self._values[val] = self._values[key]
47+
self._values[val][current_pos] = key
48+
del self._values[key]
49+
50+
current_pos += 1
51+
self.current_pos[val] = current_pos
52+
return val
53+
54+
def insert(self, key: Any, values: List[Any]) -> None:
55+
56+
if len(values) != self.list_size:
57+
raise ValueError("Invalid list size. Size={0} should be equal to {1}".format(len(values), self.list_size))
58+
59+
self._values[key] = values
60+
self.current_pos[key] = 0
61+
62+
def is_exhausted(self) -> bool:
63+
"""
64+
Return true if all keys are self.tail_value
65+
:return:
66+
"""
67+
counters = self.current_pos.values()
68+
return all(counter >= self.list_size for counter in counters)

0 commit comments

Comments
 (0)