Public Member Functions | |
LaneContainer (boolean direction) | |
synchronized void | addSorted (LaneObject object) |
synchronized void | updatePosition (LaneObject object, double newPosition) |
synchronized void | remove (LaneObject object) |
LaneObject | getHead () |
LaneObject | getTail () |
int | size () |
void | clear () |
Protected Attributes | |
final boolean | direction_ |
LaneObject | head_ |
LaneObject | tail_ |
int | size_ = 0 |
A LaneContainer contains all LaneObjects in one direction of a street. The objects may be on different lanes. but they must all be in the same direction!
It's basically a kind of a queue implemented as a LinkedList. The references to the previous and next elements are stored in the LaneObjects which makes the implementation quite efficient as no search is necessary for removal. Thus, it always takes O(1) to remove an object. Adding is O(n) in worst case but in most cases, insertion is made in O(1) as the object is added at the head. In order to update positions (for example when an overhaul occurs) you don't need to remove and re-add (which could be quite costly) but rather just call updatePosition()
. Checking for the next or previous LaneObject is O(1) and no lookup is necessary (directly stored within the object).
Insertion, removal and update is synchronized. As iterating through the objects needs to be done externally (by calling getNext()
or getPrevious()
), it is not synchronized!
|
inline |
Instantiates a new lane container.
direction | true if this lane is from startNode to endNode of the street, else false |
|
inline |
Add an element so that it's correctly ordered inside the lane container.
object | the object to add |
|
inline |
Removes all elements from this container.
|
inline |
Gets the head.
|
inline |
Gets the tail.
|
inline |
Removes an object. You must make sure that the object is really in this lane container. Otherwise this function might do very bad things (the size variable is decreased)!
object | the object to remove |
|
inline |
Gets the current amount of objects on this lane container.
|
inline |
Updates position of a LaneObject and changes the order in this LaneContainer to guarantee a consistent state.
object | the object to check |
newPosition | the new position of the object |
|
protected |
The direction of this container.
true
= going from startNode to endNode
false
= going from endNode to startNode
|
protected |
The head of the lane container.
|
protected |
The number of elements in this lane container.
|
protected |
The tail of the lane container.