c # parsing common data structure

This article is reproduced connection:  https://blog.csdn.net/suifcd/article/details/42869341

Preface:
might've been small pots of oil every man who read the blog for a summary of this data structure, but every man was little time to write the article slightly in a hurry, so today made some additions and changes, re-published in bull. As the program ape, to master the common data structure is necessary, perhaps this article a bit simple, not so fancy things, but every man also little hope that program to the U3D practitioners like it.
Some time ago a small man can read the code, to which the various data structures and flexible use of praise, but also greatly stimulated the desire of every man for various small data structures to sort and summarize. Just recently I read a number of articles of the great God, that common data structures summarize for themselves the flexibility of use become urgent. It is still an early age to start work content every man, to talk about the scenario data structures and a variety of data structures usually use U3D frequently used it.
1. several common data structures 
here mainly summarizes several small data structure often encountered in the work of every man: Array, ArrayList, List <T >, LinkedList <T>, Queue <T>, Stack <T>, Dictionary <K, T>
array array:  
An array is the most simple data structure. Which has the following characteristics:

  • Arrays are stored in a continuous memory.
  • The contents of the array are the same type.
  • Arrays can be accessed directly through the index.

  Array An array of creation:

int size = 5;

int[] test = new int[size];

When you create a new array will be allocated in the CLR managed heap A contiguous memory space to bloom quantity of size, type of the declared type of array elements. If the type is the value of this type of value type, then there will be a size unboxed be created. If the type is a reference type, then there will be a corresponding size reference type is created.
  Because it is in contiguous memory storage, so it's very fast indexing speed, access time is an element that is constant regardless of the number of elements in the array, and assign and modify elements is simple.

string[] test2 = new string[3];

//赋值

test2[0] = "chen";

test2[1] = "j";

test2[2] = "d";

//修改

test2[0] = "chenjd";

But there are advantages, then it must be accompanied by drawbacks. Because it is stored contiguously, so the new element is inserted between the two elements becomes inconvenient. And, like the code above shows, when you declare a new array, you must specify its length, this will there is a potential problem , and that is when we declare the length is too long, will obviously be a waste of memory, when we declared too short length of time, the risk that overflow. This makes writing code like speculation, very small man can dislike such behavior! In view of this shortcoming, the following grand launch ArrayList.
ArrayList:  
In order to address the shortcomings must specify the length of the array can only create and store the same type and the introduction of data structures. ArrayList is part under the System.Collections namespace, so it must be introduced to use System.Collections. As mentioned above, ArrayList solve some of the shortcomings of the array.

  • Without specifying its length when declaring ArrayList, which is due to the length of the ArrayList object is in accordance with the data stored therein to the dynamic growth and reduced.
  • ArrayList can store different types of elements. This is because it would ArrayList elements as Object to deal with. Accordingly, addition of elements of different types are allowed.

  ArrayList operations:

ArrayList test3 = new ArrayList();

//新增数据

test3.Add("chen");

test3.Add("j");

test3.Add("d");

test3.Add("is");

test3.Add(25);

//修改数据

test3[4] = 26;

//删除数据

test3.RemoveAt(4);

He said so a bunch of "advantage", but also the talk about the shortcomings of it. Why give "advantages" marked the quotes it? That reason is because ArrayList can store different types of data is due to all types as Object to do deal with, that is to say ArrayList elements are actually of type Object, spicy what the problem came.

  • ArrayList not type safe. Because of the different types are processed as Object to do, the situation is likely type mismatch occurs when using ArrayList.
  • As hereinbefore complaints, did not occur when the packing storage array value types, but all types since the ArrayList as the Object, so inevitably occur when inserting boxing operations value type, split occurs when the value of the index box operation. You may be able to do?

Note: Why is frequently unnecessary boxing and unboxing can not bear it? Listen small every man slowly come: the so-called packing (Boxing): is the value of the object instance to the type of conversion (Baidu Encyclopedia). So unboxing: reference type is to be converted to a value type slightly (or from Baidu Encyclopedia). Below is an ~ chestnut

//装箱,将String类型的值FanyoyChenjd赋值给对象。

String  info = ”FanyoyChenjd”; 

object obj=(object)info; 

 

//拆箱,从Obj中提取值给info

object obj = "FanyoyChenjd";

String info = (String)obj;

Then the conclusion? Well, let every man's very little reference to Baidu Encyclopedia low again. It is apparent from the principle, when packing, is a new generation of reference objects, which have lost time, which is resulting in reduced efficiency.

List <T> List generic  
to solve the drawbacks of unsafe ArrayList type of boxing and unboxing, hence the generic concept, introduced as a new array type. Array is often used in the type of work. And ArrayList very similar length can be flexible to change, the biggest difference is that when you declare List collection, we also need for the type of object within a set of data declarations List, and Array and this is very similar, in fact, List <T> Internal use the Array to achieve.

