• 百度apollo自动驾驶planning代码学习-Apollo\modules\planning\common\IndexedList类代码详解


    概述

    IndexedList类是apollo planning模块下modules\planning\common\indexed_list.h实现

    从类名来看,应该是实现了一个模板列表,可以存放任意的类型数据list,以及建立id和这类数据的映射map

    从代码来看IndexedList类主要是实现:
    一个通用的索引列表容器?
    里面存放一个任意类型对象的list,和一个id和对象映射的map
    比如其可以用来存放障碍物对象和其id的列表及映射关系。
    这个容器可以任意的增加(id,对象)对,也可以通过Id去查询并返回这个对象。

    就是个通用的小工具,实现任意类型对象的list,以及储存对象及id的映射map。

    indexed_list.h

    #pragma once
    
    #include <unordered_map>
    #include <vector>
    
    #include <boost/thread/shared_mutex.hpp>
    
    #include "cyber/common/log.h"
    #include "modules/common/util/map_util.h"
    
    namespace apollo {
    namespace planning {
    
    template <typename I, typename T>
    class IndexedList {
     public:
      /**
       * @brief 拷贝输入的object对象到IndexedList这个列表里,如果相应的id已经存在了
       * 那么就更新这个列表里id对应的值
       * @param id Object对象的id
       * @param 要被拷贝进IndexedList这个列表里的对象
       * @return 返回指向这个列表里这个对象的指针
       */
       //这个函数就是在这个IndexedList这个列表里object_list_增减这个对象
       //以及在其map里存放id与对象的映射关系
      T* Add(const I id, const T& object) {
        auto obs = Find(id);
        if (obs) {
          AWARN << "object " << id << " is already in container";
          *obs = object;
          return obs;
        } else {
          object_dict_.insert({id, object});
          auto* ptr = &object_dict_.at(id);
          object_list_.push_back(ptr);
          return ptr;
        }
      }
    
     //根据id查找该容器里存放的对象
      T* Find(const I id) {
        return apollo::common::util::FindOrNull(object_dict_, id);
      }
    
       //根据id查找该容器里存放的对象
      const T* Find(const I id) const {
        return apollo::common::util::FindOrNull(object_dict_, id);
      }
    
      //返回容器里的对象列表
      const std::vector<const T*>& Items() const { return object_list_; }
    
      //获取容器里存放的id和对象的字典,也就是id和对象的映射map
      const std::unordered_map<I, T>& Dict() const { return object_dict_; }
    
      //对于IndexedList类重载运算符=,进行值的拷贝
      IndexedList& operator=(const IndexedList& other) {
        this->object_list_.clear();
        this->object_dict_.clear();
        for (const auto& item : other.Dict()) {
          Add(item.first, item.second);
        }
        return *this;
      }
    
     private:
     //数据成员,就是存放的任意类型的对象列表
      std::vector<const T*> object_list_;
      
      //数据成员,就是存放的id和任意类型的对象列表,通常是Id,也可以是Name或者其他
      std::unordered_map<I, T> object_dict_;
    };
    
    //又定义了一个模板类,线程安全的索引列表类ThreadSafeIndexedList 
    //跟上面功能差不多
    template <typename I, typename T>
    class ThreadSafeIndexedList : public IndexedList<I, T> {
     public:
     //在容器里增加一个任意类型要存放的对象object,以及id和object的映射Map
      T* Add(const I id, const T& object) {
        boost::unique_lock<boost::shared_mutex> writer_lock(mutex_);
        return IndexedList<I, T>::Add(id, object);
      }
    
    //根据id去map查询这个对象并返回
      T* Find(const I id) {
        boost::shared_lock<boost::shared_mutex> reader_lock(mutex_);
        return IndexedList<I, T>::Find(id);
      }
    
    //获取该容器里的整个对象列表
      std::vector<const T*> Items() const {
        boost::shared_lock<boost::shared_mutex> reader_lock(mutex_);
        return IndexedList<I, T>::Items();
      }
    
     private:
      mutable boost::shared_mutex mutex_;
    };
    
    }  // namespace planning
    }  // namespace apollo
    
    
    • 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
  • 相关阅读:
    STM32定时器笔记
    【EI会议征稿】第七届大数据与应用统计国际学术研讨会(ISBDAS 2024)
    大数据必学Java基础(十二):基本数据类型
    圆方树 useful things
    流量调度、微服务可寻址性和注册中心
    linux查杀web后门webshell
    asp.net core webapi接收application/x-www-form-urlencoded和form-data参数
    C语言--每日五道选择题--Day4
    决策树——预剪枝和后剪枝
    python11 序列的相关操作
  • 原文地址:https://blog.csdn.net/weixin_39199083/article/details/124917895