コード例 #1
0
    public void RemoveTest()
    {
        var dict = new BidirectionalDictionary <char, int>();

        dict.Add('A', 1);
        dict.Add('B', 2);

        Assert.False(dict.Remove('C'));
        Assert.AreEqual(2, dict.Count);

        Assert.False(dict.Remove(3));
        Assert.AreEqual(2, dict.Count);

        Assert.True(dict.Remove(2));
        Assert.AreEqual(1, dict.Count);

        Assert.False(dict.Remove('B'));
        Assert.AreEqual(1, dict.Count);

        Assert.True(dict.Remove('A'));
        Assert.AreEqual(0, dict.Count);

        Assert.False(dict.Remove(1));
        Assert.AreEqual(0, dict.Count);
    }
コード例 #2
0
        /// <summary>
        /// Initializes static members of the <see cref="ColorConverter"/> class.
        /// </summary>
        static ColorConverter()
        {
            _colors = new BidirectionalDictionary <Color, char>();

            _colors.Add(Color.White, 'W');
            _colors.Add(Color.Black, 'B');
        }
コード例 #3
0
    public void AddDuplicateChecking()
    {
        var dict = new BidirectionalDictionary <char, int>();

        dict.Add('A', 1);

        Assert.Throws <InvalidOperationException>(() =>
        {
            dict.Add('A', 1);
        });
        Assert.AreEqual(1, dict.Count);
        Assert.AreEqual(1, dict['A']);
        Assert.AreEqual('A', dict[1]);

        Assert.Throws <InvalidOperationException>(() =>
        {
            dict.Add('A', 2);
        });
        Assert.AreEqual(1, dict.Count);
        Assert.AreEqual(1, dict['A']);
        Assert.AreEqual('A', dict[1]);

        Assert.Throws <InvalidOperationException>(() =>
        {
            dict.Add('B', 1);
        });
        Assert.AreEqual(1, dict.Count);
        Assert.AreEqual(1, dict['A']);
        Assert.AreEqual('A', dict[1]);
    }
コード例 #4
0
 public void Setup()
 {
     // Don't put these in constructor; constructor is only run once for all tests
     Dict = new BidirectionalDictionary <string, int>();
     Dict.Add("one", 1);
     Dict.Add("two", 2);
     Dict.Add("three", 3);
 }
コード例 #5
0
        public void AddTest()
        {
            Assert.Throws <ArgumentNullException>(() => testDictionary.Add(null, 7));
            Assert.Throws <ArgumentException>(() => testDictionary.Add("testt", 2));
            Assert.Throws <ArgumentException>(() => testDictionary.Add("test1", 10));

            testDictionary.Add("test4", 4);
            testDictionary.Add("222", int.MaxValue);
        }
コード例 #6
0
        public void Add_DuplicateValue()
        {
            var dictionary = new BidirectionalDictionary <int, string> ();

            dictionary.Add(42, String1);
            var ex = Assert.Throws <ArgumentException> (() => dictionary.Add(67, String1));

            Assert.AreEqual("value", ex.ParamName);
        }
コード例 #7
0
        private SetupBotToken SetupYourBot(IEnumerable <string> tokens)
        {
            string name = tokens.First();

            namesIdsMappingDictionary.Add(new KeyValuePair <string, int>(name, ++lastPlayerNumber));

            SetupBotToken setupBotToken = new SetupBotToken(lastPlayerNumber, OwnerPerspective.Mine);

            return(setupBotToken);
        }
コード例 #8
0
        public void Replace_ExistingKeyAndValue()
        {
            var dictionary = new BidirectionalDictionary <int, string> ();

            dictionary.Add(42, String1);
            dictionary.Add(67, String2);
            dictionary.Replace(42, String2);

            Assert.That(!dictionary.ContainsKey(67), "Did not remove key 67");
            Assert.That(!dictionary.ContainsValue(String1), "Did not remove value String1");
            Assert.That(dictionary [42], Is.EqualTo(String2), "Did not add mapping 42 -> String2");
        }
