SIMLIB/C++
3.09
Toggle main menu visibility
Loading...
Searching...
No Matches
src
list.cc
Go to the documentation of this file.
1
/////////////////////////////////////////////////////////////////////////////
2
//! \file list.cc internal double-linked list implementation
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 double-linked list
11
// base for Queue abstraction (and simple Calendar)
12
//
13
14
////////////////////////////////////////////////////////////////////////////
15
// interface
16
//
17
18
#include "
simlib.h
"
19
#include "
internal.h
"
20
21
22
////////////////////////////////////////////////////////////////////////////
23
// implementation
24
//
25
26
namespace
simlib3
{
27
28
SIMLIB_IMPLEMENTATION
;
29
30
////////////////////////////////////////////////////////////////////////////
31
// constructors
32
//
33
List::List
() :
Link
(this,this,this),
n
(0)
34
{
35
Dprintf
((
"List::List()"
));
36
}
37
38
List::List
(
const
char
*name) :
Link
(this,this,this),
n
(0)
39
{
40
Dprintf
((
"List::List(\"%s\")"
,name));
41
SetName
(name);
42
}
43
44
////////////////////////////////////////////////////////////////////////////
45
// destructor
46
//
47
List::~List
() {
48
Dprintf
((
"List::~List() // \"%s\" "
,
Name
().c_str()));
49
clear
();
50
head
= 0;
// this is important due to check in ~Link
51
}
52
53
////////////////////////////////////////////////////////////////////////////
54
// InsFirst --- insert first
55
//
56
void
List::InsFirst
(
Link
*ent)
57
{
58
if
(ent->
head
!=0)
59
SIMLIB_internal_error
();
60
ent->
pred
=
this
;
61
ent->
succ
=
Link::succ
;
62
Link::succ
->
pred
= ent;
63
Link::succ
= ent;
64
ent->
head
=
this
;
65
n
++;
// # of list items
66
}
67
68
////////////////////////////////////////////////////////////////////////////
69
// InsLast - insert last
70
//
71
void
List::InsLast
(
Link
*ent)
72
{
73
if
(ent->
head
!=0)
74
SIMLIB_internal_error
();
75
ent->
pred
=
Link::pred
;
76
ent->
succ
=
this
;
77
Link::pred
->
succ
= ent;
78
Link::pred
= ent;
79
ent->
head
=
this
;
80
n
++;
// # of list items
81
}
82
83
////////////////////////////////////////////////////////////////////////////
84
// PostIns - insert after
85
//
86
void
List::PostIns
(
Link
*ent,
iterator
pos)
87
{
88
if
(pos==
end
())
89
SIMLIB_error
(
ListActivityError
);
/// change !!!!
90
Link
*act = *pos;
91
if
(act->
head
!=
this
)
// logic-error
92
SIMLIB_error
(
ListActivityError
);
/// change !!!!
93
if
(ent->
head
!=0)
94
SIMLIB_internal_error
();
95
ent->
pred
= act;
96
ent->
succ
= act->
succ
;
97
act->
succ
->
pred
= ent;
98
act->
succ
= ent;
99
ent->
head
=
this
;
100
n
++;
// # of list items
101
}
102
103
////////////////////////////////////////////////////////////////////////////
104
// PredIns --- insert before
105
//
106
void
List::PredIns
(
Link
*ent,
iterator
pos)
107
{
108
if
(ent->
head
!=0)
109
SIMLIB_internal_error
();
110
Link
*act = *pos;
111
if
(act->
head
!=
this
)
112
SIMLIB_error
(
ListActivityError
);
/// change !!!!
113
ent->
pred
= act->
pred
;
114
ent->
succ
= act;
115
act->
pred
->
succ
= ent;
116
act->
pred
= ent;
117
ent->
head
=
this
;
118
n
++;
// # of list items
119
}
120
121
////////////////////////////////////////////////////////////////////////////
122
// GetFirst --- remove first item from list
123
//
124
Link
*
List::GetFirst
()
125
{
126
return
Get
(
begin
());
127
}
128
129
////////////////////////////////////////////////////////////////////////////
130
// GetLast --- remove last item from list
131
//
132
Link
*
List::GetLast
()
133
{
134
return
Get
(--
end
());
135
}
136
137
////////////////////////////////////////////////////////////////////////////
138
// Get --- remove selected item from list (virtual)
139
//
140
Link
*
List::Get
(
iterator
pos)
141
{
142
if
(
empty
())
143
SIMLIB_error
(
ListEmptyError
);
144
if
(pos==
end
())
145
SIMLIB_error
(
ListActivityError
);
// change !!
146
Link
*x = *pos;
147
if
(x->
head
!=
this
)
148
SIMLIB_error
(
LinkOutError
);
// can't remove if not in list
149
150
(x->
pred
->
succ
= x->
succ
)->
pred
= x->
pred
;
151
x->
head
= 0;
// ---- not in list
152
n
--;
// # of items in list
153
return
(x);
154
}
155
156
////////////////////////////////////////////////////////////////////////////
157
// clear --- list initialization (remove all items)
158
//
159
void
List::clear
()
160
{
161
while
(!
empty
()) {
162
Link
*e =
GetFirst
();
// remove entity from list
163
if
(e->
isAllocated
())
delete
e;
// free memory ?????????????###
164
}
165
}
166
167
}
168
// end
169
simlib3::Link::head
List * head
pointer to List (if any)
Definition
simlib.h:344
simlib3::Link::succ
Link * succ
next object in List
Definition
simlib.h:343
simlib3::Link::Link
Link(Link *p, Link *s, List *h)
Definition
link.cc:37
simlib3::Link::pred
Link * pred
previous object in List
Definition
simlib.h:342
simlib3::List::iterator
Definition
simlib.h:640
simlib3::List::Get
virtual Link * Get(iterator pos)
remove at position
Definition
list.cc:140
simlib3::List::GetLast
Link * GetLast()
remove last
Definition
list.cc:132
simlib3::List::empty
bool empty()
Definition
simlib.h:681
simlib3::List::begin
iterator begin()
Definition
simlib.h:677
simlib3::List::~List
~List()
Definition
list.cc:47
simlib3::List::end
iterator end()
Definition
simlib.h:678
simlib3::List::List
List()
Definition
list.cc:33
simlib3::List::InsFirst
void InsFirst(Link *e)
Definition
list.cc:56
simlib3::List::PredIns
void PredIns(Link *e, iterator pos)
Definition
list.cc:106
simlib3::List::clear
void clear()
Definition
list.cc:159
simlib3::List::PostIns
void PostIns(Link *e, iterator pos)
Definition
list.cc:86
simlib3::List::n
unsigned n
Definition
simlib.h:638
simlib3::List::InsLast
void InsLast(Link *e)
Definition
list.cc:71
simlib3::List::GetFirst
Link * GetFirst()
remove first
Definition
list.cc:124
simlib3::SimObject::isAllocated
bool isAllocated() const
Definition
simlib.h:323
simlib3::SimObject::Name
virtual std::string Name() const
get object name
Definition
object.cc:134
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::SIMLIB_error
void SIMLIB_error(const enum _ErrEnum N)
print error message and abort program
Definition
error.cc:38
simlib3::SIMLIB_IMPLEMENTATION
SIMLIB_IMPLEMENTATION
Definition
algloop.cc:34
simlib3::LinkOutError
@ LinkOutError
Definition
errors.cc:27
simlib3::ListEmptyError
@ ListEmptyError
Definition
errors.cc:36
simlib3::ListActivityError
@ ListActivityError
Definition
errors.cc:35
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