public void SmallDictionaryAlwaysBalanced()
        {
            var sd = new SmallDictionary<int, string>();

            sd.Add(1, "1");
            sd.AssertBalanced();

            sd.Add(10, "1");
            sd.AssertBalanced();

            sd.Add(2, "1");
            sd.AssertBalanced();

            sd.Add(1000, "1");
            sd.AssertBalanced();

            sd.Add(-123, "1");
            sd.AssertBalanced();

            sd.Add(0, "1");
            sd.AssertBalanced();

            sd.Add(4, "1");
            sd.AssertBalanced();

            sd.Add(5, "1");
            sd.AssertBalanced();

            sd.Add(6, "1");
            sd.AssertBalanced();
        }
		public void ThreeItems()
		{
			var dict = new SmallDictionary<int, string>();
			dict[2] = "abc";
			dict[5] = "def";
			dict[11] = "third";
			Assert.AreEqual(3, dict.Count);
			Assert.AreEqual("abc", dict[2]);
			Assert.AreEqual("def", dict[5]);
			Assert.AreEqual("third", dict[11]);
			string output;
			Assert.IsFalse(dict.TryGetValue(3, out output));
			Assert.IsNull(output);
			Assert.IsTrue(dict.TryGetValue(2, out output));
			Assert.AreEqual("abc", output);
			Assert.IsTrue(dict.TryGetValue(5, out output));
			Assert.AreEqual("def", output);
			Assert.IsTrue(dict.TryGetValue(11, out output));
			Assert.AreEqual("third", output);

			Assert.IsFalse(dict.ContainsKey(3));
			Assert.IsTrue(dict.ContainsKey(2));
			Assert.IsTrue(dict.ContainsKey(5));
			Assert.IsTrue(dict.ContainsKey(11));
		}
        internal WithPrimaryConstructorParametersBinder(MethodSymbol primaryCtor, Binder next)
            : base(next)
        {
            Debug.Assert((object)primaryCtor != null);
            this.primaryCtor = primaryCtor;
            var parameters = primaryCtor.Parameters;

            var definitionMap = new SmallDictionary<string, ParameterSymbol>();

            for (int i = parameters.Length - 1; i >= 0; i--)
            {
                var parameter = parameters[i];
                definitionMap[parameter.Name] = parameter;
            }

            this.definitionMap = definitionMap;

            var parameterMap = new MultiDictionary<string, ParameterSymbol>(parameters.Length, EqualityComparer<string>.Default);

            foreach (var parameter in parameters)
            {
                parameterMap.Add(parameter.Name, parameter);
            }

            this.parameterMap = parameterMap;
        }
Beispiel #4
0
 private LocalBinderFactory(MethodSymbol method, Binder enclosing)
 {
     Debug.Assert((object)method != null);
     _map = new SmallDictionary<CSharpSyntaxNode, Binder>(ReferenceEqualityComparer.Instance);
     _method = method;
     _enclosing = enclosing;
 }
        public void TestSmallDict()
        {
            var ht = new SmallDictionary<int, int>();
            const int elements = 150; 

            for (int i = 0; i < elements; i += 10)
            {
                ht.Add(i, i);
                ht.AssertBalanced();
                ht.Add(i - 2, i - 2);
                ht.AssertBalanced();
                ht.Add(i - 3, i - 3);
                ht.AssertBalanced();
                ht.Add(i - 4, i - 4);
                ht.AssertBalanced();
                ht.Add(i - 6, i - 6);
                ht.AssertBalanced();
                ht.Add(i - 5, i - 5);
                ht.AssertBalanced();
                ht.Add(i - 1, i - 1);
                ht.AssertBalanced();
                ht.Add(i - 7, i - 7);
                ht.AssertBalanced();
                ht.Add(i - 8, i - 8);
                ht.AssertBalanced();
                ht.Add(i - 9, i - 9);
                ht.AssertBalanced();
            }

            Assert.Equal(150, ht.Count());

            for (int i = 0; i < elements; i += 10)
            {
                Assert.Equal(ht[i], i);
                Assert.Equal(ht[i - 2], i - 2);
                Assert.Equal(ht[i - 3], i - 3);
                Assert.Equal(ht[i - 4], i - 4);
                Assert.Equal(ht[i - 6], i - 6);
                Assert.Equal(ht[i - 5], i - 5);
                Assert.Equal(ht[i - 1], i - 1);
                Assert.Equal(ht[i - 7], i - 7);
                Assert.Equal(ht[i - 8], i - 8);
                Assert.Equal(ht[i - 9], i - 9);
            }

            foreach (var p in ht)
            {
                Assert.Equal(p.Key, p.Value);
            }

            var keys = ht.Keys.ToArray();
            var values = ht.Values.ToArray();

            for (int i = 0, l = ht.Count(); i < l; i++)
            {
                Assert.Equal(keys[i], values[i]);
            }
        }
            internal void AddDeclaration(string name, SemanticModelInfo info)
            {
                if (locals == null)
                {
                    locals = new SmallDictionary<string, SemanticModelInfo>();
                }

                locals[name] = info;
            }
		public void Empty()
		{
			var dict = new SmallDictionary<int, string>();
			Assert.AreEqual(0, dict.Count);
			string output;
			Assert.IsFalse(dict.TryGetValue(2, out output));
			Assert.IsNull(output);
			Assert.IsFalse(dict.ContainsKey(3));
		}
 private void RecordDefinitions(ImmutableArray<ParameterSymbol> definitions)
 {
     var declarationMap = _definitionMap ?? (_definitionMap = new SmallDictionary<string, ParameterSymbol>());
     foreach (var s in definitions)
     {
         if (!declarationMap.ContainsKey(s.Name))
         {
             declarationMap.Add(s.Name, s);
         }
     }
 }