コード例 #9
0
 private BidirectionalDictionary <string, string> GetEnumNameMap(Type t)
 {
     if (!_enumMemberNamesPerType.TryGetValue(t, out BidirectionalDictionary <string, string> value))
     {
         lock (_enumMemberNamesPerType)
         {
             if (_enumMemberNamesPerType.TryGetValue(t, out value))
             {
                 return(value);
             }
             value = new BidirectionalDictionary <string, string>(StringComparer.OrdinalIgnoreCase, StringComparer.OrdinalIgnoreCase);
             FieldInfo[] fields = t.GetFields();
             foreach (FieldInfo fieldInfo in fields)
             {
                 string name = fieldInfo.Name;
                 string text = (from EnumMemberAttribute a in fieldInfo.GetCustomAttributes(typeof(EnumMemberAttribute), inherit: true)
                                select a.Value).SingleOrDefault() ?? fieldInfo.Name;
                 if (value.TryGetBySecond(text, out string _))
                 {
                     throw new Exception("Enum name '{0}' already exists on enum '{1}'.".FormatWith(CultureInfo.InvariantCulture, text, t.Name));
                 }
                 value.Add(name, text);
             }
             _enumMemberNamesPerType[t] = value;
             return(value);
         }
     }
     return(value);
 }
コード例 #10
0
        public void ContainsValue()
        {
            var dictionary = new BidirectionalDictionary <string, string> ();

            dictionary.Add(String1, String2);
            Assert.IsTrue(dictionary.ContainsValue(String2));
        }
コード例 #11
0
        public void Add_NullKey()
        {
            var dictionary = new BidirectionalDictionary <string, string> ();
            var ex         = Assert.Throws <ArgumentNullException> (() => dictionary.Add(null, String1));

            Assert.AreEqual("key", ex.ParamName);
        }
コード例 #12
0
        public SettingsTranslationUnit(BidirectionalDictionary <string, int> namesIdsMappingDictionary)
        {
            this.namesIdsMappingDictionary = namesIdsMappingDictionary;

            // player with id 0 will be defined as neutral
            namesIdsMappingDictionary.Add("neutral", 0);
        }
コード例 #13
0
        public void Add_NullValue()
        {
            var dictionary = new BidirectionalDictionary <string, string> ();
            var ex         = Assert.Throws <ArgumentNullException> (() => dictionary.Add(String1, null));

            Assert.AreEqual("value", ex.ParamName);
        }
コード例 #14
0
        public void Item_Get()
        {
            var dictionary = new BidirectionalDictionary <string, string> ();

            dictionary.Add(String1, String2);
            Assert.AreEqual(String2, dictionary [String1]);
        }
コード例 #15
0
        public void Item_Set_KeyToOwnValue()
        {
            var dictionary = new BidirectionalDictionary <int, string> ();

            dictionary.Add(42, String1);
            dictionary [42] = String1;
            Assert.AreEqual(String1, dictionary [42]);
        }
コード例 #16
0
 private void WorldView_TileGameObjectUpdated(Tile t, GameObject go)
 {
     if (tileGameObjectMap.ContainsKey(t) == false)
     {
         // In this case, the gameobject was just created.
         tileGameObjectMap.Add(t, go);
     }
 }
コード例 #17
0
        public void Replace_ExistingValue()
        {
            var dictionary = new BidirectionalDictionary <int, string> ();

            dictionary.Add(42, String1);
            dictionary.Replace(67, String2);
            Assert.AreEqual(String2, dictionary [67]);
        }
コード例 #18
0
        public void Clear()
        {
            var dictionary = new BidirectionalDictionary <string, string> ();

            dictionary.Add(String1, String2);
            dictionary.Clear();
            Assert.AreEqual(0, dictionary.Count);
        }
コード例 #19
0
        public void Remove()
        {
            var dictionary = new BidirectionalDictionary <int, string> ();

            dictionary.Add(42, String1);
            Assert.IsTrue(dictionary.Remove(42));
            Assert.IsFalse(dictionary.ContainsKey(42));
        }
