fprime/Fw/DataStructures/docs/ExternalRedBlackTreeMap.md
Rob Bocchino e9d7f3ab66
Data structure library, phase 2 (#4062)
* Revise RedBlackTree impl docs

* Revise RedBlackTree impl docs

* Revise RedBlackTree impl docs

* Revise RedBlackTree impl docs

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl docs

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Fix typo

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl docs

* Revise RedBlackTree impl docs

* Revise RedBlackTree impl

* Revise RedBlackTreeImpl docs

* Revise Fw/DataStructures

Reformat code

* Revise Fw/DataStructures

Fix compile errors on Linux

* Revise Fw/DataStructures

Fix compile errors on Linux

* Revise Fw/DataStructures

Fix compile errors on Linux

* Revise Fw/DataStructures

Fix compile errors on Linux

* Fix comments

* Revise Fw/DataStructures

Fix compile errors on Linux

* Revise ArrayMap docs

* Remove helper scripts

* Revise tests for Fw/DataStructures

* Fix spelling

* Fix Markdown link

* Fix uninitialized variable in test

* Fix uninitialized variable in test

* Fix "spelling"

Why is the spelling check enforcing arbitrary rules of style?

* Fix comments

* Revise RedBlackTree impl docs

* Start adding RedBlackTree impl

* Revise tests for ArraySetOrMapImpl

* Revise RedBlackTree impl

* Revise comment

* Revise comment

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise ArraySetOrMap impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Add RedBlackTree impl tester

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Fix comments

* Revise RedBlackTree impl

* Fix comments

* Revise comments

* Revise comments

* Revise comments

* Revise statement order for clarity

* Revise unit tests for RedBlackTree impl

* Revise RedBlackTree impl tests

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Add build and clean scripts

Working around regressions in the F Prime build system

* Revise build

Fix warnings

* Revise RedBlackTree impl and tests

* Revise tests for RedBlackTree impl

* Revise RedBlackTree impl and tests

* Revise RedBlackTree tests

Remove debug print statements

* Revise RedBlackTree impl tester

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise build script

* Revise RedBlackTree tests

* Revise RedBlackTree tests

* Revise RedBlackTree tests

* Revise RedBlackTree tests

* Revise RedBlackTree tests

* Revise RedBlackTree impl tests

* Revise RedBlackTree impl tests

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl tests

* Revise RedBlackTree impl tests

* Revise RedBlackTree impl tests

* Revise RedBlackTree impl tests

* Revise RedBlackTree impl tests

* Revise RedBlackTree impl tests

* Revise RedBlackTree impl test

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise comment

* Revise RedBlackTree impl and tests

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl

* Revise RedBlackTree impl tests

* Revise RedBlackTree impl tests

* Revise RedBlackTree impl

* Revise RedBlackTree impl tests

* Revise formatting

* Format code

* Revise docs

* Revise docs for Fw/DataStructures

* Revise Array and ExternalArray

Add static assertions

* Revise FifoQueue and Stack

Add static assertions

* Revise ArraySet and ArrayMap

Add static assertions

* Revise docs for ExternalRedBlackTreeMap

* Revise docs for ExternalRedBlackTreeSet

* Revise docs for RedBlackTreeMap

* Revise docs for RedBlackTreeSet

* Revise ExternalRedBlackTreeMap tests

* Refactor ExternalStackTester

* Revise ArrayMap tests

* Add RedBlackTreeMap

* Revise RedBlackTreeMap tests

* Revise RedBlackTreeMap

* Add ExternalRedBlackTreeSet

* Add missing file

* Revise SetConstIterator

* Revise ExternalRedBlackTreeSet tests

* Revise ExternalRedBlackTreeSet tests

* Revise ExternalRedBlackTreeSet tests

* Revise ExternalRedBlackTreeSet tests

* Revise ExternalRedBlackTreeSet tests

* Revise ExternalRedBlackTree tests

* Revise ExternalArraySet tests

* Revise ExternalArraySet tests

* Add RedBlackTreeSet

* Revise RedBlackTreeSet tests

* Refactor ArraySetTest

* Revise RedBlackTreeSetOrMapImpl docs

* Revise array initialization

* Revise comments

* Revise Array initialization

* Revise Array design and implementation

* Revert changes to Fw/DataStructures

* Revise Array

* Revise Array

* Revise Array

* Fix formatting

* Add SizedContainer base class

* Revise StackBase

Make it inherit from SizedContainer
Revise stack tests

* Revise MapBase

Make it inherit from SizedContainer
Revise tests

* Revise SetBase

Make it inherit from SizedContainer
Revise tests

* Revise DataStructures design

Add SizedContainer

* Revise SDD for DataStructures

* Revise DataStructures design

* Revise DataStructures design

* Revise DataStructures design

* Revise DataStructures design

* Fix spelling

* Revise zero-arg constructor for Array

* Revise Array interface

Make it consistent with the arrays generated by FPP

* Fix to assertion

* Format code

* Fix spelling

* Fix spelling

* Add -Wno-comment to suppress warnings for now

* Revise comments

* Revise diagrams

* Fix comment warnings on gcc

* Eliminate tabs

* Remove utility scripts

* Revise placement of break statements

* Rename function

getParentDirection --> getDirectionFromParent

* Add comment

* Add svg diagram

* Add svg diagram

* Add svg files

* Revise diagram

* Replace png with svg

* Replace png with svg

* Replace png with svg

* Revise comment

* Revise SDD for red-black tree

* Revise SDD for red-black tree
2025-10-06 13:26:23 -07:00

9.5 KiB

ExternalRedBlackTreeMap

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.

1. Template Parameters

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

2. Base Class

ExternalRedBlackTreeMap is publicly derived from MapBase<K, V>.

3. Public Types

ExternalRedBlackTreeMap defines the following public types:

Name Definition
ConstIterator Alias of MapConstIterator<K, V>
Entry Alias of SetOrMapImplEntry<K, V>

4. Private Member Variables

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

5. Public Constructors and Destructors

5.1. Zero-Argument Constructor

ExternalRedBlackTreeMap()

Initialize each member variable with its default value.

Example:

ExternalRedBlackTreeMap<U16, U32> map;

5.2. Constructor Providing Typed Backing Storage

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);

5.3. Constructor Providing Untyped Backing Storage

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);

5.4. Copy Constructor

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);

5.5. Destructor

~ExternalRedBlackTreeMap() override

Defined as = default.

6. Public Member Functions

6.1. operator=

ExternalRedBlackTreeMap<K, V>& operator=(const ExternalRedBlackTreeMap<K, V>& map)
  1. If &map != this

    1. Set m_impl = map.m_impl.
  2. 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);

6.2. begin

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);

6.3. clear

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);

6.4. end

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());

6.5. find

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);

6.6. getCapacity

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);

6.7. getSize

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);

6.8. insert

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);

6.9. remove

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);

6.10. setStorage (Typed Data)

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);

6.11. setStorage (Untyped Data)

void setStorage(ByteArray data, FwSizeType capacity)

data must be aligned according to getByteArrayAlignment() and must contain at least getByteArraySize(capacity) bytes.

  1. Call m_entries.setStorage(data, capacity).

  2. 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);

7. Public Static Functions

7.1. getByteArrayAlignment

static constexpr U8 getByteArrayAlignment()

Return RedBlackTreeSetOrMapImpl<K, V>::getByteArrayAlignment().

7.2. getByteArraySize

static constexpr FwSizeType getByteArraySize(FwSizeType capacity)

Return RedBlackTreeSetOrMapImpl<K, V>::getByteArraySize(capacity).