mirror of
https://github.com/MariaDB/server.git
synced 2025-07-27 18:02:13 +03:00
MDEV-20297 Support C++11 range-based for loop for List<T>
New iterator has the fastest possible implementation: just moves one pointer. It's faster that List_iterator and List_iterator_fast: both do more on increment. Overall patch brings: 1) work compile times 2) possibly(!) worse debug build performance 3) definitely better optimized build performance 4) ability to write less code 5) ability to write less bug-prone code
This commit is contained in:
@ -21,6 +21,7 @@
|
||||
#endif
|
||||
|
||||
#include "sql_alloc.h"
|
||||
#include <iterator>
|
||||
|
||||
/**
|
||||
Simple intrusive linked list.
|
||||
@ -532,6 +533,51 @@ public:
|
||||
List<T> *res= new (mem_root) List<T>;
|
||||
return res == NULL || res->push_back(first, mem_root) ? NULL : res;
|
||||
}
|
||||
|
||||
class Iterator;
|
||||
using value_type= T;
|
||||
using iterator= Iterator;
|
||||
using const_iterator= const Iterator;
|
||||
|
||||
Iterator begin() const { return Iterator(first); }
|
||||
Iterator end() const { return Iterator(); }
|
||||
|
||||
class Iterator
|
||||
{
|
||||
public:
|
||||
using iterator_category= std::forward_iterator_tag;
|
||||
using value_type= T;
|
||||
using difference_type= std::ptrdiff_t;
|
||||
using pointer= T *;
|
||||
using reference= T &;
|
||||
|
||||
Iterator(list_node *p= &end_of_list) : node{p} {}
|
||||
|
||||
Iterator &operator++()
|
||||
{
|
||||
DBUG_ASSERT(node != &end_of_list);
|
||||
|
||||
node= node->next;
|
||||
return *this;
|
||||
}
|
||||
|
||||
T operator++(int)
|
||||
{
|
||||
Iterator tmp(*this);
|
||||
operator++();
|
||||
return tmp;
|
||||
}
|
||||
|
||||
T &operator*() { return *static_cast<T *>(node->info); }
|
||||
|
||||
bool operator!=(const typename List<T>::iterator &rhs)
|
||||
{
|
||||
return node != rhs.node;
|
||||
}
|
||||
|
||||
private:
|
||||
list_node *node{&end_of_list};
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user