MapBase
MapBase is an abstract class template
defined in Fw/DataStructures.
It represents an abstract base class for a map.
1. Template Parameters
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 |
2. Base Class
MapBase<K, V> is publicly derived from SizedContainer.
3. Deleted Elements
The following elements are private and are defined = delete:
-
The copy constructor.
-
The assignment operator.
4. Public Types
MapBase defines the following public types:
| Name | Definition |
|---|---|
ConstIterator |
Alias of MapConstIterator<K, V> |
5. Protected Constructors and Destructors
5.1. Zero-Argument Constructor
Use default initialization of members.
5.2. Destructor
Defined as = default.
6. Public Member Functions
6.1. begin
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);
}
6.2. copyDataFrom
-
If
&map != thisthen-
Call
clear(). -
Let
sizebe the minimum ofmap.getSize()andgetCapacity(). -
Set
it = map.begin(). -
For
iin [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);
}
6.3. end
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());
}
6.4. find
-
If an entry
ewith valuekeyexists in the map, then setvalue = e.getValue()and returnSUCCESS. -
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);
}
6.5. insert
-
If an entry
eexists with the specified key, then update the value ineand returnSUCCESS. -
Otherwise if there is room in the map, then add a new entry
ewith the specified key-value pair and returnSUCCESS. -
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);
}
6.6. remove
-
If an entry
eexists with keykey, then store the value ofeintovalue, removeefrom the map, and returnSUCCESS. -
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);
}