diff options
author | Marko Mäkelä <marko.makela@mariadb.com> | 2020-05-31 10:28:59 +0300 |
---|---|---|
committer | Marko Mäkelä <marko.makela@mariadb.com> | 2020-05-31 10:28:59 +0300 |
commit | 4a0b56f60436cab3f2f60b15ec699cb19c1e10ba (patch) | |
tree | 141f7f177748f763dab14484514453cec367c585 /include | |
parent | 0bf843cd13981b03920bfc49c646b28a130f5d47 (diff) | |
parent | 6da14d7b4a935466de55a6aa87db14bc359dbd30 (diff) | |
download | mariadb-git-4a0b56f60436cab3f2f60b15ec699cb19c1e10ba.tar.gz |
Merge 10.4 into 10.5
Diffstat (limited to 'include')
-rw-r--r-- | include/ilist.h (renamed from include/intrusive_list.h) | 99 | ||||
-rw-r--r-- | include/mysql/service_my_snprintf.h | 6 |
2 files changed, 82 insertions, 23 deletions
diff --git a/include/intrusive_list.h b/include/ilist.h index d745c6c6c62..46e15e9998a 100644 --- a/include/intrusive_list.h +++ b/include/ilist.h @@ -21,26 +21,28 @@ #include <cstddef> #include <iterator> -namespace intrusive -{ - // Derive your class from this struct to insert to a linked list. -template <class Tag= void> struct list_node +template <class Tag= void> struct ilist_node { - list_node(list_node *next= NULL, list_node *prev= NULL) - : next(next), prev(prev) + ilist_node() +#ifndef DBUG_OFF + : + next(NULL), prev(NULL) +#endif { } - list_node *next; - list_node *prev; + ilist_node(ilist_node *next, ilist_node *prev) : next(next), prev(prev) {} + + ilist_node *next; + ilist_node *prev; }; // Modelled after std::list<T> -template <class T, class Tag= void> class list +template <class T, class Tag= void> class ilist { public: - typedef list_node<Tag> ListNode; + typedef ilist_node<Tag> ListNode; class Iterator; // All containers in C++ should define these types to implement generic @@ -103,10 +105,10 @@ public: private: ListNode *node_; - friend class list; + friend class ilist; }; - list() : sentinel_(&sentinel_, &sentinel_), size_(0) {} + ilist() : sentinel_(&sentinel_, &sentinel_) {} reference front() { return *begin(); } reference back() { return *--end(); } @@ -129,14 +131,18 @@ public: reverse_iterator rend() { return reverse_iterator(begin()); } const_reverse_iterator rend() const { return reverse_iterator(begin()); } - bool empty() const { return size_ == 0; } - size_type size() const { return size_; } + bool empty() const { return sentinel_.next == &sentinel_; } + + // Not implemented because it's O(N) + // size_type size() const + // { + // return static_cast<size_type>(std::distance(begin(), end())); + // } void clear() { sentinel_.next= &sentinel_; sentinel_.prev= &sentinel_; - size_= 0; } iterator insert(iterator pos, reference value) @@ -150,7 +156,6 @@ public: static_cast<ListNode &>(value).prev= prev; static_cast<ListNode &>(value).next= curr; - ++size_; return iterator(&value); } @@ -162,13 +167,12 @@ public: prev->next= next; next->prev= prev; - // This is not required for list functioning. But maybe it'll prevent bugs - // and ease debugging. +#ifndef DBUG_OFF ListNode *curr= pos.node_; curr->prev= NULL; curr->next= NULL; +#endif - --size_; return next; } @@ -179,12 +183,63 @@ public: void pop_front() { erase(begin()); } // STL version is O(n) but this is O(1) because an element can't be inserted - // several times in the same intrusive list. + // several times in the same ilist. void remove(reference value) { erase(iterator(&value)); } private: ListNode sentinel_; - size_type size_; }; -} // namespace intrusive +// Similar to ilist but also has O(1) size() method. +template <class T, class Tag= void> class sized_ilist : public ilist<T, Tag> +{ + typedef ilist<T, Tag> BASE; + +public: + // All containers in C++ should define these types to implement generic + // container interface. + typedef T value_type; + typedef std::size_t size_type; + typedef std::ptrdiff_t difference_type; + typedef value_type &reference; + typedef const value_type &const_reference; + typedef T *pointer; + typedef const T *const_pointer; + typedef typename BASE::Iterator iterator; + typedef const typename BASE::Iterator const_iterator; + typedef std::reverse_iterator<iterator> reverse_iterator; + typedef std::reverse_iterator<const iterator> const_reverse_iterator; + + sized_ilist() : size_(0) {} + + size_type size() const { return size_; } + + void clear() + { + BASE::clear(); + size_= 0; + } + + iterator insert(iterator pos, reference value) + { + ++size_; + return BASE::insert(pos, value); + } + + iterator erase(iterator pos) + { + --size_; + return BASE::erase(pos); + } + + void push_back(reference value) { insert(BASE::end(), value); } + void pop_back() { erase(BASE::end()); } + + void push_front(reference value) { insert(BASE::begin(), value); } + void pop_front() { erase(BASE::begin()); } + + void remove(reference value) { erase(iterator(&value)); } + +private: + size_type size_; +}; diff --git a/include/mysql/service_my_snprintf.h b/include/mysql/service_my_snprintf.h index bd1f069c527..6757a658fb6 100644 --- a/include/mysql/service_my_snprintf.h +++ b/include/mysql/service_my_snprintf.h @@ -55,7 +55,8 @@ Supported formats are 's' (null pointer is accepted, printed as "(null)"), 'b' (extension, see below), 'c', 'd', 'i', 'u', 'x', 'o', - 'X', 'p' (works as 0x%x), 'f', 'g', 'M' (extension, see below). + 'X', 'p' (works as 0x%x), 'f', 'g', 'M' (extension, see below), + 'T' (extension, see below). Standard syntax for positional arguments $n is supported. @@ -69,6 +70,9 @@ Format 'M': takes one integer, prints this integer, space, double quote error message, double quote. In other words printf("%M", n) === printf("%d \"%s\"", n, strerror(n)) + + Format 'T': takes string and print it like s but if the strints should be + truncated puts "..." at the end. */ #ifdef __cplusplus |