Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FastList<T>
- {
- private readonly List<T> _internalList = new List<T>();
- public int Count => _count;
- private int _count;
- private int _freeIndex;
- public T this[int index] => _internalList[index];
- public int Add(T input)
- {
- while (true)
- {
- if (_freeIndex == _count)
- {
- _internalList.Add(input);
- return _count++;
- }
- else
- {
- if (_internalList[_freeIndex] == null || _internalList.Equals(default(T)))
- {
- _internalList[_freeIndex] = input;
- return _freeIndex++;
- }
- else
- {
- _freeIndex++;
- }
- }
- }
- }
- public void Remove(int index)
- {
- _internalList[index] = default(T);
- Interlocked.Exchange(ref _freeIndex, index);
- }
- public bool Exists(int index)
- {
- if (_count < index || _internalList[index] == null || _internalList[index].Equals(default(T)))
- return false;
- return true;
- }
- public bool TryGetValue(int key, out T value)
- {
- if (!Exists(key))
- {
- value = default(T);
- return false;
- }
- else
- {
- value = _internalList[key];
- return true;
- }
- }
- public bool TryPickValue(int key, out T value)
- {
- if (!Exists(key))
- {
- value = default(T);
- return false;
- }
- else
- {
- value = _internalList[key];
- Remove(key);
- return true;
- }
- }
- }
Add Comment
Please, Sign In to add comment