Beispiel #9
0
        internal ILBuilder(ITokenDeferral module, LocalSlotManager localSlotManager, OptimizationLevel optimizations)
        {
            Debug.Assert(BitConverter.IsLittleEndian);

            this.module = module;
            this.LocalSlotManager = localSlotManager;
            _emitState = default(EmitState);
            _scopeManager = new LocalScopeManager();

            leaderBlock = _currentBlock = _scopeManager.CreateBlock(this);

            _labelInfos = new SmallDictionary<object, LabelInfo>(ReferenceEqualityComparer.Instance);
            _optimizations = optimizations;
        }
Beispiel #10
0
        internal ILBuilder(ITokenDeferral module, LocalSlotManager localSlotManager, bool isOptimizing)
        {
            Debug.Assert(BitConverter.IsLittleEndian);

            this.module = module;
            this.LocalSlotManager = localSlotManager;
            this.emitState = default(EmitState);
            this.scopeManager = new LocalScopeManager();

            leaderBlock = currentBlock = this.scopeManager.CreateBlock(this);

            labelInfos = new SmallDictionary<object, LabelInfo>(ReferenceEqualityComparer.Instance);
            this.isOptimizing = isOptimizing;
        }
		public void OneItem()
		{
			var dict = new SmallDictionary<int, string>();
			dict[2] = "abc";
			Assert.AreEqual(1, dict.Count);
			Assert.AreEqual("abc", dict[2]);
			string output;
			Assert.IsFalse(dict.TryGetValue(3, out output));
			Assert.IsNull(output);
			Assert.IsTrue(dict.TryGetValue(2, out output));
			Assert.AreEqual("abc", output);
			Assert.IsFalse(dict.ContainsKey(3));
			Assert.IsTrue(dict.ContainsKey(2));
		}
Beispiel #12
0
        private static SmallDictionary <string, TSymbol> BuildMap <TSymbol>(ImmutableArray <TSymbol> array)
            where TSymbol : Symbol
        {
            Debug.Assert(array.Length > 0);

            var map = new SmallDictionary <string, TSymbol>();

            // NOTE: in a rare case of having two symbols with same name the one closer to the array's start wins.
            for (int i = array.Length - 1; i >= 0; i--)
            {
                var symbol = array[i];
                map[symbol.Name] = symbol;
            }

            return(map);
        }
