Skip to content

Commit

Permalink
Merge pull request #2 from ndrwrbgs/HATList
Browse files Browse the repository at this point in the history
Make HashedArrayTree expose IList
  • Loading branch information
abdonkov committed Aug 18, 2018
2 parents d93ed86 + 42ea264 commit fb8d6fa
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions DSA/DSA/DataStructures/Arrays/HashedArrayTree.cs
Expand Up @@ -9,7 +9,7 @@ namespace DSA.DataStructures.Arrays
/// Represents a hashed array tree.
/// </summary>
/// <typeparam name="T">The stored data type.</typeparam>
public class HashedArrayTree<T> : IEnumerable<T>
public class HashedArrayTree<T> : IList<T>
{
/// <summary>
/// The base 2 logarithm of the top array size. Used for bitwise indexing calculations.
Expand Down Expand Up @@ -316,7 +316,10 @@ public bool Remove(T item)
{
int index = IndexOf(item);
if (index != -1)
return RemoveAt(index);
{
RemoveAt(index);
return true;
}
else
return false;
}
Expand All @@ -326,7 +329,7 @@ public bool Remove(T item)
/// </summary>
/// <param name="index">The index of the item to remove from the <see cref="HashedArrayTree{T}"/>.</param>
/// <returns>true if the item is removed successfully; otherwise false.</returns>
public bool RemoveAt(int index)
public void RemoveAt(int index)
{
if (index < 0 || index >= Count) throw new IndexOutOfRangeException(nameof(index));

Expand Down Expand Up @@ -354,8 +357,6 @@ public bool RemoveAt(int index)
// we can remove the last allocated array
if (Count % arrays.Length == 0)
arrays[GetTopArrayIndex(Count)] = null;

return true;
}

/// <summary>
Expand Down Expand Up @@ -454,6 +455,18 @@ public void Clear()
topArrayLog2Size = 1;
}

/// <inheritdoc />
public bool IsReadOnly { get; } = false;

public void CopyTo(T[] array, int arrayIndex)
{
// TODO: Should use Array.Copy for better performance
for (int i = 0; i < Count; i++)
{
array[arrayIndex + i] = this[i];
}
}

/// <summary>
/// Copies the elements of the <see cref="HashedArrayTree{T}"/> to a new array.
/// </summary>
Expand Down

0 comments on commit fb8d6fa

Please sign in to comment.