List<string> test4 = new List<string>(); 

   

//新增数据 

test4.Add(“Fanyoy”); 

test4.Add(“Chenjd”); 

 

//修改数据 

test4[1] = “murongxiaopifu”; 

   

//移除数据 

test4.RemoveAt(0);

The greatest advantage is to do so

  • Ensuring type safety.
  • Also canceled the operation boxing and unboxing.
  • It combines the advantages of Array provides quick access to the advantages of flexibility and length ArrayList change.

Suppose you and small, like an ordinary man, a data structure that is most commonly used at work. So again if we do a little more curiosity? That's explore what, if we own realization of a similar data structure, which from where to start it?
Here a small ordinary man to start a discussion.
Just said, internal List <T> is actually a Array, and is strongly typed, so our simple implementation (let's call it EggArray <T>) also adhering to this feature, the interior is achieved by a Array, and need to declare types. But we also see the List <T> inherit and implement a number of interfaces such as IEnumerable interfaces, and value types and reference types-take-all. Here To EggArray <T> implement lightly armed simple line, we do not inherit the List <T> inherit various interfaces, while our EggArray serve only reference types.
Well first of all clear, it is a reference type processing, and to achieve a generics. Then define came out:

//EggArray类

//定义

public class  EggArray<T> where T : class

{

}

So the next step? The determination of its internal members, and to start with the field and property begin.
Properties & Variables

Attributes Explanation
Capacity EggArray capacity
Count The number of elements in EggArray
items

T [], a Array, as the article said internal List <T> but it is still Array, so we also use internal Array

 

 

 

 get

    {

        return this.count;

    }

}

 

public int  Capacity

{

    get

    {

        return this.capacity;

    }

}

Then? It seems to be a need for a constructor. Also I said above, looks like new when it need not be specified capacity. So we put the constructor made so be it.
Constructor:

Constructor Explanation
EggArray () Initialization EggArray <T> class new instance, the instance is empty and has the default initial capacity.
EggArray(int32)

Initialization EggArray <T> class new instance, the instance is empty and has the specified initial capacity.

 

 

//EggArray的构造函数,默认容量为8

public EggArray() : this(8)

{

}

 

public EggArray(int capacity)

{

    this.capacity = capacity;

    this.items = new T[capacity];

}

Well, the constructor also finished, then tell us about private methods, because there is a private operating mechanism all the way to logistics, public method is only open to our use only. Small ordinary man is not interested in achieving a common approach here do not demonstrate.
Just also said, List <T> is the initial length does not matter, you can use Add () method entered, add elements, but it is impossible to have an infinite space to store it, then what is it mean, and what can be done this is it? Because there is a method of dynamically adjusting the size of the internal array is present, and adjust the length of the original size in accordance with the doubling. Let's call Resize.
So before the following content, small ordinary man would like to ask you a question:

List<int> test = new List<int>(){0,1,2,3,4,5,6,7,8,9};

                int count = 0;

                for(int i = 0; i < test.Count; i++)

                {

                        if(i == 1)

                                test.Remove(test[i]);

                        count++;

                }

                Debug.Log (count);

What the above code will output it? The answer is nine. Some oil basin may be surprised, when to go the length of the test is 10 obviously ah. Even among you Remove an element, why will affect the elements behind it? (For example, the index for the element 1 remove off the original index for the elements of the current index 2 to 1 on a.) The feeling of a mess of wood there? In fact, here List <T> while performing remove, but also the internal compression of the array. So certainly there is a method used to compress slightly. We tentatively called Compact.
Private methods

Private methods Explanation
Resize When the number of array elements is greater than or equal to the capacity of the array, the method calls for expansion, creates a new data storage Array, "growth factor" is 2
Compact Packed array, called the default time Remove

//当数组元素个[/size][/backcolor][/color][i][color=White][backcolor=DarkGreen][size=2]数不小于数组容量时,需要扩容,增长因子growthFactor为2

private void  Resize()

{

    int capacity = this.capacity * growthFactor;

    if (this.count > capacity)

    {

        this.count = capacity;

    }

    T[] destinationArray = new T[capacity];

    Array.Copy(this.items, destinationArray, this.count);

    this.items = destinationArray;

    this.capacity = capacity;

}

 

 private void  Compact()

        {

            int num = 0;

            for (int i = 0; i < this.count; i++)

            {

                if (this.items[i] == null)

                {

                    num++;

                }

                else if  (num > 0)

                {

                    this.items[i - num] = this.items[i];

                    this.items[i] = null;

                }

            }

            this.count -= num;

        }[i][i][i]

LinkedList <T>  
is a linked list. And the maximum array is different from the above list that is stored in the sort memory may be discontinuous. This is due to the chain by one point to the next element to element arrangement, it may not be able to access the index. Figure

