SIMLIB/C++  3.07
queue.cc
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 //! \file queue.cc Queue implementation (priority queue)
3 //
4 // Copyright (c) 1991-2004 Petr Peringer
5 //
6 // This library is licensed under GNU Library GPL. See the file COPYING.
7 //
8 
9 //
10 // implementation of queue
11 //
12 
13 #include "simlib.h"
14 #include "internal.h"
15 
16 
17 ////////////////////////////////////////////////////////////////////////////
18 // implementation
19 //
20 
21 namespace simlib3 {
22 
24 
25 
26 ////////////////////////////////////////////////////////////////////////////
27 // constructors
28 //
30 {
31  Dprintf(("Queue{%p}::Queue()", this));
32 }
33 
34 Queue::Queue(const char *name)
35 {
36  Dprintf(("Queue{%p}::Queue(\"%s\")", this, name));
37  SetName(name);
38 }
39 
40 ////////////////////////////////////////////////////////////////////////////
41 // destructor
42 //
44  Dprintf(("Queue{%p}::~Queue() // \"%s\" ", this, Name().c_str()));
45 }
46 
47 ////////////////////////////////////////////////////////////////////////////
48 // Insert --- priority insert into queue
49 //
51 {
52  Dprintf(("%s::Insert(%s)", Name().c_str(), ent->Name().c_str() ));
53  Entity::Priority_t prio = ent->Priority;
54  // find (higher priority is first)
55 #if 0 // _INS_FROM_BEGIN
56  Queue::iterator p = begin();
57  for( ; p!=end() && ((Entity*)(*p))->Priority >= prio; ++p);
58 #else
59  // this is faster (items are inserted at end usually)
60  Queue::iterator p = end();
61  while(p!=begin()) {
62  Queue::iterator q = p;
63  --p;
64  if( ((Entity*)(*p))->Priority >= prio ) { p = q; break; }
65  }
66 #endif
67  PredIns(ent,p); // works for end()
68 }
69 
70 ////////////////////////////////////////////////////////////////////////////
71 // InsFirst --- insert at first position (special case)
72 //
74 {
75  Dprintf(("%s::InsFirst(%s)", Name().c_str(), ent->Name().c_str() ));
76  PredIns(ent,begin());
77 }
78 
79 ////////////////////////////////////////////////////////////////////////////
80 // InsLast --- insert at last position (for FIFO)
81 //
83 {
84  Dprintf(("%s::InsLast(%s)", Name().c_str(), ent->Name().c_str() ));
85  PredIns(ent,end());
86 }
87 
88 ////////////////////////////////////////////////////////////////////////////
89 // PostIns --- insert after entity
90 //
92 {
93  Dprintf(("%s::PostIns(%s,pos)", Name().c_str(), ent->Name().c_str(), *pos ));
94  if(pos==end())
95  SIMLIB_internal_error(); // add error message
96  PredIns(ent, ++pos);
97 }
98 
99 ////////////////////////////////////////////////////////////////////////////
100 // PredIns --- insert before entity
101 //
103 {
104  Dprintf(("%s::PredIns(%s,pos:%p)", Name().c_str(), ent->Name().c_str(), *pos ));
105  List::PredIns(ent, *pos); // insert before pos, can be end()
106  ent->_MarkTime = Time; // marks input time
107  StatN(size()); // length statistic
108 }
109 
110 ////////////////////////////////////////////////////////////////////////////
111 // GetFirst --- remove first entity from queue
112 //
114 {
115  Dprintf(("%s::GetFirst()", Name().c_str()));
116  Entity *ent = Get(begin());
117  return ent;
118 }
119 
120 ////////////////////////////////////////////////////////////////////////////
121 // GetLast --- remove last entity from queue
122 //
124 {
125  Dprintf(("%s::GetLast()", Name().c_str()));
126  Entity *ent = Get(--end());
127  return ent;
128 }
129 
130 ////////////////////////////////////////////////////////////////////////////
131 // Get --- remove item
132 //
134 {
135  Dprintf(("%s::Get(pos:%p)", Name().c_str(), *pos));
136  Entity *ent = static_cast<Entity*>(List::Get(*pos));
137  StatDT(Time - ent->_MarkTime);
138  StatN(size()); StatN.n--; // the number of samples correction
139  return ent;
140 }
141 
142 ////////////////////////////////////////////////////////////////////////////
143 // clear - initialization of list
144 //
146 {
147  Dprintf(("%s::Clear()", Name().c_str()));
148  StatN.Clear();
149  StatDT.Clear();
150  List::clear(); // problem with WARNING
151  StatN.Clear();
152  StatDT.Clear();
153 }
154 
155 #if 0
156 ////////////////////////////////////////////////////////////////////////////
157 // Process::Name --- name of the process
158 //
159 const char *Queue::Name() const
160 {
161  const char *name = SimObject::Name();
162  if(*name) return name; // has explicit name
163  else return SIMLIB_create_tmp_name("Queue{%p}", this);
164 }
165 #endif
166 
167 }
168 // end
169 
iterator begin()
Definition: simlib.h:699
void PredIns(Link *e, iterator pos)
Definition: list.cc:106
unsigned long n
Definition: simlib.h:601
void InsLast(Entity *e)
Definition: queue.cc:82
virtual void Clear()
initialize
Definition: stat.cc:77
void SetName(const std::string &name)
assign the name
Definition: object.cc:125
Priority_t Priority
priority of the entity (scheduling,queues)
Definition: simlib.h:399
TStat StatN
Definition: simlib.h:691
virtual Link * Get(iterator pos)
remove at position
Definition: list.cc:140
unsigned size() const
Definition: simlib.h:671
Implementation of class CalendarList interface is static - using global functions in SQS namespace...
Definition: algloop.cc:32
void InsFirst(Entity *e)
Definition: queue.cc:73
std::string SIMLIB_create_tmp_name(const char *fmt,...)
printf-like function to create temporary name (the length of temporary names is limited) used only ...
Definition: name.cc:80
abstract base class for active entities (Process, Event) instances of derived classes provide Behavio...
Definition: simlib.h:375
const double & Time
model time (is NOT the block)
Definition: run.cc:48
void clear()
Definition: list.cc:159
iterator end()
Definition: simlib.h:700
#define SIMLIB_internal_error()
Definition: internal.h:167
void PostIns(Entity *e, iterator pos)
Definition: queue.cc:91
Entity * GetLast()
Definition: queue.cc:123
Internal header file for SIMLIB/C++.
Main SIMLIB/C++ interface.
virtual std::string Name() const
get object name
Definition: object.cc:134
SIMLIB_IMPLEMENTATION
Definition: algloop.cc:34
void clear()
initialize
Definition: queue.cc:145
EntityPriority_t Priority_t
Definition: simlib.h:397
Entity * GetFirst()
Definition: queue.cc:113
#define Dprintf(f)
Definition: internal.h:100
virtual Entity * Get(iterator pos)
remove at position
Definition: queue.cc:133
void PredIns(Entity *e, iterator pos)
Definition: queue.cc:102
double _MarkTime
Definition: simlib.h:384
Stat StatDT
Definition: simlib.h:692
virtual void Insert(Entity *e)
Definition: queue.cc:50
virtual void Clear(double initval=0.0)
initialize
Definition: tstat.cc:83