Make JetStream 2
[WebKit-https.git] / PerformanceTests / JetStream2 / cdjs / collision_detector.js
1 // Copyright (c) 2001-2010, Purdue University. All rights reserved.
2 // Copyright (C) 2015 Apple Inc. All rights reserved.
3 // 
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //  * Redistributions of source code must retain the above copyright
7 //    notice, this list of conditions and the following disclaimer.
8 //  * Redistributions in binary form must reproduce the above copyright
9 //    notice, this list of conditions and the following disclaimer in the
10 //    documentation and/or other materials provided with the distribution.
11 //  * Neither the name of the Purdue University nor the
12 //    names of its contributors may be used to endorse or promote products
13 //    derived from this software without specific prior written permission.
14 // 
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26 function CollisionDetector() {
27     this._state = new RedBlackTree();
28 }
29
30 CollisionDetector.prototype.handleNewFrame = function(frame) {
31     var motions = [];
32     var seen = new RedBlackTree();
33     
34     for (var i = 0; i < frame.length; ++i) {
35         var aircraft = frame[i];
36         
37         var oldPosition = this._state.put(aircraft.callsign, aircraft.position);
38         var newPosition = aircraft.position;
39         seen.put(aircraft.callsign, true);
40         
41         if (!oldPosition) {
42             // Treat newly introduced aircraft as if they were stationary.
43             oldPosition = newPosition;
44         }
45         
46         motions.push(new Motion(aircraft.callsign, oldPosition, newPosition));
47     }
48     
49     // Remove aircraft that are no longer present.
50     var toRemove = [];
51     this._state.forEach(function(callsign, position) {
52         if (!seen.get(callsign))
53             toRemove.push(callsign);
54     });
55     for (var i = 0; i < toRemove.length; ++i)
56         this._state.remove(toRemove[i]);
57     
58     var allReduced = reduceCollisionSet(motions);
59     var collisions = [];
60     for (var reductionIndex = 0; reductionIndex < allReduced.length; ++reductionIndex) {
61         var reduced = allReduced[reductionIndex];
62         for (var i = 0; i < reduced.length; ++i) {
63             var motion1 = reduced[i];
64             for (var j = i + 1; j < reduced.length; ++j) {
65                 var motion2 = reduced[j];
66                 var collision = motion1.findIntersection(motion2);
67                 if (collision)
68                     collisions.push(new Collision([motion1.callsign, motion2.callsign], collision));
69             }
70         }
71     }
72     
73     return collisions;
74 };