Created
March 21, 2018 23:31
-
-
Save PetrChudoba/145442ef450f9c4008758dcb2e23b0f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CrazyShufflingList<T> : IList<T> | |
{ | |
private List<T> _internal = new List<T>(); | |
private Random _random = new Random(); | |
public T this[int index] | |
{ | |
get | |
{ | |
return _internal[index]; | |
} | |
set | |
{ | |
_internal[index] = value; | |
shuffle(); | |
} | |
} | |
public int Count | |
{ | |
get { return _internal.Count; } | |
} | |
private void shuffle() | |
{ | |
for (int i = 0; i < _internal.Count - 1; i++) | |
{ | |
if (_random.Next(2) == 1) | |
{ | |
T temp = _internal[i]; | |
_internal[i] = _internal[i + 1]; | |
_internal[i + 1] = temp; | |
} | |
} | |
} | |
public bool IsReadOnly | |
{ | |
get { return false; } | |
} | |
public void Add(T item) | |
{ | |
shuffle(); | |
_internal.Add(item); | |
} | |
public void Clear() | |
{ | |
_internal.Clear(); | |
} | |
public bool Contains(T item) | |
{ | |
return _internal.Contains(item); | |
} | |
public void CopyTo(T[] array, int arrayIndex) | |
{ | |
_internal.CopyTo(array, arrayIndex); | |
} | |
public IEnumerator<T> GetEnumerator() | |
{ | |
return _internal.GetEnumerator(); | |
} | |
public int IndexOf(T item) | |
{ | |
return _internal.IndexOf(item); | |
} | |
public void Insert(int index, T item) | |
{ | |
shuffle(); | |
_internal.Insert(index, item); | |
} | |
public bool Remove(T item) | |
{ | |
shuffle(); | |
return _internal.Remove(item); | |
} | |
public void RemoveAt(int index) | |
{ | |
_internal.RemoveAt(index); | |
shuffle(); | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return _internal.GetEnumerator(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment