This repository has been archived by the owner on Oct 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCacheArray.cs
238 lines (209 loc) · 5.28 KB
/
CacheArray.cs
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using System.Buffers;
using System;
using System.Collections;
using System.Collections.Generic;
using GameFramework;
using System.Collections.Concurrent;
/// <summary>
/// 数组池, 可以避免频繁new数组
/// </summary>
/// <typeparam name="T"></typeparam>
public class CacheArray<T> : IList<T>, IReference
{
public T this[int index]
{
get
{
lock (SyncRoot)
{
return _items[index];
}
}
set
{
lock (SyncRoot)
{
_items[index] = value;
}
}
}
private T[] _items;
private int _size;
public bool IsFixedSize => false;
public bool IsReadOnly => false;
public int Count => _size;
public bool IsSynchronized => false;
public object SyncRoot => _items;
public CacheArray()
{
}
public static CacheArray<T> Acquire(int currentSize, int cacheSize)
{
var arr = ReferencePool.Acquire<CacheArray<T>>();
arr._items = ArrayPool<T>.Shared.Rent(cacheSize);
arr._size = currentSize;
return arr;
}
/// <summary>
/// 不用时释放并归还数组到数组池
/// </summary>
public void Release()
{
lock (SyncRoot)
{
ReferencePool.Release(this);
}
}
public void Resize(int capacity)
{
lock (SyncRoot)
{
if (_size > capacity)
{
Array.Clear(_items, capacity - 1, _size - capacity);
_size = capacity;
return;
}
if (capacity > _size)
{
int newArrLength = capacity;
var newArr = ArrayPool<T>.Shared.Rent(newArrLength);
Array.Copy(_items, newArr, _size);
ArrayPool<T>.Shared.Return(_items, true);
_items = newArr;
_size = newArrLength;
}
}
}
public void Add(T item)
{
lock (SyncRoot)
{
if (_items.Length > _size)
{
_items[_size++] = item;
return;
}
int newArrLength = _size + 1;
var newArr = ArrayPool<T>.Shared.Rent(newArrLength);
Array.Copy(_items, newArr, _size);
ArrayPool<T>.Shared.Return(_items, true);
_items = newArr;
_size = newArrLength;
}
}
public void Clear()
{
lock (SyncRoot)
{
if (_size > 0 && _items != null)
{
ArrayPool<T>.Shared.Return(_items, true);
_size = 0;
}
}
}
public bool Contains(T item)
{
lock (SyncRoot)
{
if (item == null)
{
for (int i = 0; i < _size; i++)
{
if (_items[i] == null)
{
return true;
}
}
return false;
}
EqualityComparer<T> @default = EqualityComparer<T>.Default;
for (int j = 0; j < _size; j++)
{
if (@default.Equals(_items[j], item))
{
return true;
}
}
return false;
}
}
public void CopyTo(T[] array, int arrayIndex)
{
lock (SyncRoot)
{
Array.Copy(_items, 0, array, arrayIndex, _size);
}
}
public int IndexOf(T item)
{
lock (SyncRoot)
{
return System.Array.IndexOf(_items, item);
}
}
public void Insert(int index, T item)
{
lock (SyncRoot)
{
if (_items.Length > _size)
{
for (int i = _size - 1; i >= index; i--)
{
_items[i + 1] = _items[i];
}
_items[index] = item;
return;
}
int newArrLength = _size + 1;
var newArr = ArrayPool<T>.Shared.Rent(newArrLength);
for (int i = _size - 1; i >= index; i--)
{
newArr[i + 1] = _items[i];
}
newArr[index] = item;
ArrayPool<T>.Shared.Return(_items, true);
_items = newArr;
_size = newArrLength;
}
}
public bool Remove(T item)
{
lock (SyncRoot)
{
int idx = Array.IndexOf(_items, item);
if (idx >= 0 && idx < _size)
{
RemoveAt(idx);
return true;
}
return false;
}
}
public void RemoveAt(int index)
{
lock (SyncRoot)
{
if (index == _size - 1)
{
_items[index] = default;
_size -= 1;
return;
}
for (int i = index + 1; i < _size; i++)
{
_items[i - 1] = _items[i];
}
_size -= 1;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotSupportedException("Not Supported Enumerator");
}
public IEnumerator<T> GetEnumerator()
{
throw new NotSupportedException("Not Supported Enumerator");
}
}