author | Craig Dowell <craigdo@ee.washington.edu> |
Wed, 17 Feb 2010 21:50:11 -0800 | |
changeset 5994 | ced6c14c957e |
parent 4054 | d1df606b20f8 |
child 6180 | cd0d8ba00e6c |
permissions | -rw-r--r-- |
150
663120712cd9
fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
131
diff
changeset
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
9
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
2 |
/* |
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
3 |
* Copyright (c) 2005 INRIA |
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
4 |
* |
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or modify |
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
6 |
* it under the terms of the GNU General Public License version 2 as |
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation; |
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
8 |
* |
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
12 |
* GNU General Public License for more details. |
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
13 |
* |
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
15 |
* along with this program; if not, write to the Free Software |
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
17 |
* |
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
18 |
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
19 |
*/ |
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
20 |
|
3806
d0381b7f3030
define Scheduler::Event and use it in Scheduler::Insert
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3805
diff
changeset
|
21 |
#ifndef HEAP_SCHEDULER_H |
d0381b7f3030
define Scheduler::Event and use it in Scheduler::Insert
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3805
diff
changeset
|
22 |
#define HEAP_SCHEDULER_H |
9
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
23 |
|
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
24 |
#include "scheduler.h" |
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
25 |
#include <stdint.h> |
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
26 |
#include <vector> |
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
27 |
|
16
99e833adbb46
change yans namespace to ns3
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
9
diff
changeset
|
28 |
namespace ns3 { |
9
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
29 |
|
3182 | 30 |
/** |
31 |
* \ingroup scheduler |
|
3187 | 32 |
* \brief a binary heap event scheduler |
33 |
* |
|
34 |
* This code started as a c++ translation of a java-based code written in 2005 |
|
35 |
* to implement a heap sort. So, this binary heap is really a pretty |
|
36 |
* straightforward implementation of the classic data structure. Not much to say |
|
37 |
* about it. |
|
38 |
* |
|
39 |
* What is smart about this code ? |
|
40 |
* - it does not use the index 0 in the array to avoid having to convert |
|
41 |
* C-style array indexes (which start at zero) and heap-style indexes |
|
42 |
* (which start at 1). This is why _all_ indexes start at 1, and that |
|
43 |
* the index of the root is 1. |
|
44 |
* - It uses a slightly non-standard while loop for top-down heapify |
|
45 |
* to move one if statement out of the loop. |
|
3182 | 46 |
*/ |
3806
d0381b7f3030
define Scheduler::Event and use it in Scheduler::Insert
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3805
diff
changeset
|
47 |
class HeapScheduler : public Scheduler |
d0381b7f3030
define Scheduler::Event and use it in Scheduler::Insert
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3805
diff
changeset
|
48 |
{ |
9
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
49 |
public: |
4054
d1df606b20f8
add SchedulerType global variable
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3807
diff
changeset
|
50 |
static TypeId GetTypeId (void); |
d1df606b20f8
add SchedulerType global variable
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3807
diff
changeset
|
51 |
|
2913
66dd24c80d75
bug 143: rename scheduler files
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1696
diff
changeset
|
52 |
HeapScheduler (); |
66dd24c80d75
bug 143: rename scheduler files
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1696
diff
changeset
|
53 |
virtual ~HeapScheduler (); |
9
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
54 |
|
3806
d0381b7f3030
define Scheduler::Event and use it in Scheduler::Insert
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3805
diff
changeset
|
55 |
virtual void Insert (const Event &ev); |
1009
adc3ac9baea8
optimize EventImpl refcounting
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
963
diff
changeset
|
56 |
virtual bool IsEmpty (void) const; |
3807
268b86ce5435
don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3806
diff
changeset
|
57 |
virtual Event PeekNext (void) const; |
268b86ce5435
don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3806
diff
changeset
|
58 |
virtual Event RemoveNext (void); |
268b86ce5435
don't use EventId in Schedulers: use Scheduler::Event instead.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3806
diff
changeset
|
59 |
virtual void Remove (const Event &ev); |
1009
adc3ac9baea8
optimize EventImpl refcounting
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
963
diff
changeset
|
60 |
|
9
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
61 |
private: |
3806
d0381b7f3030
define Scheduler::Event and use it in Scheduler::Insert
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3805
diff
changeset
|
62 |
typedef std::vector<Event> BinaryHeap; |
9
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
63 |
|
150
663120712cd9
fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
131
diff
changeset
|
64 |
inline uint32_t Parent (uint32_t id) const; |
663120712cd9
fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
131
diff
changeset
|
65 |
uint32_t Sibling (uint32_t id) const; |
663120712cd9
fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
131
diff
changeset
|
66 |
inline uint32_t LeftChild (uint32_t id) const; |
663120712cd9
fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
131
diff
changeset
|
67 |
inline uint32_t RightChild (uint32_t id) const; |
663120712cd9
fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
131
diff
changeset
|
68 |
inline uint32_t Root (void) const; |
206
525ffa5b4d24
make Heap scheduler remove operation first perform a linear search to find the target location and then perform a correct remove through a top-down heapification
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
194
diff
changeset
|
69 |
/* Return the position in the array of the last element included in it. */ |
150
663120712cd9
fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
131
diff
changeset
|
70 |
uint32_t Last (void) const; |
663120712cd9
fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
131
diff
changeset
|
71 |
inline bool IsRoot (uint32_t id) const; |
663120712cd9
fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
131
diff
changeset
|
72 |
inline bool IsBottom (uint32_t id) const; |
206
525ffa5b4d24
make Heap scheduler remove operation first perform a linear search to find the target location and then perform a correct remove through a top-down heapification
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
194
diff
changeset
|
73 |
inline bool IsLessStrictly (uint32_t a, uint32_t b) const; |
194
882aa1fc50fd
optimize other all comparison operators
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
186
diff
changeset
|
74 |
inline uint32_t Smallest (uint32_t a, uint32_t b) const; |
9
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
75 |
|
150
663120712cd9
fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
131
diff
changeset
|
76 |
inline void Exch (uint32_t a, uint32_t b); |
663120712cd9
fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
131
diff
changeset
|
77 |
void BottomUp (void); |
206
525ffa5b4d24
make Heap scheduler remove operation first perform a linear search to find the target location and then perform a correct remove through a top-down heapification
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
194
diff
changeset
|
78 |
void TopDown (uint32_t start); |
9
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
79 |
|
150
663120712cd9
fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
131
diff
changeset
|
80 |
BinaryHeap m_heap; |
9
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
81 |
}; |
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
82 |
|
3806
d0381b7f3030
define Scheduler::Event and use it in Scheduler::Insert
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3805
diff
changeset
|
83 |
} // namespace ns3 |
9
2c31ae7c94db
import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
84 |
|
3806
d0381b7f3030
define Scheduler::Event and use it in Scheduler::Insert
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3805
diff
changeset
|
85 |
#endif /* HEAP_SCHEDULER_H */ |