-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMPI_OneSpace.cpp
318 lines (251 loc) · 10.9 KB
/
MPI_OneSpace.cpp
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//
// MPI_OneSpace.cpp
//
#include "MPI_OneSpace.h"
#include "MPI_OneSpaceElement.h"
#include "MPI_ElementVisitor.h"
#include "MPI_ElementConstVisitor.h"
#include "MPI_OneSpaceIterator.h"
#include "MPI_OneSpaceConstIterator.h"
bool MPI_OneSpace::ItemBefore::operator()( MPI_OneSpaceElementPos const& a, MPI_OneSpaceElementPos const& b ) const
{
return a.getPosition() < b.getPosition();
}
bool MPI_OneSpace::ItemContainsElement::operator()( MPI_OneSpaceElementPos const& elementpos ) const
{
return elementpos.getElement() == element_;
}
MPI_OneSpace::~MPI_OneSpace()
{
while ( !items_.empty() )
removeItem( items_.begin() );
}
void MPI_OneSpace::addElement( MPI_OneSpaceElement *element, float position )
{
// adds the element to the one space
// ensures the elements are ordered
// assumes the items_ list is already ordered properly
// find the insert point according to position;
// insert the element (nb the insertion point can't be a const iterator)
items_.insert( getInsertIterator(position),
MPI_OneSpaceElementPos(element, position) );
// activate the item; this amounts to a post-add hook that the element can
// supply
// FIXME I'd prefer not to have an activate() method in the base element
// class -- too specific, I feel like I'm polluting it.
element->activate();
}
void MPI_OneSpace::removeElement( MPI_OneSpaceElement const *element )
{
// removes an element from the items_ list
// assumes the element exists in the list already
// assumes the items_ list is already ordered properly
// get the iterator to the given element;
// remove the element from the list (nb the deletion point can't be a const
// iterator)
// FIXME can't assume getItemIterator() will return a valid iterator; the
// argument could point to an item that's been removed or something...
removeItem( getItemIterator(element) );
}
bool MPI_OneSpace::containsElement( MPI_OneSpaceElement const *element ) const
{
return getItemIterator( element ) != items_.end();
}
float MPI_OneSpace::getElementPosition( MPI_OneSpaceElement const *element ) const
{
return getItemIterator( element )->getPosition();
}
void MPI_OneSpace::moveElement( MPI_OneSpaceElement const *element, float newpos )
{
// move the element to some target position, colliding with the other
// elements along the way
// this method assumes the list is sorted, and that element appears in it
// the collisions may modify the list, as long as the list stays consistent
// (sorted) and the element that's being moved isn't repositioned.
// FIXME think about obviating advanceBefore/advanceAfter and making this
// method handle both directions symmetrically. you'd have to do a test to
// see if newpos is between givenitem and the appropriate neighbour; if it
// is, just set the position, if it's not, call moveThroughItem(). so
// you'd need a step to determine which neighbour to use, cache these in
// left and right items (either of which could be givenitem) to test
// whether left <= newpos < right, and then take the appropriate action.
// moveThroughItem() works symmetrically, so don't need to do any cases
// there.
listiterator givenitem;
listiterator neighbourafter;
// allocate the visitor to use for element interactions
// FIXME this visitor should take a reference to the element that's doing
// the colliding, the call should be able to create different visitors
// depending on the element's type, and the visitor should be able to
// modify the element over the course of the iteration below.
MPI_ElementVisitor *visitor = allocateElementVisitor();
while ( true ) {
// find the item, find its neighbourafter
givenitem = getItemIterator( element );
neighbourafter = givenitem;
++neighbourafter;
// this is the stopping condition
// if neighbourafter is equal to the insert iterator, stop
if ( givenitem->getPosition() == newpos &&
neighbourafter == getInsertIterator( newpos ) )
break;
// if the givenitem <= newpos, move it afterward
// if the newpos < givenitem, move it before
if ( newpos < givenitem->getPosition() )
advanceBefore( givenitem, newpos, *visitor );
else
advanceAfter( givenitem, newpos, *visitor );
}
// free the visitor
delete visitor;
}
MPI_OneSpaceElement const* MPI_OneSpace::getLastElement( void ) const
{
// return null if there's no last element; otherwise, return the element
if ( items_.empty() )
return NULL;
else
return items_.back().getElement();
}
MPI_ElementIterator* MPI_OneSpace::allocateElementIterator( void ) const
{
return new MPI_OneSpaceIterator( items_ );
}
MPI_ElementConstIterator* MPI_OneSpace::allocateElementConstIterator( void ) const
{
return new MPI_OneSpaceConstIterator( items_ );
}
void MPI_OneSpace::print( std::ostream &os ) const
{
// iterate over the elements and print them out
listconstiterator currentitem;
for ( currentitem = items_.begin(); currentitem != items_.end();
++currentitem )
os << "[" << *currentitem << "], ";
os << "done";
}
MPI_ElementVisitor* MPI_OneSpace::allocateElementVisitor( void ) const
{
// to be overridden by subclasses to create particular interactions between
// the elements
// FIXME think about making this a pure virtual method, so that we couldn't
// instantiate plain onespaces.
return new MPI_ElementVisitor;
// FIXME this visitor should take a reference to the element that's doing
// the colliding, the call should be able to create different visitors
// depending on the element's type, and the visitor should be able to
// modify the element over the course of the iteration.
//
// change it to accept the pointer to the element that's doing the
// visiting. subclasses can call a visitor on this to create a *second*
// visitor, which would be returned. so we have *two* visitors being used,
// and in this way, we can define actions between pairs of specific types.
// you could rig it up so that there's no action unless powered elements
// act on intersections, for example (not sure you'd actually want to do
// that, though -- need to think about it).
}
void MPI_OneSpace::visitElements( MPI_ElementVisitor &visitor )
{
std::list<MPI_OneSpaceElementPos>::iterator item;
for (item = items_.begin(); item != items_.end(); ++item)
item->getElement()->acceptVisitor( visitor );
}
void MPI_OneSpace::visitConstElements( MPI_ElementConstVisitor &visitor )
{
std::list<MPI_OneSpaceElementPos>::const_iterator item;
for (item = items_.begin(); item != items_.end(); ++item)
item->getElement()->acceptConstVisitor( visitor );
}
MPI_OneSpace::listiterator MPI_OneSpace::getItemIterator( MPI_OneSpaceElement const *element )
{
// find and return the iterator pointing to the item corresponding to given
// element
return find_if( items_.begin(), items_.end(), ItemContainsElement(element) );
}
MPI_OneSpace::listconstiterator MPI_OneSpace::getItemIterator( MPI_OneSpaceElement const *element ) const
{
// find and return the const iterator pointing to the item corresponding to
// given element. this code must be the same as in the non-const version
// of this method.
return find_if( items_.begin(), items_.end(), ItemContainsElement(element) );
}
MPI_OneSpace::listiterator MPI_OneSpace::getInsertIterator( float position )
{
// find an return an iterator at the appropriate insert point for the given
// position.
return upper_bound( items_.begin(), items_.end(),
MPI_OneSpaceElementPos(NULL,position), ItemBefore());
}
void MPI_OneSpace::removeItem( MPI_OneSpace::listiterator item )
{
// deallocate item's element, and remove the item
// the element in the item should always be valid, so we can just delete it
// without checking.
delete item->getElement();
items_.erase( item );
}
void MPI_OneSpace::advanceAfter( MPI_OneSpace::listiterator item, float newpos, MPI_ElementVisitor& visitor )
{
// advance the element contained in the list item toward newpos.
// if there's another item in the way, collide with it and don't move any
// further
// newpos is assumed to not be before item's position
// item is assumed to be non-null
// assumes list is sorted properly; this means that
// neighbourbefore <= item <= neighbourafter
// get neighbour after the given item
listiterator neighbourafter( item );
neighbourafter++;
// if newpos is strictly before neighbourafter (newpos < neighbourafter),
// you can just set the position to newpos
if ( neighbourafter == items_.end() ||
newpos < neighbourafter->getPosition() ) {
item->setPosition( newpos );
return;
}
// so neighbourafter <= newpos. move item through neighbourafter.
moveThroughItem( item, neighbourafter, visitor );
}
void MPI_OneSpace::advanceBefore( MPI_OneSpace::listiterator item, float newpos, MPI_ElementVisitor& visitor )
{
// advance the element contained in the list item toward newpos.
// if there's another item in the way, collide with it and don't move any
// further
// newpos is assumed to be before item's position
// item is assumed to be non-null
// assumes list is sorted properly; this means that
// neighbourbefore <= item <= neighbourafter
// get neighbour before the given item
listiterator neighbourbefore( item );
neighbourbefore--;
// if neighbourbefore <= newpos, you can just set the position to newpos
if ( neighbourbefore == items_.end() ||
neighbourbefore->getPosition() <= newpos ) {
item->setPosition( newpos );
return;
}
// so newpos < neighbourbefore. move item through neighbourbefore.
moveThroughItem( item, neighbourbefore, visitor );
}
void MPI_OneSpace::moveThroughItem( MPI_OneSpace::listiterator moveitem, MPI_OneSpace::listiterator target, MPI_ElementVisitor& visitor )
{
// move moveitem to target and through it, leaving the items at the same
// position in space, but swapped in the items_ list.
// moveitem interacts with target as it passes through.
// assumptions: moveitem, target are adjacent in the list; the list is
// consistent (sorted)
// swap item, neighbourbefore positions and collide
// visit (collide with) the item we leapfrogged (originally *target), which
// ends up sitting in the moveitem iterator after the swap.
// collide after the swap in case the visitor changes the state of the list
// (need to do it last).
moveitem->setPosition( target->getPosition() );
std::swap( *moveitem, *target );
moveitem->getElement()->acceptVisitor( visitor );
}
std::ostream &operator<<( std::ostream &os, MPI_OneSpace const &space )
{
space.print(os);
return os;
}
// vim:sw=4:et:cindent: