A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
heap-scheduler.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2006 INRIA
3 * Copyright (c) 2005 Mathieu Lacage
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
8 *
9 */
10
11#include "heap-scheduler.h"
12
13#include "assert.h"
14#include "log.h"
15
16/**
17 * @file
18 * @ingroup scheduler
19 * Implementation of ns3::HeapScheduler class.
20 */
21
22namespace ns3
23{
24
25NS_LOG_COMPONENT_DEFINE("HeapScheduler");
26
28
31{
32 static TypeId tid = TypeId("ns3::HeapScheduler")
34 .SetGroupName("Core")
35 .AddConstructor<HeapScheduler>();
36 return tid;
37}
38
40{
41 NS_LOG_FUNCTION(this);
42 // we purposely waste an item at the start of
43 // the array to make sure the indexes in the
44 // array start at one.
45 Scheduler::Event empty = {nullptr, {0, 0}};
46 m_heap.push_back(empty);
47}
48
53
54std::size_t
55HeapScheduler::Parent(std::size_t id) const
56{
57 NS_LOG_FUNCTION(this << id);
58 return id / 2;
59}
60
61std::size_t
62HeapScheduler::Sibling(std::size_t id) const
63{
64 NS_LOG_FUNCTION(this << id);
65 return id + 1;
66}
67
68std::size_t
69HeapScheduler::LeftChild(std::size_t id) const
70{
71 NS_LOG_FUNCTION(this << id);
72 return id * 2;
73}
74
75std::size_t
76HeapScheduler::RightChild(std::size_t id) const
77{
78 NS_LOG_FUNCTION(this << id);
79 return id * 2 + 1;
80}
81
82std::size_t
84{
85 NS_LOG_FUNCTION(this);
86 return 1;
87}
88
89bool
90HeapScheduler::IsRoot(std::size_t id) const
91{
92 NS_LOG_FUNCTION(this << id);
93 return (id == Root());
94}
95
96std::size_t
98{
99 NS_LOG_FUNCTION(this);
100 return m_heap.size() - 1;
101}
102
103bool
104HeapScheduler::IsBottom(std::size_t id) const
105{
106 NS_LOG_FUNCTION(this << id);
107 return (id >= m_heap.size());
108}
109
110void
111HeapScheduler::Exch(std::size_t a, std::size_t b)
112{
113 NS_LOG_FUNCTION(this << a << b);
114 NS_ASSERT(b < m_heap.size() && a < m_heap.size());
115 NS_LOG_DEBUG("Exch " << a << ", " << b);
116 Event tmp(m_heap[a]);
117 m_heap[a] = m_heap[b];
118 m_heap[b] = tmp;
119}
120
121bool
122HeapScheduler::IsLessStrictly(std::size_t a, std::size_t b) const
123{
124 NS_LOG_FUNCTION(this << a << b);
125 return m_heap[a] < m_heap[b];
126}
127
128std::size_t
129HeapScheduler::Smallest(std::size_t a, std::size_t b) const
130{
131 NS_LOG_FUNCTION(this << a << b);
132 return IsLessStrictly(a, b) ? a : b;
133}
134
135bool
137{
138 NS_LOG_FUNCTION(this);
139 return (m_heap.size() == 1);
140}
141
142void
144{
145 NS_LOG_FUNCTION(this);
146 std::size_t index = Last();
147 while (!IsRoot(index) && IsLessStrictly(index, Parent(index)))
148 {
149 Exch(index, Parent(index));
150 index = Parent(index);
151 }
152}
153
154void
155HeapScheduler::TopDown(std::size_t start)
156{
157 NS_LOG_FUNCTION(this << start);
158 std::size_t index = start;
159 std::size_t right = RightChild(index);
160 while (!IsBottom(right))
161 {
162 std::size_t left = LeftChild(index);
163 std::size_t tmp = Smallest(left, right);
164 if (IsLessStrictly(index, tmp))
165 {
166 return;
167 }
168 Exch(index, tmp);
169 index = tmp;
170 right = RightChild(index);
171 }
172 if (IsBottom(index))
173 {
174 return;
175 }
176 NS_ASSERT(!IsBottom(index));
177 std::size_t left = LeftChild(index);
178 if (IsBottom(left))
179 {
180 return;
181 }
182 if (IsLessStrictly(index, left))
183 {
184 return;
185 }
186 Exch(index, left);
187}
188
189void
191{
192 NS_LOG_FUNCTION(this << &ev);
193 m_heap.push_back(ev);
194 BottomUp();
195}
196
199{
200 NS_LOG_FUNCTION(this);
201 return m_heap[Root()];
202}
203
206{
207 NS_LOG_FUNCTION(this);
208 Event next = m_heap[Root()];
209 Exch(Root(), Last());
210 m_heap.pop_back();
211 TopDown(Root());
212 return next;
213}
214
215void
217{
218 NS_LOG_FUNCTION(this << &ev);
219 std::size_t uid = ev.key.m_uid;
220 for (std::size_t i = 1; i < m_heap.size(); i++)
221 {
222 if (uid == m_heap[i].key.m_uid)
223 {
224 NS_ASSERT(m_heap[i].impl == ev.impl);
225 Exch(i, Last());
226 m_heap.pop_back();
227 TopDown(i);
228 return;
229 }
230 }
231 NS_ASSERT(false);
232}
233
234} // namespace ns3
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
a binary heap event scheduler
std::size_t Root() const
Get the root index of the heap.
void TopDown(std::size_t start)
Percolate a deletion bubble down the heap.
void Insert(const Scheduler::Event &ev) override
Insert a new Event in the schedule.
~HeapScheduler() override
Destructor.
static TypeId GetTypeId()
Register this type.
Scheduler::Event RemoveNext() override
Remove the earliest event from the event list.
bool IsLessStrictly(std::size_t a, std::size_t b) const
Compare (less than) two items.
HeapScheduler()
Constructor.
std::size_t RightChild(std::size_t id) const
Get the right child index of a given entry.
BinaryHeap m_heap
The event list.
bool IsRoot(std::size_t id) const
Test if an index is the root.
Scheduler::Event PeekNext() const override
Get a pointer to the next event.
void Remove(const Scheduler::Event &ev) override
Remove a specific event from the event list.
void Exch(std::size_t a, std::size_t b)
Swap two items.
std::size_t Smallest(std::size_t a, std::size_t b) const
Minimum of two items.
std::size_t Sibling(std::size_t id) const
Get the next sibling of a given entry.
std::size_t Parent(std::size_t id) const
Get the parent index of a given entry.
bool IsBottom(std::size_t id) const
Test if an index is at the bottom of the heap.
std::size_t LeftChild(std::size_t id) const
Get the left child of a given entry.
void BottomUp()
Percolate a newly inserted Last item to its proper position.
std::size_t Last() const
Return the index of the last element.
bool IsEmpty() const override
Test if the schedule is empty.
Maintain the event list.
Definition scheduler.h:146
a unique identifier for an interface.
Definition type-id.h:49
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:999
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:194
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:260
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
ns3::HeapScheduler declaration.
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Scheduler event.
Definition scheduler.h:173
EventKey key
Key for sorting and ordering Events.
Definition scheduler.h:175
EventImpl * impl
Pointer to the event implementation.
Definition scheduler.h:174
uint32_t m_uid
Event unique id.
Definition scheduler.h:161