Project

General

Profile

Statistics
| Branch: | Revision:

root / src / eventlog / ieventlog.cc @ 8aeaaccc

History | View | Annotate | Download (3.17 KB)

1 01873262 Georg Kunz
//=========================================================================
2
//  IEVENTLOG.CC - part of
3
//                  OMNeT++/OMNEST
4
//           Discrete System Simulation in C++
5
//
6
//  Author: Levente Meszaros
7
//
8
//=========================================================================
9
10
/*--------------------------------------------------------------*
11
  Copyright (C) 2006-2008 OpenSim Ltd.
12

13
  This file is distributed WITHOUT ANY WARRANTY. See the file
14
  `license' for details on this and other legal matters.
15
*--------------------------------------------------------------*/
16
17
#include <stdio.h>
18
#include <stdlib.h>
19
#include "ieventlog.h"
20
21
USING_NAMESPACE
22
23
24
IEventLog::IEventLog()
25
{
26
    lastNeighbourEventNumber = -1;
27
    lastNeighbourEvent = NULL;
28
}
29
30
void IEventLog::synchronize(FileReader::FileChangedState change)
31
{
32
    if (change != FileReader::UNCHANGED) {
33
        lastNeighbourEventNumber = -1;
34
        lastNeighbourEvent = NULL;
35
    }
36
}
37
38
void IEventLog::printEvents(FILE *file, eventnumber_t fromEventNumber, eventnumber_t toEventNumber, bool outputEventLogMessages)
39
{
40
    IEvent *event = fromEventNumber == -1 ? getFirstEvent() : getFirstEventNotBeforeEventNumber(fromEventNumber);
41
42
    while (event != NULL && (toEventNumber == -1 || event->getEventNumber() <= toEventNumber))
43
    {
44
        event->print(file, outputEventLogMessages);
45
        event = event->getNextEvent();
46
    }
47
}
48
49
void IEventLog::print(FILE *file, eventnumber_t fromEventNumber, eventnumber_t toEventNumber, bool outputInitializationEntries, bool outputEventLogMessages)
50
{
51
    if (outputInitializationEntries)
52
        printInitializationLogEntries(file);
53
54
    printEvents(file, fromEventNumber, toEventNumber, outputEventLogMessages);
55
}
56
57
IEvent *IEventLog::getNeighbourEvent(IEvent *event, eventnumber_t distance)
58
{
59
    Assert(event);
60
    eventnumber_t neighbourEventNumber = event->getEventNumber() + distance;
61
62
    if (lastNeighbourEvent && lastNeighbourEventNumber != -1 && abs64(neighbourEventNumber - lastNeighbourEventNumber) < abs64(distance))
63
        return getNeighbourEvent(lastNeighbourEvent, neighbourEventNumber - lastNeighbourEventNumber);
64
65
    while (event != NULL && distance != 0)
66
    {
67
        if (distance > 0) {
68
            distance--;
69
            event = event->getNextEvent();
70
        }
71
        else {
72
            distance++;
73
            event = event->getPreviousEvent();
74
        }
75
    }
76
77
    lastNeighbourEventNumber = neighbourEventNumber;
78
    lastNeighbourEvent = (IEvent *)event;
79
80
    return lastNeighbourEvent;
81
}
82
83
double IEventLog::getApproximatePercentageForEventNumber(eventnumber_t eventNumber)
84
{
85
    IEvent *firstEvent = getFirstEvent();
86
    IEvent *lastEvent = getLastEvent();
87
    IEvent *event = getEventForEventNumber(eventNumber);
88
89
    if (firstEvent == NULL)
90
        return 0.0;
91
    else if (event == NULL)
92
        return 0.5;
93
    else if (firstEvent == lastEvent)
94
        return 1.0;
95
    else {
96
        file_offset_t beginOffset = firstEvent->getBeginOffset();
97
        file_offset_t endOffset = lastEvent->getBeginOffset();
98
99
        double percentage = (double)(event->getBeginOffset() - beginOffset) / (endOffset - beginOffset);
100
        Assert(0.0 <= percentage && percentage <= 1.0);
101
        return percentage;
102
    }
103
}