コード例 #20
0
        public void TestBidirectionalDictionary()
        {
            var dict1 = new BidirectionalDictionary <int, double>();

            dict1.Add(4, 2.0);
            Assert.IsTrue(dict1.ContainsFirst(4) && dict1.ContainsSecond(2));
            bool exceptionOnDuplicate = false;

            try {
                dict1.Add(4, 3.0);
            }
            catch (ArgumentException) { exceptionOnDuplicate = true; }
            Assert.IsTrue(exceptionOnDuplicate);
            Assert.IsTrue(dict1.GetByFirst(4) == 2);
            Assert.IsTrue(dict1.GetBySecond(2) == 4);
            Assert.IsTrue(dict1.Count == 1);
            dict1.Clear();
            Assert.IsTrue(dict1.Count == 0);

            var dict2 = new BidirectionalDictionary <ComplexType, int>(new ComplexTypeEqualityComparer());

            Assert.IsTrue(!dict2.Any());
            dict2.Add(new ComplexType(1), 2);
            Assert.IsTrue(dict2.Any());
            dict2.Add(new ComplexType(2), 1);
            Assert.IsTrue(dict2.ContainsFirst(new ComplexType(2)));
            Assert.IsTrue(dict2.ContainsSecond(2));
            exceptionOnDuplicate = false;
            try {
                dict2.Add(new ComplexType(2), 3);
            }
            catch (ArgumentException) { exceptionOnDuplicate = true; }
            Assert.IsTrue(exceptionOnDuplicate);
            exceptionOnDuplicate = false;
            try {
                dict2.Add(new ComplexType(3), 1);
            }
            catch (ArgumentException) { exceptionOnDuplicate = true; }
            Assert.IsTrue(exceptionOnDuplicate);
            Assert.IsTrue(dict2.Count == 2);
            Assert.IsTrue(dict2.GetBySecond(1).Field == 2);
            Assert.IsTrue(dict2.GetByFirst(new ComplexType(1)) == 2);
            dict2.Clear();
            Assert.IsTrue(!dict2.Any());
        }
コード例 #21
0
        public void Add()
        {
            var dictionary = new BidirectionalDictionary <int, string> ();

            dictionary.Add(42, String1);
            Assert.That(dictionary.ContainsKey(42));
            Assert.That(dictionary.ContainsValue(String1));
            Assert.That(dictionary.Count, Is.EqualTo(1));
        }
コード例 #22
0
        public void Item_Set_ExistingValue()
        {
            var dictionary = new BidirectionalDictionary <int, string> ();

            dictionary.Add(42, String1);
            var ex = Assert.Throws <ArgumentException> (() => dictionary [67] = String1);

            Assert.AreEqual("value", ex.ParamName);
        }
コード例 #23
0
        public void ModifyingOriginalUpdatesInverse()
        {
            var original = new BidirectionalDictionary <int, string> ();
            var inverse  = original.Inverse;

            original.Add(42, String1);
            Assert.That(inverse.ContainsKey(String1));
            Assert.That(inverse [String1], Is.EqualTo(42));
        }
コード例 #24
0
        public void Item_Set_ExistingKey()
        {
            var dictionary = new BidirectionalDictionary <int, string> ();

            dictionary.Add(42, String1);
            dictionary [42] = String2;
            Assert.AreEqual(String2, dictionary [42]);
            Assert.AreEqual(42, dictionary.Inverse [String2]);
            Assert.Throws <KeyNotFoundException> (() => dictionary.Inverse [String1].ToString());
        }
コード例 #25
0
        protected override IDictionary <string, string> CreateDictionary(IEnumerable <KeyValuePair <string, string> > values)
        {
            var d = new BidirectionalDictionary <string, string> ();

            foreach (var v in values)
            {
                d.Add(v.Key, v.Value);
            }
            return(d);
        }
コード例 #26
0
        public void TryGetValue()
        {
            var dictionary = new BidirectionalDictionary <string, string> ();

            dictionary.Add(String1, String2);

            string value;

            Assert.IsTrue(dictionary.TryGetValue(String1, out value));
            Assert.AreEqual(String2, value);
        }
