diff --git a/BAN/include/BAN/LinkedList.h b/BAN/include/BAN/LinkedList.h index a504ec951..53eae21e8 100644 --- a/BAN/include/BAN/LinkedList.h +++ b/BAN/include/BAN/LinkedList.h @@ -30,15 +30,15 @@ namespace BAN [[nodiscard]] ErrorOr push_back(const T&); [[nodiscard]] ErrorOr push_back(T&&); - [[nodiscard]] ErrorOr insert(const_iterator, const T&); - [[nodiscard]] ErrorOr insert(const_iterator, T&&); + [[nodiscard]] ErrorOr insert(iterator, const T&); + [[nodiscard]] ErrorOr insert(iterator, T&&); template [[nodiscard]] ErrorOr emplace_back(Args&&...); template - [[nodiscard]] ErrorOr emplace(const_iterator, Args&&...); + [[nodiscard]] ErrorOr emplace(iterator, Args&&...); void pop_back(); - void remove(const_iterator); + void remove(iterator); void clear(); iterator begin() { return iterator(m_data, empty()); } @@ -51,6 +51,8 @@ namespace BAN const T& front() const; T& front(); + bool contains(const T&) const; + size_type size() const; bool empty() const; @@ -77,6 +79,7 @@ namespace BAN { public: using value_type = T; + using data_type = maybe_const_t::Node>; public: LinkedListIterator() = default; @@ -101,10 +104,10 @@ namespace BAN operator bool() const; private: - LinkedListIterator(typename LinkedList::Node*, bool); + LinkedListIterator(data_type*, bool); private: - typename LinkedList::Node* m_current = nullptr; + data_type* m_current = nullptr; bool m_past_end = false; friend class LinkedList; @@ -148,13 +151,13 @@ namespace BAN } template - ErrorOr LinkedList::insert(const_iterator iter, const T& value) + ErrorOr LinkedList::insert(iterator iter, const T& value) { return insert(iter, move(T(value))); } template - ErrorOr LinkedList::insert(const_iterator iter, T&& value) + ErrorOr LinkedList::insert(iterator iter, T&& value) { Node* next = iter.m_past_end ? nullptr : iter.m_current; Node* prev = next ? next->prev : m_last; @@ -177,7 +180,7 @@ namespace BAN template template - ErrorOr LinkedList::emplace(const_iterator iter, Args&&... args) + ErrorOr LinkedList::emplace(iterator iter, Args&&... args) { Node* next = iter.m_past_end ? nullptr : iter.m_current; Node* prev = next ? next->prev : m_last; @@ -198,7 +201,7 @@ namespace BAN } template - void LinkedList::remove(const_iterator iter) + void LinkedList::remove(iterator iter) { ASSERT(!empty() && iter); Node* node = iter.m_current; @@ -255,6 +258,19 @@ namespace BAN return *iterator(m_data); } + template + bool LinkedList::contains(const T& value) const + { + if (empty()) return false; + for (Node* node = m_data;; node = node->next) + { + if (node->value == value) + return true; + if (node == m_last) + return false; + } + } + template typename LinkedList::size_type LinkedList::size() const { @@ -287,9 +303,9 @@ namespace BAN } template - LinkedListIterator::LinkedListIterator(typename LinkedList::Node* node, bool past_end) - : m_current(node), - m_past_end(past_end) + LinkedListIterator::LinkedListIterator(data_type* node, bool past_end) + : m_current(node) + , m_past_end(past_end) { }