-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathTimeTable.cpp
More file actions
217 lines (194 loc) · 4.9 KB
/
Copy pathTimeTable.cpp
File metadata and controls
217 lines (194 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* @file TimeTable.cpp
* @brief Implements the methods of the TimeTable class.
* @copyright Copyright (C) 2025 ForeFire, Fire Team, SPE, CNRS/Universita di Corsica.
* @license This program is free software; See LICENSE file for details. (See LICENSE file).
* @author Jean‑Baptiste Filippi — 2025
*/
#include "TimeTable.h"
using namespace std;
namespace libforefire {
TimeTable::TimeTable() {
commonInitialization();
}
TimeTable::TimeTable(FFEvent* ev) {
head = nullptr;
commonInitialization();
insert(ev);
}
TimeTable::~TimeTable() {
while ( size() > 0 ) dropEvent(head);
}
void TimeTable::commonInitialization(){
incr = 0;
decr = 0;
}
void TimeTable::setHead(FFEvent* newHead){
head->getPrev()->setNext(newHead);
newHead->setPrev(head->getPrev());
head = newHead;
}
FFEvent* TimeTable::getHead(){
return head;
}
double TimeTable::getTime(){
if ( !head ){
return -numeric_limits<double>::infinity();
}
if(size()==0 ) {
return -numeric_limits<double>::infinity();
}
return head->getTime();
}
void TimeTable::increment(){
incr++;
}
void TimeTable::decrement(){
decr++;
}
size_t TimeTable::size(){
return incr-decr;
}
FFEvent* TimeTable::getUpcomingEvent(){
FFEvent* upEvent = head;
if ( size() > 1 ) {
setHead(head->getNext());
decrement();
} else if ( size() == 1 ) {
// this is the only event left
decrement();
} else {
// no events left to be treated (size=0)
//cout << "ForeFire simulation ended with no more event to be treated" << endl;
upEvent = nullptr;
}
return upEvent;
}
void TimeTable::insertBefore(FFEvent* newEv){
// checking the event consistency
double evTime = newEv->getTime();
if ( evTime < 0. ){
// deleting the event
delete newEv;
return;
}
if ( size() == 0 ) {
// First element of the timetable
head = newEv;
head->setNext(newEv);
head->setPrev(newEv);
} else {
// possible insertion at the head or the tail
// if not, searching for the time of insertion
if ( evTime < head->getTime() + EPSILONT ){
// inserting the event at the head
head->insertBefore(newEv);
head = newEv;
} else if ( evTime > head->getPrev()->getTime() ){
// inserting the event at the tail
head->insertBefore(newEv);
} else {
// searching for the time of insertion
// starting from the head
FFEvent* tmpEv = head;
while ( evTime > tmpEv->getTime() + EPSILONT ){
tmpEv = tmpEv->getNext();
}
tmpEv->insertBefore(newEv);
}
}
increment();
}
void TimeTable::insert(FFEvent* newEv){
// checking the event consistency
double evTime = newEv->getTime();
if ( evTime == numeric_limits<double>::infinity() ){
// deleting the event
delete newEv;
return;
}
if ( size() == 0 ) {
// First element of the timetable
head = newEv;
head->setNext(newEv);
head->setPrev(newEv);
} else {
// possible insertion at the head or the tail
// if not, searching for the time of insertion
if ( evTime < head->getTime() - EPSILONT ){
// inserting the event at the head
head->insertBefore(newEv);
head = newEv;
} else if ( evTime >= head->getPrev()->getTime() - EPSILONT ){
// inserting the event at the tail
FFEvent* tmpEv = head->getPrev();
tmpEv->insertAfter(newEv);
} else {
// searching for the time of insertion
// starting from the head
FFEvent* tmpEv = head;
while ( evTime > tmpEv->getTime() - EPSILONT ){
tmpEv = tmpEv->getNext();
}
tmpEv = tmpEv->getPrev();
tmpEv->insertAfter(newEv);
}
}
increment();
}
void TimeTable::dropEvent(FFEvent* ev){
if ( !head ) return;
if ( size() > 1 ) {
// classical removing
ev->getPrev()->setNext(ev->getNext());
ev->getNext()->setPrev(ev->getPrev());
if ( ev == head ){
head = head->getNext();
}
} else {
head = 0;
}
delete ev;
decrement();
}
void TimeTable::dropAtomEvents(ForeFireAtom* atom){
if ( !head ) return;
FFEvent* tmpEvNext;
// removing possible events at head
while ( head != 0 and head->getAtom() == atom ) dropEvent(head);
FFEvent* tmpEv = head->getNext();
/* scanning all the events to see if they're
* related to the searched ForeFireAtom */
while ( tmpEv != head ) {
tmpEvNext = tmpEv->getNext();
if ( tmpEv->getAtom() == atom ) dropEvent(tmpEv);
tmpEv = tmpEvNext;
}
}
string TimeTable::print(){
if ( !head ) return "";
ostringstream oss;
oss << "TIMETABLE" << endl;
FFEvent* tmpEv = head;
oss << tmpEv->getAtom()->toString() << " at " << tmpEv->getTime()
<<" at "<< tmpEv->getAtom() << endl;
while ( tmpEv->getNext() != head ){
tmpEv = tmpEv->getNext();
oss << tmpEv->getAtom()->toString() << " at " << tmpEv->getTime()
<<" at "<< tmpEv->getAtom() << endl;
}
oss << "END TIMETABLE" << endl;
return oss.str();
}
void TimeTable::clear() {
while (head != nullptr) {
FFEvent* ev = head;
// dropEvent sets head = nullptr if it's the last element
dropEvent(ev);
}
commonInitialization();
if(head ) {
cout << "Error: head is not null after clearing the timetable." << endl;
}
}
}