Exemplo n.º 1
0
        private void UpdateHelperFunctions(Type t, object key)
        {
            if (_keyType == null)
            {
                // first time through, get the sites for this specific type...
                if (t == typeof(int))
                {
                    _hashFunc = _intHash;
                    _eqFunc   = _intEquals;
                }
                else if (t == typeof(string))
                {
                    _hashFunc = _primitiveHash;
                    _eqFunc   = _stringEquals;
                }
                else if (t == typeof(double))
                {
                    _hashFunc = _doubleHash;
                    _eqFunc   = _doubleEquals;
                }
                else if (t == typeof(PythonTuple))
                {
                    _hashFunc = _tupleHash;
                    _eqFunc   = _tupleEquals;
                }
                else if (t == typeof(Type).GetType())       // this odd check checks for RuntimeType.
                {
                    _hashFunc = _primitiveHash;
                    _eqFunc   = _objectEq;
                }
                else
                {
                    // random type, but still homogeneous... get a shared site for this type.
                    PythonType pt        = DynamicHelpers.GetPythonType(key);
                    var        hashSite  = PythonContext.GetHashSite(pt);
                    var        equalSite = DefaultContext.DefaultPythonContext.GetEqualSite(pt);

                    AssignSiteDelegates(hashSite, equalSite);
                }

                _keyType = t;
            }
            else if (_keyType != HeterogeneousType)
            {
                // 2nd time through, we're adding a new type so we have mutliple types now,
                // make a new site for this storage

                SetHeterogeneousSites();

                // we need to clone the buckets so any lock-free readers will only see
                // the old buckets which are homogeneous
                _buckets = (Bucket[])_buckets.Clone();
            }
            // else we have already created a new site this dictionary
        }