Fw/DataStructures/docs/MapBase.md
MapBase is an abstract class template
defined in Fw/DataStructures.
It represents an abstract base class for a map.
MapBase has the following template parameters.
| Kind | Name | Purpose |
|---|---|---|
typename | K | The type of a key in the map |
typename | V | The type of a value in the map |
MapBase<K, V> is publicly derived from SizedContainer.
The following elements are private and are defined = delete:
The copy constructor.
MapBase(const MapBase<K, V>& map)
The assignment operator.
MapBase& operator=(const MapBase&)
MapBase defines the following public types:
| Name | Definition |
|---|---|
ConstIterator | Alias of MapConstIterator<K, V> |
MapBase()
Use default initialization of members.
virtual ~MapBase()
Defined as = default.
virtual ConstIterator begin() const = 0.
Return the begin value of the iterator for the implementation.
Example:
void f(MapBase<U16, U32>& map) {
map.clear();
// Insert an entry in the map
const auto status = map.insert(0, 1);
ASSERT_EQ(status, Fw::Success::SUCCESS);
// Get a map const iterator object
auto it = map.begin();
// Use the iterator to access the underlying map const entry
const auto key = it->getKey();
const auto value = it->getValue();
ASSERT_EQ(key, 0);
ASSERT_EQ(value, 1);
}
void copyDataFrom(const MapBase<K, V>& map)
If &map != this then
Call clear().
Let size be the minimum of map.getSize() and getCapacity().
Set it = map.begin().
For i in [0, size)
Let status = insert(it->getKey(), it->getValue()).
Assert status == Success::SUCCESS.
Call it++.
Example:
void f(MapBase<U16, U32>& m1, MapBase<U16, U32>& m2) {
m1.clear();
// Insert an entry
const U16 key = 0
const U32 value = 42;
const auto status = m1.insert(key, value);
ASSERT_EQ(status, Success::SUCCESS);
m2.clear();
ASSERT_EQ(m2.getSize(), 0);
m2.copyDataFrom(q1);
ASSERT_EQ(m2.getSize(), 1);
}
virtual ConstIterator end() const = 0
Return the end value of the iterator for the implementation.
Example:
void f(MapBase<U16, U32>& map) {
map.clear();
// Insert an entry in the map
auto status = map.insert(0, 1);
ASSERT_EQ(status, Fw::Success::SUCCESS);
// Get a map const iterator object
auto iter = map.begin();
// Check that iter is not at the end
ASSERT_NE(iter, map.end());
// Increment iter
it++;
// Check that iter is at the end
ASSERT_EQ(iter, map.end());
}
virtual Success find(const K& key, V& value) const = 0
If an entry e with value key exists in the map,
then set value = e.getValue() and return SUCCESS.
Otherwise return FAILURE.
Example:
void f(const MapBase<U16, U32>& map) {
map.clear();
U32 value = 0;
auto status = map.find(0, value);
ASSERT_EQ(status, Success::FAILURE);
status = map.insert(0, 1);
ASSERT_EQ(status, Success::SUCCESS);
status = map.find(0, value);
ASSERT_EQ(status, Success::SUCCESS);
ASSERT_EQ(value, 1);
}
virtual Success insert(const K& key, const V& value) = 0
If an entry e exists with the specified key, then update the
value in e and return SUCCESS.
Otherwise if there is room in the map, then add a new entry e with the
specified key-value pair and return SUCCESS.
Otherwise return FAILURE.
Example:
void f(MapBase<U16, U32>& map) {
map.clear();
auto size = map.getSize();
ASSERT_EQ(size, 0);
const auto status = map.insert(0, 1);
ASSERT_EQ(status, Success::SUCCESS);
size = map.getSize();
ASSERT_EQ(size, 1);
}
virtual Success remove(const K& key, V& value) = 0
If an entry e exists with key key, then
store the value of e into value,
remove e from the map, and return SUCCESS.
Otherwise return FAILURE.
Example:
void f(MapBase<U16, U32>& map) {
map.clear();
auto size = map.getSize();
ASSERT_EQ(size, 0);
auto status = map.insert(0, 1);
ASSERT_EQ(status, Success::SUCCESS);
size = map.getSize();
ASSERT_EQ(size, 1);
// Key does not exist
U32 value = 0;
status = map.remove(10, value);
ASSERT_EQ(status, Success::FAILURE);
ASSERT_EQ(size, 1);
// Key exists
status = map.remove(0, value);
ASSERT_EQ(status, Success::SUCCESS);
ASSERT_EQ(size, 0);
ASSERT_EQ(value, 1);
}