From 129742b46b7bfc613463cefa662dcfe6ef218003 Mon Sep 17 00:00:00 2001 From: Henrique Gemignani Passos Lima Date: Thu, 20 Oct 2022 17:10:03 +0300 Subject: [PATCH] Improve list::clear Former-commit-id: 16cb68c709784104f09eba760013e789a7c16d0c --- include/rstl/list.hpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/include/rstl/list.hpp b/include/rstl/list.hpp index d8f735e0..c3dc4522 100644 --- a/include/rstl/list.hpp +++ b/include/rstl/list.hpp @@ -35,16 +35,7 @@ public: } void push_back(const T& val) { do_insert_before(x8_end, val); } void clear() { - // iterator e = end(); - iterator cur = begin(); - while (cur != end()) { - cur = erase(cur); - } - // node *e = x8_end; - // node *cur = x4_start; - // while (cur != e) { - // cur = erase(cur); - // } + erase(begin(), end()); } iterator begin() { return iterator(x4_start); } @@ -52,6 +43,13 @@ public: iterator end() { return iterator(x8_end); } const_iterator end() const { return const_iterator(x8_end); } + void erase(const iterator& start, const iterator& end) { + iterator it = start; + while (it != end) { + erase(it++); + } + } + private: struct node { node* x0_prev;