57 lines
1002 B
C#
57 lines
1002 B
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace GaiaCommon1
|
|
{
|
|
[Serializable]
|
|
public class DropStack<T>
|
|
{
|
|
[SerializeField]
|
|
private T[] m_items;
|
|
|
|
[SerializeField]
|
|
private int m_topIndex;
|
|
|
|
[SerializeField]
|
|
private int m_count;
|
|
|
|
public int Capacity => m_items.Length;
|
|
|
|
public int Count => m_count;
|
|
|
|
public DropStack(int capacity)
|
|
{
|
|
m_items = new T[capacity];
|
|
m_count = 0;
|
|
}
|
|
|
|
public void Push(T item)
|
|
{
|
|
m_items[m_topIndex] = item;
|
|
m_topIndex = (m_topIndex + 1) % m_items.Length;
|
|
m_count = ((Count >= m_items.Length) ? m_items.Length : (Count + 1));
|
|
}
|
|
|
|
public void Push(IEnumerable<T> items)
|
|
{
|
|
foreach (T item in items)
|
|
{
|
|
Push(item);
|
|
}
|
|
}
|
|
|
|
public T Pop()
|
|
{
|
|
m_topIndex = (m_items.Length + m_topIndex - 1) % m_items.Length;
|
|
m_count = ((Count >= 2) ? (Count - 1) : 0);
|
|
return m_items[m_topIndex];
|
|
}
|
|
|
|
public T Peek()
|
|
{
|
|
return m_items[(m_items.Length + m_topIndex - 1) % m_items.Length];
|
|
}
|
|
}
|
|
}
|