Add project files.
This commit is contained in:
65
Assets/Cryville/Common/Buffers/ArrayPool.cs
Normal file
65
Assets/Cryville/Common/Buffers/ArrayPool.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
namespace Cryville.Common.Buffers {
|
||||
/// <summary>
|
||||
/// A resource pool that allows reusing instances of arrays of type <typeparamref name="T" />.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item type of the arrays in the pool.</typeparam>
|
||||
public class ArrayPool<T> {
|
||||
private class Bucket : ObjectPool<T[]> {
|
||||
readonly int _size;
|
||||
public Bucket(int size, int capacity) : base(capacity) {
|
||||
_size = size;
|
||||
}
|
||||
protected override T[] Construct() {
|
||||
return new T[_size];
|
||||
}
|
||||
}
|
||||
Bucket[] _buckets;
|
||||
/// <summary>
|
||||
/// Creates an instance of the <see cref="ArrayPool{T}" /> class with the default maximum list size and bucket capacity.
|
||||
/// </summary>
|
||||
public ArrayPool() : this(0x40000000, 256) { }
|
||||
/// <summary>
|
||||
/// Creates an instance of the <see cref="ArrayPool{T}" /> class.
|
||||
/// </summary>
|
||||
/// <param name="maxSize">The maximum size of the arrays in the pool.</param>
|
||||
/// <param name="capacityPerBucket">The capacity of each bucket. The pool groups arrays of similar sizes into buckets for faster access.</param>
|
||||
public ArrayPool(int maxSize, int capacityPerBucket) {
|
||||
if (maxSize < 16) maxSize = 16;
|
||||
int num = GetID(maxSize) + 1;
|
||||
_buckets = new Bucket[num];
|
||||
for (int i = 0; i < num; i++) {
|
||||
_buckets[i] = new Bucket(GetSize(i), capacityPerBucket);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Rents an array that is at least the specified size from the pool.
|
||||
/// </summary>
|
||||
/// <param name="size">The minimum size of the array.</param>
|
||||
/// <returns>An array of type <see cref="T" /> that is at least the specified size.</returns>
|
||||
public T[] Rent(int size) {
|
||||
int len2 = size;
|
||||
if (len2 < 16) len2 = 16;
|
||||
var arr = _buckets[GetID(len2)].Rent();
|
||||
return arr;
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns a rented array to the pool.
|
||||
/// </summary>
|
||||
/// <param name="arr">The array to return.</param>
|
||||
public void Return(T[] arr) {
|
||||
int len2 = arr.Length;
|
||||
if (len2 < 16) len2 = 16;
|
||||
_buckets[GetID(len2)].Return(arr);
|
||||
}
|
||||
static int GetID(int size) {
|
||||
size -= 1;
|
||||
size >>= 4;
|
||||
int num = 0;
|
||||
for (; size != 0; size >>= 1) num++;
|
||||
return num;
|
||||
}
|
||||
static int GetSize(int id) {
|
||||
return 0x10 << id;
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Cryville/Common/Buffers/ArrayPool.cs.meta
Normal file
11
Assets/Cryville/Common/Buffers/ArrayPool.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: df66519fa93e1b94ea5bb1702cc91b3f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
71
Assets/Cryville/Common/Buffers/ListPool.cs
Normal file
71
Assets/Cryville/Common/Buffers/ListPool.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Cryville.Common.Buffers {
|
||||
/// <summary>
|
||||
/// A resource pool that allows reusing instances of lists of type <typeparamref name="T" />.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item type of the lists in the pool.</typeparam>
|
||||
public class ListPool<T> {
|
||||
private class Bucket : ObjectPool<List<T>> {
|
||||
readonly int _size;
|
||||
public Bucket(int size, int capacity) : base(capacity) {
|
||||
_size = size;
|
||||
}
|
||||
protected override List<T> Construct() {
|
||||
return new List<T>(_size);
|
||||
}
|
||||
}
|
||||
Bucket[] _buckets;
|
||||
/// <summary>
|
||||
/// Creates an instance of the <see cref="ListPool{T}" /> class with the default maximum list size and bucket capacity.
|
||||
/// </summary>
|
||||
public ListPool() : this(0x40000000, 256) { }
|
||||
/// <summary>
|
||||
/// Creates an instance of the <see cref="ListPool{T}" /> class.
|
||||
/// </summary>
|
||||
/// <param name="maxSize">The maximum size of the lists in the pool.</param>
|
||||
/// <param name="capacityPerBucket">The capacity of each bucket. The pool groups lists of similar sizes into buckets for faster access.</param>
|
||||
public ListPool(int maxSize, int capacityPerBucket) {
|
||||
if (maxSize < 16) maxSize = 16;
|
||||
int num = GetID(maxSize) + 1;
|
||||
_buckets = new Bucket[num];
|
||||
for (int i = 0; i < num; i++) {
|
||||
_buckets[i] = new Bucket(GetSize(i), capacityPerBucket);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Rents a list of the specified size from the pool. The size of the list must not be changed when it is rented.
|
||||
/// </summary>
|
||||
/// <param name="size">The size of the list.</param>
|
||||
/// <returns>A <see cref="List{T}" /> of the specified size.</returns>
|
||||
public List<T> Rent(int size) {
|
||||
int len2 = size;
|
||||
if (len2 < 16) len2 = 16;
|
||||
var list = _buckets[GetID(len2)].Rent();
|
||||
if (list.Count < size)
|
||||
for (int i = list.Count; i < size; i++) list.Add(default(T));
|
||||
else if (list.Count > size)
|
||||
list.RemoveRange(size, list.Count - size);
|
||||
return list;
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns a rented list to the pool.
|
||||
/// </summary>
|
||||
/// <param name="list">The list to return.</param>
|
||||
public void Return(List<T> list) {
|
||||
int len2 = list.Capacity;
|
||||
if (len2 < 16) len2 = 16;
|
||||
_buckets[GetID(len2)].Return(list);
|
||||
}
|
||||
static int GetID(int size) {
|
||||
size -= 1;
|
||||
size >>= 4;
|
||||
int num = 0;
|
||||
for (; size != 0; size >>= 1) num++;
|
||||
return num;
|
||||
}
|
||||
static int GetSize(int id) {
|
||||
return 0x10 << id;
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Cryville/Common/Buffers/ListPool.cs.meta
Normal file
11
Assets/Cryville/Common/Buffers/ListPool.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2b7b45ff20c33ac47b476371673b037c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
42
Assets/Cryville/Common/Buffers/ObjectPool.cs
Normal file
42
Assets/Cryville/Common/Buffers/ObjectPool.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
namespace Cryville.Common.Buffers {
|
||||
/// <summary>
|
||||
/// A resource pool that allows reusing instances of type <typeparamref name="T" />.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the objects in the pool.</typeparam>
|
||||
public abstract class ObjectPool<T> where T : class {
|
||||
int _index;
|
||||
readonly T[] _objs;
|
||||
/// <summary>
|
||||
/// Creates an instance of the <see cref="ObjectPool{T}" /> class.
|
||||
/// </summary>
|
||||
/// <param name="capacity">The capacity of the pool.</param>
|
||||
public ObjectPool(int capacity) {
|
||||
_objs = new T[capacity];
|
||||
}
|
||||
/// <summary>
|
||||
/// Rents a object from the pool.
|
||||
/// </summary>
|
||||
/// <returns>The rented object.</returns>
|
||||
public T Rent() {
|
||||
T obj = null;
|
||||
if (_index < _objs.Length) {
|
||||
obj = _objs[_index];
|
||||
_objs[_index++] = null;
|
||||
}
|
||||
if (obj == null) obj = Construct();
|
||||
return obj;
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns a rented object to the pool.
|
||||
/// </summary>
|
||||
/// <param name="obj">The object to return.</param>
|
||||
public void Return(T obj) {
|
||||
if (_index > 0) _objs[--_index] = obj;
|
||||
}
|
||||
/// <summary>
|
||||
/// Constructs a new instance of type <typeparamref name="T" />.
|
||||
/// </summary>
|
||||
/// <returns>The new instance.</returns>
|
||||
protected abstract T Construct();
|
||||
}
|
||||
}
|
11
Assets/Cryville/Common/Buffers/ObjectPool.cs.meta
Normal file
11
Assets/Cryville/Common/Buffers/ObjectPool.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2745c44c3cc32be4ab3a43888c14b9a1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
16
Assets/Cryville/Common/Buffers/SimpleObjectPool.cs
Normal file
16
Assets/Cryville/Common/Buffers/SimpleObjectPool.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace Cryville.Common.Buffers {
|
||||
/// <summary>
|
||||
/// A resource pool that allows reusing instances of type <typeparamref name="T" />, which has a parameterless constructor.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the objects in the pool.</typeparam>
|
||||
public class SimpleObjectPool<T> : ObjectPool<T> where T : class, new() {
|
||||
/// <summary>
|
||||
/// Creates an instance of the <see cref="SimpleObjectPool{T}" /> class.
|
||||
/// </summary>
|
||||
/// <param name="capacity">The capacity of the pool.</param>
|
||||
public SimpleObjectPool(int capacity) : base(capacity) { }
|
||||
protected override T Construct() {
|
||||
return new T();
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Cryville/Common/Buffers/SimpleObjectPool.cs.meta
Normal file
11
Assets/Cryville/Common/Buffers/SimpleObjectPool.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b8cd439340f088d4eb83711a5bc6384d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
10
Assets/Cryville/Common/Buffers/WStringPool.cs
Normal file
10
Assets/Cryville/Common/Buffers/WStringPool.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Cryville.Common.Buffers {
|
||||
public class WStringPool {
|
||||
public WStringPool() {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Cryville/Common/Buffers/WStringPool.cs.meta
Normal file
11
Assets/Cryville/Common/Buffers/WStringPool.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 704270b37917aa1458db9d14bab07073
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user