Since the biggest feature list is stored in the memory space is not necessarily continuous, then the list relative to the largest array of advantages and disadvantages are obvious.

  • Inserted into the list or delete nodes without adjusting the capacity of the structure. Because the store itself is not continuous but by the decisions of each object pointer, so add elements and remove elements should have an advantage over the array.
  • List for add new elements required in an orderly sort of situation, there is also an array of get to do comparison, for example, to add a new element somewhere in the middle of the array, you may need to move a lot of moving elements, and for the list in terms of possible just point to several elements of change only.
  • There are advantages disadvantages, because it is not necessarily arranged in series in the memory space, the access time can not use the index, but to start from scratch node, a node until the next successive traverse to find the target. So when you need quick access to objects, arrays, certainly an advantage.

  In summary, the list is not suitable for a fixed number of elements, and often need to increase or decrease both end access node.
  On the use of the list, there are detailed examples on MSDN.
Queue <T>  
in Queue <T> this data structure, the first element will be inserted first to be deleted; otherwise the last inserted element will be removed last, so queue also known as "first in first out" (FIFO- first in first out) linear form. To achieve access to the Queue <T> Enqueue and Dequeue by using these two methods.

Some need to pay attention to:

  • FIFO scenario.
  • By default, Queue <T> The initial capacity of 32, a growth factor of 2.0.
  • When using the Enqueue, it determines whether the queue length is sufficient, if less, based on the growth factor to increase the capacity, for example, when the initial 2.0, then 2-fold increase of the capacity of the queue.
  • Lackluster.

  

  
Dictionary <K, T>  
dictionary this thing, like a small but very, very ordinary man. After Tell me what they think they would be in the dictionary is not very liked, create a dictionary can be entered, and throwing things, add, delete, access is called a fast Zile. But until a few days ago it looked a little ordinary man the god of the article, that she remembered the phrase "What a good thing that you have accounted for ye make it." So in the end what is hidden behind dictionary fog, then poke heavy fog, whether is the truth? Let's hear next points. . . Etc., should let us examine the dictionary it below.
  Mentioned dictionary would have to say Hashtable hash tables and Hashing (hash, also called hashes), since the dictionary implementation is the implementation of the hash table, but the dictionary is type-safe, which means that when creating when the dictionary, you must declare the type of key and the item, which is the difference between the first dictionary and hash table. About content hash table of recommended look at this blog hash table. About Hash, simply it is a kind of a message of arbitrary length to a fixed length compression, such as a school student number range from 00000 to 99999, a total of five numbers, if each number corresponds to an index of words, then the index is 100 000, but if we use 3 as an index, the index range becomes 000 to 999, of course, the case of conflict, the situation is the hash collision (hash Collisions) a. Pulling away, on the specific implementation principle is to see a small man can recommend the essay blog, of course, that big thing I read blog turn word is quite dazzling. . .
  Back to Dictionary <K, T>, we operate in the dictionary advantage of a variety of time have enjoyed, its weaknesses Where is it? For le, it is space. Space for time, the more memory overhead to meet our pursuit of speed. When you create a dictionary, we can pass a capacitance value, but the actual use of capacity is not the value. But the use of "not less than the minimum prime number as the value of its actual capacity is used, the minimum is 3." (Zhao), when the actual capacity has not directly implemented index, but by creating an additional 2 array to achieve indirect indices, namely int [] buckets and Entry [] entries two arrays (that is, in fact, saved buckets index entries array), the second is the difference between the dictionary and hash table here, remember Ha Greek conflict? Yes, the second difference is that policy processing hash conflict is different! Dictionary will use additional data structures to deal with hash collision, which is one of an array of buckets bucket just mentioned, and the length of the buckets is the true length of the dictionary, because the buckets that mapped the location of each dictionary, then buckets each element which is a list of the hash to the same storage element, storage and redistribution.

Therefore, the situation we face is that, even if we create an empty dictionary, then accompanied by two array of length 3. So when little data processing, or carefully use a dictionary as well, using arrays in many cases also acceptable.

2. Several data structures common usage scenarios

Array Determining the number of elements to be processed and may require the use of subscripts considered, but we recommend the use of List <T>
ArrayList Not recommended, recommended to use List <T>
List <T> generic List The number of elements to be processed is generally recommended to use uncertain
LinkedList<T> List for the number of elements is not fixed, where the need for frequent changes in node, the second end may be increased or decreased
Queue<T> FIFO case
Stack<T> LIFO case
Dictionary<K,T> Operation requires key-value pairs fast


Article reprinted from every man's elaborate Unity3D (four) - common data structures Daqi Di , thank [Murong small man can] provide a good article

Released six original articles · won praise 189 · views 280 000 +

Guess you like

Origin blog.csdn.net/newbie_xymt/article/details/103687770