Beispiel #13
0
        public void TestSmallDict()
        {
            var ht = new SmallDictionary<int, int>();
            const int elements = 150; 

            for (int i = 0; i < elements; i += 4)
            {
                ht.Add(i, i);
                ht.Add(i - 1, i - 1);
                ht.Add(i - 2, i - 2);
                ht.Add(i - 3, i - 3);
            }

            Assert.Equal(152, ht.Count());

            for (int j = 0; j < 100; j++)
            {
                for (int i = 0; i < elements; i += 4)
                {
                    int v;
                    ht.TryGetValue(i, out v);
                    Assert.Equal(i, v);

                    ht.TryGetValue(i - 1, out v);
                    Assert.Equal(i - 1, v);

                    ht.TryGetValue(i - 2, out v);
                    Assert.Equal(i - 2, v);

                    ht.TryGetValue(i - 3, out v);
                    Assert.Equal(i - 3, v);
                }
            }

            foreach(var p in ht)
            {
                Assert.Equal(p.Key, p.Value);
            }

            var keys = ht.Keys.ToArray();
            var values = ht.Values.ToArray();

            for (int i = 0, l = ht.Count(); i < l; i++)
            {
                Assert.Equal(keys[i], values[i]);
            }
        }
        public void TestSmallDict()
        {
            var       ht       = new SmallDictionary <int, int>();
            const int elements = 150;

            for (int i = 0; i < elements; i += 4)
            {
                ht.Add(i, i);
                ht.Add(i - 1, i - 1);
                ht.Add(i - 2, i - 2);
                ht.Add(i - 3, i - 3);
            }

            Assert.Equal(152, ht.Count());

            for (int j = 0; j < 100; j++)
            {
                for (int i = 0; i < elements; i += 4)
                {
                    int v;
                    ht.TryGetValue(i, out v);
                    Assert.Equal(i, v);

                    ht.TryGetValue(i - 1, out v);
                    Assert.Equal(i - 1, v);

                    ht.TryGetValue(i - 2, out v);
                    Assert.Equal(i - 2, v);

                    ht.TryGetValue(i - 3, out v);
                    Assert.Equal(i - 3, v);
                }
            }

            foreach (var p in ht)
            {
                Assert.Equal(p.Key, p.Value);
            }

            var keys   = ht.Keys.ToArray();
            var values = ht.Values.ToArray();

            for (int i = 0, l = ht.Count(); i < l; i++)
            {
                Assert.Equal(keys[i], values[i]);
            }
        }
Beispiel #15
0
                public Enumerator(SmallDictionary <K, V> dict) : this()
                {
                    var root = dict._root;

                    if (root != null)
                    {
                        // left == right only if both are nulls
                        if (root.Left == root.Right)
                        {
                            _next = root;
                        }
                        else
                        {
                            _stack = new Stack <AvlNode>(dict.HeightApprox());
                            _stack.Push(root);
                        }
                    }
                }
Beispiel #16
0
        private LocalBinderFactory(
            Symbol containingMemberOrLambda,
            SyntaxNode root,
            Binder enclosing
            )
        {
            Debug.Assert((object)containingMemberOrLambda != null);
            Debug.Assert(
                containingMemberOrLambda.Kind != SymbolKind.Local &&
                containingMemberOrLambda.Kind != SymbolKind.RangeVariable &&
                containingMemberOrLambda.Kind != SymbolKind.Parameter
                );

            _map = new SmallDictionary <SyntaxNode, Binder>(ReferenceEqualityComparer.Instance);
            _containingMemberOrLambda = containingMemberOrLambda;
            _enclosing = enclosing;
            _root      = root;
        }
        public override void InitializeTupleFieldDefinitionsToIndexMap()
        {
            Debug.Assert(this.IsTupleType);
            Debug.Assert(this.IsDefinition); // we only store a map for definitions

            var retargetedMap = new SmallDictionary <FieldSymbol, int>(
                ReferenceEqualityComparer.Instance
                );

            foreach (
                (FieldSymbol field, int index) in _underlyingType.TupleFieldDefinitionsToIndexMap
                )
            {
                retargetedMap.Add(this.RetargetingTranslator.Retarget(field), index);
            }

            this.TupleData !.SetFieldDefinitionsToIndexMap(retargetedMap);
        }
