using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GFPartitionGrid<T>
{
    struct Key
    {
        public int x;
        public int y;
        public int z;

        public Key(Vector3 pos)
        {
            x = Mathf.FloorToInt(pos.x);
            y = Mathf.FloorToInt(pos.x);
            z = Mathf.FloorToInt(pos.x);
        }
    }

    Dictionary<Key, List<T>> m_dictionary = new Dictionary<Key, List<T>>();

    public void Add(Vector3 pos, T obj)
    {
        Key k = new Key(pos);
        List<T> list;
        if (!m_dictionary.TryGetValue(k, out list))
        {
            list = new List<T>();
            m_dictionary.Add(k, list);
            list.Add(obj);
        }
        else
        {
            if (!list.Contains(obj))
                list.Add(obj);
        }
    }

    public void Remove(Vector3 pos, T obj)
    {
        Key k = new Key(pos);
        List<T> list;
        if (m_dictionary.TryGetValue(k, out list))
        {
            list.Remove(obj);
        }
    }

    public bool Contains(Vector3 pos, T obj)
    {
        Key k = new Key(pos);
        List<T> list;
        if (m_dictionary.TryGetValue(k, out list))
            return list.Contains(obj);
        else
            return false;
    }

    public List<T> GetAll(Vector3 pos)
    {
        Key k = new Key(pos);
        List<T> list;
        m_dictionary.TryGetValue(k, out list);
        return list;
    }

    public int GetCountAt(Vector3 pos)
    {
        Key k = new Key(pos);
        List<T> list;
        if (m_dictionary.TryGetValue(k, out list))
            return list.Count;
        else
            return 0;
    }

    public bool SameKeys(Vector3 p1, Vector3 p2)
    {
        Key k1 = new Key(p1);
        Key k2 = new Key(p2);
        return k1.x == k2.x && k1.y == k2.y && k1.z == k2.z;
    }
}
