Chapter 16: Build a custom set (Part 1)

Source: Internet
Author: User

Ilist <t> and idictionary <tkey, tvalue>

Both ilist <t> and idictionary <tkey, tvalue> inherit the icollection <t> generic interface. Ilist is a special case of idictionary. The key of ilist is always an integer, and the key set is always a continuous set of non-negative integers starting from 0.

Difference: ilist uses indexes for values, while idictionary uses keys for values. So although both interfaces implement the indexer, the method for implementing the indexer is different.

Icomparable <t>

This interface is very important to the set because each collection class has an sort () method. If you need to sort the elements in the set in some way, there are two ways.

1. Use the icomparable <t> interface of the element. This interface has only one method, int compareto (t other), indicating whether the passed element is greater than, less than or equal to the current element.

2. Use custom sorting. The icomparer <t> interface parameters are passed to the sort () method. This interface also has only one method, int compare (t x, t y );.

Icollection <t>

This API is derived from ienumerable <t>. And contains multiple members:

1. The Count attribute returns the elements in the set. If we want to use the for loop to return elements in the Set, the set must support retrieving values by index. icollection does not include this function. But ilist includes.

2. copyto (T [] array, int arrayindex). The index is used to specify where to insert elements in the target array. However, it should be noted that the target array should be large enough.

3. Add (T item). This method is used to implement collection initializers (set initializers ). The Set initializer must be compiled successfully. There are two methods: one is that the Set inherits the icollection <t> interface, and the other is the relaxed version. Inherits ienumerable <t> and provides one or more add () methods.

Index Operator)

T this [pairname name] {Get; set ;}. The index parameter can be of any type, multiple parameters, or overload.

The default name of the pencil attribute of the indexer ([]) is item. However, you can use the attribute [system. runtime. compilerservices. indexername ("entry")] to modify the attribute.

You can also define index operators for variable parameters.

Main collection classes

1. List set list <t>

  The key difference between a list and an array is that the List class is automatically expanded as the number of elements increases.

Each element can be accessed by index. This array is the same.

When an element is obtained based on the index, it does not cause a traversal operation, but directly jumps to a memory location.

To sort the added elements, you must call the sort () method and ensure that the elements inherit the icompareable interface.

Contains (T item), indexof (T item), and lastindexof (T item) All traverse the list. Therefore, the execution time is proportional to the number of actually searched elements before a match is found.

Binarysearch () uses the binary search algorithm, which requires that the elements have been sorted in order. The biggest feature is that if the element is not found, a negative number is returned, and this value is reversed by bit (~) The result is an index that is greater than the next element of the queried element. If there is no larger value, it is the sum of elements. The advantage is that you can insert new values at a specific position in the list and keep them sorted. That is, binary insert.

List <t> findall (predicate <t> match): The delegate instance is passed when this method is called. Public static bool Method1 (INT value). Each time true is returned, the corresponding item is added to a new list <t> instance, and the instance returns.

2. dictionary set dictionary <tkey, tvalue>

  Although the dictionary is more complex than the list, the efficiency of querying with keys is very high because keys are stored in a hash. The key can be of any data type.

If the same key value is used, the second value will overwrite the first value.

Remove an element using the remove (tkey) method.

Dictionary classes do not have specific sorting. The elements are also stored in a hash, which enables fast retrieval, calls gethashcode (), and passes key values to them to obtain the hash code. When you use foreach to traverse a dictionary, the returned data type is keyvaluepair <tkey, tvalue>. if you want to process the key or value separately, you can use the attribute keys and values. their return values are dictionary <tkey, tvalue>. keycollection and dictionary <tkey, tvalue>. valuecollection.

3. Stack set stack <t>

 The following is the set of first-in-first-out (LIFO ).

Push () sends elements to a set without being unique.

Pop () obtains and deletes elements in the reverse order of addition.

Peek () returns the element that will be obtained by pop (), but does not delete the element.

Contains () determines whether an element exists.

Using foreach will not delete the elements in the stack.

4. Queue set queue <t>

  Set of first-in-first-out (FIFO ). Elements do not have to be unique.

Enqueue () and dequeue () correspond to push () and POP () in the stack respectively.

The queue is automatically increased. When you no longer need data, use trimtosize () to restore the previous capacity.

5. Linked List listing <t>

  Allows forward and reverse traversal. There is no non-generic type corresponding to it.

Addafter (inclulistnode <t> node, inclulistnode <t> new node); addbefor (); addfirst (); addlast (); find (t); findlast (); remove (); removefirst (); removelast ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.