DefaultKeyedVector和KeyedVector用法

来源:互联网 发布:java上线项目实战视频 编辑:程序博客网 时间:2024/06/05 08:53

原址

【用法示例】

        在 Android Framework 源码中经常可以看到使用 DefaultKeyedVector 类型的容器。举个例子,在 AudioPolicyManagerBase.cpp 中我们可以看到如下代码:

[cpp] view plain copy
 print?
  1. SortedVector<audio_io_handle_t> AudioPolicyManagerBase::getOutputsForDevice(audio_devices_t device,  
  2.                         DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> openOutputs)  
  3. {  
  4.     SortedVector<audio_io_handle_t> outputs;  
  5.   
  6.     ALOGVV("getOutputsForDevice() device %04x", device);  
  7.     for (size_t i = 0; i < openOutputs.size(); i++) {  
  8.         ALOGVV("output %d isDuplicated=%d device=%04x",  
  9.                 i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices());  
  10.         if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {    // 获取第 i 个元素的 value,并查看支持的设备  
  11.             ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));  
  12.             outputs.add(openOutputs.keyAt(i));    // 获取第 i 个元素的 key,并添加到 outputs 向量容器中  
  13.         }  
  14.     }  
  15.     return outputs;  
  16. }  
        可以看到,openOutputs 是一个 DefaultKeyedVector 键值对类型的容器。在代码中,使用 openOutputs.size() 来获取到该容器中的元素个数,使用 openOutputs.valueAt(i) 来获取到该容器的第 i 个元素的 value 值,使用 openOutputs.keyAt(i) 来获取到该容器的第 i 个元素的 key 值。而 outputs 是一个 SortedVector 类型的容器。在代码中,使用 outputs.add() 方法来将从 openOutputs 中获取到的 key 值添加到 outputs 容器中。


【源码定义】

        DefaultKeyedVector 类型容器实际上是一个模板类,继承自 KeyedVector 模板类,实现在 KeyedVector.h 文件中。除了 size()、valueAt()、keyAt()、add() 方法之外,其它常用的方法还有譬如 isEmpty()、replaceValueAt()、removeItem() 等,均可在源码中找到。

KeyedVector 类定义如下:

[cpp] view plain copy
 print?
  1. template <typename KEY, typename VALUE>  
  2. class KeyedVector  
  3. {  
  4. public:  
  5.     typedef KEY    key_type;  
  6.     typedef VALUE  value_type;  
  7.   
  8.     inline                  KeyedVector();  
  9.   
  10.     /* 
  11.      * empty the vector 
  12.      */  
  13.   
  14.     inline  void            clear()                     { mVector.clear(); }  
  15.   
  16.     /*!  
  17.      * vector stats 
  18.      */  
  19.   
  20.     //! returns number of items in the vector  
  21.     inline  size_t          size() const                { return mVector.size(); }  
  22.     //! returns whether or not the vector is empty  
  23.     inline  bool            isEmpty() const             { return mVector.isEmpty(); }  
  24.     //! returns how many items can be stored without reallocating the backing store  
  25.     inline  size_t          capacity() const            { return mVector.capacity(); }  
  26.     //! sets the capacity. capacity can never be reduced less than size()  
  27.     inline ssize_t          setCapacity(size_t size)    { return mVector.setCapacity(size); }  
  28.   
  29.     // returns true if the arguments is known to be identical to this vector  
  30.     inline bool isIdenticalTo(const KeyedVector& rhs) const;  
  31.   
  32.     /*!  
  33.      * accessors 
  34.      */  
  35.             const VALUE&    valueFor(const KEY& key) const;  
  36.             const VALUE&    valueAt(size_t index) const;  
  37.             const KEY&      keyAt(size_t index) const;  
  38.             ssize_t         indexOfKey(const KEY& key) const;  
  39.             const VALUE&    operator[] (size_t index) const;  
  40.   
  41.     /*! 
  42.      * modifying the array 
  43.      */  
  44.   
  45.             VALUE&          editValueFor(const KEY& key);  
  46.             VALUE&          editValueAt(size_t index);  
  47.   
  48.             /*!  
  49.              * add/insert/replace items 
  50.              */  
  51.                
  52.             ssize_t         add(const KEY& key, const VALUE& item);  
  53.             ssize_t         replaceValueFor(const KEY& key, const VALUE& item);  
  54.             ssize_t         replaceValueAt(size_t index, const VALUE& item);  
  55.   
  56.     /*! 
  57.      * remove items 
  58.      */  
  59.   
  60.             ssize_t         removeItem(const KEY& key);  
  61.             ssize_t         removeItemsAt(size_t index, size_t count = 1);  
  62.               
  63. private:  
  64.             SortedVector< key_value_pair_t<KEY, VALUE> >    mVector;    // 重要!将 key-value 整体作为 key_value_pair_t 元素存入到 SortedVector 中  
  65. };  

        在 KeyedVctor 类声明的最后一行,我们看到实际上在内部将每个 key-value 键值对作为一个元素整体存入到 key_value_pair_t 模板结构体中,再把每个 key_value_pair_t 作为元素存入到一个名为 mVector 的 SortedVector 类型排序向量容器中。实际上在调用 valueAt()、keyAt() 等方法时,都是使用了 SortedVector 模板类和 key_value_pair_t 模板结构体的方法和特性。实现代码如下:

[cpp] view plain copy
 print?
  1. template<typename KEY, typename VALUE> inline  
  2. const VALUE& KeyedVector<KEY,VALUE>::valueAt(size_t index) const {  
  3.     return mVector.itemAt(index).value;    // 返回mVector中第index个key_value_pair_t元素的value  
  4. }  
  5.   
  6.   
  7. template<typename KEY, typename VALUE> inline  
  8. const KEY& KeyedVector<KEY,VALUE>::keyAt(size_t index) const {  
  9.     return mVector.itemAt(index).key;    // 返回mVector中第index个key_value_pair_t元素的key  
  10. }  

DefaultKeyedVector 类是从 KeyedVector 类继承而来的,其定义如下:

[cpp] view plain copy
 print?
  1. /** 
  2.  * Variation of KeyedVector that holds a default value to return when 
  3.  * valueFor() is called with a key that doesn't exist. 
  4.  */  
  5. template <typename KEY, typename VALUE>  
  6. class DefaultKeyedVector : public KeyedVector<KEY, VALUE>  
  7. {  
  8. public:  
  9.     inline                  DefaultKeyedVector(const VALUE& defValue = VALUE());  
  10.             const VALUE&    valueFor(const KEY& key) const;  
  11.   
  12. private:  
  13.             VALUE                                           mDefault;  
  14. };  
        可以看到,相较于基类 KeyedVector 而言,DefaultKeyedVector 类只是添加了 valueFor() 方法和一个默认 value 值 mDefault。
原创粉丝点击