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