﻿//VC++.Net 2008
//Console Application

public value struct Row
{
    public: int Field1;
    public: int Field2;
    public: int Field3;
};



public ref class List
{
    //________________________________________________________________________

    private: array<Row>^ m_Buffer;
    private: int m_Count;
    //________________________________________________________________________

    public: List()
    {
        this->m_Buffer = gcnew array<Row>(1);
        this->m_Count = 0;
    }
    //________________________________________________________________________

    public: void Insert(Row value, int index)
    {
        //----- Clamp ...
        if (index < 0) index = 0;
        else if (index > this->m_Count) index = this->m_Count;
        //----- Expand ...
        if (this->m_Count >= this->m_Buffer->Length)
        {
            int capacity = this->m_Buffer->Length;
            array<Row>^ newbuffer = gcnew array<Row>(capacity + capacity);

            while (--capacity >= 0)
                newbuffer[capacity] = this->m_Buffer[capacity];

            //System::GC::SuppressFinalize(this->m_Buffer);
            this->m_Buffer = newbuffer;
        }
        //----- Shift ...
        for (int i = this->m_Count; i > index; --i)
        {
            this->m_Buffer[i] = this->m_Buffer[i - 1];
        }
        //----- Set ...
        this->m_Buffer[index] = value;
        this->m_Count++;
    }
    //________________________________________________________________________

    public: void Add(Row value)
    {
        this->Insert(value, 0x7fffffff);
    }
    //________________________________________________________________________

    public: void RemoveAt(int index)
    {
        if (this->m_Count <= 0) return;
        //----- Clamp ...
        if (index < 0) index = 0;
        else if (index >= this->m_Count) index = this->m_Count - 1;
        //----- Shift ...
        while (++index < this->m_Count)
        {
            this->m_Buffer[index - 1] = this->m_Buffer[index];
        }
        //----- Set ...
        this->m_Count--;
    }
    //________________________________________________________________________

    public: property int Count
    {
        int get() { return this->m_Count; }
    }
    //________________________________________________________________________

    public: property Row default[int]
    {
        Row get(int index)
        {
            if (this->m_Count <= 0) return Row();
            if (index < 0) index = 0;
            else if (index >= this->m_Count) index = this->m_Count - 1;

            return this->m_Buffer[index];
        }
        void set(int index, Row value)
        {
            if (this->m_Count <= 0) return;
            if (index < 0) index = 0;
            else if (index >= this->m_Count) index = this->m_Count - 1;

            this->m_Buffer[index] = value;
        }
    }
    //________________________________________________________________________
};





[STAThread()] void main()
{
    List^ v = gcnew List();
    Row r;

    r.Field1 = 14;
    v->Add(r);

    r = (*v)[0];
    r.Field2 = 114;
    (*v)[0] = r;

    //...

    int iField = Console::Read();
    Console::WriteLine(L"End");
}
