Unity C# 泛型优先队列

源码

//工具类,用到了里面的一个交换方法,可以自己整合到优先队列的类中去
public static class Utilities
{
	public static void Swap<T>(ref T a, ref T b)
    {
	    T temp = a;
    	a = b;
        b = temp;
	}
}

//比较方式,选less就是小根堆,greater就是大根堆
public enum Comparator { less = -1, equal = 0, greater = 1 }

public class PriorityQueue<T> where T : IComparable
{
    private int _size;
    private int capacity;
    private T[] elements;
    private Comparator comparator;

    public int size { get => _size; }
    public bool empty { get => (size == 0); }
    public T top { get => elements[0]; }

    public PriorityQueue(Comparator comparator = Comparator.less, int capacity = 1)
    {
        _size = 0;
        this.capacity = capacity;
        this.comparator = comparator;
        elements = new T[capacity];
    }

    private void ShiftDown()
    {
        int cur = 0;
        int child = 1;
        while (child < size)
        {
            if (child + 1 < size && elements[child + 1].CompareTo(elements[child]) == (int)comparator)
                child++;

            if (elements[child].CompareTo(elements[cur]) == (int)comparator)
            {
                Utilities.Swap<T>(ref elements[child], ref elements[cur]);
                cur = child;
                child = 2 * cur + 1;
            }
            else break;
        }
    }

    private void ShiftUp()
    {
        int cur = size - 1;
        int parent = (cur - 1) / 2;
        while (cur > 0)
        {
            if (elements[cur].CompareTo(elements[parent]) == (int)comparator)
            {
                Utilities.Swap<T>(ref elements[cur], ref elements[parent]);
                cur = parent;
                parent = (cur - 1) / 2;
            }
            else break;
        }
    }

    private void ExpandCapacity()
    {
        capacity = Mathf.CeilToInt(capacity * 1.5f);
        T[] temp = new T[capacity];
        for (int i = 0; i < size; i++)
            temp[i] = elements[i];
        elements = temp;
    }

    public void Push(T value)
    {
        if (size == capacity)
            ExpandCapacity();

        elements[_size++] = value;
        ShiftUp();
    }

    public void Pop()
    {
        if (size == 0)
            return;

        Utilities.Swap<T>(ref elements[0], ref elements[size - 1]);
        _size--;
        ShiftDown();
    }
}

测试一

private void Start()
{
    PriorityQueue<int> que = new PriorityQueue<int>();
    que.Push(5);que.Push(1); que.Push(3); que.Push(2); que.Push(8);

    while (!que.empty)
    {
        Debug.Log(que.top);
        que.Pop();
    }
}

标题测试一结果

在这里插入图片描述

测试二

private void Start()
{
    PriorityQueue<int> que = new PriorityQueue<int>(Comparator.greater);
    que.Push(5);que.Push(1); que.Push(3); que.Push(2); que.Push(8);

    while (!que.empty)
    {
        Debug.Log(que.top);
        que.Pop();
    }
}

标题测试二结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Dizzrt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值