EventBlockArray.cpp 6.07 KB
Newer Older
legoc's avatar
legoc committed
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
218
219
220
221
222
223
224
225
226
227
228
/*
 * Nomad Instrument Control Software
 *
 * Copyright 2011 Institut Laue-Langevin
 *
 * Licensed under the EUPL, Version 1.1 only (the "License");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 *
 * http://www.osor.eu/eupl
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 */

#include "EventBlockArray.h"

#include <list>

#include "Event.h"

using namespace std;

namespace lstdpp128 {

EventBlock * EventBlockArray::updateBlocks() {

	//cout << "update with #blocks = " << _array.size() << " max = " << _maxBlocks << endl;

	// removing first block?
	if (_maxBlocks != -1 && _array.size() >= _maxBlocks) {
		// removing first block
		//cout << "deleting block " << _array.front()->id << endl;
		EventBlock * block = _array.front();
		_array.pop_front();
		_reduced = true;

		//cout << "array reduced, first block is " << *_array.front() << endl;

		return block;
	}

	return 0;
}

void EventBlockArray::addEventBlock() {

	// adding a block only if last is not empty
	if (_array.empty() || !_array.back()->events.empty()) {
		updateBlocks();
		_array.push_back(new EventBlock());
	}

}

bool EventBlockArray::isInsertable(time_type time) const {
	if (_reduced) {

//		if (time < _array.front()->minTime()) {
//			cout << "cannot insert event with time " << time << ", min time is " << _array.front()->minTime() << endl;
//		}

		return (time >= _array.front()->minTime());
	}

	return true;
}

EventBlockArrayIterator EventBlockArray::begin() const {
	return EventBlockArrayIterator(_array, _array.begin(), (*_array.begin())->events.begin());
}

EventBlockArrayIterator EventBlockArray::end() const {
	return EventBlockArrayIterator(_array, _array.end(), _array.back()->events.end());
}

time_type EventBlockArray::maxPreviousBlockTime(EventBlockArray::array_type::reverse_iterator const & b) const {

	EventBlockArray::array_type::const_reverse_iterator bc = b;

//	cout << "maxPreviousBlockTime " << (*b)->id << " " << (*bc)->id << endl;
	++bc;

	if (bc != array().rend()) {

//		cout << "maxPreviousBlockTime returning " << (*bc)->id << endl;
		return (*bc)->maxTime();
	}

	return -1;
}

EventBlockArrayIterator EventBlockArray::insertEvent(Event const & event) {

	//cout << "inserting event " << event << endl;

	// inserting the event in the events blocks
	for (EventBlockArray::array_type::reverse_iterator b = array().rbegin(); b != array().rend(); ++b) {

		EventBlock * block = *b;

		// testing if the event belongs to the current block
		if ((block->events.empty() && event.time() >= maxPreviousBlockTime(b))
		|| (!block->events.empty() && event.time() >= block->minTime())) {

			// iterator
			multiset<Event, CompareEvents>::const_iterator e;

			if (block->events.size() == _maxBlockSize - 1) {

				//cout << "max block size for block " << block->id << endl;

				// splitting the block
				time_type midTime = (block->minTime() + block->maxTime()) / 2;
				EventBlock * newBlock = block->split(midTime);

				// inserting new block in EventBlocks
				EventBlockArray::array_type::const_iterator fb = array().insert(b.base(), newBlock);

				//cout << "inserted new block " << newBlock->id << " " << (*fb)->id << endl;

				// selecting block
				if (event.time() <= midTime) {
					e = block->events.insert(event);
					--fb;
					//cout << "inserted in first block " << block->id << ", block fb " << (*fb)->id << endl;

				} else {
					e = newBlock->events.insert(event);
					//cout << "inserted in second block " << newBlock->id << ", block fb " << (*fb)->id << endl;
				}

				//cout << "iterator in block " << (*fb)->id << endl;

				// returning the new iterator
				return EventBlockArrayIterator(array(), fb, e);

			} else {
				// simply insert
				e = block->events.insert(event);

				// converting b to a forward iterator
				++b;
				EventBlockArray::array_type::const_iterator fb = b.base();

				//cout << "iterator in block " << (*fb)->id << endl;
/*
				EventBlockArrayIterator eba(array(), fb, e);
				if (eba.end()) {
					cout << "  at end 1" << endl;
				} else {
					++eba;
					if (eba.end()) {
						cout << "  at end 2" << endl;
					}
				}*/

				// returning the new iterator
				return EventBlockArrayIterator(array(), fb, e);
			}
		}
		// go to next block
	}

	// finally, we insert in the first block if not found the block before
	// inserting and getting the iterator
	multiset<Event, CompareEvents>::const_iterator e = array().front()->events.insert(event);

	// returning the new iterator
	return EventBlockArrayIterator(array(), array().begin(), e);
}

void testIterator(EventBlockArray const & EventBlocks) {

	EventBlockArrayIterator e = EventBlocks.begin();

	while (e != EventBlocks.end()) {
		cout << "event " << *e << endl;
		++e;
		--e;
		++e;
	}

	while (e != EventBlocks.begin()) {
		--e;
		cout << "event " << *e << endl;
	}
}

void testSplitBlocks(EventBlockArray const & EventBlocks) {

	EventBlocks.array().front()->split();
}

void printBlocks(EventBlockArray const & EventBlocks, bool printEvents) {

	for (EventBlockArray::array_type::const_iterator b = EventBlocks.array().begin(); b != EventBlocks.array().end(); ++b) {
		cout << "block " << *(*b) << endl;
		if (printEvents) {
			for (multiset<Event, CompareEvents>::const_iterator e = (*b)->events.begin(); e != (*b)->events.end(); ++e) {
				cout << "  event " << *e << endl;
			}
		}
	}
}

void checkEvents(EventBlockArray const & EventBlocks) {

	int32_t nbEvents = 0;
	time_type lastEventTime = 0;

	for (EventBlockArray::array_type::const_iterator b = EventBlocks.array().begin(); b != EventBlocks.array().end(); ++b) {
		for (multiset<Event, CompareEvents>::const_iterator e = (*b)->events.begin(); e != (*b)->events.end(); ++e) {
			nbEvents++;
			if (e->time() < lastEventTime) {
				cout << "error in events ordering" << endl;
			}
			lastEventTime = e->time();
		}
	}

	cout << "found " << nbEvents << " events" << endl;
}

}