Monday, February 22, 2016

CS 2420 - Assignment 4 - Part 3

Its been a few days since my last entry, and I have to say that I have made a lot of progress with my List class. I have now finished everything except for creating my own Enumerator. I still haven't figured out how to write that part, hopefully it won't be too hard.

Here is what I have so far:

using System;
using System.Collections;
using System.Collections.Generic;

class MyList : IList
{
    private T[] underlyingArray;
    private int element_count;

    public MyList(int initial_size)
    {
        underlyingArray = new T[initial_size];
        element_count = 0;
    }

    public T this[int index]
    {
        get
        {
            return underlyingArray[index];
        }

        set
        {
            underlyingArray[index] = value;
        }
    }

    public int Count
    {
        get
        {
            return element_count;
        }
    }

    private void UpdateCount(int amount)
    {
        element_count += amount;
    }

    public bool IsReadOnly
    {
        get
        {
            return underlyingArray.IsReadOnly;
        }
    }

    public void Add(T item)
    {
        try
        {
            underlyingArray[Count] = item;
            UpdateCount(1);
        }
        catch (IndexOutOfRangeException)
        {
            Resize(underlyingArray);
            Add(item);
        }
    }

    public void Clear()
    {
        for (int index = 0; index < Count; index++)
            underlyingArray[index] = default(T);

        UpdateCount(-Count);
    }

    public void Resize(T[] array)
    {
        T[] newArray = new T[array.Length * 2];

        for (int index = 0; index < array.Length; index++)
            newArray[index] = array[index];

        underlyingArray = newArray;
    }

    public bool Contains(T item)
    {
        for (int index = 0; index < Count; index++)
            if (EqualityComparer.Default.Equals(underlyingArray[index], item))
                return true;
        return false;
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        if (Count == array.Length)
            Resize(array);

        for (int index = Count; index > arrayIndex; index--)
            array[index] = array[index - 1];

        underlyingArray = array;
    }

    public IEnumerator GetEnumerator()
    {
        for (int i = 0; i < underlyingArray.Length; i++)
            yield return underlyingArray[i];
    }

    // TODO No yield
    public IEnumerator GetEnumeratorNoYield()
    {
        throw new NotImplementedException();
    }

    public int IndexOf(T item)
    {
        for (int index = 0; index < Count; index++)
            if (EqualityComparer.Default.Equals(underlyingArray[index], item))
                return index;
        return -1;
    }

    public void Insert(int index, T item)
    {
        if (Count == underlyingArray.Length)
            Resize(underlyingArray);
        if (index >= Count)
            Add(item);
        else
        {
            CopyTo(underlyingArray, index);
            underlyingArray[index] = item;
            UpdateCount(1);
        }
    }

    public bool Remove(T item)
    {
        for (int index = 0; index < Count; index++)
            if (EqualityComparer.Default.Equals(underlyingArray[index], item))
            {
                RemoveAt(index);
                return true;
            }
        return false;
    }

    public void RemoveAt(int index)
    {
        while (index < Count - 1)
        {
            underlyingArray[index] = underlyingArray[index + 1];
            index++;
        }
        UpdateCount(-1);
        underlyingArray[Count] = default(T);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

I know that I probably still need to fix a bunch of stuff in my class, but for now I think I'm done with it. I will now focus on writing functions that put my List class to the test.

That's all for now, thanks for reading!

No comments:

Post a Comment