summaryrefslogtreecommitdiffstats
path: root/src/base/CompositionTimeSliceAdapter.cpp
blob: b91b804817a8d845ddabb34593f6212236c0e2e3 (plain)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// -*- c-basic-offset: 4 -*-

/*
    Rosegarden
    A sequencer and musical notation editor.

    This program is Copyright 2000-2008
        Guillaume Laurent   <glaurent@telegraph-road.org>,
        Chris Cannam        <cannam@all-day-breakfast.com>,
        Richard Bown        <bownie@bownie.com>

    This file is Copyright 2002
        Randall Farmer      <rfarme@simons-rock.edu>
	with additional work by Chris Cannam.

    The moral right of the authors to claim authorship of this work
    has been asserted.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

// !!!TODO: handle timeslices

#include <list>
#include <utility>

#include "CompositionTimeSliceAdapter.h"
#include "Segment.h"
#include "Composition.h"
#include "Selection.h"

namespace Rosegarden {

using std::list;
using std::pair;

CompositionTimeSliceAdapter::CompositionTimeSliceAdapter(Composition *c,
							 timeT begin,
							 timeT end) :
    m_composition(c),
    m_begin(begin),
    m_end(end)
{
    if (begin == end) {
	m_begin = 0;
	m_end = c->getDuration();
    }

    for (Composition::iterator ci = m_composition->begin();
         ci != m_composition->end(); ++ci) {
	m_segmentList.push_back(*ci);
    }
}

CompositionTimeSliceAdapter::CompositionTimeSliceAdapter(Composition *c,
							 SegmentSelection* s,
							 timeT begin,
							 timeT end) :
    m_composition(c),
    m_begin(begin),
    m_end(end)
{
    if (begin == end) {
	m_begin = 0;
	m_end = c->getDuration();
    }

    for (Composition::iterator ci = m_composition->begin();
         ci != m_composition->end(); ++ci) {
	if (!s || s->find(*ci) != s->end()) {
	    m_segmentList.push_back(*ci);
	}
    }
}

CompositionTimeSliceAdapter::CompositionTimeSliceAdapter(Composition *c,
							 const TrackSet &trackIDs,
							 timeT begin,
							 timeT end) :
    m_composition(c),
    m_begin(begin),
    m_end(end)
{
    if (begin == end) {
	m_begin = 0;
	m_end = c->getDuration();
    }

    for (Composition::iterator ci = m_composition->begin();
         ci != m_composition->end(); ++ci) {
	if (trackIDs.find((*ci)->getTrack()) != trackIDs.end()) {
	    m_segmentList.push_back(*ci);
	}
    }
}

CompositionTimeSliceAdapter::iterator
CompositionTimeSliceAdapter::begin() const
{
    if (m_beginItr.m_a == 0) {
	m_beginItr = iterator(this);
	fill(m_beginItr, false);
    }
    return m_beginItr;
}

CompositionTimeSliceAdapter::iterator
CompositionTimeSliceAdapter::end() const
{
    return iterator(this);
}

void
CompositionTimeSliceAdapter::fill(iterator &i, bool atEnd) const
{
    // The segment iterators should all point to events starting at or
    // after m_begin (if atEnd false) or at or before m_end (if atEnd true).

    for (unsigned int k = 0; k < m_segmentList.size(); ++k) {
	Segment::iterator j = m_segmentList[k]->findTime(atEnd ? m_end : m_begin);
	i.m_segmentItrList.push_back(j);
    }

    // fill m_curEvent & m_curTrack
    if (!atEnd) ++i;
}

CompositionTimeSliceAdapter::iterator&
CompositionTimeSliceAdapter::iterator::operator=(const iterator &i)
{
    if (&i == this) return *this;
    m_segmentItrList.clear();

    for (segmentitrlist::const_iterator j = i.m_segmentItrList.begin(); 
	 j != i.m_segmentItrList.end(); ++j) {
	m_segmentItrList.push_back(Segment::iterator(*j));
    }

    m_a = i.m_a;
    m_curTrack = i.m_curTrack;
    m_curEvent = i.m_curEvent;
    m_needFill = i.m_needFill;
    return *this;
}

CompositionTimeSliceAdapter::iterator::iterator(const iterator &i) :
    m_a(i.m_a),
    m_curEvent(i.m_curEvent),
    m_curTrack(i.m_curTrack),
    m_needFill(i.m_needFill)
{
    for (segmentitrlist::const_iterator j = i.m_segmentItrList.begin(); 
	 j != i.m_segmentItrList.end(); ++j) {
	m_segmentItrList.push_back(Segment::iterator(*j));
    }
}

CompositionTimeSliceAdapter::iterator&
CompositionTimeSliceAdapter::iterator::operator++()
{
    assert(m_a != 0);

    // needFill is only set true for iterators created at end()
    if (m_needFill) {
	m_a->fill(*this, true);
	m_needFill = false;
    }

    Event *e = 0;
    unsigned int pos = 0;

    for (unsigned int i = 0; i < m_a->m_segmentList.size(); ++i) {

	if (!m_a->m_segmentList[i]->isBeforeEndMarker(m_segmentItrList[i])) continue;

        if (!e || strictLessThan(*m_segmentItrList[i], e)) {
            e = *m_segmentItrList[i];
	    m_curTrack = m_a->m_segmentList[i]->getTrack();
            pos = i;
        }
    }

    // Check whether we're past the end time, if there is one
    if (!e || e->getAbsoluteTime() >= m_a->m_end) {
        m_curEvent = 0;
	m_curTrack = -1;
        return *this;
    }

    // e is now an Event* less than or equal to any that the iterator
    // hasn't already passed over
    m_curEvent = e;

    // m_segmentItrList[pos] is a segment::iterator that points to e
    ++m_segmentItrList[pos];

    return *this;
}

CompositionTimeSliceAdapter::iterator&
CompositionTimeSliceAdapter::iterator::operator--()
{
    assert(m_a != 0);

    // needFill is only set true for iterators created at end()
    if (m_needFill) {
	m_a->fill(*this, true);
	m_needFill = false;
    }

    Event *e = 0;
    int pos = -1;

    // Decrement is more subtle than increment.  We have to scan the
    // iterators available, and decrement the one that points to
    // m_curEvent.  Then to fill m_curEvent we need to find the next
    // greatest event back that is not itself m_curEvent.

    for (unsigned int i = 0; i < m_a->m_segmentList.size(); ++i) {

	if (m_segmentItrList[i] == m_a->m_segmentList[i]->begin()) continue;

	Segment::iterator si(m_segmentItrList[i]);
	--si;

	if (*si == m_curEvent) {
	    pos = i;
	} else if (!e || !strictLessThan(*si, e)) {
	    e = *si;
	    m_curTrack = m_a->m_segmentList[i]->getTrack();
	}
    }

    if (e) m_curEvent = e;
    if (pos >= 0) {
	--m_segmentItrList[pos];
    }

    return *this;
}

bool
CompositionTimeSliceAdapter::iterator::operator==(const iterator& other) const {
    return m_a == other.m_a && m_curEvent == other.m_curEvent;
}

bool
CompositionTimeSliceAdapter::iterator::operator!=(const iterator& other) const {
    return !operator==(other);
}

Event *
CompositionTimeSliceAdapter::iterator::operator*() const {
    return m_curEvent;
}

Event &
CompositionTimeSliceAdapter::iterator::operator->() const {
    return *m_curEvent;
}

int
CompositionTimeSliceAdapter::iterator::getTrack() const {
    return m_curTrack;
}

bool
CompositionTimeSliceAdapter::iterator::strictLessThan(Event *e1, Event *e2) {
    // We need a complete ordering of events -- we can't cope with two events
    // comparing equal.  i.e. one of e1 < e2 and e2 < e1 must be true.  The
    // ordering can be arbitrary -- we just compare addresses for events the
    // event comparator doesn't distinguish between.  We know we're always
    // dealing with event pointers, not copies of events.
    if (*e1 < *e2) return true;
    else if (*e2 < *e1) return false;
    else return e1 < e2;
}

}