CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Sep 2002
    Location
    Bosnia
    Posts
    150

    Set-like collection in C#/.NET

    Is there a collection among .NET collection that doesn't allow duplicates?
    ArrayList is very suitable for what I need, except it allows duplicate values.
    I've searched the System.Collections namespace but couldn't find anything alike.
    if you find it useful - rate it!
    /******\
    |__|-o | |
    \******/

  2. #2
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Set-like collection in C#/.NET

    Couldn't find one myself so knocked up a simple implementation of Set
    Attached Files Attached Files
    Useful? Then click on (Rate This Post) at the top of this post.

  3. #3
    Join Date
    Sep 2002
    Location
    Bosnia
    Posts
    150

    Re: Set-like collection in C#/.NET

    Thank you Norfy, very nice implementation.
    I was going to implement a set anyway,
    just wanted to check if there's already an implementation among .NET's collections.
    if you find it useful - rate it!
    /******\
    |__|-o | |
    \******/

  4. #4
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Set-like collection in C#/.NET

    No problem, just thought I would save you some typing.

    For that alone could you 'rate this post'?
    Useful? Then click on (Rate This Post) at the top of this post.

  5. #5
    Join Date
    Sep 2002
    Location
    Bosnia
    Posts
    150

    Re: Set-like collection in C#/.NET

    I would like to, but...
    You must spread some Reputation around before giving it to Norfy again.
    if you find it useful - rate it!
    /******\
    |__|-o | |
    \******/

  6. #6
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Set-like collection in C#/.NET

    Ah yes, multiple inheritance post
    Useful? Then click on (Rate This Post) at the top of this post.

  7. #7
    Join Date
    Sep 2002
    Location
    Bosnia
    Posts
    150

    Re: Set-like collection in C#/.NET

    Quote Originally Posted by Norfy
    Ah yes, multiple inheritance post
    Yes.
    if you find it useful - rate it!
    /******\
    |__|-o | |
    \******/

  8. #8
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Set-like collection in C#/.NET

    Quote Originally Posted by mehdi62b (Rating Comment)
    I learnt this from you,didn't get why you used a hashtable while you set every key to null..wel we could also use an IList..
    Quite simply, the behaviour of the keys collection of a Hashtable is that of a set.
    For small sets then an IList is ok but for larger sets a Hashtable is quicker.
    Useful? Then click on (Rate This Post) at the top of this post.

  9. #9
    Join Date
    Sep 2002
    Location
    Bosnia
    Posts
    150

    Re: Set-like collection in C#/.NET

    there are some interesting collections that NHibernate uses...
    the DLL is Iesi.Collections.
    I don't know the licensing policy, but i think they're ok generally.
    if you find it useful - rate it!
    /******\
    |__|-o | |
    \******/

  10. #10
    Join Date
    Sep 2004
    Location
    Tehran(Ir)
    Posts
    469

    Re: Set-like collection in C#/.NET

    Quote Originally Posted by Norfy
    For small sets then an IList is ok but for larger sets a Hashtable is quicker.
    we want to just look for in an collection and keys in hashtable are just an ICollection ...hastable is fast but when we give a key and want its item,well its time is O(1)
    but when we want to get whether an item is included or not..
    no difference between a hashtable(keys) and IList...(both of them are an ICollection)
    both of them have O(n).

  11. #11
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Set-like collection in C#/.NET

    Sorry mehdi62b I couldn't follow your comment.

    Whether you ask a Hashtable for a value or if it contains a key the access time is not linear O(n) , it uses hashing for both.

    The keys are returned as an ICollection but are not implemented as such internally.
    Last edited by Norfy; June 7th, 2005 at 08:41 AM.
    Useful? Then click on (Rate This Post) at the top of this post.

  12. #12
    Join Date
    Sep 2004
    Location
    Tehran(Ir)
    Posts
    469

    Re: Set-like collection in C#/.NET

    Quote Originally Posted by Norfy
    Whether you ask a Hashtable for a value or if it contains a key the access time is not linear O(n) , it uses hashing for both.
    I meant when we ask a hashtable whetere it contains a key the time is not O(1)..I meant it doesn't use hashing...
    and you tell it use hashing(how?) ..I don't know exactly..
    no problem...should have a look at one of my book

  13. #13
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Set-like collection in C#/.NET

    Quote Originally Posted by mehdi62b
    I meant it doesn't use hashing...and you tell it use hashing(how?) ..I don't know exactly.
    Have you checked out Reflector yet?

    Nice tool, lets you browse the (decompiled) core .NET libraries.
    Useful? Then click on (Rate This Post) at the top of this post.

  14. #14
    Join Date
    Sep 2004
    Location
    Tehran(Ir)
    Posts
    469

    Re: Set-like collection in C#/.NET

    I will check that link

  15. #15
    Join Date
    Sep 2004
    Location
    Tehran(Ir)
    Posts
    469

    Re: Set-like collection in C#/.NET

    Hi Norfy,
    thank you very much for that link..I didn't know it..its very usefull tool
    and yes,you were right..

    public virtual bool Contains(object item);Declaring Type:System.Collections.ArrayList


    Code:
    public virtual bool Contains(object item)
    {
    	 if (item == null)
    	 {
    			for (int num1 = 0; num1 < this._size; num1++)
    			{
    				 if (this._items[num1] == null)
    				 {
    						return true;
    				 }
    			}
    			return false;
    	 }
    	 for (int num2 = 0; num2 < this._size; num2++)
    	 {
    			if (item.Equals(this._items[num2]))
    			{
    				 return true;
    			}
    	 }
    	 return false;
    }
    and it clearly shows it has O(n)

    and to be countinued...

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured