Summary and analysis of data structures commonly used in transfer-unity3d

Source: Internet
Author: User

Came to the weekend, little bastard finally have the energy and time to update the next blog. A short time ago, Bastard read a code, a variety of data structure of the flexible use of praise, but also greatly stimulated the small bastard on a variety of data structure to comb and summarize the desire. Just recently also read a number of great God's articles, feel summed up the commonly used data structure for their own can also be flexible use becomes urgent. Then still is small bastard of work content to start, talk about in peacetime use u3d often used data structure and various data structures of application scene bar.

1. Several common data structures

Here mainly summarizes the small bastard in the work often encounters several kinds of data structure: ARRAY,ARRAYLIST,LIST<T>,LINKEDLIST<T>,QUEUE<T>,STACK<T> Dictionary<k,t>

Arrays array:

Arrays are the simplest data structures. It has the following characteristics:

    1. The array is stored in contiguous memory.
    2. The contents of the array are the same type.
    3. The array can be accessed directly from the subscript.

Array of arrays is created:

1 int size = 5;2 int[] test = new Int[size];

When a new array is created, a contiguous amount of memory space is allocated in the CLR managed heap to hold the quantity as a size, and the type is an array element of the declared type. If the type is a value type, then there will be a value of size unboxed for that type to be created. If the type is a reference type, a reference to the corresponding type of size will be created.

Because it is stored in contiguous memory, its index is very fast, the time to access an element is constant, that is, regardless of the number of elements in the array, and the assignment and modification elements are simple.

string[] Test2 = new string[3];//assignment test2[0] = "Chen"; test2[1] = "J"; test2[2] = "D";//Modify test2[0] = "CHENJD";

But there are advantages, then it must be accompanied by shortcomings. Because it is continuous storage, it becomes inconvenient to insert new elements between two elements. And, as the code above shows, when declaring a new array, you must specify its length, and there is a potential problem when we declare that the length is too long, obviously wasting memory, and when we declare that the length is too short, we are at risk of this overflow. This makes writing code like speculation, small bastard very disgusted with such behavior! For this disadvantage, the following grand launch ArrayList.

ArrayList:

In order to solve the data structure that must be specified at the time of array creation and can only hold the same type of disadvantage. ArrayList is part of the System.Collections namespace, so you must introduce system.collections if you want to use it. As stated above, ArrayList solves some of the drawbacks of arrays.

    1. It is not necessary to specify its length when declaring ArrayList, because the length of the ArrayList object is dynamically growing and shrinking according to the data stored in it.
    2. ArrayList can store different types of elements. This is because ArrayList will treat its elements as object. Thus, it is permissible to add elements of different types.

Operation of ArrayList:

ArrayList test3 = new ArrayList ();//New Data test3. Add ("Chen"); test3. Add ("J"); Test3. ADD ("D"); Test3. ADD ("is"); test3. ADD (25);//modify data test3[4] = 26;//Delete data test3. RemoveAt (4);

Say so a bunch of "merits", also should say to say the shortcoming. Why do you quote "merit"? That's because ArrayList can store different types of data because all of the types are treated as objects, which means that ArrayList elements are actually object types, and spicy problems come up.

    1. ArrayList is not type-safe. Because different types are treated as objects, there is a good chance that a type mismatch will occur when using ArrayList.
    2. As mentioned above, the array stores the value type without boxing, but ArrayList because all types are treated as object, it is inevitable that a boxing operation will occur when the value type is inserted, and a unboxing operation occurs when the index is evaluated. Can this be tolerated?

Note: Why is it that there is no need to pack and unboxing frequently? and listen to small bastard slowly: the so-called boxing (boxing): Is the value type instance to the object conversion (Baidu Encyclopedia). Then unpacking: Converting the reference type to a value type (or from Baidu Encyclopedia). Here's a chestnut ~

Boxing, assigns the value of string type FANYOYCHENJD to the object. int  info = 1989;  Object Obj= (object) info;  Unboxing, extracting values from obj to infoobject obj = 1;
int info = (int) obj;

So what's the verdict? OK, please allow small bastard very low again quoted Baidu Encyclopedia. Obviously, it can be seen from the principle that when boxing occurs, a new reference object is generated, which can lead to a loss of time, which results in less efficiency.

list<t> Generic List

In order to solve the disadvantage of ArrayList type and packing unboxing, the concept of generics appeared as a new type of array introduced. is also the type of array that is frequently used in the work. and ArrayList very similar, the length can be flexibly changed, the biggest difference is that when declaring the list collection, we also need to declare the list of data in the collection object type, which is similar to the array, in fact, list<t> internal use of an array to achieve.

list<string> test4 = new list<string> ();    Added data  test4. ADD ("Fanyoy");  Test4. ADD ("Chenjd");  Modify Data  test4[1] = "Murongxiaopifu";    Removes the data  test4. RemoveAt (0);  

The best thing about doing this is

    1. That is, type safety is ensured.
    2. The boxing and unpacking operations are also canceled.
    3. It combines the advantages of an array for quick access and the flexibility of the ArrayList length.
Linkedlist<t>

That's the list. The biggest difference from the above array is that the list may not be contiguous in the sort of memory store. This is because the linked list is arranged by pointing to the next element in the previous element, so it may not be accessible by subscript.