Beispiel #18
0
        private static SmallDictionary <TypeParameterSymbol, TypeSymbolWithAnnotations> ConstructMapping(ImmutableArray <TypeParameterSymbol> from, ImmutableArray <TypeSymbolWithAnnotations> to)
        {
            var mapping = new SmallDictionary <TypeParameterSymbol, TypeSymbolWithAnnotations>(ReferenceEqualityComparer.Instance);

            Debug.Assert(from.Length == to.Length);

            for (int i = 0; i < from.Length; i++)
            {
                TypeParameterSymbol       tp = from[i];
                TypeSymbolWithAnnotations ta = to[i];
                if (!ta.Is(tp))
                {
                    mapping.Add(tp, ta);
                }
            }

            return(mapping);
        }
        /// <summary>
        /// Report an error if adding the edge (method1, method2) to the ctor-initializer
        /// graph would add a new cycle to that graph.
        /// </summary>
        /// <param name="method1">a calling ctor</param>
        /// <param name="method2">the chained-to ctor</param>
        /// <param name="syntax">where to report a cyclic error if needed</param>
        /// <param name="diagnostics">a diagnostic bag for receiving the diagnostic</param>
        internal void ReportCtorInitializerCycles(MethodSymbol method1, MethodSymbol method2, CSharpSyntaxNode syntax, DiagnosticBag diagnostics)
        {
            // precondition and postcondition: the graph _constructorInitializers is acyclic.
            // If adding the edge (method1, method2) would induce a cycle, we report an error
            // and do not add it to the set of edges. If it would not induce a cycle we add
            // it to the set of edges and return.

            if (method1 == method2)
            {
                // direct recursion is diagnosed elsewhere
                throw ExceptionUtilities.Unreachable;
            }

            if (_constructorInitializers == null)
            {
                _constructorInitializers = new SmallDictionary <MethodSymbol, MethodSymbol>();
                _constructorInitializers.Add(method1, method2);
                return;
            }

            MethodSymbol next = method2;

            while (true)
            {
                if (_constructorInitializers.TryGetValue(next, out next))
                {
                    Debug.Assert((object)next != null);
                    if (method1 == next)
                    {
                        // We found a (new) cycle containing the edge (method1, method2). Report an
                        // error and do not add the edge.
                        diagnostics.Add(ErrorCode.ERR_IndirectRecursiveConstructorCall, syntax.Location, method1);
                        return;
                    }
                }
                else
                {
                    // we've reached the end of the path without finding a cycle. Add the new edge.
                    _constructorInitializers.Add(method1, method2);
                    return;
                }
            }
        }
Beispiel #20
0
        internal ILBuilder(
            ITokenDeferral module,
            LocalSlotManager localSlotManager,
            OptimizationLevel optimizations,
            bool areLocalsZeroed
            )
        {
            Debug.Assert(BitConverter.IsLittleEndian);

            this.module           = module;
            this.LocalSlotManager = localSlotManager;
            _emitState            = default(EmitState);
            _scopeManager         = new LocalScopeManager();

            leaderBlock = _currentBlock = _scopeManager.CreateBlock(this);

            _labelInfos = new SmallDictionary <object, LabelInfo>(
                ReferenceEqualityComparer.Instance
                );
            _optimizations   = optimizations;
            _areLocalsZeroed = areLocalsZeroed;
        }
