Skip to content

Commit

Permalink
PR feedback.
Browse files Browse the repository at this point in the history
- Reduce duplication
- add comments and make code more obvious
- Use IndexOf

Author:    algorithmsarecool <algorithmsarecool@gmail.com>
  • Loading branch information
AlgorithmsAreCool committed Sep 4, 2024
1 parent 3cde632 commit 0f562d3
Showing 1 changed file with 56 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -420,12 +420,15 @@ public void Add(T item)
while (true)
{
T[] local = _volatileArray;
var newArray = new T[local.Length + 1];
Array.Copy(local, newArray, local.Length);
newArray[local.Length] = item;

if (Interlocked.CompareExchange(ref _volatileArray, newArray, local) == local)
break;
if (TryAppendItemAndSwap(item, local))
{
return;
}
else
{
//implicit continue
}
}
}

Expand All @@ -435,17 +438,21 @@ public bool AddIfNotExist(T item)
{
T[] local = _volatileArray;

foreach (T arrayItem in local)
int index = Array.IndexOf(local, item);

if (index >= 0)
{
if (EqualityComparer<T>.Default.Equals(arrayItem, item))
{
return false;
}
return false;
}

// We didn't find the item in the list, so we can add it.
Add(item);
return true;
if (TryAppendItemAndSwap(item, local))
{
return true;
}
else
{
//implicit continue
}
}
}

Expand All @@ -455,21 +462,21 @@ public bool Remove(T item)
{
T[] local = _volatileArray;

for (int i = 0; i < local.Length; i++)
{
if (EqualityComparer<T>.Default.Equals(local[i], item))
{
var newArray = new T[local.Length - 1];
Array.Copy(local, newArray, i);
Array.Copy(local, i + 1, newArray, i, local.Length - i - 1);
int index = Array.IndexOf(local, item);

if (Interlocked.CompareExchange(ref _volatileArray, newArray, local) == local)
return true;
break;
}
if (index < 0)
{
return false;
}

return false;
if (TryRemoveIndexAndSwap(index, local))
{
return true;
}
else
{
//implicit continue
}
}
}

Expand Down Expand Up @@ -514,5 +521,29 @@ public void EnumWithExceptionNotification(Activity activity, Exception exception
(item as ActivityListener)!.ExceptionRecorder?.Invoke(activity, exception, ref tags);
}
}

private bool TryAppendItemAndSwap(T item, T[] localCopy)
{
T[] newArray = new T[localCopy.Length + 1];

Array.Copy(localCopy, newArray, localCopy.Length);//copy existing items
newArray[localCopy.Length] = item;//copy new item

return Interlocked.CompareExchange(ref _volatileArray, newArray, localCopy) == localCopy;
}

private bool TryRemoveIndexAndSwap(int index, T[] localCopy)
{
T[] newArray = new T[localCopy.Length - 1];

Array.Copy(localCopy, newArray, index);//copy existing items before index

Array.Copy(
localCopy, index + 1, //position after the index, skipping it
newArray, index, localCopy.Length - index - 1//remaining items accounting for removed item
);

return Interlocked.CompareExchange(ref _volatileArray, newArray, localCopy) == localCopy;
}
}
}

0 comments on commit 0f562d3

Please sign in to comment.