Since the most important feature of the list is that the space stored in the memory is not necessarily contiguous, then the list's greatest advantages and disadvantages relative to the array are obvious.

    1. Inserting or removing nodes into a linked list does not require the capacity of the structure to be resized. Because it is not contiguous storage but is determined by pointers to each object, adding and deleting elements is an advantage over arrays.
    2. Lists are suitable for adding new elements in situations where ordered ordering is required, and there is an array of comparisons, for example, to add new elements somewhere in the middle of an array, you may need to move a lot of elements, and for a linked list it may just be a change in the direction of several elements.
    3. There are advantages, because it is not necessarily sequential in the memory space, so the access time can not be used subscript, but must start from the beginning of the node, successive traversal of the next nodes until the target is found. So when you need to quickly access objects, arrays are undoubtedly more advantageous.

In summary, the list is suitable for the number of elements is not fixed, the need to constantly increase or decrease the node situation.

For the use of linked lists, there are detailed examples on MSDN.

Queue<t>

In queue<t> this data structure, the first insertion in the element will be the first to be deleted, whereas the last inserted element will eventually be deleted, so the queue is also known as the "FIFO" (Fifo-first in first out) linear table. Access to the queue<t> is achieved by using both the Enqueue and Dequeue methods.

Some areas to note:

    1. First-out scenario.
    2. By default, the initial capacity of,queue<t> is 32, and the growth factor is 2.0.
    3. When using Enqueue, the length of the queue is determined to be sufficient, if insufficient, to increase the capacity based on the growth factor, for example, when the initial 2.0, the capacity of the queue increases by twice times.
    4. Lackluster.

There are also examples of how queue<t> are used in MSDN.

Stack<t>

  

In contrast to queue<t>, we need to use stack<t> when we need a data structure that uses a last-in, first-out (LIFO) order.

Some areas to note:

    1. A last-in-first-out scenario.
    2. The default capacity is 10.
    3. Use pop and push to operate.
    4. Lackluster.

Also, here you can see a number of examples of stack<t>.

Dictionary<k,t>

A dictionary of this thing, small bastard but like very much. Crossing themselves can also think of the dictionary is not very popular, create a dictionary can throw things inside, add, delete, access that called a fast word. But until the small bastard recently saw a great God of the article, and then think of that sentence "What good things can let you are accounted for it". So what is the hidden fog behind the dictionary, and after the fog, is it the truth? And listen to the next minute ... Wait, it should be the following let us analyze the dictionary.

Referring to a dictionary has to say that the Hashtable hash table and hashing (hash, also called hash), because the way the dictionary is implemented is the way the hash table is implemented, but the dictionary is type-safe , that is, when creating a dictionary, you must declare the type of key and item, This is the difference between the first dictionary and the hash table. The contents of the hash table are recommended to look at this blog hash table . In terms of hashing , it is simply a kind of compression of any length of message to a fixed length, such as a school student number range from 00000~99999, a total of 5 digits, if each number corresponds to an index, then is 100,000 index, But if we use the latter 3-bit as the index, then the scope of the index becomes 000~999, of course, the case of conflict, this is the hash conflict (hash collisions) . Pull away, about the specific implementation of the principle or to see the small bastard recommended blog bar, of course, that blog above that big turn word is also pretty dazzling ...

Back to Dictionary<k,t>, we enjoyed the advantages of all the time in the operation of the dictionary, so where is its disadvantage? Yes, that's space. With space to change time, through more memory overhead to meet our pursuit of speed. When we create a dictionary, we can pass in a capacity value, but the actual capacity used is not that value. Instead , the minimum prime number that is not less than the value is used as the actual capacity it uses, and the minimum is 3. "(Lao Zhao), when the actual capacity is not directly implemented index, but by creating an additional 2 arrays to implement the indirect index, that is int[] buckets and entry[] Entries two arrays (that is, the buckets is actually the subscript of the entries array), here is the second dictionary and hash table difference, remember the hash conflict ? Yes, the second difference is that the strategy for dealing with hash conflicts is different ! The dictionary uses an additional data structure to handle the hash conflict, which is one of the arrays mentioned in the buckets bucket, and the length of buckets is the true length of the dictionary, because buckets is the map for each location of the dictionary, and then each element in the buckets is a linked list. The element used to store the same hash, and then allocate storage space.

So what we're faced with is that even if we create an empty dictionary, it comes with 2 arrays of length 3. So when the data processing is not long, it is prudent to use the dictionary as well, in many cases the use of arrays is also acceptable.

2. Usage scenarios for several common data structures
Array The number of elements that need to be processed is determined and needs to be considered when using subscripts, but it is recommended to use list<t>
ArrayList Not recommended, recommended for use with list<t>
list<t> Generic List It is generally recommended to use an indeterminate number of elements to process
Linkedlist<t> List is suitable for the number of elements are not fixed, the need to constantly increase or decrease the node, 2 can be increased or decreased
Queue<t> First-out situation
Stack<t> Last-in, first-out situations
Dictionary<k,t> Requires key-value pair, fast operation

  

  

  

Transfer from-original link author information Murong Xiao Bastard

Summary and analysis of data structures commonly used in transfer-unity3d

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.