-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0070b3
commit 1ff0ac1
Showing
9 changed files
with
215 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using UnityEngine; | ||
|
||
namespace KSPCommunityFixes.Library | ||
{ | ||
internal class SimpleObjectPool<T> where T : class, new() | ||
{ | ||
private readonly Stack<T> _stack = new Stack<T>(); | ||
|
||
public T Get() | ||
{ | ||
if (!_stack.TryPop(out T pooledObject)) | ||
pooledObject = new T(); | ||
|
||
return pooledObject; | ||
} | ||
|
||
public void Release(T pooledObject) | ||
{ | ||
_stack.Push(pooledObject); | ||
} | ||
} | ||
|
||
internal class ListPool<T> | ||
{ | ||
private readonly Stack<List<T>> _stack = new Stack<List<T>>(); | ||
|
||
public List<T> Get() | ||
{ | ||
if (!_stack.TryPop(out List<T> pooledList)) | ||
pooledList = new List<T>(); | ||
|
||
return pooledList; | ||
} | ||
|
||
public void Release(List<T> pooledList) | ||
{ | ||
pooledList.Clear(); | ||
_stack.Push(pooledList); | ||
} | ||
} | ||
|
||
internal class ObjectPool<T> where T : class, new() | ||
{ | ||
private readonly Stack<T> _stack = new Stack<T>(); | ||
private readonly Action<T> _onInstantiate; | ||
private readonly Action<T> _onRelease; | ||
|
||
public ObjectPool(Action<T> onInstantiate = null, Action<T> onRelease = null) | ||
{ | ||
_onInstantiate = onInstantiate; | ||
_onRelease = onRelease; | ||
} | ||
|
||
public T Get() | ||
{ | ||
if (!_stack.TryPop(out T pooledObject)) | ||
{ | ||
pooledObject = new T(); | ||
_onInstantiate?.Invoke(pooledObject); | ||
} | ||
|
||
return pooledObject; | ||
} | ||
|
||
public void Release(T pooledObject) | ||
{ | ||
_onRelease?.Invoke(pooledObject); | ||
_stack.Push(pooledObject); | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.