Fw/DataStructures/docs/ExternalRedBlackTreeMap.md
ExternalRedBlackTreeMap is a final class template
defined in Fw/DataStructures.
It represents an map based on a red-black tree with external storage.
Internally it maintains an
RedBlackTreeSetOrMapImpl
as the map implementation.
ExternalRedBlackTreeMap 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 |
ExternalRedBlackTreeMap is publicly derived from
MapBase<K, V>.
<a name="Public-Types"></a>
ExternalRedBlackTreeMap defines the following public types:
| Name | Definition |
|---|---|
ConstIterator | Alias of MapConstIterator<K, V> |
Entry | Alias of SetOrMapImplEntry<K, V> |
ExternalRedBlackTreeMap has the following private member variables.
| Name | Type | Purpose | Default Value |
|---|---|---|---|
m_impl | RedBlackTreeSetOrMapImpl<K, V> | The map implementation | C++ default initialization |
classDiagram
ExternalRedBlackTreeMap *-- RedBlackTreeSetOrMapImpl
ExternalRedBlackTreeMap()
Initialize each member variable with its default value.
Example:
ExternalRedBlackTreeMap<U16, U32> map;
ExternalRedBlackTreeMap(Entry* entries, FwSizeType capacity)
entries must point to a primitive array of at least capacity
elements of type Entry.
Call setStorage(entries, capacity).
Example:
using Map = ExternalRedBlackTreeMap<U16, U32>;
constexpr FwSizeType capacity = 10;
Map::Entry entries[capacity];
Map map(entries, capacity);
ExternalRedBlackTreeMap(ByteArray data, FwSizeType capacity)
data must be aligned according to
getByteArrayAlignment() and must
contain at least getByteArraySize(capacity) bytes.
Call setStorage(data, capacity).
Example:
using Map = ExternalRedBlackTreeMap<U16, U32>;
constexpr FwSizeType capacity = 10;
constexpr U8 alignment = Map::getByteArrayAlignment();
constexpr FwSizeType byteArraySize = Map::getByteArraySize(capacity);
alignas(alignment) U8 bytes[byteArraySize];
Map map(ByteArray(&bytes[0], sizeof bytes), capacity);
ExternalRedBlackTreeMap(const ExternalRedBlackTreeMap<K, V>& map)
Set *this = map.
Example:
using Map = ExternalRedBlackTreeMap<U16, U32>;
constexpr FwSizeType capacity = 3;
Map::Entry entries[capacity];
// Call the constructor providing backing storage
Map m1(entries, capacity);
// Insert an item
const U16 key = 0;
const U32 value = 42;
const auto status = m1.insert(key, value);
ASSERT_EQ(status, Success::SUCCESS);
// Call the copy constructor
Map m2(m1);
ASSERT_EQ(m2.getSize(), 1);
~ExternalRedBlackTreeMap() override
Defined as = default.
ExternalRedBlackTreeMap<K, V>& operator=(const ExternalRedBlackTreeMap<K, V>& map)
If &map != this
m_impl = map.m_impl.Return *this.
Example:
using Map = ExternalRedBlackTreeMap<U16, U32>;
constexpr FwSizeType capacity = 3;
Map::Entry entries[capacity];
// Call the constructor providing backing storage
Map m1(entries, capacity);
// Insert an item
U16 key = 0;
U32 value = 42;
const auto status = m1.insert(key, value);
ASSERT_EQ(status, Success::SUCCESS);
// Call the default constructor
ExternalRedBlackTreeMap m2;
ASSERT_EQ(m2.getSize(), 0);
// Call the copy assignment operator
m2 = m1;
ASSERT_EQ(m2.getSize(), 1);
value = 0;
status = m2.find(key, value);
ASSERT_EQ(status, Success::SUCCESS);
ASSERT_EQ(value, 42);
ConstIterator begin() const
Return m_impl.begin().
Example:
using Map = ExternalRedBlackTreeMap<U16, U32>;
constexpr FwSizeType capacity = 10;
Map::Entry entries[capacity];
// Call the constructor providing backing storage
Map map(entries, capacity);
// 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 clear() override
Call m_impl.clear().
Example:
using Map = ExternalRedBlackTreeMap<U16, U32>;
constexpr FwSizeType capacity = 10;
Map::Entry entries[capacity];
Map map(entries, capacity);
const auto status = map.insert(0, 3);
ASSERT_EQ(map.getSize(), 1);
map.clear();
ASSERT_EQ(map.getSize(), 0);
ConstIterator end() const
Return m_impl.end().
Example:
using Map = ExternalRedBlackTreeMap<U16, U32>;
constexpr FwSizeType capacity = 10;
Map::Entry entries[capacity];
// Call the constructor providing backing storage
Map map(entries, capacity);
// 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());
Success find(const K& key, V& value) override
Return m_impl.find(key, value).
Example:
using Map = ExternalRedBlackTreeMap<U16, U32>;
constexpr FwSizeType capacity = 10;
Map::Entry entries[capacity];
Map map(entries, capacity);
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);
FwSizeType getCapacity() const override
Return m_impl.getCapacity().
Example:
using Map = ExternalRedBlackTreeMap<U16, U32>;
constexpr FwSizeType capacity = 10;
Map::Entry entries[capacity];
Map map(entries, capacity);
ASSERT_EQ(map.getCapacity(), capacity);
FwSizeType getSize() const override
Return m_impl.getSize().
Example:
using Map = ExternalRedBlackTreeMap<U16, U32>;
constexpr FwSizeType capacity = 10;
Map::Entry entries[capacity];
Map map(entries, capacity);
auto size = map.getSize();
ASSERT_EQ(size, 0);
const auto status = map.insert(0, 3);
ASSERT_EQ(status, Success::SUCCESS);
size = map.getSize();
ASSERT_EQ(size, 1);
Success insert(const K& key, const V& value) override
Return m_impl.insert(key, value).
Example:
using Map = ExternalRedBlackTreeMap<U16, U32>;
constexpr FwSizeType capacity = 10;
Map::Entry entries[capacity];
Map map(entries, capacity);
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);
Success remove(const K& key, V& value) override
Return m_impl.remove(key, value).
Example:
using Map = ExternalRedBlackTreeMap<U16, U32>;
constexpr FwSizeType capacity = 10;
Map::Entry entries[capacity];
Map map(entries, capacity);
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);
void setStorage(Entry* entries, FwSizeType capacity)
entries must point to a primitive array of at least capacity
elements of type Entry.
The type Entry is defined in this section.
Call m_impl.setStorage(entries, capacity).
Example:
using Map = ExternalRedBlackTreeMap<U16, U32>;
constexpr FwSizeType capacity = 10;
Map map;
Map::Entry entries[capacity];
map.setStorage(entries, capacity);
void setStorage(ByteArray data, FwSizeType capacity)
data must be aligned according to
getByteArrayAlignment() and must
contain at least getByteArraySize(capacity) bytes.
Call m_entries.setStorage(data, capacity).
Call clear().
using Map = ExternalRedBlackTreeMap<U16, U32>;
constexpr FwSizeType capacity = 10;
constexpr U8 alignment = Map::getByteArrayAlignment();
constexpr FwSizeType byteArraySize = Map::getByteArraySize(capacity);
alignas(alignment) U8 bytes[byteArraySize];
Map map;
map.setStorage(ByteArray(&bytes[0], sizeof bytes), capacity);
<a name="getByteArrayAlignment"></a>
static constexpr U8 getByteArrayAlignment()
Return RedBlackTreeSetOrMapImpl<K, V>::getByteArrayAlignment().
<a name="getByteArraySize"></a>
static constexpr FwSizeType getByteArraySize(FwSizeType capacity)
Return RedBlackTreeSetOrMapImpl<K, V>::getByteArraySize(capacity).