LCOV - code coverage report
Current view: top level - src - ConnectingRoutesCache.cpp (source / functions) Hit Total Coverage
Test: ad_rss_map_integration Lines: 30 30 100.0 %
Date: 2025-07-22 06:56:19 Functions: 4 4 100.0 %
Branches: 25 42 59.5 %

           Branch data     Line data    Source code
       1                 :            : // ----------------- BEGIN LICENSE BLOCK ---------------------------------
       2                 :            : //
       3                 :            : // Copyright (C) 2021 Intel Corporation
       4                 :            : //
       5                 :            : // SPDX-License-Identifier: LGPL-2.1-only
       6                 :            : //
       7                 :            : // ----------------- END LICENSE BLOCK -----------------------------------
       8                 :            : 
       9                 :            : #include "ad/rss/map/ConnectingRoutesCache.hpp"
      10                 :            : #include <ad/map/route/RouteOperation.hpp>
      11                 :            : 
      12                 :            : namespace ad {
      13                 :            : namespace rss {
      14                 :            : namespace map {
      15                 :            : 
      16                 :         11 : void ConnectingRoutesCache::clear()
      17                 :            : {
      18         [ +  - ]:         11 :   const std::lock_guard<std::mutex> lock(m_lock);
      19                 :         11 :   m_cache.clear();
      20                 :         11 : }
      21                 :            : 
      22                 :         19 : void ConnectingRoutesCache::addConnectingRoutes(ad::rss::world::ObjectId const &object_a,
      23                 :            :                                                 ad::rss::world::ObjectId const &object_b,
      24                 :            :                                                 ad::map::route::ConnectingRouteList const &connecting_route_list,
      25                 :            :                                                 ad::physics::Distance const &max_connecting_route_length)
      26                 :            : {
      27         [ +  - ]:         19 :   const std::lock_guard<std::mutex> lock(m_lock);
      28                 :            :   auto insert_a_result
      29   [ +  -  +  - ]:         19 :     = m_cache.insert(std::make_pair(object_a, std::map<ad::rss::world::ObjectId, ConnectingRouteStorage>()));
      30         [ +  - ]:         38 :   insert_a_result.first->second.insert(
      31   [ +  -  +  - ]:         38 :     std::make_pair(object_b, ConnectingRouteStorage(connecting_route_list, max_connecting_route_length)));
      32                 :         19 : }
      33                 :            : 
      34                 :         43 : bool ConnectingRoutesCache::getConnectingRoutes(ad::rss::world::ObjectId const &object_a,
      35                 :            :                                                 ad::rss::world::ObjectId const &object_b,
      36                 :            :                                                 ad::physics::Distance const &max_connecting_route_length,
      37                 :            :                                                 ad::map::route::ConnectingRouteList &connecting_route_list)
      38                 :            : {
      39         [ +  - ]:         43 :   const std::lock_guard<std::mutex> lock(m_lock);
      40                 :            : 
      41         [ +  - ]:         43 :   auto find_result = findEntry(object_a, object_b, max_connecting_route_length, connecting_route_list);
      42         [ +  + ]:         43 :   if (!find_result)
      43                 :            :   {
      44         [ +  - ]:         29 :     find_result = findEntry(object_b, object_a, max_connecting_route_length, connecting_route_list);
      45         [ +  + ]:         29 :     if (find_result)
      46                 :            :     {
      47                 :            :       // invert the objects in the result
      48         [ +  + ]:         24 :       for (auto &connecting_route : connecting_route_list)
      49                 :            :       {
      50         [ +  - ]:         14 :         connecting_route = ad::map::route::swapConnectingRouteObjects(connecting_route);
      51                 :            :       }
      52                 :            :     }
      53                 :            :   }
      54                 :         43 :   return find_result;
      55                 :         43 : }
      56                 :            : 
      57                 :         72 : bool ConnectingRoutesCache::findEntry(ad::rss::world::ObjectId const &object_a,
      58                 :            :                                       ad::rss::world::ObjectId const &object_b,
      59                 :            :                                       ad::physics::Distance const &max_connecting_route_length,
      60                 :            :                                       ad::map::route::ConnectingRouteList &connecting_route_list)
      61                 :            : {
      62         [ +  - ]:         72 :   auto find_a_result = m_cache.find(object_a);
      63         [ +  + ]:         72 :   if (find_a_result != m_cache.end())
      64                 :            :   {
      65         [ +  - ]:         32 :     auto find_b_result = find_a_result->second.find(object_b);
      66         [ +  + ]:         32 :     if (find_b_result != find_a_result->second.end())
      67                 :            :     {
      68         [ +  - ]:         24 :       connecting_route_list = find_b_result->second.connecting_route_list;
      69         [ -  + ]:         24 :       if (connecting_route_list.empty())
      70                 :            :       {
      71                 :            :         // we can consider the entry only as found without connecting route existing if the desired length was searched
      72                 :            :         // for on creation time of the cached one
      73         [ -  - ]:         24 :         return max_connecting_route_length <= find_b_result->second.max_connecting_route_length;
      74                 :            :       }
      75                 :         24 :       return true;
      76                 :            :     }
      77                 :            :   }
      78                 :         48 :   return false;
      79                 :            : }
      80                 :            : 
      81                 :            : } // namespace map
      82                 :            : } // namespace rss
      83                 :            : } // namespace ad

Generated by: LCOV version 1.14