Statistics
| Branch: | Revision:

root / src / scave / arraybuilder.cc @ 7c7d95e5

History | View | Annotate | Download (3.67 KB)

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

14
  This file is distributed WITHOUT ANY WARRANTY. See the file
15
  `license' for details on this and other legal matters.
16
*--------------------------------------------------------------*/
17
18
#include <memory.h>
19
#include <string.h>
20
#include "channel.h"
21
#include "arraybuilder.h"
22
23
USING_NAMESPACE
24
25
26
ArrayBuilderNode::ArrayBuilderNode()
27
{
28
    veccapacity = 0;
29
    veclen = 0;
30
    xvec = NULL;
31
    yvec = NULL;
32
    xpvec = NULL;
33
    evec = NULL;
34
    collectEvec = false;
35
}
36
37
ArrayBuilderNode::~ArrayBuilderNode()
38
{
39
    delete [] xvec;
40
    delete [] yvec;
41
    delete [] xpvec;
42
    delete [] evec;
43
}
44
45
void ArrayBuilderNode::resize()
46
{
47
    size_t newsize = 3*veccapacity/2;
48
49
    if (!newsize)
50
        newsize = 1024;
51
52
    double *newxvec = new double[newsize];
53
    memcpy(newxvec, xvec, veclen*sizeof(double));
54
    delete [] xvec;
55
    xvec = newxvec;
56
57
    double *newyvec = new double[newsize];
58
    memcpy(newyvec, yvec, veclen*sizeof(double));
59
    delete [] yvec;
60
    yvec = newyvec;
61
62
    if (xpvec) {
63
        BigDecimal *newxpvec = new BigDecimal[newsize];
64
        memcpy(newxpvec, xpvec, veccapacity*sizeof(BigDecimal));
65
        delete[] xpvec;
66
        xpvec = newxpvec;
67
    }
68
69
    if (collectEvec) {
70
        eventnumber_t *newevec = new eventnumber_t[newsize];
71
        memcpy(newevec, evec, veccapacity*sizeof(eventnumber_t));
72
        delete [] evec;
73
        evec = newevec;
74
    }
75
76
    veccapacity = newsize;
77
}
78
79
bool ArrayBuilderNode::isReady() const
80
{
81
    return in()->length()>0;
82
}
83
84
void ArrayBuilderNode::process()
85
{
86
    int n = in()->length();
87
    for (int i=0; i<n; i++)
88
    {
89
        Datum a;
90
        in()->read(&a,1);
91
        if (veclen==veccapacity)
92
            resize();
93
        Assert(xvec && yvec);
94
        xvec[veclen] = a.x;
95
        yvec[veclen] = a.y;
96
        if (!a.xp.isNil()) {
97
            if (!xpvec)
98
                xpvec = new BigDecimal[veccapacity];
99
100
            xpvec[veclen] = a.xp;
101
        }
102
        if (collectEvec)
103
            evec[veclen] = a.eventNumber;
104
105
        veclen++;
106
    }
107
}
108
109
bool ArrayBuilderNode::isFinished() const
110
{
111
    return in()->eof();
112
}
113
114
115
void ArrayBuilderNode::sort()
116
{
117
    // FIXME sort both arrays by "x"
118
    //std::sort();
119
}
120
121
#ifndef min
122
#define min(a,b)    (((a) < (b)) ? (a) : (b))
123
#endif
124
125
XYArray *ArrayBuilderNode::getArray()
126
{
127
    // transfer ownership to XYArray
128
    XYArray *array = new XYArray(veclen, xvec, yvec, xpvec, evec);
129
    xvec = yvec = NULL;
130
    xpvec = NULL;
131
    evec = NULL;
132
    veclen = 0;
133
    return array;
134
}
135
136
//------
137
138
const char *ArrayBuilderNodeType::getDescription() const
139
{
140
    return "Collects data into an array. The arrays can be accessed as two double* arrays in the C++ API.";
141
}
142
143
void ArrayBuilderNodeType::getAttrDefaults(StringMap& attrs) const
144
{
145
    attrs["collecteventnumbers"] = "false";
146
}
147
148
void ArrayBuilderNodeType::getAttributes(StringMap& attrs) const
149
{
150
    attrs["collecteventnumbers"] = "If true event numbers will be present in the output vector";
151
}
152
153
Node *ArrayBuilderNodeType::create(DataflowManager *mgr, StringMap& attrs) const
154
{
155
    checkAttrNames(attrs);
156
157
    ArrayBuilderNode *node = new ArrayBuilderNode();
158
    if (attrs.find("collecteventnumbers") != attrs.end() && !strcmp("true", attrs["collecteventnumbers"].c_str()))
159
        node->collectEvec = true;
160
    node->setNodeType(this);
161
    mgr->addNode(node);
162
    return node;
163
}
164