Skip to content

Instantly share code, notes, and snippets.

@slimshader
Created October 3, 2023 12:54
Show Gist options
  • Select an option

  • Save slimshader/d35a20042f846e51174acff3bdc382ec to your computer and use it in GitHub Desktop.

Select an option

Save slimshader/d35a20042f846e51174acff3bdc382ec to your computer and use it in GitHub Desktop.
List for UniMob
using Collections.Pooled;
using System;
using System.Collections;
using System.Collections.Generic;
using UniMob;
namespace AtomEx
{
public sealed class AtomList<T> : IList<T>, IReadOnlyList<T>
{
private readonly MutableAtom<PooledList<T>> _list = Atom.Value(new PooledList<T>());
public T this[int index] { get => _list.Value[index]; set => _list.Value[index] = value; }
public int Count => _list.Value.Count;
public bool IsReadOnly => false;
public void Add(T item)
{
_list.Value.Add(item);
_list.Invalidate();
}
public void Clear()
{
_list.Value.Clear();
_list.Invalidate();
}
public bool Contains(T item) => _list.Value.Contains(item);
public void CopyTo(T[] array, int arrayIndex) => _list.Value.CopyTo(array.AsSpan(arrayIndex));
public int IndexOf(T item) => _list.Value.IndexOf(item);
public void Insert(int index, T item)
{
_list.Value.Insert(index, item);
_list.Invalidate();
}
public bool Remove(T item)
{
if (_list.Value.Remove(item))
{
_list.Invalidate();
return true;
}
return false;
}
public void RemoveAt(int index)
{
_list.Value.RemoveAt(index);
_list.Invalidate();
}
public IEnumerator<T> GetEnumerator() => _list.Value.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment