zoukankan      html  css  js  c++  java
  • 一起学JUCE之HashMap

      基于哈希表的 Map 接口的实现。此实现提供所有可选的映射操作,并允许使用 null 值和 null 键。(除了非同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同。)此类不保证映射的顺序,特别是它不保证该顺序恒久不变。 此实现假定哈希函数将元素适当地分布在各桶之间,可为基本操作(get 和 put)提供稳定的性能。迭代 collection 视图所需的时间与 HashMap 实例的“容量”(桶的数量)及其大小(键-值映射关系数)成比例。所以,如果迭代性能很重要,则不要将初始容量设置得太高(或将加载因子设置得太低)。

      HashMap 的实例有两个参数影响其性能:初始容量 和加载因子,JUCE的HashMap初始容量为101,加载因子是当值的数量大于键值的3/2时重新加载。JUCE中的HashMap是线程安全的,可以在多线程中使用。JUCE的HashMap实现要点和通用的实现方式相同,简单的自己画了个内存结构图。

    HashEntry

     class HashEntry
        {
        public:
            HashEntry (KeyTypeParameter k, ValueTypeParameter val, HashEntry* const next)
                : key (k), value (val), nextEntry (next)
            {}
    
            const KeyType key;
            ValueType value;
            HashEntry* nextEntry;
    
            JUCE_DECLARE_NON_COPYABLE (HashEntry)
        };

    迭代器

     class Iterator
        {
        public:
            //==============================================================================
            Iterator (const HashMap& hashMapToIterate)
                : hashMap (hashMapToIterate), entry (nullptr), index (0)
            {}
    
            /** Moves to the next item, if one is available.
                When this returns true, you can get the item's key and value using getKey() and
                getValue(). If it returns false, the iteration has finished and you should stop.
            */
            bool next()
            {
                if (entry != nullptr)
                    entry = entry->nextEntry;
    
                while (entry == nullptr)
                {
                    if (index >= hashMap.getNumSlots())
                        return false;
    
                    entry = hashMap.hashSlots.getUnchecked (index++);
                }
    
                return true;
            }
    
            /** Returns the current item's key.
                This should only be called when a call to next() has just returned true.
            */
            KeyType getKey() const
            {
                return entry != nullptr ? entry->key : KeyType();
            }
    
            /** Returns the current item's value.
                This should only be called when a call to next() has just returned true.
            */
            ValueType getValue() const
            {
                return entry != nullptr ? entry->value : ValueType();
            }
    
        private:
            //==============================================================================
            const HashMap& hashMap;
            HashEntry* entry;
            int index;
    
            JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Iterator)
        };
  • 相关阅读:
    参考阿里规范,优秀的 Java 项目代码该如何分层?
    SpringBoot 中实现跨域的5种方式
    美团一面:你既然写过Mybatis插件,说说它底层是怎么加载一个自定义插件的
    陌陌面试官:说说Spring AOP 的原理、SpringMVC 的处理过程?
    这16条规范代码,同事,拍桌子 大喊 “666”
    微服务很简单,用一张架构图了解一下
    K8S部署Metrics-Server服务
    cookie
    html标签默认样式整理
    html 语义化标签
  • 原文地址:https://www.cnblogs.com/davygeek/p/4278930.html
Copyright © 2011-2022 走看看