#include <iostream>
#include <vector>
#include <assert.h>

using namespace std;

void print(float t) {cout << t << endl;}


class array {
public:

    size_t size;
    float *data; 

    // constructor
    array(int m)
    {
        size = m;
        data = new float[size];
    }
    // destructor
    ~array()
    {
        delete [] data;
    }

    friend void swap(array& first, array&second)
    {
        using std::swap;
        swap(first.data,second.data);
        swap(first.size,second.size);
    }

    //! copy constructor
    array(const array& other): size(other.size), data(size ? new float[size]:0)
    {
        std::copy(other.data, other.data + size, data);
    }

    // move constructor
    array(array&& other) 
    {
        swap(*this,other);
    }

    //! copy assignment
    array& operator=(array other)
    {
        swap(*this,other);
        return *this;
    }

    // operator- between two arrays
    const array operator-(const array& other) 
    {
        assert(size==other.size && "Cannot add array of different sizes!");
        array tmp(other.size); 
        for (size_t i=0;i<size;++i)
            tmp.data[i] = data[i]-other.data[i];

        return tmp;
    }

    void fill(const float& num)
    {
        std::fill(data,data+size,num);
    }
};


array operator -(float lhs, const array& rhs)
{
    array tmp(rhs.size);
    for (size_t i=0;i<rhs.size;++i)
       tmp.data[i] = lhs-rhs.data[i];
    cout << "called first overload" << endl;
    return tmp;
}

array operator -( array& lhs, float rhs)
{
    array tmp(lhs.size);
    for (size_t i=0;i<lhs.size;++i)
       tmp.data[i] = lhs.data[i]-rhs;
    cout << "called second overload" << endl;
    return tmp;
}

void print(const array& t) {
    for (size_t i=0;i<t.size;++i)
        cout << t.data[i] << " ";
    cout << endl;
}



int main()
{
    array f(5);
    f.fill(13.4);
    print(f);

    print(2-f-5);
    print(2-(f-5));

    cout << "All Good!" << endl;
    return 0;
} 