LCOV - code coverage report
Current view: top level - src/microsim - MSVehicleContainer.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 69.9 % 93 65
Test Date: 2024-10-24 15:46:30 Functions: 75.0 % 20 15

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
       4              : // This program and the accompanying materials are made available under the
       5              : // terms of the Eclipse Public License 2.0 which is available at
       6              : // https://www.eclipse.org/legal/epl-2.0/
       7              : // This Source Code may also be made available under the following Secondary
       8              : // Licenses when the conditions for such availability set forth in the Eclipse
       9              : // Public License 2.0 are satisfied: GNU General Public License, version 2
      10              : // or later which is available at
      11              : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
      12              : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
      13              : /****************************************************************************/
      14              : /// @file    MSVehicleContainer.cpp
      15              : /// @author  Christian Roessel
      16              : /// @author  Daniel Krajzewicz
      17              : /// @author  Michael Behrisch
      18              : /// @date    Mon, 12 Mar 2001
      19              : ///
      20              : // vehicles sorted by their departures
      21              : /****************************************************************************/
      22              : #include <config.h>
      23              : 
      24              : #include <algorithm>
      25              : #include <cassert>
      26              : #include <iterator>
      27              : #include "MSVehicle.h"
      28              : #include "MSVehicleContainer.h"
      29              : 
      30              : 
      31              : // ===========================================================================
      32              : // method definitions
      33              : // ===========================================================================
      34              : /* -------------------------------------------------------------------------
      35              :  * methods from MSVehicleContainer::VehicleDepartureVectorSortCrit
      36              :  * ----------------------------------------------------------------------- */
      37              : bool
      38            0 : MSVehicleContainer::VehicleDepartureVectorSortCrit::operator()
      39              : (const VehicleDepartureVector& e1, const VehicleDepartureVector& e2) const {
      40            0 :     return e1.first < e2.first;
      41              : }
      42              : 
      43              : 
      44              : 
      45              : /* -------------------------------------------------------------------------
      46              :  * methods from MSVehicleContainer::DepartFinder
      47              :  * ----------------------------------------------------------------------- */
      48      4470602 : MSVehicleContainer::DepartFinder::DepartFinder(SUMOTime time)
      49      4470602 :     : myTime(time) {}
      50              : 
      51              : 
      52              : bool
      53     14158337 : MSVehicleContainer::DepartFinder::operator()
      54              : (const VehicleDepartureVector& e) const {
      55     14158337 :     return myTime + DELTA_T > e.first && myTime <= e.first;
      56              : }
      57              : 
      58              : 
      59              : 
      60              : /* -------------------------------------------------------------------------
      61              :  * methods from MSVehicleContainer
      62              :  * ----------------------------------------------------------------------- */
      63        42753 : MSVehicleContainer::MSVehicleContainer(int capacity)
      64        85506 :     : currentSize(0), array(capacity + 1, VehicleDepartureVector()) {}
      65              : 
      66              : 
      67        39793 : MSVehicleContainer::~MSVehicleContainer() {
      68              :     // !!! vehicles are deleted in MSVehicle
      69        39793 : }
      70              : 
      71              : 
      72              : void
      73      4468303 : MSVehicleContainer::add(SUMOVehicle* veh) {
      74              :     // check whether a new item shall be added or the vehicle may be
      75              :     //  added to an existing list
      76              :     VehicleHeap::iterator i =
      77      4468303 :         find_if(array.begin() + 1, array.begin() + currentSize + 1, DepartFinder(veh->getParameter().depart));
      78      4468303 :     if (currentSize == 0 || i == array.begin() + currentSize + 1) {
      79              :         // a new heap-item is necessary
      80      3094787 :         const SUMOTime delay = veh->getParameter().depart % DELTA_T;
      81      3094787 :         const SUMOTime depart = veh->getParameter().depart + (delay == 0 ? 0 : DELTA_T - delay);
      82      3094787 :         VehicleDepartureVector newElem(depart, VehicleVector());
      83      3094787 :         newElem.second.push_back(veh);
      84      3094787 :         addReplacing(newElem);
      85              :     } else {
      86              :         // add vehicle to an existing heap-item
      87      1373516 :         (*i).second.push_back(veh);
      88              :     }
      89      4468303 : }
      90              : 
      91              : 
      92              : void
      93         2299 : MSVehicleContainer::remove(SUMOVehicle* veh) {
      94              :     // check whether a new item shall be added or the vehicle may be
      95              :     //  added to an existing list
      96              :     VehicleHeap::iterator i =
      97         2299 :         find_if(array.begin() + 1, array.begin() + currentSize + 1, DepartFinder(veh->getParameter().depart));
      98         2299 :     if (!(currentSize == 0 || i == array.begin() + currentSize + 1)) {
      99              :         // remove vehicle from an existing heap-item
     100         2060 :         (*i).second.erase(std::remove((*i).second.begin(), (*i).second.end(), veh), (*i).second.end());
     101              :     }
     102         2299 : }
     103              : 
     104              : 
     105              : void
     106            0 : MSVehicleContainer::add(SUMOTime time, const VehicleVector& cont) {
     107              :     VehicleHeap::iterator j =
     108            0 :         find_if(array.begin() + 1, array.begin() + currentSize + 1,
     109              :                 DepartFinder(time));
     110            0 :     if (currentSize == 0 || j == array.begin() + currentSize + 1) {
     111              :         VehicleDepartureVector newElem(time,
     112            0 :                                        VehicleVector(cont));
     113            0 :         addReplacing(newElem);
     114              :     } else {
     115            0 :         VehicleVector& stored = (*j).second;
     116            0 :         stored.reserve(stored.size() + cont.size());
     117              :         copy(cont.begin(), cont.end(), back_inserter(stored));
     118              :     }
     119            0 : }
     120              : 
     121              : 
     122              : 
     123              : void
     124      3094787 : MSVehicleContainer::addReplacing(const VehicleDepartureVector& x) {
     125      3094787 :     if (isFull()) {
     126         2936 :         std::vector<VehicleDepartureVector> array2((array.size() - 1) * 2 + 1, VehicleDepartureVector());
     127        59436 :         for (int i = (int)array.size(); i-- > 0;) {
     128              :             assert(i < (int)array2.size());
     129        57968 :             array2[i] = array[i];
     130              :         }
     131         1468 :         array = array2;
     132         1468 :     }
     133              : 
     134              :     // Percolate up
     135      3094787 :     int hole = ++currentSize;
     136      3107574 :     for (; hole > 1 && (x.first < array[ hole / 2 ].first); hole /= 2) {
     137              :         assert((int)array.size() > hole);
     138        12787 :         array[hole] = array[ hole / 2 ];
     139              :     }
     140              :     assert((int)array.size() > hole);
     141      3094787 :     array[hole] = x;
     142      3094787 : }
     143              : 
     144              : 
     145              : bool
     146     85407094 : MSVehicleContainer::anyWaitingBefore(SUMOTime time) const {
     147     85407094 :     return !isEmpty() && topTime() <= time;
     148              : }
     149              : 
     150              : 
     151              : const MSVehicleContainer::VehicleVector&
     152      3092738 : MSVehicleContainer::top() {
     153      3092738 :     if (isEmpty()) {
     154            0 :         throw 1;    //!!!Underflow( );
     155              :     }
     156              :     assert(array.size() > 1);
     157      3092738 :     return array[ 1 ].second;
     158              : }
     159              : 
     160              : 
     161              : SUMOTime
     162      4791930 : MSVehicleContainer::topTime() const {
     163      4791930 :     if (isEmpty()) {
     164            0 :         throw 1;    //!!!Underflow( );
     165              :     }
     166              :     assert(array.size() > 1);
     167      4791930 :     return array[ 1 ].first;
     168              : }
     169              : 
     170              : 
     171              : void
     172      3092738 : MSVehicleContainer::pop()
     173              : 
     174              : {
     175      3092738 :     if (isEmpty()) {
     176            0 :         throw 1;    //!!!Underflow( );
     177              :     }
     178              : 
     179              :     assert(array.size() > 1);
     180      3092738 :     array[ 1 ] = array[ currentSize-- ];
     181      3092738 :     percolateDown(1);
     182      3092738 : }
     183              : 
     184              : 
     185              : bool
     186     96384500 : MSVehicleContainer::isEmpty() const {
     187     96384500 :     return currentSize == 0;
     188              : }
     189              : 
     190              : 
     191              : bool
     192      3094787 : MSVehicleContainer::isFull() const {
     193      3094787 :     return currentSize >= ((int) array.size()) - 1;
     194              : }
     195              : 
     196              : 
     197              : void
     198      3092738 : MSVehicleContainer::percolateDown(int hole) {
     199              :     int child;
     200              :     assert((int)array.size() > hole);
     201      3092738 :     VehicleDepartureVector tmp = array[ hole ];
     202              : 
     203      3831512 :     for (; hole * 2 <= currentSize; hole = child) {
     204              :         child = hole * 2;
     205       767669 :         if (child != currentSize && (array[child + 1].first < array[child].first)) {
     206              :             child++;
     207              :         }
     208       767669 :         if ((array[ child ].first < tmp.first)) {
     209              :             assert((int)array.size() > hole);
     210       738774 :             array[hole] = array[child];
     211              :         } else {
     212              :             break;
     213              :         }
     214              :     }
     215              :     assert((int)array.size() > hole);
     216      3092738 :     array[hole] = tmp;
     217      3092738 : }
     218              : 
     219              : 
     220              : int
     221            0 : MSVehicleContainer::size() const {
     222            0 :     return currentSize;
     223              : }
     224              : 
     225              : 
     226              : void
     227            0 : MSVehicleContainer::showArray() const {
     228            0 :     for (VehicleHeap::const_iterator i = array.begin() + 1; i != array.begin() + currentSize + 1; ++i) {
     229            0 :         if (i != array.begin() + 1) {
     230            0 :             std::cout << ", ";
     231              :         }
     232            0 :         std::cout << (*i).first;
     233              :     }
     234              :     std::cout << std::endl << "-------------------------" << std::endl;
     235            0 : }
     236              : 
     237              : 
     238            0 : std::ostream& operator << (std::ostream& strm, MSVehicleContainer& cont) {
     239              :     strm << "------------------------------------" << std::endl;
     240            0 :     while (!cont.isEmpty()) {
     241            0 :         const MSVehicleContainer::VehicleVector& v = cont.top();
     242            0 :         for (MSVehicleContainer::VehicleVector::const_iterator i = v.begin(); i != v.end(); ++i) {
     243            0 :             strm << (*i)->getParameter().depart << std::endl;
     244              :         }
     245            0 :         cont.pop();
     246              :     }
     247            0 :     return strm;
     248              : }
     249              : 
     250              : void
     251          186 : MSVehicleContainer::clearState() {
     252         3822 :     for (VehicleDepartureVector& vdv : array) {
     253         3636 :         vdv.first = 0;
     254              :         vdv.second.clear();
     255              :     }
     256          186 :     currentSize = 0;
     257          186 : }
     258              : 
     259              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1