wangguan
2020-12-23 aadfae81e6a511cd2c062ab0b05f3ee3419f1a7f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using UnityEngine;
using System.Collections.Generic;
 
namespace EnhancedUI
{
    /// <summary>
    /// This is a super light implementation of an array that 
    /// behaves like a list, automatically allocating new memory
    /// when needed, but not releasing it to garbage collection.
    /// </summary>
    /// <typeparam name="T">The type of the list</typeparam>
    public class SmallList<T>
    {
        /// <summary>
        /// internal storage of list data
        /// </summary>
        public T[] data;
 
        /// <summary>
        /// The number of elements in the list
        /// </summary>
        public int Count = 0;
 
        /// <summary>
        /// Indexed access to the list items
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public T this[int i]
        {
            get { return data[i]; }
            set { data[i] = value; }
        }
 
        /// <summary>
        /// Resizes the array when more memory is needed.
        /// </summary>
        private void ResizeArray()
        {
            T[] newData;
 
            if (data != null)
                newData = new T[Mathf.Max(data.Length << 1, 64)];
            else
                newData = new T[64];
 
            if (data != null && Count > 0)
                data.CopyTo(newData, 0);
 
            data = newData;
        }
 
        /// <summary>
        /// Instead of releasing the memory to garbage collection, 
        /// the list size is set back to zero
        /// </summary>
        public void Clear()
        {
            Count = 0;
        }
 
        /// <summary>
        /// Returns the first element of the list
        /// </summary>
        /// <returns></returns>
        public T First()
        {
            if (data == null || Count == 0) return default(T);
            return data[0];
        }
 
        /// <summary>
        /// Returns the last element of the list
        /// </summary>
        /// <returns></returns>
        public T Last()
        {
            if (data == null || Count == 0) return default(T);
            return data[Count - 1];
        }
 
        /// <summary>
        /// Adds a new element to the array, creating more
        /// memory if necessary
        /// </summary>
        /// <param name="item"></param>
        public void Add(T item)
        {
            if (data == null || Count == data.Length)
                ResizeArray();
 
            data[Count] = item;
            Count++;
        }
 
        /// <summary>
        /// Adds a new element to the start of the array, creating more
        /// memory if necessary
        /// </summary>
        /// <param name="item"></param>
        public void AddStart(T item)
        {
            Insert(item, 0);
        }
 
        /// <summary>
        /// Inserts a new element to the array at the index specified, creating more
        /// memory if necessary
        /// </summary>
        /// <param name="item"></param>
        public void Insert(T item, int index)
        {
            if (data == null || Count == data.Length)
                ResizeArray();
 
            for (var i = Count; i > index; i--)
            {
                data[i] = data[i - 1];
            }
 
            data[index] = item;
            Count++;
        }
 
        /// <summary>
        /// Removes an item from the start of the data
        /// </summary>
        /// <returns></returns>
        public T RemoveStart()
        {
            return RemoveAt(0);
        }
 
        /// <summary>
        /// Removes an item from the index of the data
        /// </summary>
        /// <returns></returns>
        public T RemoveAt(int index)
        {
            if (data != null && Count != 0)
            {
                T val = data[index];
 
                for (var i = index; i < Count - 1; i++)
                {
                    data[i] = data[i + 1];
                }
 
                Count--;
                data[Count] = default(T);
                return val;
            }
            else
            {
                return default(T);
            }
        }
 
        /// <summary>
        /// Removes an item from the data
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public T Remove(T item)
        {
            if (data != null && Count != 0)
            {
                for (var i = 0; i < Count; i++)
                {
                    if (data[i].Equals(item))
                    {
                        return RemoveAt(i);
                    }
                }
            }
 
            return default(T);
        }
 
        /// <summary>
        /// Removes an item from the end of the data
        /// </summary>
        /// <returns></returns>
        public T RemoveEnd()
        {
            if (data != null && Count != 0)
            {
                Count--;
                T val = data[Count];
                data[Count] = default(T);
 
                return val;
            }
            else
            {
                return default(T);
            }
        }
 
        /// <summary>
        /// Determines if the data contains the item
        /// </summary>
        /// <param name="item">The item to compare</param>
        /// <returns>True if the item exists in teh data</returns>
        public bool Contains(T item)
        {
            if (data == null)
                return false;
 
            for (var i = 0; i < Count; i++)
            {
                if (data[i].Equals(item))
                    return true;
            }
 
            return false;
        }
    }
}