/// <summary>
        /// Gets or sets the value associated with the specified key.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public TValue this[TKey key]
        {
            get
            {
                TValue value = map.Get(key);
                if (ReferenceEquals(value, default(TValue)) && !map.Contains(key)) // as lookups are lighning fast,  we spare us the implicit
                                                                                   // allocation a call to TryGetValue would mean.
                {
                    throw new KeyNotFoundException();
                }

                return(value);
            }
            set
            {
                map.Put(key, value);
            }
        }
Ejemplo n.º 2
0
        public void testPutIfAbsent()
        {
            var key1 = new MyKey();
            var key2 = new MyKey();
            var key3 = new MyKey();
            var value1 = new MyValue();
            var value2 = new MyValue();
            var value3 = new MyValue();

            var map = new OpenIdentityHashMap<MyKey, MyValue>();

            AssertEquals(null, map.Put(key1, value1));
            AssertEquals(value1, map.Put(key1, value2));
            AssertEquals(value2, map.Put(key1, value1));
            AssertEquals(null, map.Put(key3, value3));

            AssertEquals(value1, map.PutIfAbsent(key1, value2));
            AssertEquals(null, map.PutIfAbsent(key2, value2));
            AssertEquals(value2, map.PutIfAbsent(key2, value1));
            AssertEquals(value3, map.Put(key3, value2));
        }
Ejemplo n.º 3
0
        public void testPutIfAbsent()
        {
            var key1   = new MyKey();
            var key2   = new MyKey();
            var key3   = new MyKey();
            var value1 = new MyValue();
            var value2 = new MyValue();
            var value3 = new MyValue();

            var map = new OpenIdentityHashMap <MyKey, MyValue>();

            AssertEquals(null, map.Put(key1, value1));
            AssertEquals(value1, map.Put(key1, value2));
            AssertEquals(value2, map.Put(key1, value1));
            AssertEquals(null, map.Put(key3, value3));

            AssertEquals(value1, map.PutIfAbsent(key1, value2));
            AssertEquals(null, map.PutIfAbsent(key2, value2));
            AssertEquals(value2, map.PutIfAbsent(key2, value1));
            AssertEquals(value3, map.Put(key3, value2));
        }