2020-10-01 13:58:20 +02:00
|
|
|
#ifndef FSFW_CONTAINER_FIXEDORDEREDMULTIMAP_H_
|
|
|
|
#define FSFW_CONTAINER_FIXEDORDEREDMULTIMAP_H_
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-08-13 20:53:35 +02:00
|
|
|
#include "ArrayList.h"
|
2016-06-15 23:48:41 +02:00
|
|
|
#include <cstring>
|
2020-10-01 13:58:20 +02:00
|
|
|
|
2019-08-28 14:50:24 +02:00
|
|
|
/**
|
2020-10-01 17:27:24 +02:00
|
|
|
* @brief An associative container which allows multiple entries of the same key.
|
2020-10-01 13:30:28 +02:00
|
|
|
* @details
|
|
|
|
* Same keys are ordered by KEY_COMPARE function which is std::less<key_t> > by default.
|
|
|
|
*
|
|
|
|
* It uses the ArrayList, so technically this is not a real map, it is an array of pairs
|
2020-10-01 14:59:26 +02:00
|
|
|
* of type key_t, T. It is ordered by key_t as FixedMap but allows same keys. Thus it has a linear
|
2020-10-01 13:58:20 +02:00
|
|
|
* complexity O(n). As long as the number of entries remains low, this
|
|
|
|
* should not be an issue.
|
|
|
|
* The number of insertion and deletion operation should be minimized
|
2020-10-01 14:59:26 +02:00
|
|
|
* as those incur extensive memory move operations (the underlying container
|
2020-10-01 13:58:20 +02:00
|
|
|
* is not node based).
|
2020-10-01 13:30:28 +02:00
|
|
|
*
|
|
|
|
* Its of fixed size so no allocations are performed after the construction.
|
|
|
|
*
|
|
|
|
* The maximum size is given as first parameter of the constructor.
|
|
|
|
*
|
|
|
|
* It provides an iterator to do list iterations.
|
|
|
|
*
|
|
|
|
* The type T must have a copy constructor if it is not trivial copy-able.
|
|
|
|
*
|
|
|
|
* @warning Iterators return a non-const key_t in the pair.
|
|
|
|
* @warning A User is not allowed to change the key, otherwise the map is corrupted.
|
|
|
|
*
|
2019-08-28 14:50:24 +02:00
|
|
|
* \ingroup container
|
|
|
|
*/
|
2016-06-15 23:48:41 +02:00
|
|
|
template<typename key_t, typename T, typename KEY_COMPARE = std::less<key_t>>
|
|
|
|
class FixedOrderedMultimap {
|
|
|
|
public:
|
2020-09-22 16:13:45 +02:00
|
|
|
static const uint8_t INTERFACE_ID = CLASS_ID::FIXED_MULTIMAP;
|
|
|
|
static const ReturnValue_t MAP_FULL = MAKE_RETURN_CODE(0x01);
|
|
|
|
static const ReturnValue_t KEY_DOES_NOT_EXIST = MAKE_RETURN_CODE(0x02);
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-10-01 13:30:28 +02:00
|
|
|
/***
|
|
|
|
* Constructor which needs a size_t for the maximum allowed size
|
|
|
|
*
|
|
|
|
* Can not be resized during runtime
|
|
|
|
*
|
|
|
|
* Allocates memory at construction
|
|
|
|
* @param maxSize size_t of Maximum allowed size
|
|
|
|
*/
|
2020-10-01 14:59:26 +02:00
|
|
|
FixedOrderedMultimap(size_t maxSize):theMap(maxSize), _size(0){
|
|
|
|
}
|
|
|
|
|
2020-10-01 13:30:28 +02:00
|
|
|
/***
|
|
|
|
* Virtual destructor frees Memory by deleting its member
|
|
|
|
*/
|
2016-06-15 23:48:41 +02:00
|
|
|
virtual ~FixedOrderedMultimap() {
|
|
|
|
}
|
|
|
|
|
2020-10-01 13:30:28 +02:00
|
|
|
/***
|
|
|
|
* Special iterator for FixedOrderedMultimap
|
|
|
|
*/
|
|
|
|
class Iterator: public ArrayList<std::pair<key_t, T>, size_t>::Iterator {
|
2016-06-15 23:48:41 +02:00
|
|
|
public:
|
|
|
|
Iterator() :
|
2020-10-01 13:30:28 +02:00
|
|
|
ArrayList<std::pair<key_t, T>, size_t>::Iterator() {
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Iterator(std::pair<key_t, T> *pair) :
|
2020-10-01 13:30:28 +02:00
|
|
|
ArrayList<std::pair<key_t, T>, size_t>::Iterator(pair) {
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-01 13:30:28 +02:00
|
|
|
/***
|
2020-10-01 14:59:26 +02:00
|
|
|
* Returns an iterator pointing to the first element
|
2020-10-01 13:30:28 +02:00
|
|
|
* @return Iterator pointing to first element
|
|
|
|
*/
|
2016-06-15 23:48:41 +02:00
|
|
|
Iterator begin() const {
|
|
|
|
return Iterator(&theMap[0]);
|
|
|
|
}
|
|
|
|
|
2020-10-01 13:30:28 +02:00
|
|
|
/**
|
|
|
|
* Returns an iterator pointing to one element past the end
|
|
|
|
* @return Iterator pointing to one element past the end
|
|
|
|
*/
|
2016-06-15 23:48:41 +02:00
|
|
|
Iterator end() const {
|
|
|
|
return Iterator(&theMap[_size]);
|
|
|
|
}
|
|
|
|
|
2020-10-01 13:30:28 +02:00
|
|
|
/***
|
|
|
|
* Returns the current size of the map (not maximum size!)
|
|
|
|
* @return Current size
|
|
|
|
*/
|
2020-10-01 14:59:26 +02:00
|
|
|
size_t size() const{
|
2016-06-15 23:48:41 +02:00
|
|
|
return _size;
|
|
|
|
}
|
|
|
|
|
2020-10-01 14:59:26 +02:00
|
|
|
/**
|
|
|
|
* Clears the map, does not deallocate any memory
|
|
|
|
*/
|
|
|
|
void clear(){
|
|
|
|
_size = 0;
|
|
|
|
}
|
2020-10-01 13:58:20 +02:00
|
|
|
|
|
|
|
/**
|
2020-10-01 14:59:26 +02:00
|
|
|
* Returns the maximum size of the map
|
|
|
|
* @return Maximum size of the map
|
2020-10-01 13:58:20 +02:00
|
|
|
*/
|
2020-10-01 14:59:26 +02:00
|
|
|
size_t maxSize() const{
|
|
|
|
return theMap.maxSize();
|
|
|
|
}
|
|
|
|
|
2020-10-01 13:30:28 +02:00
|
|
|
/***
|
|
|
|
* Used to insert a key and value separately.
|
|
|
|
*
|
|
|
|
* @param[in] key Key of the new element
|
|
|
|
* @param[in] value Value of the new element
|
|
|
|
* @param[in/out] (optional) storedValue On success this points to the new value, otherwise a nullptr
|
|
|
|
* @return RETURN_OK if insert was successful, MAP_FULL if no space is available
|
|
|
|
*/
|
2020-10-01 14:59:26 +02:00
|
|
|
ReturnValue_t insert(key_t key, T value, Iterator *storedValue = nullptr);
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-10-01 13:30:28 +02:00
|
|
|
/***
|
|
|
|
* Used to insert new pair instead of single values
|
|
|
|
*
|
|
|
|
* @param pair Pair to be inserted
|
|
|
|
* @return RETURN_OK if insert was successful, MAP_FULL if no space is available
|
|
|
|
*/
|
2020-10-01 14:59:26 +02:00
|
|
|
ReturnValue_t insert(std::pair<key_t, T> pair);
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-10-01 13:30:28 +02:00
|
|
|
/***
|
|
|
|
* Can be used to check if a certain key is in the map
|
|
|
|
* @param key Key to be checked
|
|
|
|
* @return RETURN_OK if the key exists KEY_DOES_NOT_EXIST otherwise
|
|
|
|
*/
|
2020-10-01 14:59:26 +02:00
|
|
|
ReturnValue_t exists(key_t key) const;
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-10-01 13:30:28 +02:00
|
|
|
/***
|
|
|
|
* Used to delete the element in the iterator
|
|
|
|
*
|
|
|
|
* The iterator will point to the element before or begin(),
|
|
|
|
* but never to one element in front of the map.
|
|
|
|
*
|
|
|
|
* @warning The iterator needs to be valid and dereferenceable
|
|
|
|
* @param[in/out] iter Pointer to iterator to the element that needs to be ereased
|
|
|
|
* @return RETURN_OK if erased, KEY_DOES_NOT_EXIST if the there is no element like this
|
|
|
|
*/
|
2020-10-01 14:59:26 +02:00
|
|
|
ReturnValue_t erase(Iterator *iter);
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-10-01 13:30:28 +02:00
|
|
|
/***
|
|
|
|
* Used to erase by key
|
|
|
|
* @param key Key to be erased
|
|
|
|
* @return RETURN_OK if erased, KEY_DOES_NOT_EXIST if the there is no element like this
|
|
|
|
*/
|
2020-10-01 14:59:26 +02:00
|
|
|
ReturnValue_t erase(key_t key);
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-10-01 13:30:28 +02:00
|
|
|
/***
|
|
|
|
* Find returns the first appearance of the key
|
|
|
|
*
|
|
|
|
* If the key does not exist, it points to end()
|
|
|
|
*
|
|
|
|
* @param key Key to search for
|
|
|
|
* @return Iterator pointing to the first entry of key
|
|
|
|
*/
|
2020-10-01 14:59:26 +02:00
|
|
|
Iterator find(key_t key) const;
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-10-01 13:30:28 +02:00
|
|
|
/***
|
|
|
|
* Finds first entry of the given key and returns a
|
|
|
|
* pointer to the value
|
|
|
|
*
|
|
|
|
* @param key Key to search for
|
|
|
|
* @param value Found value
|
|
|
|
* @return RETURN_OK if it points to the value,
|
|
|
|
* KEY_DOES_NOT_EXIST if the key is not in the map
|
|
|
|
*/
|
2020-10-01 14:59:26 +02:00
|
|
|
ReturnValue_t find(key_t key, T **value) const;
|
|
|
|
|
|
|
|
friend bool operator==(const typename FixedOrderedMultimap::Iterator& lhs,
|
|
|
|
const typename FixedOrderedMultimap::Iterator& rhs) {
|
|
|
|
return (lhs.value == rhs.value);
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
2020-10-01 14:59:26 +02:00
|
|
|
friend bool operator!=(const typename FixedOrderedMultimap::Iterator& lhs,
|
|
|
|
const typename FixedOrderedMultimap::Iterator& rhs) {
|
|
|
|
return not (lhs.value == rhs.value);
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
2020-10-01 13:30:28 +02:00
|
|
|
private:
|
|
|
|
typedef KEY_COMPARE compare;
|
|
|
|
compare myComp;
|
|
|
|
ArrayList<std::pair<key_t, T>, size_t> theMap;
|
|
|
|
size_t _size;
|
|
|
|
|
2020-10-01 14:59:26 +02:00
|
|
|
size_t findFirstIndex(key_t key, size_t startAt = 0) const;
|
2020-10-01 13:30:28 +02:00
|
|
|
|
2020-10-01 14:59:26 +02:00
|
|
|
size_t findNicePlace(key_t key) const;
|
2020-10-01 13:30:28 +02:00
|
|
|
|
2020-10-01 14:59:26 +02:00
|
|
|
void removeFromPosition(size_t position);
|
2016-06-15 23:48:41 +02:00
|
|
|
};
|
|
|
|
|
2020-10-01 13:58:20 +02:00
|
|
|
#include "FixedOrderedMultimap.tpp"
|
|
|
|
|
|
|
|
#endif /* FSFW_CONTAINER_FIXEDORDEREDMULTIMAP_H_ */
|