-
Notifications
You must be signed in to change notification settings - Fork 0
om_list
I&CG edited this page Jan 17, 2019
·
1 revision
om::list<T> implements a doubly linked list of objects of type T. This implementation shares almost the same API as it is known from std::list<T>. It provides only the random iterator
, which should be sufficient for most Arduino projects. It provides special features as splice, find, find_if, remove, remove_if, sort. Some of them do even take lambda expressions to complete the task.
The following struct and method is used in the following list-operations:
struct Player
{
char m_name[8+1];
int m_val1;
int m_val2;
int m_val3;
Player(const char* name, int val1, int val2, int val3)
: m_val1(val1)
, m_val2(val2)
, m_val3(val3)
{
strncpy(m_name, name, 8);
m_name[8] = '\0';
}
bool operator< (const Player &rhs)
{
return strncmp(m_name, rhs.m_name, 8) < 0;
}
int sum() const
{
return m_val1 + m_val2 + m_val3;
}
};
bool compareSumV123(const Player&a, const Player&b)
{
return a.sum() < b.sum();
}
om::list<Player> someData;
someData.push_back(Player("Alpha", 1, 2, 3));
someData.push_back(Player("Bravo", 4, 5, 6));
someData.push_back(Player("Charly", 7, 8, 9));
someData.push_back(Player("Delta", 10, 0, 20));
om::list<Player> moreData;
moreData.push_front(Player("Echo", 1, 2, 3));
moreData.push_back(Player("Foxtrott", 4, 5, 6));
moreData.push_front(Player("Golf", 7, 8, 9));
moreData.push_back(Player("Hotel", 10, 0, -20));
moreData.push_front(Player("India", 30,40, -50));
auto it = someData.begin();
it += 2; // pointing to "Charly", iterator arithmetics
// move all elements from moreData into someData, insert before element referend by it
someData.splice(it, moreData);
someData.sort(); // using Player::operator<, would fail at compile time if no matching operator< found
// find and move "Golf" back to moreData, use lambda-expression as search criteria
// (yes, it compiles and works on Arduino UNO, too!)
const char* suche = "Golf";
it = someData.find_if( [&](const Player &ds){
return strncmp(suche, ds.m_name, 8) == 0;
});
if (it != someData.end())
moreData.splice(moreData.end(), someData, it);
Serial.println("Scanning through list<Player>, sorted by operator<, where is 'Golf'?");
Serial.println(someData.size());
for(auto it = someData.begin(); it != someData.end(); ++it)
{
Serial.println(it->m_name);
Serial.print(it->m_val1); Serial.print(", ");
Serial.print(it->m_val2); Serial.print(", ");
Serial.println(it->m_val3);
}
// using an explicit function
someData.sort( compareSumV123 );
// same but using a lambda:
someData.sort( [](const Player &a, const Player &b){
return a.sum() < b.sum();
});
Serial.println("Scanning through list<Player>, sorted by compareSumV123");
Serial.println(someData.size());
for(auto it = someData.begin(); it != someData.end(); ++it)
{
Serial.println(it->m_name);
Serial.print(it->m_val1); Serial.print(" + ");
Serial.print(it->m_val2); Serial.print(" + ");
Serial.print(it->m_val3); Serial.print(" = ");
Serial.println(it->sum());
}
-
list();
-
list(const list<T> &other);
-
list(list<T> &&other);
-
~list();
-
uint32_t size() const;
-
void resize(uint32_t count);
-
void resize(uint32_t count, const T& value);
-
bool empty() const;
-
T& front();
-
T& back();
-
void push_front(const T &e);
-
void push_front(T &&e);
-
void push_back(const T &e);
-
void push_back(T &&e);
-
iterator insert(iterator pos, const T &e);
-
void insert(iterator pos, uint32_t count, const T &e);
-
void splice(iterator pos, list<T> &other);
-
void splice(iterator pos, list<T> &other, iterator it);
-
void splice(iterator pos, list<T> &other, iterator first, iterator last);
-
void pop_front();
-
void pop_back();
-
void erase(iterator pos);
-
void erase(iterator first, iterator last);
-
void remove(const T& data);
-
template <typename F> void remove_if( F test );
-
void clear();
-
void sort();
-
template<typename F> void sort( F comp );
-
iterator find(const T& data) const;
-
template <typename F> iterator find_if( F test ) const;
-
iterator begin();
-
iterator end();
-
iterator();
-
T &operator*() const;
-
T *operator->() const;
-
bool operator != (const iterator &rhs) const;
-
bool operator == (const iterator &rhs) const;
-
iterator& operator++(int);
-
iterator operator++();
-
iterator& operator--(int);
-
iterator operator--();
-
iterator& operator += (unsigned int count);
-
iterator& operator -= (unsigned int count);
-
iterator operator+(unsigned int count) const;
-
iterator operator-(unsigned int count) const;