using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
namespace PrefsGUI.Kvs
{
///
/// Avoid boxing/unboxing object cache
///
public class KvsCache : IEnumerable<(string, object)>
{
private readonly Dictionary keyToListAndIndex = new();
private readonly Dictionary typeToList = new();
public bool ContainsKey(string key) => keyToListAndIndex.ContainsKey(key);
public void Set(string key, T value)
{
if (keyToListAndIndex.TryGetValue(key, out var pair))
{
var (list, index) = pair;
((List) list)[index] = value;
}
else
{
if (!typeToList.TryGetValue(typeof(T), out var iList))
{
iList = new List();
typeToList[typeof(T)] = iList;
}
((List) iList).Add(value);
pair = (iList, iList.Count - 1);
keyToListAndIndex[key] = pair;
}
}
public bool TryGetValue(string key, out T value)
{
if (!keyToListAndIndex.TryGetValue(key, out var pair))
{
value = default;
return false;
}
var (list, index) = pair;
value = ((List) list)[index];
return true;
}
public bool Remove(string key) => keyToListAndIndex.Remove(key);
public void Clear()
{
keyToListAndIndex.Clear();
typeToList.Clear();
}
public IEnumerator<(string, object)> GetEnumerator()
{
foreach (var (key, pair) in keyToListAndIndex)
{
var (list, index) = pair;
yield return (key, list[index]);
}
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}