HashSet with Index Access

后端 未结 5 1385
轻奢々
轻奢々 2021-01-07 11:58

I would need a data structure that

  1. Allows me to add/item to it
  2. Do not allow duplication
  3. access the collection via index

I am t

相关标签:
5条回答
  • 2021-01-07 12:29

    I think you need to develope your own List extension class. List can match your point 1 and 3, but to match point 2 you need to override Add methods.

    0 讨论(0)
  • 2021-01-07 12:35

    does it work?

    class MyHashSet<T> : HashSet<T>
    {
        public T this[int index]
        {
            get
            {
                int i = 0;
                foreach (T t in this)
                {
                    if (i == index)
                        return t;
                    i++;
                }
                throw new IndexOutOfRangeException();
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-07 12:47

    I think what you want is a Dictionary.

    0 讨论(0)
  • 2021-01-07 12:48
    You can do it by extending the HashSet, meat of it to see if it contains the element, and thats O(1), reaturn that  element, so no harm done in that case. Here is the derived one:
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    
    namespace Tester
    {
        // Summary:
        //     Represents a set of values.
        //
        // Type parameters:
        //   T:
        //     The type of elements in the hash set.
        [Serializable]
        public class HashSetExt<T> : HashSet<T>
        {
            // Summary:
            //     Initializes a new instance of the System.Collections.Generic.HashSetExt<T> class
            //     that is empty and uses the default equality comparer for the set type.
            public HashSetExt() : base() { }
    
            public HashSetExt(IEnumerable<T> collection) : base(collection) { }
            public HashSetExt(IEqualityComparer<T> comparer) : base(comparer) { }
    
            public HashSetExt(IEnumerable<T> collection, IEqualityComparer<T> comparer) : base(collection, comparer) { }
    
            protected HashSetExt(SerializationInfo info, StreamingContext context) : base(info, context) { }
    
            public T this[T item]
            {
                get
                {
                    if (this.Contains(item))
                    {
                        return item;
                    }
                    throw new KeyNotFoundException();
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-07 12:52

    How about a collection derived from KeyedCollection<TKey, TItem>? This represents a collection of items where each key is derived from the item itself. By default it does not allow you to add duplicates (i.e. items with the same key). It allows lookup by key or index.

    internal class Program
    {
        private static void Main(string[] args)
        {
            TestItemCollection items = new TestItemCollection();
            items.Add(new TestItem("a"));
            items.Add(new TestItem("a")); // throws ArgumentException -- duplicate key
    
            TestItem a = items["a"];
            a = items[0];
        }
    
        private sealed class TestItem
        {
            public TestItem(string value)
            {
                this.Value = value;
            }
    
            public string Value { get; private set; }
        }
    
        private sealed class TestItemCollection : KeyedCollection<string, TestItem>
        {
            public TestItemCollection()
            {
            }
    
            protected override string GetKeyForItem(TestItem item)
            {
                return item.Value;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题