TryAdd() public method

public TryAdd ( System value, int &key ) : bool
value System
key int
return bool
		public void TryAddDuplicate ()
		{
			XmlDictionary dic = new XmlDictionary ();
			XmlDictionaryString d1 = dic.Add ("foo");
			XmlBinaryWriterSession s = new XmlBinaryWriterSession ();
			int idx;
			s.TryAdd (d1, out idx);
			s.TryAdd (d1, out idx);
		}
		public void TryAddIndex ()
		{
			XmlDictionary dic = new XmlDictionary ();
			XmlDictionaryString d1 = dic.Add ("foo");
			XmlDictionaryString d2 = dic.Add ("bar");
			XmlDictionaryString d3 = dic.Add ("baz");
			XmlBinaryWriterSession s = new XmlBinaryWriterSession ();
			int idx;
			s.TryAdd (d1, out idx);
			Assert.AreEqual (0, idx, "#1");
			s.TryAdd (d3, out idx);
			Assert.AreEqual (1, idx, "#2"); // not 2
		}
Esempio n. 3
0
        void WriteDictionaryIndex(XmlDictionaryString ds)
        {
            XmlDictionaryString ds2;
            bool isSession = false;
            int  idx       = ds.Key;

            if (ds.Dictionary != dict_ext)
            {
                isSession = true;
                if (dict_int.TryLookup(ds.Value, out ds2))
                {
                    ds = ds2;
                }
                if (!session.TryLookup(ds, out idx))
                {
                    session.TryAdd(dict_int.Add(ds.Value), out idx);
                }
            }
            if (idx >= 0x80)
            {
                writer.Write((byte)(0x80 + ((idx % 0x80) << 1) + (isSession ? 1 : 0)));
                writer.Write((byte)((byte)(idx / 0x80) << 1));
            }
            else
            {
                writer.Write((byte)(((idx % 0x80) << 1) + (isSession ? 1 : 0)));
            }
        }
		public void Beyond128DictionaryEntries ()
		{
			XmlDictionaryString ds;
			MemoryStream ms = new MemoryStream ();
			XmlDictionary dic = new XmlDictionary ();
			for (int i = 0; i < 260; i++)
				Assert.AreEqual (i, dic.Add ("n" + i).Key, "d");
			XmlDictionary dic2 = new XmlDictionary ();
			XmlBinaryWriterSession session = new XmlBinaryWriterSession ();
			int idx;
			for (int i = 0; i < 260; i++)
				session.TryAdd (dic2.Add ("n" + i), out idx);
			XmlDictionaryWriter w = XmlDictionaryWriter.CreateBinaryWriter (ms, dic, session);
			w.WriteStartElement (dic.Add ("n128"), dic.Add ("n129"));
			w.WriteStartElement (dic2.Add ("n130"), dic2.Add ("n131"));
			w.WriteStartElement (dic.Add ("n132"), dic2.Add ("n133"));
			w.WriteStartElement (dic.Add ("n256"), dic2.Add ("n256"));
			w.Close ();

			byte [] bytes = new byte [] {
				// so, when it went beyond 128, the index
				// becomes 2 bytes, where
				// - the first byte always becomes > 80, and
				// - the second byte becomes (n / 0x80) * 2.
				0x42, 0x80, 2, 0x0A, 0x82, 2,
				0x42, 0x85, 2, 0x0A, 0x87, 2,
				0x42, 0x88, 2, 0x0A, 0x8B, 2,
				0x42, 0x80, 4, 0x0A, 0x81, 4,
				1, 1, 1, 1};
			Assert.AreEqual (bytes, ms.ToArray (), "result");
		}