00001 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2005 INRIA 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 * 00018 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> 00019 */ 00020 00021 #ifndef HEAP_SCHEDULER_H 00022 #define HEAP_SCHEDULER_H 00023 00024 #include "scheduler.h" 00025 #include <stdint.h> 00026 #include <vector> 00027 00028 namespace ns3 { 00029 00030 /** 00031 * \ingroup scheduler 00032 * \brief a binary heap event scheduler 00033 * 00034 * This code started as a c++ translation of a java-based code written in 2005 00035 * to implement a heap sort. So, this binary heap is really a pretty 00036 * straightforward implementation of the classic data structure. Not much to say 00037 * about it. 00038 * 00039 * What is smart about this code ? 00040 * - it does not use the index 0 in the array to avoid having to convert 00041 * C-style array indexes (which start at zero) and heap-style indexes 00042 * (which start at 1). This is why _all_ indexes start at 1, and that 00043 * the index of the root is 1. 00044 * - It uses a slightly non-standard while loop for top-down heapify 00045 * to move one if statement out of the loop. 00046 */ 00047 class HeapScheduler : public Scheduler 00048 { 00049 public: 00050 static TypeId GetTypeId (void); 00051 00052 HeapScheduler (); 00053 virtual ~HeapScheduler (); 00054 00055 virtual void Insert (const Event &ev); 00056 virtual bool IsEmpty (void) const; 00057 virtual Event PeekNext (void) const; 00058 virtual Event RemoveNext (void); 00059 virtual void Remove (const Event &ev); 00060 00061 private: 00062 typedef std::vector<Event> BinaryHeap; 00063 00064 inline uint32_t Parent (uint32_t id) const; 00065 uint32_t Sibling (uint32_t id) const; 00066 inline uint32_t LeftChild (uint32_t id) const; 00067 inline uint32_t RightChild (uint32_t id) const; 00068 inline uint32_t Root (void) const; 00069 /* Return the position in the array of the last element included in it. */ 00070 uint32_t Last (void) const; 00071 inline bool IsRoot (uint32_t id) const; 00072 inline bool IsBottom (uint32_t id) const; 00073 inline bool IsLessStrictly (uint32_t a, uint32_t b) const; 00074 inline uint32_t Smallest (uint32_t a, uint32_t b) const; 00075 00076 inline void Exch (uint32_t a, uint32_t b); 00077 void BottomUp (void); 00078 void TopDown (uint32_t start); 00079 00080 BinaryHeap m_heap; 00081 }; 00082 00083 } // namespace ns3 00084 00085 #endif /* HEAP_SCHEDULER_H */