From aa81380353a27d9d216cafdd88df08a5eef43b74 Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Wed, 12 May 2010 21:35:19 +0000 Subject: [PATCH] ADT: Add ilist_node::get{Prev,Next}Node, which return the adjacent node or null. - This provides a convenient alternative to using something llvm::prior or manual iterator access, for example:: if (T *Prev = foo->getPrevNode()) ... instead of:: iterator it(foo); if (it != begin()) { --it; ... } - Chris, please review. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@103647 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/ADT/ilist_node.h | 50 +++++++++++++++++++++++++++++++++++ unittests/ADT/ilistTest.cpp | 39 +++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 unittests/ADT/ilistTest.cpp diff --git a/include/llvm/ADT/ilist_node.h b/include/llvm/ADT/ilist_node.h index da25f959e61..3de4f156d20 100644 --- a/include/llvm/ADT/ilist_node.h +++ b/include/llvm/ADT/ilist_node.h @@ -49,6 +49,56 @@ class ilist_node : private ilist_half_node { void setNext(NodeTy *N) { Next = N; } protected: ilist_node() : Next(0) {} + +public: + /// @name Adjacent Node Accessors + /// @{ + + /// \brief Get the previous node, or 0 for the list head. + NodeTy *getPrevNode() { + NodeTy *Prev = this->getPrev(); + + // Check for sentinel. + if (!Prev->getNext()) + return 0; + + return Prev; + } + + /// \brief Get the previous node, or 0 for the list head. + const NodeTy *getPrevNode() const { + NodeTy *Prev = this->getPrev(); + + // Check for sentinel. + if (!Prev->getNext()) + return 0; + + return Prev; + } + + /// \brief Get the next node, or 0 for the list tail. + NodeTy *getNextNode() { + NodeTy *Next = getNext(); + + // Check for sentinel. + if (!Next->getNext()) + return 0; + + return Next; + } + + /// \brief Get the next node, or 0 for the list tail. + const NodeTy *getNextNode() const { + NodeTy *Next = getNext(); + + // Check for sentinel. + if (!Next->getNext()) + return 0; + + return Next; + } + + /// @} }; } // End llvm namespace diff --git a/unittests/ADT/ilistTest.cpp b/unittests/ADT/ilistTest.cpp new file mode 100644 index 00000000000..3bf04dccc2c --- /dev/null +++ b/unittests/ADT/ilistTest.cpp @@ -0,0 +1,39 @@ +//===- llvm/unittest/ADT/APInt.cpp - APInt unit tests ---------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include +#include "gtest/gtest.h" +#include "llvm/ADT/ilist.h" +#include "llvm/ADT/ilist_node.h" + +using namespace llvm; + +namespace { + +struct Node : ilist_node { + int Value; + + Node() {} + Node(int _Value) : Value(_Value) {} +}; + +TEST(ilistTest, Basic) { + ilist List; + List.push_back(Node(1)); + EXPECT_EQ(1, List.back().Value); + EXPECT_EQ(0, List.back().getPrevNode()); + EXPECT_EQ(0, List.back().getNextNode()); + + List.push_back(Node(2)); + EXPECT_EQ(2, List.back().Value); + EXPECT_EQ(2, List.front().getNextNode()->Value); + EXPECT_EQ(1, List.back().getPrevNode()->Value); +} + +}