linked_list.hpp
Go to the documentation of this file.
1 /*
2  * Singly-linked list.
3  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
4  */
5 
6 #ifndef UAVCAN_UTIL_LINKED_LIST_HPP_INCLUDED
7 #define UAVCAN_UTIL_LINKED_LIST_HPP_INCLUDED
8 
9 #include <cstdlib>
10 #include <cassert>
11 #include <uavcan/build_config.hpp>
13 
14 namespace uavcan
15 {
19 template <typename T>
21 {
22  T* next_;
23 
24 protected:
26  : next_(UAVCAN_NULLPTR)
27  { }
28 
30 
31 public:
32  T* getNextListNode() const { return next_; }
33 
35  {
36  next_ = node;
37  }
38 };
39 
43 template <typename T>
45 {
46  T* root_;
47 
48 public:
50  : root_(UAVCAN_NULLPTR)
51  { }
52 
53  T* get() const { return root_; }
54  bool isEmpty() const { return get() == UAVCAN_NULLPTR; }
55 
59  unsigned getLength() const;
60 
66  void insert(T* node);
67 
73  template <typename Predicate>
74  void insertBefore(T* node, Predicate predicate);
75 
80  void remove(const T* node);
81 };
82 
83 // ----------------------------------------------------------------------------
84 
85 /*
86  * LinkedListRoot<>
87  */
88 template <typename T>
90 {
91  T* node = root_;
92  unsigned cnt = 0;
93  while (node)
94  {
95  cnt++;
96  node = node->getNextListNode();
97  }
98  return cnt;
99 }
100 
101 template <typename T>
103 {
104  if (node == UAVCAN_NULLPTR)
105  {
106  UAVCAN_ASSERT(0);
107  return;
108  }
109  remove(node); // Making sure there will be no loops
110  node->setNextListNode(root_);
111  root_ = node;
112 }
113 
114 template <typename T>
115 template <typename Predicate>
116 void LinkedListRoot<T>::insertBefore(T* node, Predicate predicate)
117 {
118  if (node == UAVCAN_NULLPTR)
119  {
120  UAVCAN_ASSERT(0);
121  return;
122  }
123 
124  remove(node);
125 
126  if (root_ == UAVCAN_NULLPTR || predicate(root_))
127  {
128  node->setNextListNode(root_);
129  root_ = node;
130  }
131  else
132  {
133  T* p = root_;
134  while (p->getNextListNode())
135  {
136  if (predicate(p->getNextListNode()))
137  {
138  break;
139  }
140  p = p->getNextListNode();
141  }
142  node->setNextListNode(p->getNextListNode());
143  p->setNextListNode(node);
144  }
145 }
146 
147 template <typename T>
149 {
150  if (root_ == UAVCAN_NULLPTR || node == UAVCAN_NULLPTR)
151  {
152  return;
153  }
154 
155  if (root_ == node)
156  {
157  root_ = root_->getNextListNode();
158  }
159  else
160  {
161  T* p = root_;
162  while (p->getNextListNode())
163  {
164  if (p->getNextListNode() == node)
165  {
166  p->setNextListNode(p->getNextListNode()->getNextListNode());
167  break;
168  }
169  p = p->getNextListNode();
170  }
171  }
172 }
173 
174 }
175 
176 #endif // UAVCAN_UTIL_LINKED_LIST_HPP_INCLUDED
UAVCAN_NULLPTR
#define UAVCAN_NULLPTR
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:51
uavcan::Noncopyable
Definition: templates.hpp:46
templates.hpp
uavcan::LinkedListNode::~LinkedListNode
~LinkedListNode()
Definition: linked_list.hpp:29
uavcan::LinkedListRoot::LinkedListRoot
LinkedListRoot()
Definition: linked_list.hpp:49
get
ROSCPP_DECL bool get(const std::string &key, bool &b)
uavcan::LinkedListRoot::getLength
unsigned getLength() const
Definition: linked_list.hpp:89
uavcan::LinkedListRoot::get
T * get() const
Definition: linked_list.hpp:53
uavcan::LinkedListRoot::remove
void remove(const T *node)
Definition: linked_list.hpp:148
uavcan::LinkedListNode::LinkedListNode
LinkedListNode()
Definition: linked_list.hpp:25
uavcan::LinkedListRoot::insert
void insert(T *node)
Definition: linked_list.hpp:102
uavcan::LinkedListRoot::root_
T * root_
Definition: linked_list.hpp:46
uavcan::LinkedListNode::next_
T * next_
Definition: linked_list.hpp:22
uavcan::LinkedListRoot::isEmpty
bool isEmpty() const
Definition: linked_list.hpp:54
UAVCAN_EXPORT
#define UAVCAN_EXPORT
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:108
uavcan::LinkedListNode::setNextListNode
void setNextListNode(T *node)
Definition: linked_list.hpp:34
build_config.hpp
uavcan::LinkedListRoot::insertBefore
void insertBefore(T *node, Predicate predicate)
Definition: linked_list.hpp:116
uavcan::LinkedListRoot
Definition: linked_list.hpp:44
pyuavcan_v0.introspect.node
node
Definition: introspect.py:398
uavcan::LinkedListNode::getNextListNode
T * getNextListNode() const
Definition: linked_list.hpp:32
uavcan::LinkedListNode
Definition: linked_list.hpp:20
uavcan
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:204
UAVCAN_ASSERT
#define UAVCAN_ASSERT(x)
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:184


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:02