[llvm-commits] [llvm] r103647 - in /llvm/trunk: include/llvm/ADT/ilist_node.h unittests/ADT/ilistTest.cpp
Chris Lattner
clattner at apple.com
Wed May 12 16:17:50 PDT 2010
On May 12, 2010, at 2:35 PM, Daniel Dunbar wrote:
> Author: ddunbar
> Date: Wed May 12 16:35:19 2010
> New Revision: 103647
>
> URL: http://llvm.org/viewvc/llvm-project?rev=103647&view=rev
> Log:
> 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::
Works for me, thanks.
-Chris
>
> if (T *Prev = foo->getPrevNode())
> ...
>
> instead of::
>
> iterator it(foo);
> if (it != begin()) {
> --it;
> ...
> }
>
> - Chris, please review.
>
> Added:
> llvm/trunk/unittests/ADT/ilistTest.cpp
> Modified:
> llvm/trunk/include/llvm/ADT/ilist_node.h
>
> Modified: llvm/trunk/include/llvm/ADT/ilist_node.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/ilist_node.h?rev=103647&r1=103646&r2=103647&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/ADT/ilist_node.h (original)
> +++ llvm/trunk/include/llvm/ADT/ilist_node.h Wed May 12 16:35:19 2010
> @@ -49,6 +49,56 @@
> 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
>
> Added: llvm/trunk/unittests/ADT/ilistTest.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/ilistTest.cpp?rev=103647&view=auto
> ==============================================================================
> --- llvm/trunk/unittests/ADT/ilistTest.cpp (added)
> +++ llvm/trunk/unittests/ADT/ilistTest.cpp Wed May 12 16:35:19 2010
> @@ -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 <ostream>
> +#include "gtest/gtest.h"
> +#include "llvm/ADT/ilist.h"
> +#include "llvm/ADT/ilist_node.h"
> +
> +using namespace llvm;
> +
> +namespace {
> +
> +struct Node : ilist_node<Node> {
> + int Value;
> +
> + Node() {}
> + Node(int _Value) : Value(_Value) {}
> +};
> +
> +TEST(ilistTest, Basic) {
> + ilist<Node> 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);
> +}
> +
> +}
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
More information about the llvm-commits
mailing list