-
Notifications
You must be signed in to change notification settings - Fork 0
/
IcWindowArray.h
214 lines (173 loc) · 4.78 KB
/
IcWindowArray.h
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
#include <vector>
#include <string>
#include "query/Aggregate.h"
#include "array/DelegateArray.h"
#include "array/Metadata.h"
#include "array/MemArray.h"
#include "query/FunctionDescription.h"
#include "query/Expression.h"
#include "query/Aggregate.h"
namespace scidb
{
using namespace std;
using namespace boost;
class IcWindowArray;
class IcWindowArrayIterator;
class MaterializedIcWindowChunkIterator;
struct WindowBoundaries
{
WindowBoundaries()
{
_boundaries.first = _boundaries.second = 0;
}
WindowBoundaries(Coordinate first, Coordinate second)
{
SCIDB_ASSERT(first >= 0);
SCIDB_ASSERT(second >= 0);
_boundaries.first = first;
_boundaries.second = second;
}
std::pair<Coordinate, Coordinate> _boundaries;
};
struct PriorityQueueNode {
friend bool operator < (PriorityQueueNode n1, PriorityQueueNode n2)
{
return n1._value < n2._value;
}
public:
PriorityQueueNode(int pos, double value)
{
_value = value;
_pos = pos;
}
double _value;
int _pos;
};
class IcWindowChunk : public ConstChunk
{
friend class IcWindowChunkIterator;
public:
IcWindowChunk(IcWindowArray const& array, AttributeID attrID);
virtual const ArrayDesc& getArrayDesc() const;
virtual const AttributeDesc& getAttributeDesc() const;
virtual Coordinates const& getFirstPosition(bool withOverlap) const;
virtual Coordinates const& getLastPosition(bool withOverlap) const;
virtual shared_ptr<ConstChunkIterator> getConstIterator(int iteratorMode) const;
virtual int getCompressionMethod() const;
virtual Array const& getArray() const;
void setPosition(IcWindowArrayIterator const* iterator, Coordinates const& pos);
private:
IcWindowArray const& _array;
IcWindowArrayIterator const* _arrayIterator;
size_t _nDims;
Coordinates _arrSize;
Coordinates _firstPos;
Coordinates _lastPos;
AttributeID _attrID;
AggregatePtr _aggregate;
Value _nextValue;
};
class IncrementalComputer
{
friend class IcWindowChunkIterator;
public:
void removeOld();
void insertNew(int pos, double v);
void baseWindowInitialize();
double calculateCurrentValue(int pos);
~IncrementalComputer()
{
delete []_interBuf;
}
private:
// sum | avg
int _aggrType;
int _currPos;
int _headPos;
int _bufferSize;
double _windowSize;
double _sum;
double* _interBuf;
priority_queue<PriorityQueueNode> _heap;
// max | min
};
class IcWindowChunkIterator : public ConstChunkIterator
{
public:
virtual int getMode();
virtual bool isEmpty();
virtual Value& getItem();
virtual void operator ++();
virtual bool end();
virtual Coordinates const& getPosition();
virtual bool setPosition(Coordinates const& pos);
virtual void reset();
ConstChunk const& getChunk();
IcWindowChunkIterator(IcWindowArrayIterator const& arrayIterator, IcWindowChunk const& aChunk, int mode);
private:
Value& calculateNextValue();
int getAggrTypeFromName(string const& name);
void calculateWindowUnit(Coordinates const& first, Coordinates const& last, int d);
void accumulate(Value const& v);
IncrementalComputer _IcWorker;
IcWindowArray const& _array;
IcWindowChunk const& _chunk;
Coordinates const& _firstPos;
Coordinates const& _lastPos;
Coordinates _currPos;
bool _hasCurrent;
AttributeID _attrID;
AggregatePtr _aggregate;
Value _defaultValue;
int _iterationMode;
shared_ptr<ConstChunkIterator> _inputIterator;
shared_ptr<ConstArrayIterator> _emptyTagArrayIterator;
shared_ptr<ConstChunkIterator> _emptyTagIterator;
Value _nextValue;
bool _noNullsCheck;
double _result;
double number;
};
class IcWindowArrayIterator : public ConstArrayIterator
{
friend class IcWindowChunkIterator;
friend class IcWindowChunk;
public:
virtual ConstChunk const& getChunk();
virtual Coordinates const& getPosition();
virtual bool setPosition(Coordinates const &pos);
virtual bool end();
virtual void operator ++();
virtual void reset();
IcWindowArrayIterator(IcWindowArray const& array, AttributeID id, AttributeID input);
private:
IcWindowArray const& array;
shared_ptr<ConstArrayIterator> iterator; // iterator of the input array;
Coordinates currPos;
bool hasCurrent;
IcWindowChunk chunk;
bool chunkInitialized;
};
class IcWindowArray : public Array
{
friend class IcWindowArrayIterator;
friend class IcWindowChunkIterator;
friend class IcWindowChunk;
public:
virtual ArrayDesc const& getArrayDesc() const;
virtual shared_ptr<ConstArrayIterator> getConstIterator(AttributeID attr) const;
IcWindowArray(ArrayDesc const& desc,
shared_ptr<Array> const& inputArray,
vector<WindowBoundaries> const& window,
vector<AttributeID> const& inputAttrIDs,
vector<AggregatePtr> const& aggregates);
private:
ArrayDesc _desc;
ArrayDesc _inputDesc;
vector<WindowBoundaries> _window;
Dimensions _dimensions;
shared_ptr<Array> _inputArray;
vector<AttributeID> _inputAttrIDs;
vector<AggregatePtr> _aggregates;
};
}