コード例 #27
0
        public void Item_Set_KeyToNewValue()
        {
            var dictionary = new BidirectionalDictionary <string, string> ();

            dictionary.Add("key", "value1");
            dictionary ["key"] = "value2";

            Assert.AreEqual("value2", dictionary["key"]);
            Assert.AreEqual("key", dictionary.Inverse["value2"]);
            Assert.Throws <KeyNotFoundException> (() => dictionary.Inverse ["value1"].ToString());
        }
コード例 #28
0
        private static void CollectRemovableFieldLikeDeclarations(RemovableDeclarationCollector declarationCollector,
                                                                  HashSet <ISymbol> declaredPrivateSymbols, BidirectionalDictionary <ISymbol, SyntaxNode> fieldLikeSymbols)
        {
            var declarationKinds           = ImmutableHashSet.Create(SyntaxKind.FieldDeclaration, SyntaxKind.EventFieldDeclaration);
            var removableFieldsDefinitions = declarationCollector.GetRemovableFieldLikeDeclarations(declarationKinds, maxAccessibility);

            foreach (var fieldsDefinitions in removableFieldsDefinitions)
            {
                declaredPrivateSymbols.Add(fieldsDefinitions.Symbol);
                fieldLikeSymbols.Add(fieldsDefinitions.Symbol, fieldsDefinitions.SyntaxNode);
            }
        }
コード例 #29
0
        public TyonAddress RegisterExternalObject(object obj)
        {
            TyonAddress address;

            if (external_address_to_objects.TryGetValueByRight(obj, out address) == false)
            {
                address = new TyonAddress_Int(next_external_address++);
                external_address_to_objects.Add(address, obj);
            }

            return(address);
        }
コード例 #30
0
        public string GetReference(object context, object value)
        {
            BidirectionalDictionary <string, object> mappings = GetMappings(context);

            if (!mappings.TryGetBySecond(value, out var first))
            {
                _referenceCount++;
                first = _referenceCount.ToString(CultureInfo.InvariantCulture);
                mappings.Add(first, value);
            }
            return(first);
        }
コード例 #31
0
 private static void CreateAnnotationForDiagnostics(System.Collections.Immutable.ImmutableArray<Diagnostic> diagnostics,
     string annotationKind,
     BidirectionalDictionary<Diagnostic, SyntaxAnnotation> diagnosticAnnotationPairs)
 {
     foreach (var diagnostic in diagnostics)
     {
         diagnosticAnnotationPairs.Add(diagnostic, new SyntaxAnnotation(annotationKind));
     }
 }
コード例 #32
0
    /// <summary>
    /// A cached representation of the Enum string representation to respect per Enum field name.
    /// </summary>
    /// <param name="t">The type of the Enum.</param>
    /// <returns>A map of enum field name to either the field name, or the configured enum member name (<see cref="EnumMemberAttribute"/>).</returns>
    private BidirectionalDictionary<string, string> GetEnumNameMap(Type t)
    {
      BidirectionalDictionary<string, string> map;

      if (!_enumMemberNamesPerType.TryGetValue(t, out map))
      {
        lock (_enumMemberNamesPerType)
        {
          if (_enumMemberNamesPerType.TryGetValue(t, out map))
            return map;

          map = new BidirectionalDictionary<string, string>(
            StringComparer.OrdinalIgnoreCase,
            StringComparer.OrdinalIgnoreCase);

          foreach (FieldInfo f in t.GetFields())
          {
            string n1 = f.Name;
            string n2;
            

            n2 = f.GetCustomAttributes(typeof (EnumMemberAttribute), true)
                          .Cast<EnumMemberAttribute>()
                          .Select(a => a.Value)
                          .SingleOrDefault() ?? f.Name;

            string s;
            if (map.TryGetBySecond(n2, out s))
            {
              throw new Exception("Enum name '{0}' already exists on enum '{1}'."
                .FormatWith(CultureInfo.InvariantCulture, n2, t.Name));
            }

            map.Add(n1, n2);
          }

          _enumMemberNamesPerType[t] = map;
        }
      }

      return map;
    }
