SIMLIB/C++
3.09
Toggle main menu visibility
Loading...
Searching...
No Matches
src
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
23
SIMLIB_IMPLEMENTATION
;
24
25
26
////////////////////////////////////////////////////////////////////////////
27
// constructors
28
//
29
Queue::Queue
()
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
//
43
Queue::~Queue
() {
44
Dprintf
((
"Queue{%p}::~Queue() // \"%s\" "
,
this
,
Name
().c_str()));
45
}
46
47
////////////////////////////////////////////////////////////////////////////
48
// Insert --- priority insert into queue
49
//
50
void
Queue::Insert
(
Entity
*ent)
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
//
73
void
Queue::InsFirst
(
Entity
*ent)
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
//
82
void
Queue::InsLast
(
Entity
*ent)
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
//
91
void
Queue::PostIns
(
Entity
*ent,
iterator
pos)
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
//
102
void
Queue::PredIns
(
Entity
*ent,
iterator
pos)
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
//
113
Entity
*
Queue::GetFirst
()
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
//
123
Entity
*
Queue::GetLast
()
124
{
125
Dprintf
((
"%s::GetLast()"
,
Name
().c_str()));
126
Entity
*ent =
Get
(--
end
());
127
return
ent;
128
}
129
130
////////////////////////////////////////////////////////////////////////////
131
// Get --- remove item
132
//
133
Entity
*
Queue::Get
(
iterator
pos)
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
//
145
void
Queue::clear
()
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
simlib3::Entity
abstract base class for active entities (Process, Event) instances of derived classes provide Behavio...
Definition
simlib.h:380
simlib3::Entity::Priority_t
EntityPriority_t Priority_t
Definition
simlib.h:402
simlib3::Entity::_MarkTime
double _MarkTime
Definition
simlib.h:389
simlib3::Entity::Priority
Priority_t Priority
priority of the entity (scheduling,queues)
Definition
simlib.h:404
simlib3::List::Get
virtual Link * Get(iterator pos)
remove at position
Definition
list.cc:140
simlib3::List::size
unsigned size() const
Definition
simlib.h:676
simlib3::List::PredIns
void PredIns(Link *e, iterator pos)
Definition
list.cc:106
simlib3::List::clear
void clear()
Definition
list.cc:159
simlib3::Queue::StatDT
Stat StatDT
Definition
simlib.h:697
simlib3::Queue::end
iterator end()
Definition
simlib.h:705
simlib3::Queue::Get
virtual Entity * Get(iterator pos)
remove at position
Definition
queue.cc:133
simlib3::Queue::GetFirst
Entity * GetFirst()
Definition
queue.cc:113
simlib3::Queue::InsFirst
void InsFirst(Entity *e)
Definition
queue.cc:73
simlib3::Queue::PredIns
void PredIns(Entity *e, iterator pos)
Definition
queue.cc:102
simlib3::Queue::Insert
virtual void Insert(Entity *e)
Definition
queue.cc:50
simlib3::Queue::begin
iterator begin()
Definition
simlib.h:704
simlib3::Queue::iterator
List::iterator iterator
Definition
simlib.h:695
simlib3::Queue::GetLast
Entity * GetLast()
Definition
queue.cc:123
simlib3::Queue::StatN
TStat StatN
Definition
simlib.h:696
simlib3::Queue::clear
void clear()
initialize
Definition
queue.cc:145
simlib3::Queue::InsLast
void InsLast(Entity *e)
Definition
queue.cc:82
simlib3::Queue::PostIns
void PostIns(Entity *e, iterator pos)
Definition
queue.cc:91
simlib3::Queue::~Queue
~Queue()
Definition
queue.cc:43
simlib3::Queue::Queue
Queue()
Definition
queue.cc:29
simlib3::SimObject::Name
virtual std::string Name() const
get object name
Definition
object.cc:134
simlib3::Stat::Clear
virtual void Clear()
initialize
Definition
stat.cc:77
simlib3::TStat::n
unsigned long n
Definition
simlib.h:606
simlib3::TStat::Clear
virtual void Clear(double initval=0.0)
initialize
Definition
tstat.cc:83
internal.h
Internal header file for SIMLIB/C++.
Dprintf
#define Dprintf(f)
Definition
internal.h:100
SIMLIB_internal_error
#define SIMLIB_internal_error()
Definition
internal.h:167
simlib3
Implementation of class CalendarList interface is static - using global functions in SQS namespace.
Definition
algloop.cc:32
simlib3::Time
const double & Time
model time (is NOT the block)
Definition
run.cc:48
simlib3::SIMLIB_IMPLEMENTATION
SIMLIB_IMPLEMENTATION
Definition
algloop.cc:34
simlib3::SIMLIB_create_tmp_name
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
simlib3::SetName
void SetName(SimObject &o, const std::string &name)
assign name to object
Definition
name.cc:42
simlib.h
Main SIMLIB/C++ interface.
Generated on
for SIMLIB/C++ by
1.18.0