Beispiel #21
0
        internal static SmallDictionary <TypeParameterSymbol, bool> BuildIsValueTypeMap(
            Symbol container,
            ImmutableArray <TypeParameterSymbol> typeParameters,
            ImmutableArray <TypeParameterConstraintClause> constraintClauses
            )
        {
            Debug.Assert(constraintClauses.Length == typeParameters.Length);

            var isValueTypeMap = new SmallDictionary <TypeParameterSymbol, bool>(
                ReferenceEqualityComparer.Instance
                );

            foreach (TypeParameterSymbol typeParameter in typeParameters)
            {
                isValueType(
                    typeParameter,
                    constraintClauses,
                    isValueTypeMap,
                    ConsList <TypeParameterSymbol> .Empty
                    );
            }

            return(isValueTypeMap);
Beispiel #22
0
            private bool TryOptimizeSameAsNext(BasicBlock next, ref int delta)
            {
                if (next.HasNoRegularInstructions &&
                    next.BranchCode == this.BranchCode &&
                    next.BranchBlock.Start == this.BranchBlock.Start)
                {
                    if (next.EnclosingHandler == this.EnclosingHandler)
                    {
                        int diff = this.BranchCode.Size() + this.BranchCode.GetBranchOperandSize();
                        delta -= diff;
                        this.SetBranch(null, ILOpCode.Nop);

                        // If current block has no regular instructions the resulting block is a trivial noop
                        // TryOptimizeBranchOverUncondBranch relies on an invariant that
                        // trivial blocks are not targeted by branches,
                        // make sure we are not breaking this condition.
                        if (this.HasNoRegularInstructions)
                        {
                            SmallDictionary <object, LabelInfo> labelInfos            = builder._labelInfos;
                            SmallDictionary <object, LabelInfo> .KeyCollection labels = labelInfos.Keys;
                            foreach (object label in labels)
                            {
                                LabelInfo info = labelInfos[label];
                                if (info.bb == this)
                                {
                                    // move the label from "this" to "next"
                                    labelInfos[label] = info.WithNewTarget(next);
                                }
                            }
                        }

                        return(true);
                    }
                }

                return(false);
            }
        /// <summary>
        /// Report an error if adding the edge (method1, method2) to the ctor-initializer
        /// graph would add a new cycle to that graph.
        /// </summary>
        /// <param name="method1">a calling ctor</param>
        /// <param name="method2">the chained-to ctor</param>
        /// <param name="syntax">where to report a cyclic error if needed</param>
        /// <param name="diagnostics">a diagnostic bag for receiving the diagnostic</param>
        internal void ReportCtorInitializerCycles(MethodSymbol method1, MethodSymbol method2, SyntaxNode syntax, DiagnosticBag diagnostics)
        {
            // precondition and postcondition: the graph _constructorInitializers is acyclic.
            // If adding the edge (method1, method2) would induce a cycle, we report an error
            // and do not add it to the set of edges. If it would not induce a cycle we add
            // it to the set of edges and return.

            if (method1 == method2)
            {
                // direct recursion is diagnosed elsewhere
                throw ExceptionUtilities.Unreachable;
            }

            if (_constructorInitializers == null)
            {
                _constructorInitializers = new SmallDictionary<MethodSymbol, MethodSymbol>();
                _constructorInitializers.Add(method1, method2);
                return;
            }

            MethodSymbol next = method2;
            while (true)
            {
                if (_constructorInitializers.TryGetValue(next, out next))
                {
                    Debug.Assert((object)next != null);
                    if (method1 == next)
                    {
                        // We found a (new) cycle containing the edge (method1, method2). Report an
                        // error and do not add the edge.
                        diagnostics.Add(ErrorCode.ERR_IndirectRecursiveConstructorCall, syntax.Location, method1);
                        return;
                    }
                }
                else
                {
                    // we've reached the end of the path without finding a cycle. Add the new edge.
                    _constructorInitializers.Add(method1, method2);
                    return;
                }
            }
        }
		public void Overwrite()
		{
			var dict = new SmallDictionary<int, string>();
			dict[2] = "abc";
			Assert.AreEqual("abc", dict[2]);
			dict[2] = "first";
			Assert.AreEqual("first", dict[2]);
			Assert.AreEqual(1, dict.Count);
			dict[5] = "def";
			dict[11] = "third";
			Assert.AreEqual(3, dict.Count);
			Assert.AreEqual("first", dict[2]);
			Assert.AreEqual("def", dict[5]);
			Assert.AreEqual("third", dict[11]);
			dict[5] = "second";
			Assert.AreEqual(3, dict.Count);
			Assert.AreEqual("first", dict[2]);
			Assert.AreEqual("second", dict[5]);
			Assert.AreEqual("third", dict[11]);
			dict[11] = "change";
			Assert.AreEqual(3, dict.Count);
			Assert.AreEqual("first", dict[2]);
			Assert.AreEqual("second", dict[5]);
			Assert.AreEqual("change", dict[11]);
		}
        /// <summary> Free resources allocated for this method collection </summary>
        public void Free()
        {
            if (_synthesizedMethods != null)
            {
                _synthesizedMethods.Free();
                _synthesizedMethods = null;
            }

            _wrappers = null;
            _constructorInitializers = null;
        }
Beispiel #26
0
 public TsMultiString()
 {
     m_strings = new SmallDictionary <int, ITsString>();
 }
Beispiel #27
0
 private TypeMap(SmallDictionary <TypeParameterSymbol, TypeSymbolWithAnnotations> mapping)
     : base(new SmallDictionary <TypeParameterSymbol, TypeSymbolWithAnnotations>(mapping, ReferenceEqualityComparer.Instance))
 {
     // mapping contents are read-only hereafter
 }
 public PlatformAttributes(Callsite callsite, SmallDictionary <string, Versions> platforms)
 {
     Callsite  = callsite;
     Platforms = platforms;
 }
Beispiel #29
0
 internal TypeMap(SmallDictionary<TypeParameterSymbol, TypeWithModifiers> mapping)
     : base(new SmallDictionary<TypeParameterSymbol, TypeWithModifiers>(mapping, ReferenceEqualityComparer.Instance))
 {
     // mapping contents are read-only hereafter
     Debug.Assert(!mapping.Keys.Any(tp => tp is SubstitutedTypeParameterSymbol));
 }
		public void Remove()
		{
			var dict = new SmallDictionary<int, string>();
			Assert.IsFalse(dict.Remove(2), "Remove a missing item from an empty dictionary");
			dict.Add(2, "abc");
			Assert.IsFalse(dict.Remove(3), "Remove a missing item from a dictionary with one item");
			Assert.IsTrue(dict.Remove(2), "Remove the only item from a dictionary");
			Assert.AreEqual(0, dict.Count, "Nothing remains after removing the only item");
			dict.Add(2, "abc");
			dict.Add(5, "def");
			dict.Add(11, "third");
			Assert.IsFalse(dict.Remove(7), "Remove a missing item from a dictionary with three items");
			Assert.IsTrue(dict.Remove(2), "Remove the first item from a dictionary with three items");
			Assert.AreEqual(2, dict.Count, "Two items remain after removing the first of three");
			string output;
			Assert.IsFalse(dict.TryGetValue(2, out output), "Removed item (first) should be gone from original three");
			Assert.AreEqual("def", dict[5]);
			Assert.AreEqual("third", dict[11]);
			dict.Add(2, "abc");
			Assert.IsTrue(dict.Remove(5), "Remove first of two items in others");
			Assert.AreEqual(2, dict.Count);
			Assert.IsFalse(dict.TryGetValue(5, out output));
			Assert.AreEqual("abc", dict[2]);
			Assert.AreEqual("third", dict[11]);

			Assert.IsTrue(dict.Remove(2), "Remove only item in others");
			Assert.AreEqual(1, dict.Count);
			Assert.IsFalse(dict.TryGetValue(2, out output));
			Assert.AreEqual("third", dict[11]);

			Assert.IsTrue(dict.Remove(11), "Remove only item in dictionary which previously had three");
			Assert.AreEqual(0, dict.Count);
			Assert.IsFalse(dict.TryGetValue(11, out output));

			dict.Add(2, "abc");
			dict.Add(5, "def");
			dict.Add(11, "third");
			dict.Add(13, "fourth");
			Assert.IsTrue(dict.Remove(11), "Remove middle of three items in others");
			Assert.AreEqual(3, dict.Count);
			Assert.IsFalse(dict.TryGetValue(11, out output));
			Assert.AreEqual("abc", dict[2]);
			Assert.AreEqual("def", dict[5]);
			Assert.AreEqual("fourth", dict[13]);

			Assert.IsTrue(dict.Remove(13), "Remove last of two items in others");
			Assert.AreEqual(2, dict.Count);
			Assert.IsFalse(dict.TryGetValue(13, out output));
			Assert.AreEqual("abc", dict[2]);
			Assert.AreEqual("def", dict[5]);

			Assert.IsTrue(dict.Remove(2), "Remove first item when others contains exactly one");
			Assert.AreEqual(1, dict.Count);
			Assert.IsFalse(dict.TryGetValue(2, out output));
			Assert.AreEqual("def", dict[5]);
		}
Beispiel #31
0
 private TypeMap(SmallDictionary<TypeParameterSymbol, TypeWithModifiers> mapping)
     : base(new SmallDictionary<TypeParameterSymbol, TypeWithModifiers>(mapping, ReferenceEqualityComparer.Instance))
 {
     // mapping contents are read-only hereafter
 }
 public void SmallDictionary()
 {
     IDictionary<string, int> dict = new SmallDictionary<string, int>();
     DoTests(dict);
 }
		public void Enumerator()
		{
			var dict = new SmallDictionary<int, string>();
			foreach (var kvp in dict)
			{
				Assert.Fail("Should get no iterations looping over empty dictionary");
			}
			dict.Add(2, "abc");
			int count = 0;
			foreach (var kvp in dict)
			{
				count++;
				Assert.AreEqual(2, kvp.Key);
				Assert.AreEqual("abc", kvp.Value);
			}
			Assert.AreEqual(1,count);

			dict.Add(5, "def");
			dict.Add(11, "third");
			count = 0;
			Dictionary<int, string> normalDict = new Dictionary<int, string>(dict);
			Assert.AreEqual("abc", normalDict[2]);
			Assert.AreEqual("def", normalDict[5]);
			Assert.AreEqual("third", normalDict[11]);
			Assert.AreEqual(3, normalDict.Count);
			foreach (var kvp in dict)
			{
				count++;
				Assert.IsTrue(normalDict.ContainsKey(kvp.Key));
				Assert.AreEqual(normalDict[kvp.Key], kvp.Value);
				normalDict.Remove(kvp.Key);
			}
			Assert.AreEqual(3, count);
		}
		public void KeysAndValues()
		{
			var dict = new SmallDictionary<int, string>();
			VerifyIntArrays(new int[0], dict.Keys.ToArray());
			VerifyStringArrays(new string[0], dict.Values.ToArray());

			dict.Add(2, "abc");
			VerifyIntArrays(new int[] {2}, dict.Keys.ToArray());
			VerifyStringArrays(new string[] {"abc"}, dict.Values.ToArray());

			// This test is too strict, it enforces a particular order of the results.
			dict.Add(5, "def");
			dict.Add(11, "third");
			VerifyIntArrays(new int[] { 2, 5, 11 }, dict.Keys.ToArray());
			VerifyStringArrays(new string[] { "abc", "def", "third" }, dict.Values.ToArray());
		}
 protected AbstractTypeParameterMap(SmallDictionary<TypeParameterSymbol, TypeSymbol> mapping)
 {
     this.Mapping = mapping;
 }
Beispiel #36
0
 /// <summary>
 /// Constructs a unification with the given dictionary.
 /// </summary>
 /// <param name="dict">
 /// The dictionary to use in construction.
 /// </param>
 internal ImmutableTypeMap(SmallDictionary <TypeParameterSymbol, TypeWithModifiers> dict)
     : base(new SmallDictionary <TypeParameterSymbol, TypeWithModifiers>(dict, EqualityComparer <TypeParameterSymbol> .Default))
 {
     Debug.Assert(IsNormalised, "map should be normalised on construction");
 }
Beispiel #37
0
        private LocalBinderFactory(Symbol containingMemberOrLambda, SyntaxNode root, Binder enclosing, ArrayBuilder<SyntaxNode> methodsWithYields)
        {
            Debug.Assert((object)containingMemberOrLambda != null);
            Debug.Assert(containingMemberOrLambda.Kind != SymbolKind.Local && containingMemberOrLambda.Kind != SymbolKind.RangeVariable && containingMemberOrLambda.Kind != SymbolKind.Parameter);

            _map = new SmallDictionary<SyntaxNode, Binder>(ReferenceEqualityComparer.Instance);
            _containingMemberOrLambda = containingMemberOrLambda;
            _enclosing = enclosing;
            _methodsWithYields = methodsWithYields;
            _root = root;
        }
		public void GetMissingKeyThree()
		{
			var dict = new SmallDictionary<int, string>();
			dict.Add(2, "abc");
			dict.Add(5, "def");
			dict.Add(11, "third");
			var temp = dict[3];
		}
		public void Add()
		{
			var dict = new SmallDictionary<int, string>();
			dict.Add(2, "abc");
			Assert.AreEqual(1, dict.Count);
			dict.Add(5,"def");
			Assert.AreEqual(2, dict.Count);
			dict.Add(11,"third");
			Assert.AreEqual(3, dict.Count);
			Assert.AreEqual("abc", dict[2]);
			Assert.AreEqual("def", dict[5]);
			Assert.AreEqual("third", dict[11]);
		}
Beispiel #40
0
        public void SmallDictionary()
        {
            IDictionary <string, int> dict = new SmallDictionary <string, int>();

            DoTests(dict);
        }
 internal TypeMap(SmallDictionary <TypeParameterSymbol, TypeWithModifiers> mapping)
     : base(new SmallDictionary <TypeParameterSymbol, TypeWithModifiers>(mapping, ReferenceEqualityComparer.Instance))
 {
     // mapping contents are read-only hereafter
     Debug.Assert(!mapping.Keys.Any(tp => tp is SubstitutedTypeParameterSymbol));
 }
 protected AbstractTypeParameterMap(
     SmallDictionary <TypeParameterSymbol, TypeWithAnnotations> mapping
     )
 {
     this.Mapping = mapping;
 }
Beispiel #43
0
        private static SmallDictionary<TypeParameterSymbol, TypeWithModifiers> ConstructMapping(ImmutableArray<TypeParameterSymbol> from, ImmutableArray<TypeWithModifiers> to)
        {
            var mapping = new SmallDictionary<TypeParameterSymbol, TypeWithModifiers>(ReferenceEqualityComparer.Instance);

            Debug.Assert(from.Length == to.Length);

            for (int i = 0; i < from.Length; i++)
            {
                TypeParameterSymbol tp = from[i];
                TypeWithModifiers ta = to[i];
                if (!ta.Is(tp))
                {
                    mapping.Add(tp, ta);
                }
            }

            return mapping;
        }
		public void AddExistingKeyTwoKeys()
		{
			var dict = new SmallDictionary<int, string>();
			dict.Add(2, "abc");
			dict.Add(3, "def");
			dict.Add(3, "def");
		}
Beispiel #45
0
 internal TypeMap(SmallDictionary <TypeParameterSymbol, TypeWithModifiers> mapping)
     : base(new SmallDictionary <TypeParameterSymbol, TypeWithModifiers>(mapping, ReferenceEqualityComparer.Instance))
 {
     // mapping contents are read-only hereafter
 }
Beispiel #46
0
 /// <summary>
 /// Constructs a unification with the given dictionary.
 /// </summary>
 /// <param name="dict">
 /// The dictionary to use in construction.
 /// </param>
 internal ImmutableTypeMap(SmallDictionary <TypeParameterSymbol, TypeWithModifiers> dict)
     : base(new SmallDictionary <TypeParameterSymbol, TypeWithModifiers>(dict, EqualityComparer <TypeParameterSymbol> .Default))
 {
 }
Beispiel #47
0
            static bool isValueType(TypeParameterSymbol thisTypeParameter, ImmutableArray <TypeParameterConstraintClause> constraintClauses, SmallDictionary <TypeParameterSymbol, bool> isValueTypeMap, ConsList <TypeParameterSymbol> inProgress)
            {
                if (inProgress.ContainsReference(thisTypeParameter))
                {
                    return(false);
                }

                if (isValueTypeMap.TryGetValue(thisTypeParameter, out bool knownIsValueType))
                {
                    return(knownIsValueType);
                }

                TypeParameterConstraintClause constraintClause = constraintClauses[thisTypeParameter.Ordinal];

                bool result = false;

                if ((constraintClause.Constraints & TypeParameterConstraintKind.AllValueTypeKinds) != 0)
                {
                    result = true;
                }
                else
                {
                    Symbol container = thisTypeParameter.ContainingSymbol;
                    inProgress = inProgress.Prepend(thisTypeParameter);

                    foreach (TypeWithAnnotations constraintType in constraintClause.ConstraintTypes)
                    {
                        TypeSymbol type = constraintType.IsResolved ? constraintType.Type : constraintType.DefaultType;

                        if (type is TypeParameterSymbol typeParameter && (object)typeParameter.ContainingSymbol == (object)container)
                        {
                            if (isValueType(typeParameter, constraintClauses, isValueTypeMap, inProgress))
                            {
                                result = true;
                                break;
                            }
                        }
 public KeyCollection(SmallDictionary <K, V> dict)
 {
     _dict = dict;
 }
 public ValueCollection(SmallDictionary <K, V> dict)
 {
     _dict = dict;
 }
		public void Clear()
		{
			var dict = new SmallDictionary<int, string>();
			dict.Clear();
			Assert.AreEqual(0, dict.Count);
			dict[2] = "abc";
			dict.Clear();
			Assert.AreEqual(0, dict.Count);
			dict[2] = "abc";
			dict[5] = "def";
			dict[11] = "third";
			dict.Clear();
			Assert.AreEqual(0, dict.Count);

		}
        private void MakeMemberMissing(int member)
        {
            if (lazyMakeMemberMissingMap == null)
            {
                lazyMakeMemberMissingMap = new SmallDictionary<int, bool>();
            }

            lazyMakeMemberMissingMap[member] = true;
        }
 private ExpressionSyntax ParseExpressionExperimental(string text)
 {
     var experimentalFeatures = new SmallDictionary<string, string>(); // no experimental features to enable
     return SyntaxFactory.ParseExpression(text, options: CSharpParseOptions.Default.WithFeatures(experimentalFeatures));
 }