コード例 #33
0
 private static BidirectionalDictionary<string, BitArray> InitializeBasesCodec()
 {
     var bases = new BidirectionalDictionary<string, BitArray>(new BitArrayEqualityComparer());
     bases.Add(".", EncodeZeroOneStringToBitArrays(EncodingResource.bases_Dot));
     bases.Add(",", EncodeZeroOneStringToBitArrays(EncodingResource.bases_Comma));
     bases.Add("A", EncodeZeroOneStringToBitArrays(EncodingResource.bases_CapitalA));
     bases.Add("a", EncodeZeroOneStringToBitArrays(EncodingResource.bases_SmallA));
     bases.Add("C", EncodeZeroOneStringToBitArrays(EncodingResource.bases_CapitalC));
     bases.Add("c", EncodeZeroOneStringToBitArrays(EncodingResource.bases_SmallC));
     bases.Add("G", EncodeZeroOneStringToBitArrays(EncodingResource.bases_CapitalG));
     bases.Add("g", EncodeZeroOneStringToBitArrays(EncodingResource.bases_SmallG));
     bases.Add("T", EncodeZeroOneStringToBitArrays(EncodingResource.bases_CapitalT));
     bases.Add("t", EncodeZeroOneStringToBitArrays(EncodingResource.bases_SmallT));
     bases.Add("N", EncodeZeroOneStringToBitArrays(EncodingResource.bases_CapitalN));
     bases.Add("n", EncodeZeroOneStringToBitArrays(EncodingResource.bases_SmallN));
     bases.Add("*", EncodeZeroOneStringToBitArrays(EncodingResource.bases_Asterisk));
     return bases;
 }
コード例 #34
0
 private static BidirectionalDictionary<string, BitArray> InitializeBasesQualCodec()
 {
     var basesQual = new BidirectionalDictionary<string, BitArray>(new BitArrayEqualityComparer());
     for (int intReprOfChar = 33 /*'!'*/; intReprOfChar <= 126 /*'~'*/; intReprOfChar++)
     {
         char ch = (char)intReprOfChar;
         string stringReprOfChar = new string(new char[] { ch });
         string stringBitOfChar = ConvertQualityCharToBitString(intReprOfChar);
         basesQual.Add(stringReprOfChar, EncodeZeroOneStringToBitArrays(stringBitOfChar));
     }
     return basesQual;
 }
コード例 #35
0
 private static BidirectionalDictionary<string, BitArray> InitializeRefNucCodec()
 {
     var refNuc = new BidirectionalDictionary<string, BitArray>(new BitArrayEqualityComparer());
     refNuc.Add("A", EncodeZeroOneStringToBitArrays(EncodingResource.refNuc_A));
     refNuc.Add("C", EncodeZeroOneStringToBitArrays(EncodingResource.refNuc_C));
     refNuc.Add("G", EncodeZeroOneStringToBitArrays(EncodingResource.refNuc_G));
     refNuc.Add("T", EncodeZeroOneStringToBitArrays(EncodingResource.refNuc_T));
     return refNuc;
 }
コード例 #36
0
 public void Setup()
 {
     // Don't put these in constructor; constructor is only run once for all tests
     Dict = new BidirectionalDictionary<string, int>();
     Dict.Add("one", 1);
     Dict.Add("two", 2);
     Dict.Add("three", 3);
 }
コード例 #37
0
        private static void CollectRemovableFieldLikeDeclarations(RemovableDeclarationCollector declarationCollector,
            HashSet<ISymbol> declaredPrivateSymbols, BidirectionalDictionary<ISymbol, SyntaxNode> fieldLikeSymbols)
        {
            var declarationKinds = ImmutableHashSet.Create(SyntaxKind.FieldDeclaration, SyntaxKind.EventFieldDeclaration);
            var removableFieldsDefinitions = declarationCollector.GetRemovableFieldLikeDeclarations(declarationKinds, maxAccessibility);

            foreach (var fieldsDefinitions in removableFieldsDefinitions)
            {
                declaredPrivateSymbols.Add(fieldsDefinitions.Symbol);
                fieldLikeSymbols.Add(fieldsDefinitions.Symbol, fieldsDefinitions.SyntaxNode);
            }
        }